Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
edelclaux committed Jul 18, 2024
2 parents 135fb93 + d590a28 commit 3ffb8d1
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions backend/gn_module_zh/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,43 @@
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_geom = 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_geom, 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_geom)))
) not in ["ST_LineString", "ST_MultiLineString"]:
is_intersected = True
if DB.session.scalar(
select(
func.ST_Contains(
zh_geom,
zh.geom,
polygon_geom,
)
)
Expand All @@ -65,16 +61,16 @@ def set_geom(geometry, id_zh=None):
select(
func.ST_Contains(
polygon_geom,
zh_geom,
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_geom = DB.session.scalar(select(func.ST_Difference(polygon_geom, zh.geom)))

return {"polygon": polygon, "is_intersected": is_intersected}
return {"polygon": polygon_geom, "is_intersected": is_intersected}


def set_area(geom):
Expand Down

0 comments on commit 3ffb8d1

Please sign in to comment.