Skip to content

Commit 9635b43

Browse files
authored
Merge pull request #1023 from MTES-MCT/feat-simple-geom
Ajout d'un champ simple_geom respectant la topology sur tous les territoires
2 parents 85810a9 + 014ee0c commit 9635b43

File tree

12 files changed

+132
-17
lines changed

12 files changed

+132
-17
lines changed

airflow/include/sql/sparte/macros/admin_express/commune.sql

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
{% macro commune(source_table_name) %}
22
{{ config(materialized="table") }}
3-
with
4-
epci_and_ept as (
3+
with simplified as (
4+
{{
5+
simplify(
6+
source=source('public', source_table_name),
7+
geo_field='geom',
8+
id_field='insee_com',
9+
tolerance='50'
10+
)
11+
}}
12+
), epci_and_ept as (
513
select
614
insee_com as commune_code,
715
case
@@ -29,8 +37,10 @@
2937
insee_reg as region,
3038
{{ get_ept_from_epci_array("epci_and_ept.epcis") }} as ept,
3139
{{ get_non_ept_from_epci_array("epci_and_ept.epcis") }} as epci,
32-
st_area(geom) as surface,
33-
geom
40+
st_area(commune.geom) as surface,
41+
commune.geom,
42+
simplified.geom as simple_geom
3443
from {{ source("public", source_table_name) }} as commune
3544
left join epci_and_ept on commune.insee_com = epci_and_ept.commune_code
45+
left join simplified on commune.insee_com = simplified.id_field
3646
{% endmacro %}
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
{% macro departement(source_table_name) %}
22
{{ config(materialized='table') }}
3-
3+
with simplified as (
4+
{{
5+
simplify(
6+
source=source('public', source_table_name),
7+
geo_field='geom',
8+
id_field='insee_dep',
9+
tolerance='1500'
10+
)
11+
}}
12+
)
413
SELECT
514
id,
615
nom as name,
716
nom_m as name_uppercase,
817
insee_dep as code,
918
insee_reg as region,
10-
ST_Area(geom) as surface,
11-
geom
19+
ST_Area(departement.geom) as surface,
20+
departement.geom,
21+
simplified.geom as simple_geom
1222
FROM
1323
{{ source('public', source_table_name) }} as departement
24+
LEFT JOIN
25+
simplified
26+
ON
27+
departement.insee_dep = simplified.id_field
1428
{% endmacro %}
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
{% macro epci(source_table_name) %}
22
{{ config(materialized='table') }}
3-
3+
with simplified as (
4+
{{
5+
simplify(
6+
source=source('public', source_table_name),
7+
geo_field='geom',
8+
id_field='code_siren',
9+
tolerance='250'
10+
)
11+
}}
12+
)
413
SELECT
514
id,
615
nom as name,
716
code_siren as code,
817
nature,
9-
ST_Area(geom) as surface,
10-
geom,
11-
{{ is_ept('code_siren') }} as is_ept
18+
ST_Area(epci.geom) as surface,
19+
epci.geom,
20+
{{ is_ept('code_siren') }} as is_ept,
21+
simplified.geom as simple_geom
1222
FROM
13-
{{ source('public', source_table_name) }}
23+
{{ source('public', source_table_name) }} as epci
24+
LEFT JOIN
25+
simplified
26+
ON
27+
epci.code_siren = simplified.id_field
1428
{% endmacro %}
Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
{% macro region(source_table_name) %}
22
{{ config(materialized='table') }}
33

4+
with simplified as (
5+
{{
6+
simplify(
7+
source=source('public', source_table_name),
8+
geo_field='geom',
9+
id_field='insee_reg',
10+
tolerance='1500'
11+
)
12+
}}
13+
)
414
SELECT
515
id,
616
nom as name,
717
insee_reg as code,
8-
ST_Area(geom) as surface,
9-
geom
18+
ST_Area(source.geom) as surface,
19+
source.geom,
20+
simplified.geom as simple_geom
1021
FROM
11-
{{ source('public', source_table_name) }}
22+
{{ source('public', source_table_name) }} as source
23+
LEFT JOIN
24+
simplified
25+
ON
26+
source.insee_reg = simplified.id_field
27+
1228
{% endmacro %}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{% macro simplify(source, geo_field, id_field, tolerance) %}
2+
sELECT * FROM (
3+
with poly as (
4+
select
5+
{{ id_field }} as id_field,
6+
(st_dump({{ geo_field }})).geom as geom
7+
from {{ source }}
8+
),
9+
10+
rings as (
11+
select
12+
st_exteriorRing((st_dumpRings(geom)).geom) as geom
13+
from poly
14+
),
15+
16+
merged as (
17+
select
18+
st_linemerge(st_union(geom)) as geom
19+
from rings
20+
),
21+
22+
simplified as (
23+
select
24+
(st_dump(st_simplifyPreserveTopology(geom, {{ tolerance }}))).geom as geom
25+
from merged
26+
),
27+
28+
polygonized as (
29+
select
30+
(st_dump(st_polygonize(distinct geom))).geom as geom
31+
from simplified
32+
), last as (
33+
select
34+
d.id_field,
35+
p.geom
36+
from polygonized p
37+
join poly d
38+
on st_intersects(d.geom, p.geom)
39+
where st_area(st_intersection(d.geom, p.geom)) / st_area(p.geom) > 0.5
40+
)
41+
SELECT id_field, ST_Union(geom) as geom
42+
FROM last
43+
GROUP BY id_field
44+
)
45+
46+
{% endmacro %}

airflow/include/sql/sparte/models/admin_express/land.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
}}
1010

1111

12-
{% set common_fields = "geom, surface" %}
12+
{% set common_fields = "geom, simple_geom, surface" %}
1313
{% set admin_express_common_fields = "name" %}
1414

1515
SELECT

airflow/include/sql/sparte/models/admin_express/schema.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ code_is_unique_and_not_null: &code_is_unique_and_not_null
77
data_tests:
88
- not_null
99
- unique
10+
- name: simple_geom
11+
data_tests:
12+
- not_null
13+
- is_not_empty_geom
14+
- is_valid_geom
1015

1116

1217
models:

airflow/include/sql/sparte/models/for_app/for_app_land.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ SELECT
1212
{{ m2_to_ha('land.surface') }} as surface,
1313
'ha' as surface_unit,
1414
ST_Transform(geom, 4326) as geom,
15+
ST_Transform(simple_geom, 4326) as simple_geom,
1516
{{ m2_to_ha('artif.surface') }} as surface_artif,
1617
artif.percent as percent_artif,
1718
artif.years as years_artif,

airflow/include/sql/sparte/models/sudocuh/models.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ models:
1616
- unique
1717
- is_valid_geom
1818
- is_not_empty_geom
19+
- name: simple_geom
20+
data_tests:
21+
- not_null
22+
- unique
23+
- is_not_empty_geom
24+
- is_valid_geom
1925
- name: scot
2026
columns:
2127
- name: id_scot

airflow/include/sql/sparte/models/sudocuh/scot.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ SELECT
5555
perimetre_en_cours_pcsuperficie,
5656
scot_geom.geom as geom,
5757
scot_geom.srid_source as srid_source,
58-
ST_Area(scot_geom.geom) as surface
58+
ST_Area(geom) as surface,
59+
simple_geom
5960
FROM {{ source('public', 'sudocuh_scot') }} as scot
6061
LEFT JOIN {{ ref('scot_geom') }} as scot_geom
6162
ON scot_geom.id_scot::text = scot.id_scot::text

0 commit comments

Comments
 (0)