Skip to content

Commit

Permalink
fix(synthese) :Fix area filter (#3372)
Browse files Browse the repository at this point in the history
* fix(synthese): area filters in the synthese verify SRID coherence

---------

Co-authored-by: Jbrieuc LP <[email protected]>
  • Loading branch information
jacquesfize and jbrieuclp authored Feb 11, 2025
1 parent d027fe6 commit 34d2bd6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion backend/geonature/core/gn_synthese/utils/blurring.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def build_blurred_precise_geom_queries(
CorAreaSyntheseAlias,
CorAreaSyntheseAlias.id_synthese == Synthese.id_synthese,
),
geom_column=LAreas.geom_4326,
geom_column=LAreas.geom,
)
# Joins here are needed to retrieve the blurred geometry
blurred_geom_query.add_join(LAreasAlias, LAreasAlias.id_area, CorAreaSyntheseAlias.id_area)
Expand Down
44 changes: 40 additions & 4 deletions backend/geonature/core/gn_synthese/utils/query_select_sqla.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ def __init__(
)
)

self.srid = DB.session.scalar(
select(
func.Find_SRID(
self.geom_column.table.schema, self.geom_column.table.name, self.geom_column.key
)
)
)

def add_join(self, right_table, right_column, left_column, join_type="right"):
if self.first:
if join_type == "right":
Expand Down Expand Up @@ -437,12 +445,18 @@ def filter_other_filters(self, user):
geo_filters = []
for feature in features:
geom_wkb = from_shape(shape(feature["geometry"]), srid=4326)
if self.srid != 4326:
geom_wkb = func.ST_Transform(

Check warning on line 449 in backend/geonature/core/gn_synthese/utils/query_select_sqla.py

View check run for this annotation

Codecov / codecov/patch

backend/geonature/core/gn_synthese/utils/query_select_sqla.py#L449

Added line #L449 was not covered by tests
func.ST_GeogFromWKB(geom_wkb),
self.srid,
)
geom_wkb = func.ST_GeogFromWKB(geom_wkb)
# if the geom is a circle
if "radius" in feature["properties"]:
radius = feature["properties"]["radius"]
geo_filter = func.ST_DWithin(
func.ST_GeogFromWKB(self.geom_column),
func.ST_GeogFromWKB(geom_wkb),
geom_wkb,
radius,
)
else:
Expand Down Expand Up @@ -479,9 +493,31 @@ def filter_other_filters(self, user):
if colname.startswith("area"):
if self.geom_column.class_ != self.model:
l_areas_cte = LAreas.query.filter(LAreas.id_area.in_(value)).cte("area_filter")
self.query = self.query.where(
func.ST_Intersects(self.geom_column, l_areas_cte.c.geom)
)
if self.srid != 4326:
srid_l_areas = DB.session.scalar(

Check warning on line 497 in backend/geonature/core/gn_synthese/utils/query_select_sqla.py

View check run for this annotation

Codecov / codecov/patch

backend/geonature/core/gn_synthese/utils/query_select_sqla.py#L496-L497

Added lines #L496 - L497 were not covered by tests
select(
func.Find_SRID(
LAreas.__table__.schema, LAreas.__table__.name, "geom"
)
)
)
if self.srid != srid_l_areas:
self.query = self.query.where(

Check warning on line 505 in backend/geonature/core/gn_synthese/utils/query_select_sqla.py

View check run for this annotation

Codecov / codecov/patch

backend/geonature/core/gn_synthese/utils/query_select_sqla.py#L504-L505

Added lines #L504 - L505 were not covered by tests
func.ST_Intersects(
self.geom_column,
func.ST_Transform(l_areas_cte.c.geom, self.srid),
)
)

else:
self.query = self.query.where(

Check warning on line 513 in backend/geonature/core/gn_synthese/utils/query_select_sqla.py

View check run for this annotation

Codecov / codecov/patch

backend/geonature/core/gn_synthese/utils/query_select_sqla.py#L513

Added line #L513 was not covered by tests
func.ST_Intersects(self.geom_column, l_areas_cte.c.geom)
)

else:
self.query = self.query.where(

Check warning on line 518 in backend/geonature/core/gn_synthese/utils/query_select_sqla.py

View check run for this annotation

Codecov / codecov/patch

backend/geonature/core/gn_synthese/utils/query_select_sqla.py#L518

Added line #L518 was not covered by tests
func.ST_Intersects(self.geom_column, l_areas_cte.c.geom_4326)
)
else:
cor_area_synthese_alias = aliased(CorAreaSynthese)
self.add_join(
Expand Down

0 comments on commit 34d2bd6

Please sign in to comment.