From 3180196d1401b11db9b57a5e7308abe679e9a50b Mon Sep 17 00:00:00 2001 From: Pierre Narcisi Date: Wed, 23 Aug 2023 13:45:40 +0200 Subject: [PATCH] feat(check): add valid geom check --- .../gn_module_import/checks/sql/__init__.py | 45 ++++++++++++------- backend/gn_module_import/tasks.py | 2 + .../tests/files/geom_file.csv | 1 + .../gn_module_import/tests/test_imports.py | 3 +- dependencies/GeoNature | 2 +- 5 files changed, 35 insertions(+), 18 deletions(-) diff --git a/backend/gn_module_import/checks/sql/__init__.py b/backend/gn_module_import/checks/sql/__init__.py index eb4e2ad9..dfe403b8 100644 --- a/backend/gn_module_import/checks/sql/__init__.py +++ b/backend/gn_module_import/checks/sql/__init__.py @@ -8,7 +8,7 @@ from sqlalchemy.dialects.postgresql import array_agg, aggregate_order_by from geoalchemy2.functions import ( ST_Transform, - ST_GeomFromWKB, + ST_IsValid, ST_Centroid, ST_GeomFromText, ST_MakePoint, @@ -701,6 +701,21 @@ def complete_others_geom_columns(imprt, fields): ) +def check_is_valid_geography(imprt, fields): + if "WKT" in fields: + where_clause = sa.and_( + ImportSyntheseData.src_WKT != None, + ImportSyntheseData.src_WKT != "", + sa.not_(ST_IsValid(ST_GeomFromText(ImportSyntheseData.src_WKT))), + ) + report_erroneous_rows( + imprt, + error_type="INVALID_GEOMETRY", + error_column="WKT", + whereclause=where_clause, + ) + + def check_geography_outside(imprt, fields): id_area = current_app.config["IMPORT"]["ID_AREA_RESTRICTION"] where_clause = () @@ -735,24 +750,22 @@ def check_geography_outside(imprt, fields): ) if "longitude" in fields and "latitude" in fields: - where_clause = sa.or_( - sa.and_( - sa.not_(WKT_present), - lat_long_present, - area.geom.ST_Intersects( - ST_Transform( - ST_SetSRID( - ST_MakePoint( - ImportSyntheseData.src_longitude.cast(sa.Float), - ImportSyntheseData.src_latitude.cast(sa.Float), - ), - imprt.srid, + where_clause = sa.and_( + sa.not_(WKT_present), + lat_long_present, + area.geom.ST_Intersects( + ST_Transform( + ST_SetSRID( + ST_MakePoint( + ImportSyntheseData.src_longitude.cast(sa.Float), + ImportSyntheseData.src_latitude.cast(sa.Float), ), - local_srid, + imprt.srid, ), - ) - == False, + local_srid, + ), ) + == False, ) report_erroneous_rows( imprt, diff --git a/backend/gn_module_import/tasks.py b/backend/gn_module_import/tasks.py index 3eb60545..14535d0f 100644 --- a/backend/gn_module_import/tasks.py +++ b/backend/gn_module_import/tasks.py @@ -34,6 +34,7 @@ check_depths, check_digital_proof_urls, check_geography_outside, + check_is_valid_geography, ) from geonature.core.notifications.utils import dispatch_notifications @@ -104,6 +105,7 @@ def do_import_checks(self, import_id): check_digital_proof_urls, check_mandatory_fields, check_geography_outside, + check_is_valid_geography, ] with start_sentry_child(op="check.sql", description="run all checks"): for i, check in enumerate(sql_checks): diff --git a/backend/gn_module_import/tests/files/geom_file.csv b/backend/gn_module_import/tests/files/geom_file.csv index 5b810260..7adf4605 100644 --- a/backend/gn_module_import/tests/files/geom_file.csv +++ b/backend/gn_module_import/tests/files/geom_file.csv @@ -13,3 +13,4 @@ date_min;cd_nom;nom_cite;observateurs;WKT;latitude;longitude;codecommune;codedep 2017-01-01;67111;Ablette;Toto;;44.85;6.5;;;;Valide (X/Y) 2017-01-01;67111;Ablette;Toto;POINT(6.5 44.85);44.85;6.5;;;;MULTIPLE_ATTACHMENT_TYPE_CODE 2017-01-01;67111;Ablette;Toto;;;;;;;NO-GEOM +2017-01-01;67111;Ablette;Toto;POLYGON((0 0, 1 1, 1 2, 1 1, 0 0));;;;;;INVALID_GEOMETRY diff --git a/backend/gn_module_import/tests/test_imports.py b/backend/gn_module_import/tests/test_imports.py index 4dbcbee3..e022411f 100644 --- a/backend/gn_module_import/tests/test_imports.py +++ b/backend/gn_module_import/tests/test_imports.py @@ -929,9 +929,10 @@ def test_import_geometry_file(self, area_restriction, prepared_import): "Champs géométriques", frozenset([10, 13]), ), - ("GEOMETRY_OUTSIDE", "WKT", frozenset([8, 11])), + ("GEOMETRY_OUTSIDE", "WKT", frozenset([8, 11, 15])), ("GEOMETRY_OUTSIDE", "longitude", frozenset([9, 12])), ("NO-GEOM", "Champs géométriques", frozenset([14])), + ("INVALID_GEOMETRY", "WKT", frozenset([15])), }, ) diff --git a/dependencies/GeoNature b/dependencies/GeoNature index e0eeaf20..34223aad 160000 --- a/dependencies/GeoNature +++ b/dependencies/GeoNature @@ -1 +1 @@ -Subproject commit e0eeaf20368eab5ee2be1ae3ec2cffb3710b2a8e +Subproject commit 34223aad0c1cba8e145da2a90ffe24fbdf6918dc