Skip to content

Commit

Permalink
💥 [#2177] Handling geojson in plugins and map formatter
Browse files Browse the repository at this point in the history
The backend registration plugins (Objects API, ZGW APIs and StUF-ZDS) now support point, line and polygon geojson geometries.
  • Loading branch information
robinmolen committed Jan 15, 2025
1 parent 211fd18 commit 3b6909e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 32 deletions.
30 changes: 27 additions & 3 deletions src/openforms/formio/formatters/custom.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# TODO implement: iban, bsn, postcode, licenseplate, npFamilyMembers, cosign
from typing import NotRequired, TypedDict
from typing import Literal, NotRequired, TypedDict

from django.template.defaultfilters import date as fmt_date, time as fmt_time
from django.utils.dateparse import parse_date, parse_datetime
Expand All @@ -21,10 +21,34 @@ def format(self, component: Component, value: str) -> str:
return f"{fmt_date(parsed_value)} {fmt_time(parsed_value, 'H:i')}"


type Coordinates = tuple[float, float]


class PointGeometry(TypedDict):
type: Literal["Point"]
coordinates: Coordinates


class LineStringGeometry(TypedDict):
type: Literal["LineString"]
coordinates: list[Coordinates]


class PolygonGeometry(TypedDict):
type: Literal["Polygon"]
coordinates: list[list[Coordinates]]


type GeoJsonGeometry = PointGeometry | LineStringGeometry | PolygonGeometry


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


class AddressValue(TypedDict):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,7 @@ 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)

# 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,11 +33,12 @@
create_csv_document,
create_report_document,
)
from openforms.formio.formatters.custom import GeoJsonGeometry
from openforms.formio.service import FormioData
from openforms.formio.typing import Component
from openforms.registrations.exceptions import RegistrationFailed
from openforms.submissions.exports import create_submission_export
from openforms.submissions.mapping import SKIP, FieldConf, apply_data_mapping
from openforms.submissions.mapping import FieldConf, apply_data_mapping
from openforms.submissions.models import (
Submission,
SubmissionFileAttachment,
Expand Down Expand Up @@ -69,11 +70,8 @@
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: GeoJsonGeometry) -> GeoJsonGeometry:
return value


def _resolve_documenttype(
Expand Down
7 changes: 3 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,7 @@

from json_logic.typing import Primitive

from openforms.formio.formatters.custom import GeoJsonGeometry
from openforms.plugins.exceptions import InvalidPluginConfiguration
from openforms.registrations.base import BasePlugin, PreRegistrationResult
from openforms.registrations.constants import (
Expand Down Expand Up @@ -111,10 +112,8 @@ 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: GeoJsonGeometry) -> GeoJsonGeometry:
return value


def _gender_choices(value):
Expand Down
8 changes: 3 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,7 @@
create_attachment_document,
create_report_document,
)
from openforms.formio.formatters.custom import 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 +76,8 @@ 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: GeoJsonGeometry) -> GeoJsonGeometry:
return value


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 3b6909e

Please sign in to comment.