From 12d73bdc71930dc73c65f82be52aac3aeffd3ad2 Mon Sep 17 00:00:00 2001 From: Julien Corny Date: Wed, 26 Jul 2023 14:49:25 +0200 Subject: [PATCH] feat: improve zh already splitted detection by computing percentage of recovering intersection compared to zh superficy --- backend/gn_module_zh/geometry.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/backend/gn_module_zh/geometry.py b/backend/gn_module_zh/geometry.py index 76851ad2..b9429a5f 100644 --- a/backend/gn_module_zh/geometry.py +++ b/backend/gn_module_zh/geometry.py @@ -8,38 +8,53 @@ from .api_error import ZHApiError from .model.zh_schema import TZH, CorZhRb, TRiverBasin +import pdb + def set_geom(geometry, id_zh=None): if not id_zh: id_zh = 0 # SetSRID for POSTGIS < 3.0 compat - polygon = DB.session.query(func.ST_SetSRID(func.ST_GeomFromGeoJSON(str(geometry)), 4326)).one()[ - 0 - ] + + # select only already existing ZH geometries which intersect with the new ZH geometry q_zh = ( DB.session.query(TZH) .filter( func.ST_Intersects( func.ST_GeogFromWKB(func.ST_AsEWKB(TZH.geom)), - func.ST_GeomFromGeoJSON(str(geometry)), + func.ST_GeogFromWKB(func.ST_AsEWKB(str(geometry))), ) ) + .filter( + func.ST_Touches( + func.ST_GeomFromWKB(func.ST_AsEWKB(TZH.geom), 4326), + func.ST_GeomFromWKB(func.ST_AsEWKB(str(geometry)), 4326), + ) + == False + ) .all() ) + is_intersected = False for zh in q_zh: if zh.id_zh != id_zh: zh_geom = DB.session.query(func.ST_GeogFromWKB(func.ST_AsEWKB(zh.geom))).scalar() - polygon_geom = DB.session.query(func.ST_GeogFromWKB(func.ST_AsEWKB(polygon))).scalar() + polygon_geom = DB.session.query( + func.ST_GeogFromWKB(func.ST_AsEWKB(str(geometry))) + ).scalar() if DB.session.query(func.ST_Intersects(polygon_geom, zh_geom)).scalar(): - is_intersected = True + if DB.session.query( + func.ST_GeometryType(func.ST_Intersection(zh_geom, polygon_geom, 0.1)) + ).scalar() not in ["ST_LineString", "ST_MultiLineString"]: + is_intersected = True if DB.session.query( func.ST_Contains( - func.ST_GeomFromText(func.ST_AsText(zh_geom)), - func.ST_GeomFromText(func.ST_AsText(polygon_geom)), + zh_geom, + polygon_geom, ) ).scalar(): raise BadRequest("La ZH est entièrement dans une ZH existante") + # TODO: not detected if contained entirely in 2 or more ZH polygons polygon = DB.session.query(func.ST_Difference(polygon_geom, zh_geom)).scalar() return {"polygon": polygon, "is_intersected": is_intersected}