Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pr30 review #67

Merged
merged 1 commit into from
May 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading