Skip to content

Commit

Permalink
💥 [#2177] Changing map data to geoJson
Browse files Browse the repository at this point in the history
For the map interaction changes, we need to save the map data in a different format. This is to allow us to differentiate between point, line or polygon map information.
  • Loading branch information
robinmolen committed Dec 12, 2024
1 parent 0dd2174 commit 987a92c
Show file tree
Hide file tree
Showing 2 changed files with 558 additions and 11 deletions.
57 changes: 51 additions & 6 deletions src/openforms/formio/components/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.utils.html import format_html
from django.utils.translation import gettext as _

from drf_polymorphic.serializers import PolymorphicSerializer
from glom import glom
from rest_framework import ISO_8601, serializers
from rest_framework.request import Request
Expand Down Expand Up @@ -185,6 +186,53 @@ def build_serializer_field(
return serializers.ListField(child=base) if multiple else base


class GeoJsonPointGeometrySerializer(serializers.Serializer):
coordinates = serializers.ListField(
child=serializers.FloatField(required=True, allow_null=False),
min_length=2,
max_length=2,
)


class GeoJsonLineStringGeometrySerializer(serializers.Serializer):
coordinates = serializers.ListField(
child=serializers.ListField(
child=serializers.FloatField(required=True, allow_null=False),
min_length=2,
max_length=2,
),
)


class GeoJsonPolygonGeometrySerializer(serializers.Serializer):
coordinates = serializers.ListField(
child=serializers.ListField(
child=serializers.ListField(
child=serializers.FloatField(required=True, allow_null=False),
min_length=2,
max_length=2,
),
),
)


class GeoJsonGeometryPolymorphicSerializer(PolymorphicSerializer):
type = serializers.RegexField("^Point|LineString|Polygon$", required=True)

discriminator_field = "type"
serializer_mapping = {
str("Point"): GeoJsonPointGeometrySerializer,
str("LineString"): GeoJsonLineStringGeometrySerializer,
str("Polygon"): GeoJsonPolygonGeometrySerializer,
}


class GeoJsonSerializer(serializers.Serializer):
type = serializers.RegexField("^Feature$", required=True)
properties = serializers.DictField()
geometry = GeoJsonGeometryPolymorphicSerializer()


@register("map")
class Map(BasePlugin[Component]):
formatter = MapFormatter
Expand All @@ -198,14 +246,11 @@ def rewrite_for_request(component, request: Request):
component["initialCenter"]["lat"] = config.form_map_default_latitude
component["initialCenter"]["lng"] = config.form_map_default_longitude

def build_serializer_field(self, component: Component) -> serializers.ListField:
def build_serializer_field(self, component: Component) -> GeoJsonSerializer:
validate = component.get("validate", {})
required = validate.get("required", False)
base = serializers.FloatField(
required=required,
allow_null=not required,
)
return serializers.ListField(child=base, min_length=2, max_length=2)

return GeoJsonSerializer(required=required, allow_null=not required)


@register("postcode")
Expand Down
Loading

0 comments on commit 987a92c

Please sign in to comment.