Skip to content

Commit

Permalink
💥 [#2177] Handling geojson in plugins and map formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmolen committed Dec 18, 2024
1 parent 8800419 commit 6763718
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 20 deletions.
8 changes: 8 additions & 0 deletions src/openforms/formio/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.db import models

COMPONENT_DATATYPES = {
"date": "date",
"time": "time",
Expand All @@ -11,3 +13,9 @@
"editgrid": "array",
"datetime": "datetime",
}


class GeoJsonGeometryTypes(models.TextChoices):
point = "Point", "Point"
polygon = "Polygon", "Polygon"
line_string = "LineString", "LineString"
16 changes: 14 additions & 2 deletions src/openforms/formio/formatters/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.utils.html import format_html
from django.utils.safestring import mark_safe

from ..constants import GeoJsonGeometryTypes
from ..typing import AddressNLComponent, Component, MapComponent
from .base import FormatterBase

Expand All @@ -21,10 +22,21 @@ def format(self, component: Component, value: str) -> str:
return f"{fmt_date(parsed_value)} {fmt_time(parsed_value, 'H:i')}"


class GeoJsonGeometry(TypedDict):
type: str
coordinates: list[float]


class GeoJson(TypedDict):
type: GeoJsonGeometryTypes
properties: dict[str, str]
geometry: GeoJsonGeometry


class MapFormatter(FormatterBase):
def format(self, component: MapComponent, value: list[float]) -> str:
def format(self, component: MapComponent, value: GeoJson) -> str:
# use a comma here since its a single data element
return ", ".join((str(x) for x in value))
return ", ".join((str(x) for x in value["geometry"]["coordinates"]))


class AddressValue(TypedDict):
Expand Down
14 changes: 8 additions & 6 deletions src/openforms/registrations/contrib/objects_api/handlers/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from glom import Assign, Path, glom

from openforms.api.utils import underscore_to_camel
from openforms.formio.constants import GeoJsonGeometryTypes
from openforms.formio.typing import Component
from openforms.typing import JSONObject, JSONValue

Expand Down Expand Up @@ -105,12 +106,13 @@ def process_mapped_variable(
value = value[0] if value else ""

case {"type": "map"}:
# Currently we only support Point coordinates
assert isinstance(value, list) and len(value) == 2
value = {
"type": "Point",
"coordinates": [value[0], value[1]],
}
assert isinstance(value, dict)
if (
(geometry := value.get("geometry"))
and geometry.get("type", None) in GeoJsonGeometryTypes
and len(geometry.get("coordinates", [])) == 2
):
value = geometry

# not a component or standard behaviour where no transformation is necessary
case None | _:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
create_csv_document,
create_report_document,
)
from openforms.formio.constants import GeoJsonGeometryTypes
from openforms.formio.formatters.custom import GeoJson, GeoJsonGeometry
from openforms.formio.service import FormioData
from openforms.formio.typing import Component
from openforms.registrations.exceptions import RegistrationFailed
Expand Down Expand Up @@ -69,10 +71,16 @@
logger = logging.getLogger(__name__)


def _point_coordinate(value: Any) -> dict[str, Any] | object:
if not isinstance(value, list) or len(value) != 2:
return SKIP
return {"type": "Point", "coordinates": [value[0], value[1]]}
def _point_coordinate(value: GeoJson) -> GeoJsonGeometry | object:
return (
geometry
if (
(geometry := value.get("geometry"))
and geometry.get("type", None) in GeoJsonGeometryTypes
and len(geometry.get("coordinates", [])) == 2
)
else SKIP
)


def _resolve_documenttype(
Expand Down
17 changes: 13 additions & 4 deletions src/openforms/registrations/contrib/stuf_zds/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from json_logic.typing import Primitive

from openforms.formio.constants import GeoJsonGeometryTypes
from openforms.formio.formatters.custom import GeoJson
from openforms.plugins.exceptions import InvalidPluginConfiguration
from openforms.registrations.base import BasePlugin, PreRegistrationResult
from openforms.registrations.constants import (
Expand Down Expand Up @@ -38,7 +40,7 @@
from ...utils import execute_unless_result_exists
from .options import ZaakOptionsSerializer
from .registration_variables import register as variables_registry
from .typing import RegistrationOptions
from .typing import PointCoordinate, RegistrationOptions
from .utils import flatten_data

if TYPE_CHECKING:
Expand Down Expand Up @@ -111,10 +113,17 @@ def _safe_int(num):
)


def _point_coordinate(value):
if not value or not isinstance(value, list) or len(value) != 2:
def _point_coordinate(value: GeoJson) -> PointCoordinate | object:
if (
not (geometry := value.get("geometry"))
or geometry.get("type") not in GeoJsonGeometryTypes
or len(geometry.get("coordinates", [])) != 2
):
return SKIP
return {"lat": value[0], "lng": value[1]}

# GeoJson geometry uses [lng, lat] format for the coordinates
coordinates = geometry.get("coordinates")
return {"lat": coordinates[1], "lng": coordinates[0]}


def _gender_choices(value):
Expand Down
5 changes: 5 additions & 0 deletions src/openforms/registrations/contrib/stuf_zds/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ class RegistrationOptions(TypedDict):
"OPENBAAR",
]
payment_status_update_mapping: NotRequired[list[MappingItem]]


class PointCoordinate(TypedDict):
lat: float
lng: float
16 changes: 12 additions & 4 deletions src/openforms/registrations/contrib/zgw_apis/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
create_attachment_document,
create_report_document,
)
from openforms.formio.constants import GeoJsonGeometryTypes
from openforms.formio.formatters.custom import GeoJson, GeoJsonGeometry
from openforms.submissions.mapping import SKIP, FieldConf, apply_data_mapping
from openforms.submissions.models import Submission, SubmissionReport
from openforms.utils.date import datetime_in_amsterdam
Expand Down Expand Up @@ -75,10 +77,16 @@ def get_property_mappings_from_submission(
return property_mappings


def _point_coordinate(value):
if not value or not isinstance(value, list) or len(value) != 2:
return SKIP
return {"type": "Point", "coordinates": [value[0], value[1]]}
def _point_coordinate(value: GeoJson) -> GeoJsonGeometry | object:
return (
geometry
if (
(geometry := value.get("geometry"))
and geometry.get("type", None) in GeoJsonGeometryTypes
and len(geometry.get("coordinates", [])) == 2
)
else SKIP
)


def _gender_choices(value):
Expand Down

0 comments on commit 6763718

Please sign in to comment.