Skip to content

Commit

Permalink
💥 [#2177] Handling geojson in plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmolen committed Dec 12, 2024
1 parent 987a92c commit e1429f0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
7 changes: 7 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,8 @@
"editgrid": "array",
"datetime": "datetime",
}

class GeoJsonGeometryTypes(models.TextChoices):
point = "Point", "Point"
polygon = "Polygon", "Polygon"
line_string = "LineString", "LineString"
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
create_csv_document,
create_report_document,
)
from openforms.formio.constants import GeoJsonGeometryTypes
from openforms.formio.service import FormioData
from openforms.formio.typing import Component
from openforms.registrations.exceptions import RegistrationFailed
Expand Down Expand Up @@ -70,9 +71,13 @@


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

if geometry.get("type", None) not in GeoJsonGeometryTypes or len(geometry.get("coordinates", [])) != 2:
return SKIP

return geometry


def _resolve_documenttype(
Expand Down
11 changes: 9 additions & 2 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.constants import GeoJsonGeometryTypes
from openforms.plugins.exceptions import InvalidPluginConfiguration
from openforms.registrations.base import BasePlugin, PreRegistrationResult
from openforms.registrations.constants import (
Expand Down Expand Up @@ -112,9 +113,15 @@ def _safe_int(num):


def _point_coordinate(value):
if not value or not isinstance(value, list) or len(value) != 2:
if not value or not (geometry := value.get("geometry")):
return SKIP
return {"lat": value[0], "lng": value[1]}

if geometry.get("type", None) not in GeoJsonGeometryTypes or len(geometry.get("coordinates", [])) != 2:
return SKIP

# 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
9 changes: 7 additions & 2 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.constants import GeoJsonGeometryTypes
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 @@ -76,9 +77,13 @@ def get_property_mappings_from_submission(


def _point_coordinate(value):
if not value or not isinstance(value, list) or len(value) != 2:
if not value or not (geometry := value.get("geometry")):
return SKIP
return {"type": "Point", "coordinates": [value[0], value[1]]}

if geometry.get("type", None) not in GeoJsonGeometryTypes or len(geometry.get("coordinates", [])) != 2:
return SKIP

return geometry


def _gender_choices(value):
Expand Down

0 comments on commit e1429f0

Please sign in to comment.