diff --git a/backend/gn_module_zh/geometry.py b/backend/gn_module_zh/geometry.py index b3fb94bb..ef05fb40 100644 --- a/backend/gn_module_zh/geometry.py +++ b/backend/gn_module_zh/geometry.py @@ -14,48 +14,44 @@ import pdb -def set_geom(geometry, id_zh=None): +def set_geom(geometry_geojson, id_zh=None): if not id_zh: id_zh = 0 + + geometry = func.ST_SetSRID(func.ST_GeomFromGeoJSON(str(geometry_geojson)), 4326) # SetSRID for POSTGIS < 3.0 compat # select only already existing ZH geometries which intersect with the new ZH geometry q_zh = DB.session.scalars( select(TZH) .where( func.ST_Intersects( - func.ST_GeogFromWKB(func.ST_AsEWKB(TZH.geom)), - func.ST_GeogFromWKB(func.ST_AsEWKB(func.ST_GeomFromGeoJSON(str(geometry)))), + TZH.geom, + geometry, ) ) .where( func.ST_Touches( - func.ST_GeomFromWKB(func.ST_AsEWKB(TZH.geom), 4326), - func.ST_GeomFromWKB(func.ST_AsEWKB(func.ST_GeomFromGeoJSON(str(geometry))), 4326), + TZH.geom, + geometry, ) == False ) ).all() is_intersected = False - polygon = DB.session.scalar( - select(func.ST_SetSRID(func.ST_GeomFromGeoJSON(str(geometry)), 4326)) - ) + polygon = geometry for zh in q_zh: if zh.id_zh != id_zh: - zh_geom = DB.session.scalar(select(func.ST_GeogFromWKB(func.ST_AsEWKB(zh.geom)))) - polygon_geom = DB.session.scalar( - select(func.ST_GeogFromWKB(func.ST_AsEWKB(func.ST_GeomFromGeoJSON(str(geometry))))) - ) - if DB.session.scalar(select(func.ST_Intersects(polygon_geom, zh_geom))): + if DB.session.scalar(select(func.ST_Intersects(polygon, zh.geom))): if DB.session.scalar( - select(func.ST_GeometryType(func.ST_Intersection(zh_geom, polygon_geom, 0.1))) + select(func.ST_GeometryType(func.ST_Intersection(zh.geom, polygon, 0.1))) ) not in ["ST_LineString", "ST_MultiLineString"]: is_intersected = True if DB.session.scalar( select( func.ST_Contains( - zh_geom, - polygon_geom, + zh.geom, + polygon, ) ) ): @@ -64,15 +60,15 @@ def set_geom(geometry, id_zh=None): if DB.session.scalar( select( func.ST_Contains( - polygon_geom, - zh_geom, + polygon, + zh.geom, ) ) ): raise BadRequest("La ZH englobe complètement une ZH existante") # TODO: not detected if contained entirely in 2 or more ZH polygons - polygon = DB.session.scalar(select(func.ST_Difference(polygon_geom, zh_geom))) + polygon = DB.session.scalar(select(func.ST_Difference(polygon, zh.geom))) return {"polygon": polygon, "is_intersected": is_intersected}