Skip to content

Commit

Permalink
fix: convert all geography to geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
edelclaux committed Jul 5, 2024
1 parent 135fb93 commit 148024a
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions backend/gn_module_zh/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
)
):
Expand All @@ -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}

Expand Down

0 comments on commit 148024a

Please sign in to comment.