init depot renew
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
from pycen import zh
|
||||
import geopandas as gpd
|
||||
import os
|
||||
|
||||
def get_intersected(df1,df2):
|
||||
is_intersect = df1.intersects(df2.unary_union)
|
||||
return df1[is_intersect].copy()
|
||||
|
||||
def format_data(df):
|
||||
istype_date = df.dtypes == 'datetime64[ns]'
|
||||
is_date = df.columns.str.contains('date|heure',case=False,regex=True)
|
||||
col_date = df.columns[istype_date|is_date]
|
||||
df[col_date] = df[col_date].astype(str)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
PATH = '/home/colas/Documents/tmp/PNE'
|
||||
file_pne = 'fwdcoucheszoneshumidesisroises/PNE_aoa.shp'
|
||||
|
||||
zh = zh()
|
||||
v_zh = zh.v_zoneshumides()
|
||||
zhp = zh.zh_ponctuelles()
|
||||
pne = gpd.read_file(os.path.join(PATH,file_pne))
|
||||
vzh_pne = get_intersected(v_zh,pne)
|
||||
zhp_pne = get_intersected(zhp,pne)
|
||||
|
||||
format_data(vzh_pne)
|
||||
format_data(zhp_pne)
|
||||
|
||||
vzh_pne.drop(['old_code','type_site'],axis=1).to_file(os.path.join(PATH,'PNE_ZH.gpkg'),driver='GPKG',layer='inventaire_zh')
|
||||
zhp_pne.to_file(os.path.join(PATH,'PNE_ZH.gpkg'),driver='GPKG',layer='zh_ponctuelle')
|
||||
@@ -0,0 +1,41 @@
|
||||
import geopandas as gpd
|
||||
from os import path
|
||||
|
||||
PATH = '/home/cgeier/Documents/9_PROJETS/2_PS/2026/Anne BELET - couchepelousessches'
|
||||
anne = gpd.read_file(path.join(PATH,'couche_finale_modif_CEN.gpkg')).set_index('site_code')
|
||||
mosa = gpd.read_file(path.join(PATH,'couche_finale_modif_mosaique.gpkg')).set_index('site_code')
|
||||
call = gpd.read_file(path.join(PATH,'couche_finale2.gpkg')).set_index('site_code')
|
||||
|
||||
date_cols = anne.columns[anne.columns.str.contains('date')]
|
||||
for tab in [anne,mosa,call]:
|
||||
for d in date_cols :
|
||||
tab[d] = (gpd.pd.to_datetime(tab[d],format='mixed',utc=True)
|
||||
.astype(str)
|
||||
.str.split(' ', expand=True)[0]
|
||||
)
|
||||
|
||||
|
||||
anne.loc[anne.auteur_attrs=='Anne BELET (GAM)','organisme'] = 'Grenoble Alpes Métropole'
|
||||
anne.loc[anne.auteur_geom=='Anne BELET (GAM)','organisme'] = 'Grenoble Alpes Métropole'
|
||||
anne.loc[anne.auteur_geom=='Ag Mosaique Env','organisme'] = 'Mosaïque Environnement'
|
||||
anne.loc[anne.auteur_geom=='Ag Mosaique Env','auteur_geom'] = 'Mosaïque Environnement'
|
||||
|
||||
anne.loc[anne.auteur_attrs=='Anne BELET (GAM)','auteur'] = 'BELET Anne'
|
||||
anne.loc[anne.auteur_geom=='Anne BELET (GAM)','auteur'] = 'BELET Anne'
|
||||
anne.loc[anne.auteur_geom=='Mosaïque Environnement','auteur'] = 'Mosaïque Environnement'
|
||||
|
||||
anne['id_origine'] = anne.index.str[:10]
|
||||
anne['id_site'] = anne.index.copy()
|
||||
anne.loc[anne.index.str.len()<10,'id_site'] = None
|
||||
anne.loc[anne.index.str.len()>10,'id_site'] = None
|
||||
|
||||
anne.rename(columns={
|
||||
'rmq_interet_patri':'remarques','rmq_usage_process':'legende'
|
||||
}, inplace=True)
|
||||
|
||||
anne.to_file(path.join(PATH,'GAM2026_couche_finale_modif_CEN (corrige).gpkg'),index=False)
|
||||
mosa.to_file(path.join(PATH,'GAM2026_couche_finale_modif_mosaique (corrige).gpkg'))
|
||||
call.to_file(path.join(PATH,'GAM2026_couche_finale (corrige).gpkg'))
|
||||
|
||||
anne.to_excel(path.join(PATH,'couche_finale_modif_CEN.xlsx'),index=False)
|
||||
mosa.to_excel(path.join(PATH,'couche_finale_modif_mosaique.xlsx'))
|
||||
@@ -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)))
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
import pycen
|
||||
import geopandas as gpd
|
||||
|
||||
dstct = pycen.ref.get_districtNat()
|
||||
ps = pycen.ps.get_sitesGeom(statut='all')
|
||||
|
||||
ps['org_bd'] = ps.id_site.str[2:6]
|
||||
ps['num'] = ps.id_site.str[6:]
|
||||
|
||||
|
||||
df = gpd.sjoin(ps,dstct[['abrev','geom']]).drop(columns=['index_right'])
|
||||
df.sort_values('id_site',inplace=True)
|
||||
|
||||
tmp = pycen.tools.calc_recouvrmt(df1=df[df.org != df.abrev], df2=dstct)
|
||||
|
||||
|
||||
from geopandas import sjoin
|
||||
tmp = sjoin(
|
||||
df1,
|
||||
df2[['geom']],
|
||||
op = 'intersects',
|
||||
how = 'left')
|
||||
tmp.dropna(subset=['index_right'],inplace=True)
|
||||
tmp.index_right = tmp.index_right.astype(int)
|
||||
tmp.reset_index(inplace=True)
|
||||
tmp = tmp.join(
|
||||
df2[['geom']].rename(columns={'geom': 'right_geom'}),
|
||||
on=['index_right'], how='left')
|
||||
tmp2 = tmp[['index_right','right_geom']].copy() \
|
||||
.rename(columns={'right_geom': 'geom'}) \
|
||||
.set_geometry('geom')
|
||||
tmp1 = tmp[['id_site','geom']].copy() \
|
||||
.set_geometry('geom')
|
||||
|
||||
if not tmp1.geom.values.is_valid.all():
|
||||
tmp1.loc[~tmp1.geom.values.is_valid,'geom'] = tmp1.loc[~tmp1.geom.values.is_valid,'geom'].buffer(0)
|
||||
if not tmp2.geom.values.is_valid.all():
|
||||
tmp2.loc[~tmp2.geom.values.is_valid,'geom'] = tmp2.loc[~tmp2.geom.values.is_valid,'geom'].buffer(0)
|
||||
|
||||
tmp['perc_rcvmt'] = (tmp1.intersection(tmp2).area/tmp1.area)*100
|
||||
tmp = tmp.groupby(['id_site']).sum().reset_index()
|
||||
df1 = df1.merge(tmp[['id_site','perc_rcvmt']], on=['id_site'], how='left')
|
||||
df1.perc_rcvmt.fillna(0, inplace=True)
|
||||
df1.perc_rcvmt = df1.perc_rcvmt.round(2)
|
||||
|
||||
tmp1['area_rcvmt'] = [gpd.overlay(tmp1.iloc[[i]],tmp2.iloc[[i]]).area.round()[0] for i in range(0,tmp1.shape[0]) ]
|
||||
|
||||
|
||||
|
||||
# Maj origine district
|
||||
df = ps[ps.org_bd!=ps.org].sort_values('id_site').copy()
|
||||
pycen.update_to_sql(
|
||||
df[['id_site','org']].rename(columns={'id_site':'id'}).drop_duplicates(),
|
||||
pycen.con,'sites','sites','id'
|
||||
)
|
||||
|
||||
|
||||
# Redéfinition des numéro de sites
|
||||
ps_sit = pycen.ps.get_sitesInfos(statut='all')
|
||||
ps_sit.loc[ps_sit.id.isin(df.id_site),'num'] = None
|
||||
# attribution de numéro non utilisé
|
||||
for o in ps_sit.org.unique():
|
||||
tmp_sit = ps_sit.loc[ps_sit.org==o,'num'].dropna().astype(int)
|
||||
min = tmp_sit.min()
|
||||
max = tmp_sit.max()
|
||||
lst = [str(i) for i in range(min,max) if i not in [*tmp_sit]]
|
||||
tt = (ps_sit.org==o)&(ps_sit.num.isna())
|
||||
tmp_sitna = ps_sit[tt]
|
||||
if tmp_sitna.shape[0] <= len(lst):
|
||||
ps_sit.loc[tt,'num'] = lst[:tmp_sitna.shape[0]]
|
||||
else:
|
||||
ps_sit.loc[(ps_sit.index.isin(tt[tt].index[:len(lst)])),'num'] = lst
|
||||
|
||||
tt2 = (ps_sit.org==o)&(ps_sit.num.isna())
|
||||
min2 = max + 1
|
||||
max2 = max + 1 + ps_sit[tt2].shape[0]
|
||||
lst2 = [str(i) for i in range(min2,max2)]
|
||||
ps_sit.loc[tt2,'num'] = lst2
|
||||
|
||||
df2 = ps_sit.loc[ps_sit.id.isin(df.id_site)]
|
||||
df2.loc[df2.num.str.len() == 1,'num'] = '000'+ df2.loc[df2.num.str.len() == 1,'num']
|
||||
df2.loc[df2.num.str.len() == 2,'num'] = '00' + df2.loc[df2.num.str.len() == 2,'num']
|
||||
df2.loc[df2.num.str.len() == 3,'num'] = '0' + df2.loc[df2.num.str.len() == 3,'num']
|
||||
|
||||
pycen.update_to_sql(
|
||||
df2[['id','num']].drop_duplicates(),
|
||||
pycen.con,'sites','sites','id'
|
||||
)
|
||||
|
||||
|
||||
# Redéfinition des écritures
|
||||
ps_sitnew = pycen.ps.get_sitesInfos(id_site=[*df.id_site],statut='all')
|
||||
ps_sitnew = ps_sitnew[[
|
||||
'id','dept','org','num',
|
||||
]]
|
||||
ps_sitnew['idnew'] = ps_sitnew.dept+ps_sitnew.org+ps_sitnew.num
|
||||
# incrémentation de l'historique
|
||||
ps_sitnew[['id','idnew']]\
|
||||
.rename(columns={
|
||||
'id':'id_site_old',
|
||||
'idnew':'id_site_new'
|
||||
}).to_sql(
|
||||
'hist_id_site',
|
||||
pycen.con,'sites',if_exists='append',index=False
|
||||
)
|
||||
# modification des noms
|
||||
pycen.update_to_sql(
|
||||
ps_sitnew[['index','dept','org','num']].rename(columns={'index':'id'}).drop_duplicates(),
|
||||
pycen.con,'sites','sites',['dept','org','num']
|
||||
)
|
||||
pycen.update_to_sql(
|
||||
ps_sitnew[['idnew','dept','org','num']].rename(columns={'idnew':'id'}).drop_duplicates(),
|
||||
pycen.con,'sites','sites',['dept','org','num']
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
# 2024-11-26
|
||||
# LE script permet d'intégrer de nouvelles géométries
|
||||
# dans la table sites.r_sites_geom. Les AUTEURS ont été
|
||||
# intégrés manuellement.
|
||||
|
||||
import geopandas as gpd
|
||||
from pycen import con, zh as ZH
|
||||
from pycen.tools import dropZ, Polygons_to_MultiPolygon
|
||||
from os import path
|
||||
from shapely import wkb
|
||||
from matplotlib import pyplot as plt
|
||||
import pycen
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.engine import URL
|
||||
|
||||
# isin_bdd = True
|
||||
# # Parametres bdd CEN38 OUT
|
||||
user = 'cen_admin'
|
||||
pwd = "#CEN38@venir"
|
||||
adr = '91.134.194.221'
|
||||
base = 'azalee_restore'
|
||||
url = URL.create("postgresql+psycopg2", username=user, password=pwd, host=adr, database=base)
|
||||
con = create_engine(url)
|
||||
|
||||
|
||||
zh = ZH()
|
||||
r_geom = zh.get_sitesGeom()
|
||||
r_geom = gpd.read_postgis('''
|
||||
SELECT r_sites_geom.* FROM sites.r_sites_geom
|
||||
JOIN sites.sites ON sites.id = r_sites_geom.id_site AND sites.id_type_milieu = 1
|
||||
''',con)
|
||||
r_geom.dropna(how="all",axis=1,inplace=True)
|
||||
r_geom.drop(columns=['id','date_insert','id_origine','id_lot'],inplace=True)
|
||||
r_geom.sort_values(['id_site','date'],inplace=True)
|
||||
r_geom.drop_duplicates(['id_site'],keep='last',inplace=True)
|
||||
|
||||
|
||||
PATH = '/home/colas/Documents/9_PROJETS/1_ZH/MAJ/Actu 2024/FAIT/CBNA/zh38'
|
||||
file = 'hab_agreg.gpkg'
|
||||
df = gpd.read_file(path.join(PATH,file))
|
||||
df.rename_geometry('geom', inplace=True)
|
||||
df.geom = [wkb.loads(wkb.dumps(g,output_dimension=2)) for g in df.geom]
|
||||
df.dropna(how='all',axis=1,inplace=True)
|
||||
|
||||
# gpd.GeoSeries([r_geom.unary_union],crs=2154).contains(df.geom)
|
||||
# df.contains(r_geom.geom).any()
|
||||
# res = df.within(r_geom.unary_union)
|
||||
|
||||
perc = 80
|
||||
|
||||
zh_caract = df[df.zh=='caractéristique'].copy()
|
||||
zh_caract['surfzhall'] = zh_caract.groupby(['idpolyfinal'])['n06rechab'].transform('sum')
|
||||
lst_idpoly = zh_caract[zh_caract.surfzhall >= perc].idpolyfinal.unique()
|
||||
# zh_caract.within(r_geom.unary_union)
|
||||
# zh_caract.contains(r_geom.unary_union).any()
|
||||
|
||||
# Non Intersection Après 2015 - tout milieux
|
||||
no_inters = df[
|
||||
(df.lien_zh == 'aucun') & (df.n07anneehab > 2015) #& (df.zh=='caractéristique')
|
||||
& (df.idpolyfinal.isin(lst_idpoly))
|
||||
& (~df.n05lbhab.str.contains('invasif',na=False,case=False))
|
||||
].copy()
|
||||
no_inters = no_inters.drop(columns=['id_site']).sjoin(r_geom[['id_site','geom']])
|
||||
no_inters.to_file(path.join(PATH,'inters.gpkg'),driver='GPKG',layer='no_inters%i'%perc)
|
||||
len(no_inters.id_site.unique())
|
||||
|
||||
# Intersection Après 2015 - tout milieux
|
||||
inters = df[
|
||||
(df.lien_zh == 'intersecte') & (df.n07anneehab > 2015) #& (df.zh=='caractéristique')
|
||||
# & (df.n06rechab.astype(int)>45)
|
||||
& (df.idpolyfinal.isin(lst_idpoly))
|
||||
& (~df.n05lbhab.str.contains('invasif',na=False,case=False))
|
||||
].copy()
|
||||
inters = inters.drop(columns=['id_site']).sjoin(r_geom[['id_site','geom']])
|
||||
inters.to_file(path.join(PATH,'inters.gpkg'),driver='GPKG',layer='inters%i'%perc)
|
||||
len(inters.id_site.unique())
|
||||
|
||||
# Intersection Après 2009 - milieux Forêts uniquement
|
||||
inters_foret = df[
|
||||
(df.lien_zh == 'intersecte') & (df.zh=='caractéristique') #& (df.n07anneehab > 1999)
|
||||
# & (df.n06rechab.astype(int)>45)
|
||||
& (df.idpolyfinal.isin(lst_idpoly)) & (df.n05lbhab.str.contains('for.t',na=False,case=False))
|
||||
& (~df.n05lbhab.str.contains('invasif',na=False,case=False))
|
||||
& (~df.idpolyfinal.isin(inters.idpolyfinal.tolist()))
|
||||
].copy()
|
||||
inters_foret = inters_foret.drop(columns=['id_site']).sjoin(r_geom[['id_site','geom']])
|
||||
inters_foret.to_file(path.join(PATH,'inters.gpkg'),driver='GPKG',layer='inters_forets')
|
||||
|
||||
# Fusion Géométries
|
||||
intersF = gpd.pd.concat([inters,inters_foret])
|
||||
intersF.to_csv(path.join(PATH,'intersection_finale_hab_zh.csv'))
|
||||
rgeo = (r_geom[r_geom.id_site.isin(intersF.id_site.unique())]
|
||||
.copy())
|
||||
for id_site in rgeo.id_site.unique():
|
||||
rgeo.loc[rgeo.id_site==id_site,'geom'] = gpd.GeoSeries([
|
||||
gpd.GeoSeries(
|
||||
[*rgeo[rgeo.id_site==id_site].geom,
|
||||
*intersF[intersF.id_site==id_site].geom],
|
||||
crs=2154).unary_union
|
||||
],
|
||||
crs=2154,
|
||||
index=rgeo[rgeo.id_site==id_site].index)
|
||||
rgeo.date = '2024-01-21'
|
||||
|
||||
# Filtre geométries plus grandes
|
||||
rgeo2 = rgeo[
|
||||
rgeo.sort_values('id_site').area
|
||||
> r_geom[r_geom.id_site.isin(rgeo.id_site.unique())].sort_values('id_site').area
|
||||
]
|
||||
|
||||
# Check habitats describes
|
||||
sql = 'SELECT * FROM zones_humides.r_site_habitat WHERE valid IS True and id_site IN {lst}'.format(lst=tuple(intersF.id_site.unique()))
|
||||
hab_bdd = gpd.pd.read_sql(sql,con)
|
||||
|
||||
insert_hab = gpd.pd.DataFrame()
|
||||
for id_site in intersF.id_site.unique():
|
||||
lst_bddhab = hab_bdd[hab_bdd.id_site==id_site].id_cb.tolist()
|
||||
cbn_hab = intersF[(intersF.id_site==id_site)&(~intersF.n24cdcb.astype(str).isin(lst_bddhab))]
|
||||
insert_hab = gpd.pd.concat([insert_hab,cbn_hab])
|
||||
|
||||
ins_hab = (insert_hab[['n03observat','n04organism','n07anneehab','n24cdcb','id_site']]
|
||||
.rename(columns={'n24cdcb':'id_cb','n03observat':'observer','n04organism':'organisme','n07anneehab':'annee'})
|
||||
.replace(
|
||||
{'AMODEI T':'AMODEI Thomas',
|
||||
'Lo Parvi':'LO PARVI',
|
||||
'ACER CAMPESTRE':'Acer Campestre',
|
||||
'Réflex environnement':'REFLEX Environnement',
|
||||
'Office National des Forets':'Office National des Forêts',
|
||||
'Conservatoire botanique national alpin':'Conservatoire Botanique National Alpin',
|
||||
'Mosaique Environnement, M. Voirin':'Mosaïque Environnement',
|
||||
'Boucard E.':'BOUCARD E.',
|
||||
'MALET, A., JOUD. D., LINOSSIER, T.':'MALET A., JOUD Didier, LINOSSIER T.',
|
||||
'AURAND T., TAIN C':'AURAND Theo, TAIN C.',
|
||||
'FOLCHER C.':'FOLCHER Caroline',
|
||||
"Conservatoire d'espaces naturels": 'CEN Isère'
|
||||
})
|
||||
.drop_duplicates())
|
||||
ins_hab['auteur'] = None
|
||||
ins_hab.loc[ins_hab.observer.isna(),['auteur']] = ins_hab[ins_hab.observer.isna()].organisme
|
||||
ins_hab.loc[~ins_hab.observer.isna(),['auteur']] = (ins_hab[~ins_hab.observer.isna()].observer +
|
||||
' (' +ins_hab[~ins_hab.observer.isna()].organisme + ')')
|
||||
ins_hab['date'] = ins_hab.annee.astype(str) + '-01-01'
|
||||
ins_hab.drop(columns=['observer','organisme','annee'],inplace=True)
|
||||
|
||||
# Insertion geom BDD
|
||||
rgeo2.to_postgis('r_sites_geom',pycen.con,'sites',if_exists='append',index=False)
|
||||
|
||||
siteid = gpd.pd.read_sql('''
|
||||
SELECT DISTINCT ON (id_site) id,id_site
|
||||
FROM sites.r_sites_geom
|
||||
WHERE id_site IN {lst}
|
||||
ORDER BY id_site,id DESC
|
||||
'''.format(lst=tuple(rgeo2.id_site.unique())),pycen.con)
|
||||
usr = siteid.drop(columns=['id_site']).copy()
|
||||
usr.rename(columns={'id':'id_geom_site'},inplace=True)
|
||||
usr['id_auteur'] = 96
|
||||
usr.to_sql('r_geomsites_auteur',pycen.con,schema='sites',if_exists='append',index=False)
|
||||
|
||||
# Insertion habitat BDD
|
||||
siteidgeo = gpd.pd.read_sql('''
|
||||
SELECT DISTINCT ON (id_site) id id_geom_site,id_site
|
||||
FROM sites.r_sites_geom
|
||||
WHERE id_site IN {lst}
|
||||
ORDER BY id_site,id DESC
|
||||
'''.format(lst=tuple(ins_hab.id_site.unique())),pycen.con)
|
||||
ins_habF = ins_hab.merge(siteidgeo, on='id_site',how='left')
|
||||
ins_habF.sort_values(['id_site','id_cb','date'],inplace=True)
|
||||
ins_habF.drop_duplicates(subset=['id_site','id_cb'],keep='last',inplace=True)
|
||||
ins_habF['valid'] = True
|
||||
ins_habF.to_csv(path.join(PATH,'habs_inserted.csv'))
|
||||
(ins_habF
|
||||
.drop(columns=['auteur'])
|
||||
.to_sql('r_site_habitat',pycen.con,schema='zones_humides',if_exists='append',index=False))
|
||||
|
||||
|
||||
refpers = (pycen.pers.get_auteur2()
|
||||
.replace({'nom_prenom':{
|
||||
'ONF38 (ONF)':'Office National des Forêts',
|
||||
'CBNA (CBNA)':'Conservatoire Botanique National Alpin',
|
||||
'LO PARVI (LO PARVI)':'LO PARVI',
|
||||
'CEN Isère (CEN Isère)':'CEN Isère',
|
||||
'Ecosphère (Ecosphère)':'Ecosphère',
|
||||
'BURGEAP (BURGEAP)':'BURGEAP',
|
||||
'Acer Campestre (Acer Campestre)':'Acer Campestre',
|
||||
'REFLEX Environnement (REFLEX Environnement)':'REFLEX Environnement',
|
||||
'REFLEX Environnement (REFLEX Environnement)':'REFLEX Environnement'
|
||||
}})
|
||||
.replace({'nom_prenom':{
|
||||
'CBNA':'Conservatoire Botanique National Alpin',
|
||||
'ONF':'Office National des Forêts',
|
||||
'BÉGUIN Lucile':'BEGUIN Lucile',
|
||||
'CEN AURA':'Conservatoire Régional des Espaces Naturels Rhône-Alpes',
|
||||
'CD Isère':"Département de l'Isère"
|
||||
}},regex=True))
|
||||
pers_dict = dict(zip(refpers.nom_prenom,refpers.index))
|
||||
siteidhab = gpd.pd.read_sql('''
|
||||
SELECT DISTINCT id,id_site,id_cb,date,id_geom_site
|
||||
FROM zones_humides.r_site_habitat
|
||||
WHERE (id_site,id_cb,date,id_geom_site) IN {lst}
|
||||
ORDER BY id_site,id DESC
|
||||
'''.format(lst=tuple(tuple(ins_habF[['id_site','id_cb','date','id_geom_site']].itertuples(index=False,name=None)))),pycen.con)
|
||||
hab_auth = (ins_habF[['id_site','auteur','id_cb','date']]
|
||||
.astype(str)
|
||||
.merge(siteidhab.astype(str), on=['id_site','id_cb','date'],how='left')
|
||||
.rename(columns={'id':'id_sitehab'})
|
||||
.set_index(['id_sitehab']))
|
||||
hab_auth['orga'] = (hab_auth.auteur.str.split('(').str[-1]
|
||||
.str.split(')').str[0])
|
||||
|
||||
test_auth = hab_auth.auteur.str.contains(',')
|
||||
hab_auth1 = (hab_auth[~test_auth].auteur
|
||||
.replace(pers_dict)
|
||||
.to_frame('id_auteur'))
|
||||
hab_auth_tmp = (hab_auth[test_auth]
|
||||
.auteur.str.split('(').str[0]
|
||||
.str.split(',')
|
||||
.explode()
|
||||
.str.strip()
|
||||
.to_frame('auteur')
|
||||
.merge(hab_auth.loc[test_auth,['orga']],left_index=True,right_index=True))
|
||||
hab_auth2 = ((hab_auth_tmp.auteur+' ('+hab_auth_tmp.orga+')')
|
||||
.replace({
|
||||
'BURGEAP (BURGEAP, ECOSPHERE)':'BURGEAP',
|
||||
'ECOSPHERE (BURGEAP, ECOSPHERE)':'Ecosphère',})
|
||||
.replace(pers_dict)
|
||||
.to_frame('id_auteur'))
|
||||
|
||||
(gpd.pd.concat([hab_auth1,hab_auth2])
|
||||
.reset_index(drop=False)
|
||||
.to_sql('r_rsitehab_auteur',pycen.con,schema='zones_humides',if_exists='append',index=False))
|
||||
@@ -0,0 +1,153 @@
|
||||
from pycen import zh as ZH
|
||||
import geopandas as gpd
|
||||
import pandas as pd
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.engine import URL
|
||||
|
||||
import os
|
||||
|
||||
zh = ZH()
|
||||
# isin_bdd = True
|
||||
# # Parametres bdd CEN38 OUT
|
||||
from pycen import con
|
||||
|
||||
lst_table = [
|
||||
'r_sites_geom',
|
||||
'r_site_critdelim',
|
||||
'r_site_fctecosociopatri',
|
||||
'r_site_habitat',
|
||||
'r_site_reghydro',
|
||||
'r_site_sub',
|
||||
'r_site_type_connect',
|
||||
'r_site_usageprocess',
|
||||
]
|
||||
|
||||
result = {}
|
||||
for table in lst_table:
|
||||
sql_old = "SELECT max(t.id) FROM {sch}.{tab} t".format(tab=table,sch='sites' if table == 'r_sites_geom' else 'zones_humides')
|
||||
if table == 'r_sites_geom':
|
||||
sql_old += ' JOIN sites.sites ON sites.id = t.id_site AND sites.id_type_milieu = 1'
|
||||
# else:
|
||||
# sql_old += ''' JOIN sites.sites ON sites.id = r_sites_geom.id_site AND sites.id_type_milieu = 1'''
|
||||
|
||||
id_old = pd.read_sql(sql_old,con)
|
||||
|
||||
sql = "SELECT t.* FROM {sch}.{tab} t".format(tab=table,sch='sites' if table == 'r_sites_geom' else 'zones_humides')
|
||||
if table == 'r_sites_geom':
|
||||
sql += ' JOIN sites.sites ON sites.id = t.id_site AND sites.id_type_milieu = 1'
|
||||
|
||||
sql += " WHERE t.id > {id} AND id_site != '26PNRV0208'".format(id=id_old.values[0][0])
|
||||
result[table] = pd.read_sql(sql,zh.con)
|
||||
|
||||
if table == 'r_sites_geom':
|
||||
sql_lot = "SELECT * FROM sites.lots WHERE id_lot IN {id}".format(id=tuple(result[table].id_lot.dropna().unique()))
|
||||
lot = pd.read_sql(sql_lot,zh.con).dropna(how='all',axis=1)
|
||||
result[table] = result[table].merge(lot, on='id_lot',how='left')
|
||||
|
||||
r_geo = result['r_sites_geom']
|
||||
|
||||
sql = '''
|
||||
with lots as (
|
||||
SELECT DISTINCT ON (id_site)
|
||||
id_site,lots.*
|
||||
FROM sites.r_sites_geom
|
||||
LEFT JOIN sites.lots USING (id_lot)
|
||||
ORDER BY id_site, "date" DESC
|
||||
)
|
||||
SELECT v.*, lots.libelle FROM zones_humides.v_zoneshumides v
|
||||
JOIN lots ON v.site_code = lots.id_site
|
||||
WHERE v.site_code IN {lst_code}
|
||||
'''.format(lst_code=str(tuple(r_geo.id_site.unique())))
|
||||
vzh = gpd.read_postgis(sql,zh.con).dropna(subset=['libelle']).dropna(how='all',axis=1)
|
||||
vzh_old = gpd.read_postgis('SELECT * FROM zones_humides.v_zoneshumides',con).dropna(how='all',axis=1)
|
||||
vzh_new = gpd.read_postgis('SELECT * FROM zones_humides.v_zoneshumides',zh.con).dropna(how='all',axis=1)
|
||||
|
||||
date_cols = vzh.columns[vzh.columns.str.contains('date|heure')]
|
||||
vzh[date_cols] = vzh[date_cols].astype(str)
|
||||
vzh_old[['old_code',*date_cols]] = vzh_old[['old_code',*date_cols]].astype(str)
|
||||
vzh_new[['old_code',*date_cols]] = vzh_new[['old_code',*date_cols]].astype(str)
|
||||
|
||||
|
||||
PATH = '/home/colas/Documents/9_PROJETS/1_ZH/2024'
|
||||
vzh_old.to_file(os.path.join(PATH,'20250415_avancement.gpkg'),driver='GPKG',layer='Inventaire_initiale')
|
||||
vzh_new.to_file(os.path.join(PATH,'20250415_avancement.gpkg'),driver='GPKG',layer='Inv_actualisee_20250415')
|
||||
for _lib in vzh.libelle.unique():
|
||||
lib = (_lib.removesuffix('2024')
|
||||
.removeprefix('ZH')
|
||||
.replace('actualisation','CENIsere'))
|
||||
|
||||
tmp = vzh[vzh.libelle == _lib]
|
||||
new = tmp[~tmp.site_code.isin(vzh_old.site_code.tolist())]
|
||||
upt = tmp[tmp.site_code.isin(vzh_old.site_code.tolist())]
|
||||
if not new.empty:
|
||||
print(f'NEW {lib} : ',str(new.site_code.tolist()))
|
||||
# new.to_file(os.path.join(PATH,'20250415_avancement.gpkg'),driver='GPKG',layer='newzh'+lib+str(new.shape[0]))
|
||||
if not upt.empty:
|
||||
print(f'UPDT {lib} : ',str(upt.site_code.tolist()))
|
||||
# upt.to_file(os.path.join(PATH,'20250415_avancement.gpkg'),driver='GPKG',layer='majzh'+lib+str(upt.shape[0]))
|
||||
|
||||
vzh_reduc = (vzh[vzh.site_code.isin(vzh_old.site_code.tolist())].sort_values('site_code').set_index('site_code').area.sub(vzh_old[vzh_old.site_code.isin(vzh.site_code.tolist())].sort_values('site_code').set_index('site_code').area,fill_value=0))
|
||||
vzh_reduc[vzh_reduc<0].sum() / 10000
|
||||
vzh_reduc[vzh_reduc>0].sum() / 10000
|
||||
|
||||
area_maj = (vzh[vzh.site_code.isin(vzh_old.site_code.tolist())].area.sum() - vzh_old[vzh_old.site_code.isin(vzh.site_code.tolist())].area.sum()) / 10000
|
||||
area_new = vzh[~vzh.site_code.isin(vzh_old.site_code.tolist())].area.sum() / 10000
|
||||
|
||||
cbna = vzh[vzh.libelle=='ZH_CBNA_2024']
|
||||
new_cbna = cbna[(~cbna.site_code.isin(vzh_old.site_code.tolist()))&(vzh.libelle=='ZH_CBNA_2024')].area.sum() / 10000
|
||||
maj_cbna = (cbna[cbna.site_code.isin(vzh_old.site_code.tolist())].area.sum() - vzh_old[vzh_old.site_code.isin(cbna.site_code.tolist())].area.sum()) / 10000
|
||||
|
||||
pgszh = vzh[vzh.libelle=='ZH_pgszhBelledonne_2024']
|
||||
new_pgszh = pgszh[(~pgszh.site_code.isin(vzh_old.site_code.tolist()))&(vzh.libelle=='ZH_pgszhBelledonne_2024')].area.sum() / 10000
|
||||
maj_pgszh = (pgszh[pgszh.site_code.isin(vzh_old.site_code.tolist())].area.sum() - vzh_old[vzh_old.site_code.isin(pgszh.site_code.tolist())].area.sum()) / 10000
|
||||
|
||||
evin = vzh[vzh.libelle=='ZH_EVINERUDE_2024']
|
||||
new_evin = evin[(~evin.site_code.isin(vzh_old.site_code.tolist()))&(vzh.libelle=='ZH_EVINERUDE_2024')].area.sum() / 10000
|
||||
maj_evin = (evin[evin.site_code.isin(vzh_old.site_code.tolist())].area.sum() - vzh_old[vzh_old.site_code.isin(evin.site_code.tolist())].area.sum()) / 10000
|
||||
|
||||
lst_attrs = []
|
||||
r_geo = result['r_sites_geom']
|
||||
for table in result.keys():
|
||||
if table == 'r_sites_geom':
|
||||
continue
|
||||
df = result[table]
|
||||
no_geo = df[~df.id_site.isin(r_geo.id_site.unique())]
|
||||
lst_attrs += no_geo.id_site.unique().tolist()
|
||||
|
||||
lst_attrs = [x for x in list(set(lst_attrs)) if x is not None]
|
||||
|
||||
r_crit = result['r_site_critdelim']
|
||||
r_fct = result['r_site_fctecosociopatri']
|
||||
r_hab = result['r_site_habitat']
|
||||
r_hyd = result['r_site_reghydro']
|
||||
r_sub = result['r_site_sub']
|
||||
r_connect = result['r_site_type_connect']
|
||||
r_usg = result['r_site_usageprocess']
|
||||
|
||||
new_r_crit = r_crit[r_crit.id_site.isin(lst_attrs)]
|
||||
new_r_fct = r_fct[r_fct.id_site.isin(lst_attrs)]
|
||||
new_r_hab = r_hab[r_hab.id_site.isin(lst_attrs)]
|
||||
new_r_hyd = r_hyd[r_hyd.id_site.isin(lst_attrs)]
|
||||
new_r_sub = r_sub[r_sub.id_site.isin(lst_attrs)]
|
||||
new_r_connect = r_connect[r_connect.id_site.isin(lst_attrs)]
|
||||
new_r_usg = r_usg[r_usg.id_site.isin(lst_attrs)]
|
||||
|
||||
len_new_crit = new_r_crit.shape[0]
|
||||
len_new_fct = new_r_fct.shape[0]
|
||||
len_new_hab = new_r_hab.shape[0]
|
||||
len_new_hyd = new_r_hyd.shape[0]
|
||||
len_new_sub = new_r_sub.shape[0]
|
||||
len_new_connect = new_r_connect.shape[0]
|
||||
len_new_usg = new_r_usg.shape[0]
|
||||
|
||||
lst_new_crit = new_r_crit.id_site.unique().tolist()
|
||||
lst_new_fct = new_r_fct.id_site.unique().tolist()
|
||||
lst_new_hab = new_r_hab.id_site.unique().tolist()
|
||||
lst_new_hyd = new_r_hyd.id_site.unique().tolist()
|
||||
lst_new_sub = new_r_sub.id_site.unique().tolist()
|
||||
lst_new_connect = new_r_connect.id_site.unique().tolist()
|
||||
lst_new_usg = new_r_usg.id_site.unique().tolist()
|
||||
|
||||
tmp = gpd.read_file('/home/colas/Documents/9_PROJETS/1_ZH/MAJ/Actu 2024/BE EPODE - LEZE/KIT INVENTAIRE38 MODIFIE/a envoyer prêt/GEOM/38CG0105_Epode2025.gpkg')
|
||||
tmp_old = gpd.read_postgis('SELECT * FROM zones_humides.v_zoneshumides WHERE site_code = \'38CG0105\'',con)
|
||||
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding:UTF-8 -*-
|
||||
from pycen import con, update_to_sql
|
||||
import geopandas as gpd
|
||||
|
||||
file = "/media/cgeier/SRV/FICHIERS/OUTILS/CARTOGRAPHIE/ESPACE DE TRAVAIL/ETUDES/PGSZH_Belledonne/Inventaire 2023/ZH_Belledonne_MAJ_2023_NB.shp"
|
||||
|
||||
df = gpd.read_file(file)
|
||||
df = (df[df.site_code.isin(['38BB0028','38BB0055','38RD0170'])]
|
||||
.filter(items=['site_code', 'geometry']))
|
||||
df.rename_geometry('geom', inplace=True)
|
||||
df.rename(columns={'site_code': 'id_site'}, inplace=True)
|
||||
|
||||
df_updt = df[df.id_site.isin(['38RD0170'])]
|
||||
update_to_sql(df_updt,con,'r_sites_geom','sites','id_site')
|
||||
|
||||
df_ins = df[df.id_site.isin(['38BB0028','38BB0055'])]
|
||||
df_ins['date'] = '2023-06-21'
|
||||
df_ins.to_postgis('r_sites_geom',con,if_exists='append',index=False,schema='sites')
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
import geopandas as gpd
|
||||
from pycen import con
|
||||
|
||||
path0 = '/home/colas/Documents/9_PROJETS/2_PS/TO IMPORT/'
|
||||
file = 'CDisère_PS2016_4Montagnes.shp'
|
||||
dic = {
|
||||
'identifian':'ident',
|
||||
'habitat1' : 'code_hab1',
|
||||
'partpoly1': 'n_hab1',
|
||||
'habitat2' : 'code_hab2',
|
||||
'partpoly2': 'n_hab2',
|
||||
'habitat3' : 'code_hab3',
|
||||
'partpoly3': 'n_hab3',
|
||||
'habitat4' : 'code_hab4',
|
||||
'partpoly4': 'n_hab4',
|
||||
'légende':'legende',
|
||||
'embrou_cod':'niv_embrous',
|
||||
'embrous':'%_embrous',
|
||||
'sol nu_cod':'recouvmnt',
|
||||
'sol nu':'%_recouvmnt',
|
||||
'orchidées':'rmq_phyto',
|
||||
'alliance':'rmq_phytosocio',
|
||||
'maec':'rmq_contract_agri',
|
||||
}
|
||||
dic_auth = {
|
||||
'Anne-Lyse Murro':'MURRO Anne-Lyse',
|
||||
'Lucas Berardi' : 'BERARDI Lucas'
|
||||
}
|
||||
dic_prat = {
|
||||
'Mixte' : 'Fauche, Pâturage',
|
||||
'Adandon':'Abandon',
|
||||
'P�turage':'Pâturage'
|
||||
}
|
||||
dic_stat = {
|
||||
'Aucun' : 'Non communautaire',
|
||||
'Prioriataire' : 'Prioritaire'
|
||||
}
|
||||
dic_date = {
|
||||
'mai 2016' : '2016-05-01',
|
||||
'juin 2016' : '2016-06-01',
|
||||
'juillet 2016' : '2016-07-01',
|
||||
'aout 2016' : '2016-08-01',
|
||||
'août 2016' : '2016-08-01',
|
||||
}
|
||||
keep_cols = ['auteur','date',*dic.values(),'rosette','pratique','statut','remarques','geom']
|
||||
|
||||
def emb_correct(df):
|
||||
l1 = ['GIR3','AU7']
|
||||
l2 = ['BOI1','VIL10','VIL8a']
|
||||
l3 = ['MEA4a','ME']
|
||||
l4 = ['MO3','MA3','VIL14','BD1','VF','CHA2','CHA5','ROM3']
|
||||
l5 = ['BEL1','JA5','AU4']
|
||||
df.loc[df.index.isin(l1),'niv_embrous'] = 1
|
||||
df.loc[df.index.isin(l2),'%_embrous'] = 4
|
||||
df.loc[df.index.isin(l3),'%_embrous'] = 29
|
||||
df.loc[df.index.isin(l4),'%_embrous'] = 30
|
||||
df.loc[df.index.isin(l5),'%_embrous'] = 60
|
||||
return df
|
||||
|
||||
def sol_correct(df):
|
||||
# df.loc[df.index=='GEY1','%_recouvmnt'] = 19
|
||||
df.loc[df.index=='CHA5','%_recouvmnt'] = 21
|
||||
# df.loc[df.index=='GEY2','%_recouvmnt'] = 19
|
||||
# df.loc[df.index=='SN1','%_recouvmnt'] = 19
|
||||
df.loc[df.index=='AU1','%_recouvmnt'] = 21
|
||||
df.loc[(df.recouvmnt==1)&(df['%_recouvmnt']>0),'%_recouvmnt'] = 0
|
||||
df.loc[(df.recouvmnt==2)&(df['%_recouvmnt']>10),'%_recouvmnt'] = 10
|
||||
df.loc[(df.recouvmnt==3)&(df['%_recouvmnt']>20),'%_recouvmnt'] = 20
|
||||
df.loc[(df.recouvmnt==4)&(df['%_recouvmnt']<21),'%_recouvmnt'] = 21
|
||||
return df
|
||||
|
||||
|
||||
# récupération des données
|
||||
sql = 'SELECT site_code,geom FROM ps.v_pelouseseches'
|
||||
v_ps = gpd.read_postgis(sql, con)
|
||||
df0 = gpd.read_file(path0 + file)
|
||||
df0.columns = df0.columns.str.lower()
|
||||
df0.rename(columns=dic, inplace=True)
|
||||
df0.rename_geometry('geom', inplace=True)
|
||||
df = df0[keep_cols].copy()
|
||||
df.set_index('ident', inplace=True)
|
||||
df.pratique.replace(dic_prat,inplace=True)
|
||||
df.statut.replace(dic_stat,inplace=True)
|
||||
df.auteur.replace(dic_auth,inplace=True,regex=True)
|
||||
df.date = df.date.str.lower()
|
||||
df.date.replace(dic_date,inplace=True)
|
||||
df.date = gpd.pd.to_datetime(df.date)
|
||||
df = emb_correct(df)
|
||||
df = sol_correct(df)
|
||||
|
||||
# Correction code Corine Biotope
|
||||
df.loc[df.code_hab2=='41D','code_hab2'] = '41.D'
|
||||
|
||||
# Concaténation des remarques
|
||||
df.loc[~df.rmq_phyto.isna(),'remarques'] = df.loc[~df.rmq_phyto.isna(),'remarques'] +\
|
||||
'; rmq_phyto : ' + df.loc[~df.rmq_phyto.isna(),'rmq_phyto']
|
||||
df.loc[~df.rosette.isna(),'remarques'] = df.loc[~df.rosette.isna(),'remarques'] +\
|
||||
'; rosette : ' + df.loc[~df.rosette.isna(),'rosette']
|
||||
df.loc[~df.rmq_phytosocio.isna(),'remarques'] = df.loc[~df.rmq_phytosocio.isna(),'remarques'] +\
|
||||
'; rmq_phytosocio : ' + df.loc[~df.rmq_phytosocio.isna(),'rmq_phytosocio']
|
||||
df.loc[~df.rmq_contract_agri.isna(),'remarques'] = df.loc[~df.rmq_contract_agri.isna(),'remarques'] +\
|
||||
'; rmq_contract_agri : ' + df.loc[~df.rmq_contract_agri.isna(),'rmq_contract_agri']
|
||||
del df['rmq_phyto']
|
||||
del df['rosette']
|
||||
del df['rmq_phytosocio']
|
||||
del df['rmq_contract_agri']
|
||||
|
||||
|
||||
df.reset_index(drop=False,inplace=True)
|
||||
df.rename(columns={'ident':'id_origine'}, inplace=True)
|
||||
df['table_org'] = file
|
||||
df['structure'] = 'CD Isère'
|
||||
|
||||
# END SCRIPT #
|
||||
|
||||
|
||||
## tri des géométries avant insertion
|
||||
# Identification des superpositions new_site/old_site
|
||||
df_inters = gpd.sjoin(df,v_ps, how='left')
|
||||
del df_inters['index_right']
|
||||
lst_old_site = df_inters.site_code.unique()
|
||||
v_ps = v_ps[v_ps.site_code.isin(lst_old_site)].copy()
|
||||
# Calcul du recouvrement
|
||||
df.reset_index(drop=False,inplace=True)
|
||||
v_ps.loc[:,'surf'] = v_ps.area
|
||||
tmp = gpd.overlay(v_ps,df[['ident','geom']],how='intersection',make_valid=True)
|
||||
tmp['perc_rcvmt'] = 100*tmp.area/tmp['surf']
|
||||
tmp_sitemaj = tmp[(tmp.perc_rcvmt > 10)&(~tmp.ident.duplicated(keep=False))].ident
|
||||
tmp_sitenew = tmp[tmp.perc_rcvmt <= 10].ident.unique()
|
||||
|
||||
|
||||
site_newvsold = {
|
||||
'old' : [*tmp.site_code.unique()],
|
||||
'new' : [*tmp.ident.unique()],
|
||||
}
|
||||
|
||||
df_new = df_inters[df_inters.site_code.isna()]
|
||||
df_inters.drop(df_new.index,inplace=True)
|
||||
df_maj = df_inters[~df_inters.site_code.duplicated(keep=False)]
|
||||
df_inters.drop(df_maj.index,inplace=True)
|
||||
df_rep = df_inters.copy()
|
||||
|
||||
df_new = gpd.pd.concat([df_new,df_rep],axis=0)
|
||||
@@ -0,0 +1,70 @@
|
||||
import geopandas as gpd
|
||||
import os
|
||||
import pycen
|
||||
import numpy as np
|
||||
|
||||
path = '/home/colas/Documents/9_PROJETS/2_PS/TO IMPORT'
|
||||
file = 'PS_AGGREGATION_NB_AG.shp'
|
||||
|
||||
|
||||
def split_auth(col):
|
||||
return col.str.split(r' \(',expand=True).replace('\)','',regex=True)
|
||||
|
||||
def format_auth(col):
|
||||
auth = pycen.pers.get_auteur()
|
||||
for c in col.unique():
|
||||
tmp = c.lower().split(' ')
|
||||
cd1 = (np.array([tmp[0] == n for n in auth.nom.str.lower()]) |
|
||||
np.array([tmp[0] == n for n in auth.prenom.str.lower()]))
|
||||
cd2 = (np.array([tmp[1] == n for n in auth.nom.str.lower()]) |
|
||||
np.array([tmp[1] == n for n in auth.prenom.str.lower()]))
|
||||
r = auth[cd1&cd2].nom_prenom.values[0]
|
||||
col.replace(c,r,inplace=True)
|
||||
return col
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
df = gpd.read_file(os.path.join(path,file))
|
||||
df.columns = df.columns.str.lower()
|
||||
df.dropna(axis=1,how='all', inplace=True)
|
||||
for c in df.columns:
|
||||
if df[c].dtype == object:
|
||||
df[c] = df[c].str.strip()
|
||||
if 'date' in c :
|
||||
df[c] = gpd.pd.to_datetime(df[c])
|
||||
|
||||
n_hab = df.columns[df.columns.str.startswith('n_hab')]
|
||||
df[df[n_hab].sum(axis=1) != 100]
|
||||
|
||||
# Auteurs
|
||||
df[['auteur_split','structure']] = split_auth(df.auteur)
|
||||
df.auteur_split = format_auth(df.auteur_split)
|
||||
del df['auteur']
|
||||
df.rename(columns={'auteur_split':'auteur'},inplace=True)
|
||||
|
||||
# Pratiques
|
||||
df.pratiques.replace([r' \/ '],r', ',regex=True,inplace=True)
|
||||
df.pratiques.replace({
|
||||
'paturage broyage':'pâturage, broyage',
|
||||
'abandon paturage':'abandon, pâturage',
|
||||
'paturage':'pâturage'
|
||||
}, inplace=True)
|
||||
df.type_patur.replace({'N.D':None
|
||||
}, inplace=True)
|
||||
cd3 = (df.pratiques=='pâturage')&(~df.type_patur.isna())
|
||||
df.loc[cd3,'pratiques'] = df.loc[cd3,'pratiques'] +' '+ df.loc[cd3,'type_patur']
|
||||
df.loc[cd3,'type_patur'] = None
|
||||
cd4 = (~df.pratiques.isin(['pâturage','pâturage ?']))&(df.pratiques.str.contains('pâturage'))&(~df.type_patur.isna())
|
||||
df.loc[cd4,'pratiques'] = df.loc[cd4,'pratiques'].str.split(', ',expand=True)[0] + ' ' + df.loc[cd4,'type_patur'] + ', ' + df.loc[cd4,'pratiques'].str.split(', ',expand=True)[1]
|
||||
df.loc[cd4,'type_patur'] = None
|
||||
|
||||
df.pratiques.replace({
|
||||
'pâturage caprin - ovin':'pâturage caprin, pâturage ovin',
|
||||
'jardin':'tonte',
|
||||
'pâturage anin' : 'pâturage asin',
|
||||
'pâturage lama' : 'pâturage asin',
|
||||
'abandon ?' : 'incertain',
|
||||
'pâturage ?' : 'incertain'
|
||||
}, inplace=True)
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
|
||||
import pandas as pd
|
||||
import geopandas as gpd
|
||||
import pycen
|
||||
|
||||
|
||||
|
||||
path0 = '/home/colas/Documents/9_PROJETS/2_PS/TO IMPORT/'
|
||||
path = 'cartohabitatsplatiere/'
|
||||
file = 'Habitats I33 2008.gpkg'
|
||||
df = gpd.read_file(path0+path+file)
|
||||
df.rename_geometry('geom', inplace=True)
|
||||
|
||||
if file == 'Habitats I33 2008.gpkg' :
|
||||
df.columns = df.columns.str.lower()
|
||||
df.rename(columns={'date_maj':'date'}, inplace=True)
|
||||
df['remarques'] = \
|
||||
'détails : ' + df['type_détaillé'] + ';' +\
|
||||
' regroupement : ' + df['type_regroupé'] + ';'\
|
||||
' code_n2000 : ' + df['code_n2000'] + ';'\
|
||||
' age : ' + df['age'] + ';'
|
||||
del df['id']
|
||||
del df['code_enjeu']
|
||||
del df['enjeu_numérique']
|
||||
del df['complexe_hab']
|
||||
del df['echelle']
|
||||
del df['entretien']
|
||||
del df['type_détaillé']
|
||||
del df['type_regroupé']
|
||||
del df['surface']
|
||||
del df['num_site']
|
||||
del df['code_n2000']
|
||||
del df['age']
|
||||
# df.set_index('num_site',inplace=True)
|
||||
df['auteur'] = 'PONT Bernard'
|
||||
df['structure'] = "AAIP"
|
||||
|
||||
lst_hab = ['34.1','34.3','34.4','34.7','35.2','62.3','64.1']
|
||||
df = df[df.code_cb.str.contains('|'.join(lst_hab))]
|
||||
|
||||
lst_hab2 = ['41','42','43','44','81','82','83']
|
||||
df = df[~df.code_cb.str.contains('|'.join(lst_hab2))]
|
||||
|
||||
df.replace({'IC':'Communautaire'}, inplace=True)
|
||||
|
||||
|
||||
df.index.name = 'ident'
|
||||
df.reset_index(inplace=True, drop=False)
|
||||
df.rename(columns={'code_cb':'code_hab1'}, inplace=True)
|
||||
df['n_hab1'] = 100
|
||||
|
||||
ps = pycen.ps.get_sitesGeom()
|
||||
# nv = gpd.read_file(path0+'PS_nature_vivante_2014.shp')
|
||||
|
||||
|
||||
# nv.geometry = nv.buffer(50)
|
||||
intersect = gpd.sjoin(df,ps,op='intersects')['ident'].tolist()
|
||||
drop_index = df[df['ident'].isin(intersect)].index
|
||||
df.drop(drop_index,inplace=True)
|
||||
df.reset_index(inplace=True, drop=True)
|
||||
# df.to_file(path0+path+'Habitats I33 2008 (manquants).gpkg', driver='GPKG')
|
||||
# df = gpd.read_file(path0+path+'Habitats I33 2008 (manquants).gpkg')
|
||||
@@ -0,0 +1,9 @@
|
||||
import geopandas as gpd
|
||||
import os
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from pycen import con
|
||||
|
||||
PATH = "/home/colas/Documents/9_PROJETS/1_ZH/MAJ/GERECO - Schéma_strat_ZH_Huez"
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
from pathlib import Path
|
||||
from marshmallow import (
|
||||
Schema,
|
||||
fields,
|
||||
validates_schema,
|
||||
ValidationError,
|
||||
post_load,
|
||||
)
|
||||
|
||||
BACKEND_DIR = Path(__file__).absolute().parent.parent.parent
|
||||
ROOT_DIR = BACKEND_DIR.parent
|
||||
CUSTOM_STATIC_FOLDER = fields.String(load_default=ROOT_DIR / "1_Packages")
|
||||
print(CUSTOM_STATIC_FOLDER)
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
# 2024-11-26
|
||||
# LE script permet d'intégrer de nouvelles géométries
|
||||
# dans la table sites.r_sites_geom. Les AUTEURS ont été
|
||||
# intégrés manuellement.
|
||||
|
||||
import geopandas as gpd
|
||||
from pycen import con, zh as ZH
|
||||
from pycen.tools import dropZ, Polygons_to_MultiPolygon
|
||||
from os import path
|
||||
from matplotlib import pyplot as plt
|
||||
|
||||
zh = ZH()
|
||||
r_geom = zh.get_sitesGeom()
|
||||
r_geom = gpd.read_postgis('''
|
||||
SELECT r_sites_geom.* FROM sites.r_sites_geom
|
||||
JOIN sites.sites ON sites.id = r_sites_geom.id_site AND sites.id_type_milieu = 1
|
||||
''',con)
|
||||
r_geom.dropna(how="all",axis=1,inplace=True)
|
||||
r_geom.drop(columns=['id','date_insert','id_origine','id_lot'],inplace=True)
|
||||
|
||||
|
||||
PATH = '/home/colas/Documents/9_PROJETS/1_ZH/MAJ/Actu 2024/CBNA/zh38'
|
||||
file = 'zh_to_integ.gpkg'
|
||||
df = gpd.read_file(path.join(PATH,file))
|
||||
df.rename_geometry('geom', inplace=True)
|
||||
df = dropZ(df)
|
||||
|
||||
# DEL geo A SUPP
|
||||
del_id=df[df.lien_zh=='A SUPP'].index.tolist()
|
||||
df.drop(del_id,inplace=True)
|
||||
|
||||
lstdel_id2 = [
|
||||
'38RH0171','38RH0066','38RH0077','38BO0144','38BO0109',
|
||||
'38BO0105','38RH0012','38BO0139','38BO0200','38RH0292',
|
||||
'38BO0105 + combl vid'
|
||||
]
|
||||
del_id2 = df[df.lien_zh.isin(lstdel_id2)].index.tolist()
|
||||
df.drop(del_id2,inplace=True)
|
||||
|
||||
lstdel_id3 = [
|
||||
'38RH0293', '38RH0297', '38RH0296', '38RH0294', '38BO0331',
|
||||
'38RH0295'
|
||||
]
|
||||
del_id3 = df[df.lien_zh.isin(lstdel_id3)].index.tolist()
|
||||
df.drop(del_id3,inplace=True)
|
||||
|
||||
ins = df[df.lien_zh!='aucun'].copy()
|
||||
ins.lien_zh.unique()
|
||||
|
||||
# # Fait
|
||||
# r_geom[r_geom.id_site=='38RH0171']
|
||||
# r_geom[r_geom.id_site=='38RH0066']
|
||||
# r_geom[r_geom.id_site=='38RH0077']
|
||||
# r_geom[r_geom.id_site=='38BO0144']
|
||||
# r_geom[r_geom.id_site=='38BO0109']
|
||||
# r_geom[r_geom.id_site=='38BO0105']
|
||||
# r_geom[r_geom.id_site=='38RH0012']
|
||||
|
||||
# # Fait, Ne pas reproduire.
|
||||
# BO139 = r_geom[r_geom.id_site=='38BO0139'].copy()
|
||||
# BO200 = r_geom[r_geom.id_site=='38BO0200'].copy()
|
||||
# RH292 = r_geom[r_geom.id_site=='38RH0292'].copy()
|
||||
# BO139.date = '2024-11-26'
|
||||
# BO200.date = '2024-11-26'
|
||||
# RH292.date = '2024-11-26'
|
||||
# BO139.geom = gpd.GeoSeries([
|
||||
# BO139.geom.values[-1],
|
||||
# *ins[ins.lien_zh=='38BO0139'].geom.values
|
||||
# ],crs=2154).unary_union
|
||||
# BO200.geom = gpd.GeoSeries([
|
||||
# BO200.geom.values[-1],
|
||||
# *ins[ins.lien_zh=='38BO0200'].geom.values
|
||||
# ],crs=2154).unary_union
|
||||
# RH292.geom = gpd.GeoSeries([
|
||||
# RH292.geom.values[-1],
|
||||
# *ins[ins.lien_zh=='38RH0292'].geom.values
|
||||
# ],crs=2154).unary_union
|
||||
# Polygons_to_MultiPolygon(BO139).to_postgis('r_sites_geom',con,schema='sites',if_exists='append',index=False)
|
||||
# Polygons_to_MultiPolygon(BO200).to_postgis('r_sites_geom',con,schema='sites',if_exists='append',index=False)
|
||||
# Polygons_to_MultiPolygon(RH292).to_postgis('r_sites_geom',con,schema='sites',if_exists='append',index=False)
|
||||
|
||||
# Traitement zh id3
|
||||
gdf = gpd.GeoDataFrame()
|
||||
for i in ins.lien_zh.unique():
|
||||
if i == 'A prospecter':
|
||||
continue
|
||||
tmp = gpd.GeoDataFrame(
|
||||
{'site_code':[i]},
|
||||
geometry=[ins[ins.lien_zh==i].unary_union],
|
||||
crs=2154
|
||||
).rename_geometry('geom')
|
||||
gdf = gpd.pd.concat([gdf,tmp])
|
||||
|
||||
# Traitement ZH id4, après tri des données avec Mathieu Juton
|
||||
df = gpd.read_file(path.join(PATH,file),layer='zh_to_integ')
|
||||
df.rename_geometry('geom', inplace=True)
|
||||
del_id=df[df.lien_zh=='A SUPP'].index.tolist()
|
||||
df.drop(del_id,inplace=True)
|
||||
id_updtgeo = [
|
||||
'38BI0086', '38BO0128', '38BO0160 + remove trou', '38BO0181',
|
||||
'38BO0182', '38CG0001', '38CG0002', '38CG0019',
|
||||
'38CG0023 + bouche trou', '38CG0025 + remove vide', '38CG0027',
|
||||
'38CG0031', '38CG0147', '38CG0148', '38RH0054', '38RH0076',
|
||||
'38RH0122', '38RH0123 + remove vide', '38RH0132 + remove vide',
|
||||
'38RH0135 + remove route', '38RH0143', '38RH0148',
|
||||
'38RH0148 + remove route'
|
||||
]
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user