Skip to content

Commit 6e7a2b6

Browse files
authored
Merge pull request #950 from MTES-MCT/feat-imper-for-diag
Ajout du precalcul des données des graphiques d'imperméabilisation
2 parents c9216e2 + 6e6d648 commit 6e7a2b6

File tree

64 files changed

+417
-8
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+417
-8
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{% macro merge_imper_commune_by_sol(sol) %}
2+
3+
4+
{% set code_sol = "code_cs" if sol == 'couverture' else "code_us" %}
5+
6+
with
7+
without_percent as (
8+
select
9+
commune_code,
10+
year,
11+
sum(percent) as percent_of_commune,
12+
sum(surface) as surface,
13+
{{ code_sol }} as {{ sol }}
14+
from {{ ref("commune_couverture_et_usage") }}
15+
where is_impermeable
16+
group by commune_code, year, {{ code_sol }}
17+
)
18+
select
19+
without_percent.*,
20+
(without_percent.surface / imper_commune.surface) * 100 as percent_of_imper
21+
from without_percent
22+
left join
23+
{{ ref("imper_commune") }}
24+
on without_percent.commune_code = imper_commune.commune_code
25+
and without_percent.year = imper_commune.year
26+
27+
{% endmacro %}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{% macro merge_imper_commune_by_sol_and_admin_level(group_by_column, sol) %}
2+
3+
4+
with without_percent as (
5+
SELECT
6+
{{ group_by_column }},
7+
year,
8+
{{ sol }},
9+
sum(imper_commune_by_{{ sol }}.surface) as impermeable_surface,
10+
sum(commune.surface) as surface,
11+
array_agg(distinct commune.departement) as departements
12+
FROM
13+
{{ ref('imper_commune_by_' + sol) }}
14+
LEFT JOIN
15+
{{ ref('commune') }}
16+
ON imper_commune_by_{{ sol }}.commune_code = commune.code
17+
WHERE
18+
{{ group_by_column }} IS NOT NULL
19+
GROUP BY
20+
{{ group_by_column }}, {{ sol }}, year
21+
)
22+
SELECt
23+
without_percent.{{ group_by_column }},
24+
without_percent.year,
25+
without_percent.impermeable_surface / without_percent.surface * 100 as percent_of_{{ group_by_column }},
26+
without_percent.impermeable_surface as surface,
27+
without_percent.{{ sol }},
28+
(100 * without_percent.impermeable_surface) / imper_{{ group_by_column }}.impermeable_surface
29+
as percent_of_imper
30+
FROM without_percent
31+
LEFT JOIN
32+
{{ ref('imper_' + group_by_column) }} as imper_{{ group_by_column }}
33+
ON without_percent.{{ group_by_column }} = imper_{{ group_by_column }}.{{ group_by_column }}
34+
AND without_percent.year = imper_{{ group_by_column }}.year
35+
36+
37+
{% endmacro %}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{% macro merge_imper_commune_flux_by_admin_level(group_by_column) %}
2+
3+
with without_percent as (
4+
SELECT
5+
commune.{{ group_by_column }},
6+
year_old,
7+
year_new,
8+
sum(imper_commune.flux_imper) as flux_imper,
9+
sum(imper_commune.flux_desimper) as flux_desimper,
10+
sum(commune.surface) as surface,
11+
array_agg(distinct commune.departement) as departements
12+
FROM
13+
{{ ref('imper_net_flux_commune') }} as imper_commune
14+
LEFT JOIN
15+
{{ ref('commune') }}
16+
ON imper_commune.commune_code = commune.code
17+
WHERE
18+
commune.{{ group_by_column }} IS NOT NULL
19+
GROUP BY
20+
commune.{{ group_by_column }}, year_old, year_new
21+
)
22+
SELECt
23+
without_percent.{{ group_by_column }},
24+
without_percent.departements,
25+
without_percent.surface as {{ group_by_column }}_surface,
26+
without_percent.year_old,
27+
without_percent.year_new,
28+
without_percent.flux_imper as flux_imper,
29+
without_percent.flux_desimper as flux_desimper,
30+
without_percent.flux_imper - without_percent.flux_desimper as flux_imper_net
31+
FROM without_percent
32+
33+
{% endmacro %}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{% macro merge_imper_commune_flux_by_sol(sol) %}
2+
3+
4+
{% set code_sol_old = "cs_old" if sol == 'couverture' else "us_old" %}
5+
{% set code_sol_new = "cs_new" if sol == 'couverture' else "us_new" %}
6+
7+
8+
with
9+
imper as (
10+
select
11+
commune_code,
12+
commune_surface,
13+
year_old,
14+
year_new,
15+
{{ code_sol_new }} as {{ sol }},
16+
sum(surface) as surface_imper
17+
from {{ ref("commune_flux_couverture_et_usage") }}
18+
where new_is_impermeable
19+
group by
20+
commune_code,
21+
commune_surface,
22+
year_old,
23+
year_new,
24+
new_is_impermeable,
25+
{{ code_sol_new }}
26+
),
27+
desimper as (
28+
select
29+
commune_code,
30+
commune_surface,
31+
year_old,
32+
year_new,
33+
{{ code_sol_old }} as {{ sol }},
34+
sum(surface) as surface_desimper
35+
from {{ ref("commune_flux_couverture_et_usage") }}
36+
where new_not_impermeable
37+
group by
38+
commune_code,
39+
commune_surface,
40+
year_old,
41+
year_new,
42+
new_is_impermeable,
43+
{{ code_sol_old }}
44+
)
45+
select
46+
coalesce(imper.commune_code, desimper.commune_code) as commune_code,
47+
coalesce(imper.commune_surface, desimper.commune_surface) as commune_surface,
48+
coalesce(imper.year_old, desimper.year_old) as year_old,
49+
coalesce(imper.year_new, desimper.year_new) as year_new,
50+
coalesce(imper.{{ sol }}, desimper.{{ sol }}) as {{ sol }},
51+
coalesce(imper.surface_imper, 0) as flux_imper,
52+
coalesce(desimper.surface_desimper, 0) as flux_desimper
53+
from imper
54+
full outer join
55+
desimper
56+
on imper.commune_code = desimper.commune_code
57+
and imper.{{ sol }} = desimper.{{ sol }}
58+
and imper.year_old = desimper.year_old
59+
and imper.year_new = desimper.year_new
60+
61+
{% endmacro %}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{% macro merge_imper_commune_flux_by_sol_and_admin_level(group_by_column, sol) %}
2+
3+
-- TODO : test
4+
5+
6+
with without_percent as (
7+
SELECT
8+
{{ group_by_column }},
9+
year_old,
10+
year_new,
11+
{{ sol }},
12+
sum(imper_flux_commune_by_{{ sol }}.flux_imper) as flux_imper,
13+
sum(imper_flux_commune_by_{{ sol }}.flux_desimper) as flux_desimper,
14+
sum(commune.surface) as surface,
15+
array_agg(distinct commune.departement) as departements
16+
FROM
17+
{{ ref('imper_flux_commune_by_' + sol) }}
18+
LEFT JOIN
19+
{{ ref('commune') }}
20+
ON imper_flux_commune_by_{{ sol }}.commune_code = commune.code
21+
WHERE
22+
{{ group_by_column }} IS NOT NULL
23+
GROUP BY
24+
{{ group_by_column }}, {{ sol }}, year_old, year_new
25+
)
26+
SELECt
27+
without_percent.{{ group_by_column }},
28+
without_percent.departements,
29+
without_percent.surface as {{ group_by_column }}_surface,
30+
without_percent.year_old,
31+
without_percent.year_new,
32+
without_percent.{{ sol }},
33+
without_percent.flux_imper as flux_imper,
34+
without_percent.flux_desimper as flux_desimper
35+
FROM
36+
without_percent
37+
{% endmacro %}

airflow/include/sql/sparte/models/ocsge/calculated/flux/artif_flux_commune.sql renamed to airflow/include/sql/sparte/models/ocsge/calculated/flux/artif/artif_flux_commune.sql

File renamed without changes.

airflow/include/sql/sparte/models/ocsge/calculated/flux/artif_net_flux_commune.sql renamed to airflow/include/sql/sparte/models/ocsge/calculated/flux/artif/artif_net_flux_commune.sql

File renamed without changes.

airflow/include/sql/sparte/models/ocsge/calculated/flux/artif_net_flux_departement.sql renamed to airflow/include/sql/sparte/models/ocsge/calculated/flux/artif/artif_net_flux_departement.sql

File renamed without changes.

airflow/include/sql/sparte/models/ocsge/calculated/flux/artif_net_flux_epci.sql renamed to airflow/include/sql/sparte/models/ocsge/calculated/flux/artif/artif_net_flux_epci.sql

File renamed without changes.

airflow/include/sql/sparte/models/ocsge/calculated/flux/artif_net_flux_region.sql renamed to airflow/include/sql/sparte/models/ocsge/calculated/flux/artif/artif_net_flux_region.sql

File renamed without changes.

0 commit comments

Comments
 (0)