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 Jan 7, 2025
1 parent 54e1fcf commit 5b61fca
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 31 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"
21 changes: 19 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,26 @@ 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] | list[list[float]] | list[list[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))
if (geometry := value.get("geometry")) and geometry.get(
"type"
) in GeoJsonGeometryTypes:
return ", ".join((str(x) for x in geometry["coordinates"]))
else:
return ""


class AddressValue(TypedDict):
Expand Down
15 changes: 8 additions & 7 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,13 +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",
# Providing the coordinates as [lng, lat] #4955
"coordinates": [value[1], value[0]],
}
assert isinstance(value, dict)
if (
"geometry" in value
and "type" in value["geometry"]
and value["geometry"]["type"] in GeoJsonGeometryTypes
):
value = 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,11 +71,15 @@
logger = logging.getLogger(__name__)


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


def _resolve_documenttype(
Expand Down
17 changes: 12 additions & 5 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,15 @@ def _safe_int(num):
)


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


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: 11 additions & 5 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,11 +77,15 @@ 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
# Providing the coordinates as [lng, lat] #4955
return {"type": "Point", "coordinates": [value[1], value[0]]}
def _point_coordinate(value: GeoJson) -> GeoJsonGeometry | object:
return (
geometry
if (
(geometry := value.get("geometry"))
and geometry.get("type", None) in GeoJsonGeometryTypes
)
else SKIP
)


def _gender_choices(value):
Expand Down
32 changes: 25 additions & 7 deletions src/stuf/stuf_zds/templates/stuf_zds/soap/creeerZaak.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,32 @@
</ZKN:kenmerk>
{% endfor %}
{% if locatie %}
<ZKN:anderZaakObject>
<ZKN:omschrijving>{{ locatie.key }}</ZKN:omschrijving>
<ZKN:lokatie>
<GML:Point srsName="http://www.opengis.net/def/crs/EPSG/0/4326">
<GML:pos>{{ locatie.lat }} {{ locatie.lng }}</GML:pos>
</GML:Point>
<ZKN:anderZaakObject>
<ZKN:omschrijving>{{ locatie.key }}</ZKN:omschrijving>
<ZKN:lokatie>
{% if locatie.type == 'Point' %}
<GML:Point srsName="http://www.opengis.net/def/crs/EPSG/0/4326">
<GML:pos>{{ locatie.coordinates.0 }} {{ locatie.coordinates.1 }}</GML:pos>
</GML:Point>
{% elif locatie.type == 'Polygon' %}
<GML:Polygon srsName="http://www.opengis.net/def/crs/EPSG/0/4326">
<GML:exterior>
<GML:LinearRing>
{% for coordinates in locatie.coordinates.0 %}
<GML:pos>{{ coordinates.0 }} {{ coordinates.1 }}</GML:pos>
{% endfor %}
</GML:LinearRing>
</GML:exterior>
</GML:Polygon>
{% elif locatie.type == 'LineString' %}
<GML:LineString srsName="http://www.opengis.net/def/crs/EPSG/0/4326">
<GML:posList srsDimension="2">
{% for coordinates in locatie.coordinates %}{{ coordinates.0 }} {{ coordinates.1 }} {% endfor %}
</GML:posList>
</GML:LineString>
{% endif %}
</ZKN:lokatie>
</ZKN:anderZaakObject>
</ZKN:anderZaakObject>
{% endif %}
<ZKN:startdatum>{{ datum_vandaag }}</ZKN:startdatum>
<ZKN:registratiedatum>{{ datum_vandaag }}</ZKN:registratiedatum>
Expand Down

0 comments on commit 5b61fca

Please sign in to comment.