Skip to content

Commit

Permalink
Merge pull request #67 from naturalsolutions/PR30_review
Browse files Browse the repository at this point in the history
pr 30 review
  • Loading branch information
edelclaux authored May 27, 2024
2 parents 3677661 + 12d73bd commit b25d477
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions backend/gn_module_zh/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down

0 comments on commit b25d477

Please sign in to comment.