new scripts

This commit is contained in:
Colas Geier 2026-04-28 14:17:26 +02:00
parent 14c0ae4f5b
commit ed227b4abc
6 changed files with 295 additions and 0 deletions

View File

@ -0,0 +1,36 @@
import pandas as pd
import geopandas as gpd
from pycen import con_gn
old = gpd.read_file('/home/cgeier/Téléchargements/Telechargement_1772622390_2696/data/n_zone_humide_s_038/n_zone_humide_s_038.json')
new = gpd.read_postgis('SELECT * FROM gn_exports.v_synthese_zones_humides_isere',con_gn).to_crs(2154)
tab = pd.read_csv('/home/cgeier/Documents/9_PROJETS/1_ZH/2024/RENDU SIG/pv_evolution_dossier.csv',sep=',')
for etude in tab.nom_etude.unique():
if tab[(tab.nom_etude==etude)].organisme_transmission.any():
continue
print('\n',etude)
news = tab[(tab.nom_etude==etude) & (tab.status_inventaire == 'Nouvelle')]
modif = tab[(tab.nom_etude==etude) & (tab.status_inventaire== 'Modification')]
supp = tab[(tab.nom_etude==etude) & (tab.status_inventaire == 'Supression')]
new_add = new[new.code.isin(news.code_zh.tolist())].copy()
del_zh = old[old.id_local.isin(supp.code_zh.tolist())]
print('\tNouvelles surfaces ajoutées : %f ha'%(round(new_add.area.sum()/10000,2)),' ; %i zh'%new_add.shape[0],' ; \n\tcode : ',', '.join(list(new_add.code)))
print('\tSurfaces supprimées : %f ha'%(round(del_zh.area.sum()/10000,2)),' ; %i zh'%del_zh.shape[0],' ; \n\tcode : ',', '.join(list(del_zh.id_local)))
code_modif = modif.code_zh.tolist()
diff = (new[new.code.isin(code_modif)]
.sort_values('code').set_index('code').area - old[old.id_local.isin(code_modif)]
.sort_values('id_local').set_index('id_local').area
)
add = diff[diff > 0]
red = diff[diff < 0]
print('\tModification surfaces ajoutées : %f ha'%(round(add.sum()/10000,2)),' ; %i zh'%add.shape[0],' ; \n\tcode : ',', '.join(list(add.index)))
print('\tModification surfaces perdues : %f ha'%(round(red.sum()/10000,2)),' ; %i zh'%red.shape[0],' ; \n\tcode : ',', '.join(list(red.index)))

View File

@ -0,0 +1,61 @@
from pycen import con
sql1 = """
DROP VIEW IF EXISTS zones_humides.v_zh_ponctuelles;
CREATE VIEW zones_humides.v_zh_ponctuelles AS
SELECT
id,
territoire,
commune,
lieu_dit,
observer,
datemodif,
x,
y,
code_insee,
descripti2,
geom,
qui,
statut,
source,
comment,
type,
ponctuelle,
classe
FROM zones_humides.zh_ponctuelles
WHERE in_zh is False
"""
with con.begin() as cnx:
cnx.execute(sql1)
sql1 = """
DROP VIEW IF EXISTS zones_humides.v_zh_ponctuelles;
CREATE VIEW zones_humides.v_zh_ponctuelles AS
SELECT
id,
territoire,
commune,
lieu_dit,
observer,
datemodif,
x,
y,
code_insee,
descripti2,
geom,
qui,
statut,
source,
comment,
type,
ponctuelle,
classe
FROM zones_humides.zh_ponctuelles
WHERE in_zh is True
"""
with con.begin() as cnx:
cnx.execute(sql1)

View File

@ -0,0 +1,41 @@
from pycen import con_gn
sql = '''
DROP VIEW IF EXISTS gn_exports.v_eee_n2000_platiere;
CREATE OR REPLACE VIEW gn_exports.v_eee_n2000_platiere
AS
SELECT
v.*
FROM gn_synthese.v_synthese_for_export v
JOIN ref_geo.l_areas ON st_intersects(ST_GeomFromGeoJSON(v.geojson_4326),st_transform(l_areas.geom,4326))
JOIN ref_geo.bib_areas_types bib USING (id_type)
WHERE bib.type_code = 'ZPS'
AND area_name ilike '%%plati%%re%%'
AND v.cd_ref in (100330, 101055, 101237, 101286, 103139, 103543, 103545, 103547,
103557, 104074, 104805, 105017, 105433, 105615, 105689, 105960,
106252, 106571, 106742, 106748, 106754, 106965, 107446, 108628,
108642, 108810, 109141, 109911, 109926, 109954, 109980, 110762,
111863, 111881, 112100, 112111, 112130, 112195, 112463, 112467,
112482, 112712, 112790, 113418, 114024, 115527, 116089, 116137,
116485, 117503, 117505, 117723, 117860, 117937, 118477, 119474,
119558, 119595, 119854, 122630, 124025, 124164, 124168, 124378,
124635, 124646, 124719, 124730, 125331, 128504, 128748, 128863,
128954, 129468, 130484, 130491, 159690, 159937, 4419, 446169,
448413, 454932, 4872, 611690, 611753, 612522, 717136, 717180,
79766, 79877, 80086, 80824, 81955, 81978, 82018, 82080,
82093, 82164, 83938, 84057, 84251, 85186, 85469, 85949,
85957, 86167, 86513, 86817, 86869, 86975, 89452, 90192,
90234, 92572, 92649, 92663, 92793, 93020, 93129, 93613,
93923, 93924, 94168, 94489, 95679, 95823, 95831, 95965,
95975, 95980, 96149, 96585, 96644, 96739, 96749, 96775,
97346, 97594, 97666, 97961, 99260, 99359, 95983, 96814,
117507, 129959, 81992, 90532, 97623, 104353, 111886, 112483,
125369, 109037, 96624, 101056, 106800, 109949, 116762, 125324
)
;
'''
with con_gn.begin() as cnx:
cnx.execute(sql)

View File

@ -0,0 +1,45 @@
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)

View File

@ -0,0 +1,98 @@
import geopandas as gpd
from pycen import con_gn
uuid_gn38 = gpd.pd.read_sql('SELECT unique_id_sinp uuid_perm_sinp FROM gn_synthese.synthese',con_gn)
lst_gn38 = uuid_gn38.uuid_perm_sinp.astype(str).tolist()
PATH = '/media/cgeier/SRV/FICHIERS'
biodiv = gpd.read_file(PATH+'/OUTILS/CARTOGRAPHIE/ESPACE DE TRAVAIL/SITES/Lone de la Negria/Faune-Flore/Données partenaires/0.Données transmises/BiodivAura_Synthese_Export_2026-04-21.geojson')
cenra_point = gpd.read_file(PATH+'/OUTILS/CARTOGRAPHIE/ESPACE DE TRAVAIL/SITES/Lone de la Negria/Faune-Flore/Données partenaires/0.Données transmises/CENRA_20260421_gn_export_lones_cen38.gpkg',layer = 'points')
cenra_poly = gpd.read_file(PATH+'/OUTILS/CARTOGRAPHIE/ESPACE DE TRAVAIL/SITES/Lone de la Negria/Faune-Flore/Données partenaires/0.Données transmises/CENRA_20260421_gn_export_lones_cen38.gpkg',layer = 'polygones')
# synt = gpd.pd.read_csv(PATH+'/OUTILS/CARTOGRAPHIE/ESPACE DE TRAVAIL/SITES/Lone de la Negria/Faune-Flore/Données partenaires/0.Données transmises/synthese_observations_2025-04-24T12_11_45.488Z (completer).csv',sep=',')
# synt['code_com'] = synt.communes.str.split('(',expand=True)[1].str[:5]
# synt['code_dep'] = synt.communes.str.split('(',expand=True)[1].str[:2]
# synt.nombre_min = synt.nombre_min.astype(str).str.strip('.0').replace('nan',None)
# synt.nombre_max = synt.nombre_max.astype(str).str.strip('.0').replace('nan',None)
# synt.precision_geographique = synt.precision_geographique.astype(str).str.strip('.0').replace('nan',None)
# synt.alti_min = synt.alti_min.astype(str).str.strip('.0').replace('nan',None)
for cenra in [cenra_point,cenra_poly]:
test_nb_cenra = cenra.nombre_min.astype('float') > cenra.nombre_max.astype('float')
if test_nb_cenra.any():
cenra.loc[test_nb_cenra,'nb_tmp'] = cenra[test_nb_cenra].nombre_min
cenra.loc[test_nb_cenra,'nombre_min'] = cenra[test_nb_cenra].nombre_max
cenra.loc[test_nb_cenra,'nombre_max'] = cenra[test_nb_cenra].nb_tmp
cenra.drop(columns='nb_tmp',inplace=True)
# cenra.nombre_min = cenra.nombre_min.astype(str).str.strip('.0').replace('nan',None)
# cenra.nombre_max = cenra.nombre_max.astype(str).str.strip('.0').replace('nan',None)
cenra.profondeur_min = cenra.profondeur_min.astype(str).str.strip('.0').replace('nan',None)
cenra.profondeur_max = cenra.profondeur_max.astype(str).str.strip('.0').replace('nan',None)
cenra.precision = cenra.precision.astype(str).str.strip('.0').replace('nan',None)
cenra.code_habitat = cenra.code_habitat.astype(str).str.strip('.0').replace('nan',None)
# cenra.altitude_min = cenra.altitude_min.astype(str).str.strip('.0').replace('nan',None)
# cenra.altitude_max = cenra.altitude_max.astype(str).str.strip('.0').replace('nan',None)
cenra.precision_diffusion.fillna('Non déterminé', inplace=True)
# synt_l93 = synt[synt.x_centroid_4326>500000]
# synt_l93_geom = gpd.GeoDataFrame(synt_l93,geometry=gpd.geoseries.points_from_xy(synt_l93.x_centroid_4326,synt_l93.y_centroid_4326),crs=2154)
# synt_l93_geom.to_crs(4326,inplace=True)
# synt_l93_geom.x_centroid_4326 = synt_l93_geom.geometry.x
# synt_l93_geom.y_centroid_4326 = synt_l93_geom.geometry.y
# synt_w84 = synt[synt.x_centroid_4326<500000]
# synt = gpd.pd.concat([synt_l93_geom,synt_w84])
biodiv.nombre_min = biodiv.nombre_min.astype(str).str.strip('.0').replace('nan',None).replace('',None)
biodiv.nombre_max = biodiv.nombre_max.astype(str).str.strip('.0').replace('nan',None).replace('',None)
biodiv.alti_min = biodiv.alti_min.astype(str).str.strip('.0').replace('nan',None)
biodiv.precision_geographique = biodiv.precision_geographique.astype(str).str.strip('.0').replace('nan',None)
biodiv.observateurs.fillna('Inconnu', inplace=True)
biodiv.stade_vie.fillna('Inconnu', inplace=True)
biodiv.loc[biodiv.cd_ref == 1078938, 'cd_ref'] = 5289 # Geheebia lurida (Hornsch. ex Spreng.) J.A.Jiménez & M.J.Cano, 2021
biodiv.loc[biodiv.cd_ref == 1049429, 'cd_ref'] = 108898 # Muscari neglectum Guss. ex Ten. & Sangiov., 1841
test_nb_biodiv = biodiv.nombre_min.astype('float') > biodiv.nombre_max.astype('float')
if test_nb_biodiv.any():
biodiv.loc[test_nb_biodiv,'nb_tmp'] = biodiv[test_nb_biodiv].nombre_min
biodiv.loc[test_nb_biodiv,'nombre_min'] = biodiv[test_nb_biodiv].nombre_max
biodiv.loc[test_nb_biodiv,'nombre_max'] = biodiv[test_nb_biodiv].nb_tmp
biodiv.drop(columns='nb_tmp',inplace=True)
lst_cenra = [
*cenra_point.id_perm_sinp.tolist(),
*cenra_poly.id_perm_sinp.tolist()
]
# synt = synt[~synt.uuid_perm_sinp.isin(lst_cenra)]
biodiv = biodiv[~biodiv.uuid_perm_sinp.isin(lst_cenra)]
biodiv = biodiv[~biodiv.uuid_perm_sinp.isin(lst_cenra)]
# lst_synt = synt.uuid_perm_sinp.tolist()
lst_biodiv = biodiv.uuid_perm_sinp.tolist()
# biodiv = biodiv[~biodiv.uuid_perm_sinp.isin(lst_synt)]
biodiv = biodiv[~biodiv.uuid_perm_sinp.isin(lst_gn38)]
# synt[~synt.uuid_perm_sinp.isin(lst_biodiv)]
from shapely.geometry import shape
import json
biodiv['geom'] = biodiv.apply(lambda row: shape(json.loads(row['geojson_4326'])), axis=1).set_crs(4326)
biodiv.set_geometry('geom',inplace=True)
biodiv.drop(columns='geometry',inplace=True)
biodiv['geom_4326'] = biodiv.geom.to_wkt()
gpd.pd.concat([cenra_point,cenra_poly]).to_csv(PATH+'/OUTILS/CARTOGRAPHIE/ESPACE DE TRAVAIL/SITES/Lone de la Negria/Faune-Flore/Données partenaires/1.Données nettoyées/CENRA_20260421_gn_export_lones_cen38.csv',index=False)
# cenra.to_csv(PATH+'/OUTILS/CARTOGRAPHIE/ESPACE DE TRAVAIL/SITES/Lone de la Negria/Faune-Flore/Données partenaires/1.Données nettoyées/CENRA_synthese_observations_geonature_2026-03-16T15_25_59.351Z.csv',index=False)
# synt.to_csv(PATH+'/OUTILS/CARTOGRAPHIE/ESPACE DE TRAVAIL/SITES/Lone de la Negria/Faune-Flore/Données partenaires/1.Données nettoyées/synthese_observations_2025-04-24T12_11_45.488Z.csv',index=False)
biodiv.to_csv(PATH+'/OUTILS/CARTOGRAPHIE/ESPACE DE TRAVAIL/SITES/Lone de la Negria/Faune-Flore/Données partenaires/1.Données nettoyées/BiodivAura_Synthese_Export_2026-04-21.csv',index=False)
gpd.pd.concat([cenra_point,cenra_poly]).to_file(PATH+'/OUTILS/CARTOGRAPHIE/ESPACE DE TRAVAIL/SITES/Lone de la Negria/Faune-Flore/Données partenaires/1.Données nettoyées/CENRA_20260421_gn_export_lones_cen38.geojson',index=False)
# cenra.to_file(PATH+'/OUTILS/CARTOGRAPHIE/ESPACE DE TRAVAIL/SITES/Lone de la Negria/Faune-Flore/Données partenaires/1.Données nettoyées/CENRA_synthese_observations_geonature_2026-03-16T15_25_59.351Z.geojson',index=False)
# synt.to_file(PATH+'/OUTILS/CARTOGRAPHIE/ESPACE DE TRAVAIL/SITES/Lone de la Negria/Faune-Flore/Données partenaires/1.Données nettoyées/synthese_observations_2025-04-24T12_11_45.488Z.geojson',index=False)
# synt_geom = gpd.GeoDataFrame(synt,geometry=gpd.geoseries.points_from_xy(synt.x_centroid_4326,synt.y_centroid_4326),crs=4326)
# synt_geom.to_file(PATH+'/OUTILS/CARTOGRAPHIE/ESPACE DE TRAVAIL/SITES/Lone de la Negria/Faune-Flore/Données partenaires/1.Données nettoyées/synthese_observations_2025-04-24T12_11_45.488Z.geojson',index=False)
biodiv.to_file(PATH+'/OUTILS/CARTOGRAPHIE/ESPACE DE TRAVAIL/SITES/Lone de la Negria/Faune-Flore/Données partenaires/1.Données nettoyées/BiodivAura_Synthese_Export_2026-04-21.geojson',index=False)

14
order_polygon.py Normal file
View File

@ -0,0 +1,14 @@
import geopandas as gpd
from os import path
PATH = '/media/cgeier/SRV/FICHIERS'
file = 'OUTILS/CARTOGRAPHIE/ESPACE DE TRAVAIL/SITES/Lone de la Negria/Faune-Flore/Synthèse/synthese_observations_2026-04-23.geojson'
df = gpd.read_file(path.join(PATH,file), use_arrow=True)
df['ha'] = df.to_crs(2154).area
df.sort_values('ha', ascending=False, inplace=True)
df.reset_index(inplace=True)
df.drop(columns='ha', inplace=True)
df.to_file(path.join(PATH,file))