# Geometry form fix

* fixes bugs in tests
* refactors and simplifies geometry merging on GeometryForm
This commit is contained in:
2025-09-12 13:22:35 +02:00
parent a12c2fb57e
commit 63a824f9d9
6 changed files with 25 additions and 20 deletions

View File

@@ -72,9 +72,8 @@ class SimpleGeomForm(BaseForm):
# will be rendered again on failed submit
self.initialize_form_field("geom", self.data["geom"])
# Read geojson into gdal geometry
# HINT: This can be simplified if the geojson format holds data in epsg:4326 (GDAL provides direct creation for
# this case)
# Initialize features list with empty MultiPolygon, so that an empty input will result in a
# proper empty MultiPolygon object
features = []
features_json = geom.get("features", [])
accepted_ogr_types = [
@@ -102,19 +101,22 @@ class SimpleGeomForm(BaseForm):
return is_valid
is_valid &= self.__is_area_valid(g)
polygon = Polygon.from_ewkt(g.ewkt)
is_valid &= polygon.valid
if not polygon.valid:
self.add_error("geom", polygon.valid_reason)
g = Polygon.from_ewkt(g.ewkt)
is_valid &= g.valid
if not g.valid:
self.add_error("geom", g.valid_reason)
return is_valid
features.append(polygon)
if isinstance(g, Polygon):
features.append(g)
elif isinstance(g, MultiPolygon):
features.extend(list(g))
# Unionize all geometry features into one new MultiPolygon
form_geom = MultiPolygon(srid=DEFAULT_SRID_RLP)
for feature in features:
form_geom = form_geom.union(feature)
if features:
form_geom = MultiPolygon(*features, srid=DEFAULT_SRID_RLP).unary_union
else:
form_geom = MultiPolygon(srid=DEFAULT_SRID_RLP)
# Make sure to convert into a MultiPolygon. Relevant if a single Polygon is provided.
form_geom = Geometry.cast_to_multipolygon(form_geom)