Skip to content

Commit

Permalink
feat(import): move geom_from_area_code to synthese imports
Browse files Browse the repository at this point in the history
  • Loading branch information
edelclaux committed Feb 16, 2024
1 parent 9aea5a2 commit 1c32acf
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 90 deletions.
21 changes: 11 additions & 10 deletions backend/geonature/core/gn_synthese/imports/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,8 @@
import sqlalchemy as sa
from sqlalchemy import func, distinct

from geonature.utils.env import db
from geonature.utils.sentry import start_sentry_child
from geonature.core.gn_commons.models import TModules
from geonature.core.gn_synthese.models import Synthese, TSources

from geonature.core.imports.models import Entity, EntityField, BibFields
from geonature.core.imports.utils import (
load_transient_data_in_dataframe,
update_transient_data_from_dataframe,
)
from geonature.core.imports.checks.dataframe import (
concat_dates,
check_required_values,
Expand All @@ -26,7 +18,6 @@
check_nomenclature_exist_proof,
check_nomenclature_blurring,
check_nomenclature_source_status,
convert_geom_columns,
check_cd_nom,
check_cd_hab,
generate_altitudes,
Expand All @@ -41,6 +32,16 @@
check_geography_outside,
check_is_valid_geography,
)
from geonature.core.imports.models import Entity, EntityField, BibFields
from geonature.core.imports.utils import (
load_transient_data_in_dataframe,
update_transient_data_from_dataframe,
)

from geonature.utils.env import db
from geonature.utils.sentry import start_sentry_child

from .checks import convert_geom_columns_from_area_code


def check_transient_data(task, logger, imprt):
Expand Down Expand Up @@ -146,7 +147,7 @@ def update_batch_progress(batch, step):
update_batch_progress(batch, 7)

# Checks in SQL
convert_geom_columns(
convert_geom_columns_from_area_code(
imprt,
entity,
geom_4326_field=fields["the_geom_4326"],
Expand Down
102 changes: 102 additions & 0 deletions backend/geonature/core/gn_synthese/imports/checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from sqlalchemy.sql.expression import select, update, join
import sqlalchemy as sa

from geoalchemy2.functions import ST_Transform, ST_Centroid
from geonature.core.imports.checks.sql.utils import report_erroneous_rows
from geonature.utils.env import db

from ref_geo.models import LAreas, BibAreasTypes


def set_geom_from_area_code(
imprt, entity, geom_4326_col, geom_local_col, source_column, area_type_filter
): # XXX specific synthese
transient_table = imprt.destination.get_transient_table()
# Find area in CTE, then update corresponding column in statement
cte = (
select(
transient_table.c.id_import,
transient_table.c.line_no,
LAreas.id_area,
LAreas.geom,
# TODO: add LAreas.geom_4326
)
.select_from(
join(transient_table, LAreas, source_column == LAreas.area_code).join(BibAreasTypes)
)
.where(transient_table.c.id_import == imprt.id_import)
.where(transient_table.c[entity.validity_column] == True)
.where(transient_table.c[geom_4326_col] == None) # geom_4326 & local should be aligned
.where(area_type_filter)
.cte("cte")
)
stmt = (
update(transient_table)
.values(
{
transient_table.c.id_area_attachment: cte.c.id_area,
transient_table.c[geom_local_col]: cte.c.geom,
transient_table.c[geom_4326_col]: ST_Transform(
cte.c.geom, 4326
), # TODO: replace with cte.c.geom_4326
}
)
.where(transient_table.c.id_import == cte.c.id_import)
.where(transient_table.c.line_no == cte.c.line_no)
)
db.session.execute(stmt)


def convert_geom_columns_from_area_code(
imprt,
entity,
geom_4326_field,
geom_local_field,
geom_point_field=None,
codecommune_field=None,
codemaille_field=None,
codedepartement_field=None,
):
transient_table = imprt.destination.get_transient_table()

for field, area_type_filter in [
(codecommune_field, BibAreasTypes.type_code == "COM"),
(codedepartement_field, BibAreasTypes.type_code == "DEP"),
(codemaille_field, BibAreasTypes.type_code.in_(["M1", "M5", "M10"])),
]:
if field is None:
continue
source_column = transient_table.c[field.source_field]
# Set geom from area of the given type and with matching area_code:
set_geom_from_area_code(
imprt,
entity,
geom_4326_field.dest_field,
geom_local_field.dest_field,
source_column,
area_type_filter,
)
# Mark rows with code specified but geom still empty as invalid:
report_erroneous_rows(
imprt,
entity,
error_type="INVALID_ATTACHMENT_CODE",
error_column=field.name_field,
whereclause=sa.and_(
transient_table.c[geom_4326_field.dest_field] == None,
transient_table.c[entity.validity_column] == True,
source_column != None,
),
)

# Mark rows with no geometry as invalid:
report_erroneous_rows(
imprt,
entity,
error_type="NO-GEOM",
error_column="Colonnes géométriques",
whereclause=sa.and_(
transient_table.c[geom_4326_field.dest_field] == None,
transient_table.c[entity.validity_column] == True,
),
)
83 changes: 3 additions & 80 deletions backend/geonature/core/imports/checks/sql/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,17 @@

from geonature.core.imports.checks.sql.utils import report_erroneous_rows

from ref_geo.models import LAreas, BibAreasTypes
from ref_geo.models import LAreas


__all__ = [
"set_geom_from_area_code",
"convert_geom_columns",
"check_is_valid_geography",
"check_geography_outside",
]


def set_geom_from_area_code(
imprt, entity, geom_4326_col, geom_local_col, source_column, area_type_filter
): # XXX specific synthese
transient_table = imprt.destination.get_transient_table()
# Find area in CTE, then update corresponding column in statement
cte = (
select(
transient_table.c.id_import,
transient_table.c.line_no,
LAreas.id_area,
LAreas.geom,
# TODO: add LAreas.geom_4326
)
.select_from(
join(transient_table, LAreas, source_column == LAreas.area_code).join(BibAreasTypes)
)
.where(transient_table.c.id_import == imprt.id_import)
.where(transient_table.c[entity.validity_column] == True)
.where(transient_table.c[geom_4326_col] == None) # geom_4326 & local should be aligned
.where(area_type_filter)
.cte("cte")
)
stmt = (
update(transient_table)
.values(
{
transient_table.c.id_area_attachment: cte.c.id_area,
transient_table.c[geom_local_col]: cte.c.geom,
transient_table.c[geom_4326_col]: ST_Transform(
cte.c.geom, 4326
), # TODO: replace with cte.c.geom_4326
}
)
.where(transient_table.c.id_import == cte.c.id_import)
.where(transient_table.c.line_no == cte.c.line_no)
)
db.session.execute(stmt)


def convert_geom_columns(
imprt,
entity,
geom_4326_field,
geom_local_field,
geom_point_field=None,
codecommune_field=None,
codemaille_field=None,
codedepartement_field=None,
):
def convert_geom_columns(imprt, entity, geom_4326_field, geom_local_field, geom_point_field=None):
local_srid = db.session.execute(sa.func.Find_SRID("ref_geo", "l_areas", "geom")).scalar()
transient_table = imprt.destination.get_transient_table()
if geom_4326_field is None:
Expand All @@ -94,35 +45,6 @@ def convert_geom_columns(
)
db.session.execute(stmt)

for field, area_type_filter in [
(codecommune_field, BibAreasTypes.type_code == "COM"),
(codedepartement_field, BibAreasTypes.type_code == "DEP"),
(codemaille_field, BibAreasTypes.type_code.in_(["M1", "M5", "M10"])),
]:
if field is None:
continue
source_column = transient_table.c[field.source_field]
# Set geom from area of the given type and with matching area_code:
set_geom_from_area_code(
imprt,
entity,
geom_4326_field.dest_field,
geom_local_field.dest_field,
source_column,
area_type_filter,
)
# Mark rows with code specified but geom still empty as invalid:
report_erroneous_rows(
imprt,
entity,
error_type="INVALID_ATTACHMENT_CODE",
error_column=field.name_field,
whereclause=sa.and_(
transient_table.c[geom_4326_field.dest_field] == None,
transient_table.c[entity.validity_column] == True,
source_column != None,
),
)
# Mark rows with no geometry as invalid:
report_erroneous_rows(
imprt,
Expand All @@ -134,6 +56,7 @@ def convert_geom_columns(
transient_table.c[entity.validity_column] == True,
),
)

# Set the_geom_point:
if geom_point_field is not None:
stmt = (
Expand Down

0 comments on commit 1c32acf

Please sign in to comment.