46 lines
1.6 KiB
Python
46 lines
1.6 KiB
Python
from pycen import con_gn
|
|
|
|
sql = '''
|
|
DROP VIEW IF EXISTS gn_exports.v_synthese_zh_rhomeosite;
|
|
|
|
CREATE OR REPLACE VIEW gn_exports.v_synthese_zh_rhomeosite
|
|
AS WITH t1 AS (
|
|
SELECT t_zh.code,
|
|
l_areas.area_name,
|
|
st_area(st_intersection(t_zh.geom, l_areas.geom_4326)) AS area_intersect
|
|
FROM pr_zh.t_zh
|
|
JOIN ref_geo.l_areas ON st_intersects(t_zh.geom, l_areas.geom_4326)
|
|
JOIN ref_geo.bib_areas_types bib ON l_areas.id_type = bib.id_type and bib.type_name = 'Zones biogéographiques'
|
|
), t2 AS (
|
|
SELECT t1.code,
|
|
t1.area_name,
|
|
row_number() OVER (PARTITION BY t1.code ORDER BY t1.area_intersect DESC) AS ismax_ter
|
|
FROM t1
|
|
)
|
|
SELECT
|
|
t_zh.id_zh,
|
|
t_zh.main_name AS "NAME",
|
|
concat(t_roles.nom_role, ' ', t_roles.prenom_role) AS "REFERENT",
|
|
bib.nom_organisme AS "ORG",
|
|
CASE
|
|
WHEN t_zh.id_sage IS NULL
|
|
THEN ref_nomenclatures.get_cd_nomenclature(t_zh.id_sdage)::text
|
|
ELSE ref_nomenclatures.get_cd_nomenclature(t_zh.id_sage)::text
|
|
END AS "TYPE",
|
|
CASE
|
|
WHEN t2.area_name = 'alpin'::text THEN '1'::text
|
|
WHEN t2.area_name = 'continental'::text THEN '2'::text
|
|
WHEN t2.area_name = 'mediterraneen'::text THEN '4'::text
|
|
ELSE NULL::text
|
|
END AS "ODONATE",
|
|
ST_TRANSFORM(t_zh.geom,2154)::geometry(geometry,2154) geom
|
|
FROM pr_zh.t_zh
|
|
LEFT JOIN utilisateurs.t_roles ON t_zh.update_author = t_roles.id_role
|
|
LEFT JOIN utilisateurs.bib_organismes bib USING (id_organisme)
|
|
JOIN t2 ON t_zh.code::text = t2.code::text AND t2.ismax_ter = 1;
|
|
'''
|
|
|
|
with con_gn.begin() as cnx:
|
|
cnx.execute(sql)
|
|
|