# Refactors API geometry processing

* refactors API geometry processing to use GeoJsonValidator and GeometryProcessor just like SimpleGeomForm
* moves cast_to_multipolygon, cast_to_rlp_srid, flatten_geom_to_2D, is_valid_4326 and is_area_valid into GeometryProcessor
* adds changeable SRID to GeoJsonValidator constructor
* updates test data
* updates test cases
This commit is contained in:
2026-09-12 17:58:00 +02:00
parent a067aafde1
commit 8ac8952729
13 changed files with 224 additions and 275 deletions
+16 -10
View File
@@ -17,6 +17,8 @@ from django.db.models import Q
from api.models import ExternalIdentifier
from konova.models import Geometry
from konova.sub_settings.lanis_settings import DEFAULT_SRID
from konova.utils.geometry.geometry_validator import GeoJsonValidator
from konova.utils.message_templates import DATA_UNSHARED
@@ -153,16 +155,20 @@ class AbstractModelAPISerializer:
Returns:
geometry (GEOSGeometry)
"""
if isinstance(geojson, dict):
geojson = json.dumps(geojson)
geometry = geos.fromstr(geojson)
if not geometry.valid:
raise ValueError(f"Invalid geometry: {geometry.valid_reason}")
is_4326 = Geometry.is_valid_4326(geometry)
if not is_4326:
raise ValueError("Geometry not in EPSG:4326 (WGS84). Unknown spatial reference system.")
geometry = Geometry.cast_to_rlp_srid(geometry)
geometry = Geometry.cast_to_multipolygon(geometry)
if isinstance(geojson, str):
geojson = json.loads(geojson)
geojson_validator = GeoJsonValidator(geojson, DEFAULT_SRID)
geojson_validator.validate()
if geojson_validator.errors:
raise ValueError(geojson_validator.errors)
geometry = geojson_validator.validated_geometry
is_input_geometry_too_small = geometry.area == 0.0 and geojson_validator.num_ignored_geometries > 0
if is_input_geometry_too_small:
raise ValueError(f"Your area was too small to be processed! Must be > 1m²!")
return geometry
def _get_obj_from_db(self, id, user):