Skip to content

Commit 479decc

Browse files
Merge pull request #26 from PnX-SI/dev
Dev > Main / 1.1.1
2 parents 1377dbc + 7ce8ce7 commit 479decc

File tree

5 files changed

+210
-3
lines changed

5 files changed

+210
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.1.1 (2023-06-06)
4+
5+
- Remplacement de la vue matérialisée `pr_zh.atlas_app` par une vue (utilisée par la route `/api/zones_humides/pbf/complete`) pour corriger et simplifier la mise à jour des données de l'[atlas des zones humides](https://github.com/PnX-SI/GeoNature-ZH-atlas) (#24)
6+
37
## 1.1.0 - Taillefer (2023-06-02)
48

59
Nécessite la version 2.12.0 (ou plus) de GeoNature.

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.1.0
1+
1.1.1

backend/gn_module_zh/blueprint.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,7 @@ def get_pbf_complete():
366366
"""
367367
query = DB.session.execute(sql)
368368
row = query.first()
369-
if row["pbf"]:
370-
return Response(bytes(row["pbf"]), mimetype="application/protobuf")
369+
return Response(bytes(row["pbf"]) if row["pbf"] else bytes(), mimetype="application/protobuf")
371370

372371

373372
@blueprint.route("/geojson", methods=["GET"])
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""add indexes
2+
3+
Revision ID: 26d6515219fe
4+
Revises: 22b14fc3abe0
5+
Create Date: 2023-06-05 12:07:39.416188
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = "26d6515219fe"
14+
down_revision = "22b14fc3abe0"
15+
branch_labels = None
16+
depends_on = None
17+
18+
SCHEMA = "pr_zh"
19+
20+
21+
def upgrade():
22+
op.create_index("index_t_zh_id_sdage", table_name="t_zh", columns=["id_sdage"], schema=SCHEMA)
23+
op.create_index(
24+
"index_t_zh_id_thread", table_name="t_zh", columns=["id_thread"], schema=SCHEMA
25+
)
26+
op.create_index(
27+
"index_t_zh_id_diag_bio", table_name="t_zh", columns=["id_diag_bio"], schema=SCHEMA
28+
)
29+
op.create_index(
30+
"index_t_zh_id_diag_hydro", table_name="t_zh", columns=["id_diag_hydro"], schema=SCHEMA
31+
)
32+
op.create_index(
33+
"index_t_zh_id_lim_list", table_name="t_zh", columns=["id_lim_list"], schema=SCHEMA
34+
)
35+
op.create_index("index_t_zh_id_zh_uuid", table_name="t_zh", columns=["zh_uuid"], schema=SCHEMA)
36+
op.create_index(
37+
"index_cor_zh_rb_id_zh", table_name="cor_zh_rb", columns=["id_zh"], schema=SCHEMA
38+
)
39+
op.create_index(
40+
"index_cor_lim_list_id_lim_list",
41+
table_name="cor_lim_list",
42+
columns=["id_lim_list"],
43+
schema=SCHEMA,
44+
)
45+
op.create_index(
46+
"index_cor_zh_area_id_zh", table_name="cor_zh_area", columns=["id_zh"], schema=SCHEMA
47+
)
48+
op.create_index(
49+
"index_cor_zh_area_id_area", table_name="cor_zh_area", columns=["id_area"], schema=SCHEMA
50+
)
51+
52+
53+
def downgrade():
54+
op.drop_index(
55+
"index_t_zh_id_sdage",
56+
table_name="t_zh",
57+
schema=SCHEMA,
58+
)
59+
op.drop_index(
60+
"index_t_zh_id_thread",
61+
table_name="t_zh",
62+
schema=SCHEMA,
63+
)
64+
op.drop_index(
65+
"index_t_zh_id_diag_bio",
66+
table_name="t_zh",
67+
schema=SCHEMA,
68+
)
69+
op.drop_index(
70+
"index_t_zh_id_diag_hydro",
71+
table_name="t_zh",
72+
schema=SCHEMA,
73+
)
74+
op.drop_index(
75+
"index_t_zh_id_lim_list",
76+
table_name="t_zh",
77+
schema=SCHEMA,
78+
)
79+
op.drop_index(
80+
"index_t_zh_id_zh_uuid",
81+
table_name="t_zh",
82+
schema=SCHEMA,
83+
)
84+
op.drop_index(
85+
"index_cor_zh_rb_id_zh",
86+
table_name="cor_zh_rb",
87+
schema=SCHEMA,
88+
)
89+
op.drop_index(
90+
"index_cor_lim_list_id_lim_list",
91+
table_name="cor_lim_list",
92+
schema=SCHEMA,
93+
)
94+
op.drop_index(
95+
"index_cor_zh_area_id_zh",
96+
table_name="cor_zh_area",
97+
schema=SCHEMA,
98+
)
99+
op.drop_index(
100+
"index_cor_zh_area_id_area",
101+
table_name="cor_zh_area",
102+
schema=SCHEMA,
103+
)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
"""add atlas view
2+
3+
Revision ID: 643743e807f6
4+
Revises: 26d6515219fe
5+
Create Date: 2023-06-05 12:20:36.897280
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = "643743e807f6"
14+
down_revision = "26d6515219fe"
15+
branch_labels = None
16+
depends_on = None
17+
18+
# This revision performs 2 things:
19+
# - replaces the materialized view by a view
20+
# - treats better the downgrade part by removing the slugify function
21+
# used only for the view (thus this function need to be re-created)
22+
23+
24+
def upgrade():
25+
# Recreate function here if it is dropped by the
26+
op.execute(
27+
"""
28+
CREATE OR REPLACE FUNCTION pr_zh.slugify("value" TEXT)
29+
RETURNS TEXT AS $$
30+
-- removes accents (diacritic signs) from a given string --
31+
WITH "unaccented" AS (
32+
SELECT unaccent("value") AS "value"
33+
),
34+
-- lowercases the string
35+
"lowercase" AS (
36+
SELECT lower("value") AS "value"
37+
FROM "unaccented"
38+
),
39+
-- replaces anything that's not a letter, number, hyphen('-'), or underscore('_') with a hyphen('-')
40+
"hyphenated" AS (
41+
SELECT regexp_replace("value", '[^a-z0-9\\-_]+', '-', 'gi') AS "value"
42+
FROM "lowercase"
43+
),
44+
-- trims hyphens('-') if they exist on the head or tail of the string
45+
"trimmed" AS (
46+
SELECT regexp_replace(regexp_replace("value", '\\-+$', ''), '^\\-', '') AS "value"
47+
FROM "hyphenated"
48+
)
49+
SELECT "value" FROM "trimmed";
50+
$$ LANGUAGE SQL STRICT IMMUTABLE;
51+
"""
52+
)
53+
op.execute("DROP MATERIALIZED VIEW IF EXISTS pr_zh.atlas_app")
54+
op.execute(
55+
"""
56+
CREATE OR REPLACE VIEW pr_zh.atlas_app
57+
AS SELECT tzh.id_zh AS id,
58+
tzh.main_name AS nom,
59+
( SELECT pr_zh.slugify(tzh.main_name::text) AS slugify) AS slug,
60+
tzh.code,
61+
tzh.create_date AS date,
62+
tzh.geom AS polygon_4326,
63+
( SELECT st_area(st_transform(st_setsrid(tzh.geom, 4326), 2154)) AS st_area) AS superficie,
64+
bo.nom_organisme AS operateur,
65+
sdage.cd_nomenclature AS type_code,
66+
sdage.mnemonique AS type,
67+
thread.mnemonique AS menaces,
68+
diag_bio.mnemonique AS diagnostic_bio,
69+
diag_hydro.mnemonique AS diagnostic_hydro,
70+
( SELECT array_agg(DISTINCT tn.mnemonique) AS array_agg) AS criteres_delim,
71+
( SELECT array_agg(DISTINCT la.area_name) AS array_agg) AS communes,
72+
( SELECT array_agg(DISTINCT trb.name) AS array_agg) AS bassin_versant,
73+
( SELECT COALESCE(json_agg(t.*), '[]'::json) AS "coalesce"
74+
FROM ( SELECT t_medias.title_fr AS label,
75+
t_medias.media_path AS url
76+
FROM gn_commons.t_medias
77+
WHERE t_medias.uuid_attached_row = tzh.zh_uuid) t) AS images
78+
FROM pr_zh.t_zh tzh
79+
LEFT JOIN pr_zh.cor_lim_list cll ON tzh.id_lim_list = cll.id_lim_list
80+
LEFT JOIN ref_nomenclatures.t_nomenclatures tn ON cll.id_lim = tn.id_nomenclature
81+
LEFT JOIN pr_zh.cor_zh_area cza ON tzh.id_zh = cza.id_zh
82+
LEFT JOIN ref_geo.l_areas la ON cza.id_area = la.id_area
83+
LEFT JOIN pr_zh.cor_zh_rb czr ON tzh.id_zh = czr.id_zh
84+
LEFT JOIN pr_zh.t_river_basin trb ON czr.id_rb = trb.id_rb
85+
LEFT JOIN gn_commons.t_medias med ON med.uuid_attached_row = tzh.zh_uuid
86+
LEFT JOIN utilisateurs.t_roles tr ON tr.id_role = tzh.create_author
87+
LEFT JOIN utilisateurs.bib_organismes bo ON bo.id_organisme = tr.id_organisme
88+
LEFT JOIN ref_nomenclatures.t_nomenclatures sdage ON sdage.id_nomenclature = tzh.id_sdage
89+
LEFT JOIN ref_nomenclatures.t_nomenclatures thread ON thread.id_nomenclature = tzh.id_thread
90+
LEFT JOIN ref_nomenclatures.t_nomenclatures diag_bio ON diag_bio.id_nomenclature = tzh.id_diag_bio
91+
LEFT JOIN ref_nomenclatures.t_nomenclatures diag_hydro ON diag_hydro.id_nomenclature = tzh.id_diag_hydro
92+
WHERE cza.cover IS NOT NULL
93+
GROUP BY tzh.id_zh, bo.nom_organisme, sdage.cd_nomenclature, sdage.mnemonique, thread.mnemonique, diag_bio.mnemonique, diag_hydro.mnemonique
94+
ORDER BY tzh.id_zh;
95+
"""
96+
)
97+
98+
99+
def downgrade():
100+
op.execute("DROP VIEW pr_zh.atlas_app")
101+
op.execute("DROP FUNCTION pr_zh.slugify")

0 commit comments

Comments
 (0)