Initial commit
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
import pandas as pd
|
||||
import pyzh
|
||||
zh = pyzh.zh()
|
||||
|
||||
con = pyzh.con
|
||||
sch = 'zones_humides'
|
||||
lst_site_old = ['38RD0146','38RD0147']
|
||||
lst_site_new = ['38RD0164','38RD0165']
|
||||
df = zh.get_connex(id_site=lst_site_old)
|
||||
geomsite = zh.get_sitesGeom(id_site=lst_site_new)
|
||||
aut = pyzh.pers.get_auteur()
|
||||
|
||||
tab = 'r_site_type_connect'
|
||||
rtab_aut = 'r_rsiteconnect_auteur'
|
||||
id_rsiteaut = 'id_siteconnect'
|
||||
date_champ = 'date_conne'
|
||||
nom_champ = 'connexion'
|
||||
id_champ = 'id_param_connect'
|
||||
delim = zh._get_param(param_table='param_type_connect', type_court=False)
|
||||
|
||||
|
||||
df.drop(
|
||||
columns=['id','valid', 'date_geom', 'auteur_geom', 'description','type','lb_hab_fr'],
|
||||
inplace=True,
|
||||
errors='ignore')
|
||||
|
||||
for i, s in enumerate(lst_site_new):
|
||||
df.loc[df.id_site==lst_site_old[i], 'id_site'] = s
|
||||
df.loc[df.id_site==s, ['id_geom_site']] = geomsite.loc[geomsite.id_site==s,'id'].values[0].astype(str)
|
||||
|
||||
df['id_geom_site'] = df.id_geom_site.astype(int)
|
||||
df[id_champ] = df[nom_champ]
|
||||
df[id_champ] = df[id_champ].replace(delim.nom.tolist(),delim.id.tolist())
|
||||
df['date'] = df[date_champ]
|
||||
|
||||
df.drop(columns=['id_site',nom_champ,date_champ], inplace=True)
|
||||
lst_idgeom = df.id_geom_site.unique().astype(str)
|
||||
|
||||
# check si data in bdd
|
||||
sql = 'select exists(select * from {sch}.{tab} where id_geom_site in ({lst_id}));'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
res = pd.read_sql(sql,con)
|
||||
if res.exists[0]:
|
||||
# delete if exist
|
||||
sql = 'delete from {sch}.{tab} where id_geom_site in ({lst_id});'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
pd.read_sql(sql,con)
|
||||
# insert new line in bdd
|
||||
col = df.columns[~df.columns.str.contains('auteur')]
|
||||
colaut = df.columns[df.columns.str.contains('auteur')]
|
||||
df[col].to_sql(
|
||||
tab, con, schema=sch,
|
||||
if_exists='append',index=False,method='multi'
|
||||
)
|
||||
# Récupération des nouveaux ids insérés
|
||||
sql = 'select * from {sch}.{tab} where id_geom_site in ({lst_id});'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
res = pd.read_sql(sql,con)
|
||||
df = df.merge(res, how='left', on=[*col])
|
||||
|
||||
ins_aut = df[['id',*colaut]].copy()
|
||||
aut['nom_prenom'] = aut.nom + ' ' + aut.prenom
|
||||
aut.loc[aut.nom_prenom.isna(), 'nom_prenom'] = aut.loc[aut.nom_prenom.isna(), 'nom']
|
||||
ins_aut[colaut] = ins_aut[colaut].replace(aut.nom_prenom.tolist(), aut.id.tolist())
|
||||
ins_aut.columns = [id_rsiteaut, 'id_auteur']
|
||||
ins_aut.to_sql(
|
||||
rtab_aut, con, schema=sch,
|
||||
if_exists='append',index=False,method='multi')
|
||||
@@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
import pandas as pd
|
||||
import pyzh
|
||||
zh = pyzh.zh()
|
||||
|
||||
con = pyzh.con
|
||||
tab = 'r_site_critdelim'
|
||||
sch = 'zones_humides'
|
||||
|
||||
lst_site_old = ['38RD0146','38RD0147']
|
||||
lst_site_new = ['38RD0164','38RD0165']
|
||||
|
||||
df = zh.get_delim(id_site=lst_site_old)
|
||||
geomsite = zh.get_sitesGeom(id_site=lst_site_new)
|
||||
delim = zh._get_param(param_table='param_delim_fct',type_table='type_param_delim_fct')
|
||||
aut = pyzh.pers.get_auteur()
|
||||
|
||||
df.drop(columns=['id','valid', 'date_geom', 'auteur_geom', 'desc_param','type'], inplace=True)
|
||||
for i, s in enumerate(lst_site_new):
|
||||
df.loc[df.id_site==lst_site_old[i], 'id_site'] = s
|
||||
df.loc[df.id_site==s, ['id_geom_site']] = geomsite.loc[geomsite.id_site==s,'id'].values[0].astype(str)
|
||||
|
||||
df['id_geom_site'] = df.id_geom_site.astype(int)
|
||||
df['id_crit_delim'] = df.nom_crit
|
||||
df['id_crit_delim'] = df['id_crit_delim'].replace(delim.nom.tolist(),delim.id.tolist())
|
||||
df['date'] = df.date_critd
|
||||
|
||||
|
||||
df.drop(columns=['id_site','nom_crit','date_critd'], inplace=True)
|
||||
lst_idgeom = df.id_geom_site.unique().astype(str)
|
||||
|
||||
sql = 'select exists(select * from {sch}.{tab} where id_geom_site in ({lst_id}));'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
res = pd.read_sql(sql,con)
|
||||
if res.exists[0]:
|
||||
sql = 'delete from {sch}.{tab} where id_geom_site in ({lst_id});'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
pd.read_sql(sql,con)
|
||||
col = df.columns[~df.columns.str.contains('auteur')]
|
||||
colaut = df.columns[df.columns.str.contains('auteur')]
|
||||
df[col].to_sql(
|
||||
tab, con, schema=sch,
|
||||
if_exists='append',index=False,method='multi'
|
||||
)
|
||||
sql = 'select * from {sch}.{tab} where id_geom_site in ({lst_id});'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
res = pd.read_sql(sql,con)
|
||||
df = df.merge(res, how='left', on=[*col])
|
||||
|
||||
ins_aut = df[['id',*colaut]].copy()
|
||||
aut['nom_prenom'] = aut.nom + ' ' + aut.prenom
|
||||
aut.loc[aut.nom_prenom.isna(), 'nom_prenom'] = aut.loc[aut.nom_prenom.isna(), 'nom']
|
||||
ins_aut[colaut] = ins_aut[colaut].replace(aut.nom_prenom.tolist(), aut.id.tolist())
|
||||
ins_aut.columns = ['id_sitedelim', 'id_auteur']
|
||||
ins_aut.to_sql(
|
||||
'r_rsitedelim_auteur', con, schema=sch,
|
||||
if_exists='append',index=False,method='multi')
|
||||
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
import pandas as pd
|
||||
import pyzh
|
||||
zh = pyzh.zh()
|
||||
|
||||
con = pyzh.con
|
||||
sch = 'zones_humides'
|
||||
lst_site_old = ['38RD0146','38RD0147']
|
||||
lst_site_new = ['38RD0164','38RD0165']
|
||||
df = zh.get_fct(id_site=lst_site_old)
|
||||
geomsite = zh.get_sitesGeom(id_site=lst_site_new)
|
||||
aut = pyzh.pers.get_auteur()
|
||||
|
||||
tab = 'r_site_fctecosociopatri'
|
||||
rtab_aut = 'r_rsitefct_auteur'
|
||||
id_rsiteaut = 'id_sitefct'
|
||||
delim = zh._get_param(param_table='param_fct_eco_socio_patri',type_table='type_param_fct')
|
||||
|
||||
|
||||
df.drop(
|
||||
columns=['id','valid', 'date_geom', 'auteur_geom', 'desc_param','type'],
|
||||
inplace=True,
|
||||
errors='ignore')
|
||||
|
||||
for i, s in enumerate(lst_site_new):
|
||||
df.loc[df.id_site==lst_site_old[i], 'id_site'] = s
|
||||
df.loc[df.id_site==s, ['id_geom_site']] = geomsite.loc[geomsite.id_site==s,'id'].values[0].astype(str)
|
||||
|
||||
df['id_geom_site'] = df.id_geom_site.astype(int)
|
||||
df['id_fct'] = df['nom_fct']
|
||||
df['id_fct'] = df['id_fct'].replace(delim.nom.tolist(),delim.id.tolist())
|
||||
df['date'] = df.date_fctec
|
||||
|
||||
df.drop(columns=['id_site','nom_fct','date_fctec'], inplace=True)
|
||||
lst_idgeom = df.id_geom_site.unique().astype(str)
|
||||
|
||||
# check si data in bdd
|
||||
sql = 'select exists(select * from {sch}.{tab} where id_geom_site in ({lst_id}));'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
res = pd.read_sql(sql,con)
|
||||
if res.exists[0]:
|
||||
# delete if exist
|
||||
sql = 'delete from {sch}.{tab} where id_geom_site in ({lst_id});'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
pd.read_sql(sql,con)
|
||||
# insert new line in bdd
|
||||
col = df.columns[~df.columns.str.contains('auteur')]
|
||||
colaut = df.columns[df.columns.str.contains('auteur')]
|
||||
df[col].to_sql(
|
||||
tab, con, schema=sch,
|
||||
if_exists='append',index=False,method='multi'
|
||||
)
|
||||
# Récupération des nouveaux ids insérés
|
||||
sql = 'select * from {sch}.{tab} where id_geom_site in ({lst_id});'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
res = pd.read_sql(sql,con)
|
||||
df = df.merge(res, how='left', on=[*col])
|
||||
|
||||
ins_aut = df[['id',*colaut]].copy()
|
||||
aut['nom_prenom'] = aut.nom + ' ' + aut.prenom
|
||||
aut.loc[aut.nom_prenom.isna(), 'nom_prenom'] = aut.loc[aut.nom_prenom.isna(), 'nom']
|
||||
ins_aut[colaut] = ins_aut[colaut].replace(aut.nom_prenom.tolist(), aut.id.tolist())
|
||||
ins_aut.columns = [id_rsiteaut, 'id_auteur']
|
||||
ins_aut.to_sql(
|
||||
rtab_aut, con, schema=sch,
|
||||
if_exists='append',index=False,method='multi')
|
||||
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
import pandas as pd
|
||||
import pyzh
|
||||
zh = pyzh.zh()
|
||||
|
||||
con = pyzh.con
|
||||
sch = 'zones_humides'
|
||||
lst_site_old = ['38RD0146','38RD0147']
|
||||
lst_site_new = ['38RD0164','38RD0165']
|
||||
df = zh.get_habitat(id_site=lst_site_old)
|
||||
geomsite = zh.get_sitesGeom(id_site=lst_site_new)
|
||||
aut = pyzh.pers.get_auteur()
|
||||
|
||||
tab = 'r_site_habitat'
|
||||
rtab_aut = 'r_rsitehab_auteur'
|
||||
id_rsiteaut = 'id_sitehab'
|
||||
date_champ = 'date_habit'
|
||||
nom_champ = 'code_cb'
|
||||
id_champ = 'id_cb'
|
||||
# delim = zh._get_param(param_table='param_fct_eco_socio_patri',type_table='type_param_fct')
|
||||
|
||||
|
||||
df.drop(
|
||||
columns=['id','valid', 'date_geom', 'auteur_geom', 'desc_param','type','lb_hab_fr'],
|
||||
inplace=True,
|
||||
errors='ignore')
|
||||
|
||||
for i, s in enumerate(lst_site_new):
|
||||
df.loc[df.id_site==lst_site_old[i], 'id_site'] = s
|
||||
df.loc[df.id_site==s, ['id_geom_site']] = geomsite.loc[geomsite.id_site==s,'id'].values[0].astype(str)
|
||||
|
||||
df['id_geom_site'] = df.id_geom_site.astype(int)
|
||||
df[id_champ] = df[nom_champ]
|
||||
# df[id_champ] = df[id_champ].replace(delim.nom.tolist(),delim.id.tolist())
|
||||
df['date'] = df[date_champ]
|
||||
|
||||
df.drop(columns=['id_site',nom_champ,date_champ], inplace=True)
|
||||
lst_idgeom = df.id_geom_site.unique().astype(str)
|
||||
|
||||
# check si data in bdd
|
||||
sql = 'select exists(select * from {sch}.{tab} where id_geom_site in ({lst_id}));'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
res = pd.read_sql(sql,con)
|
||||
if res.exists[0]:
|
||||
# delete if exist
|
||||
sql = 'delete from {sch}.{tab} where id_geom_site in ({lst_id});'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
pd.read_sql(sql,con)
|
||||
# insert new line in bdd
|
||||
col = df.columns[~df.columns.str.contains('auteur')]
|
||||
colaut = df.columns[df.columns.str.contains('auteur')]
|
||||
df[col].to_sql(
|
||||
tab, con, schema=sch,
|
||||
if_exists='append',index=False,method='multi'
|
||||
)
|
||||
# Récupération des nouveaux ids insérés
|
||||
sql = 'select * from {sch}.{tab} where id_geom_site in ({lst_id});'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
res = pd.read_sql(sql,con)
|
||||
df = df.merge(res, how='left', on=[*col])
|
||||
|
||||
ins_aut = df[['id',*colaut]].copy()
|
||||
aut['nom_prenom'] = aut.nom + ' ' + aut.prenom
|
||||
aut.loc[aut.nom_prenom.isna(), 'nom_prenom'] = aut.loc[aut.nom_prenom.isna(), 'nom']
|
||||
ins_aut[colaut] = ins_aut[colaut].replace(aut.nom_prenom.tolist(), aut.id.tolist())
|
||||
ins_aut.columns = [id_rsiteaut, 'id_auteur']
|
||||
ins_aut.to_sql(
|
||||
rtab_aut, con, schema=sch,
|
||||
if_exists='append',index=False,method='multi')
|
||||
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
import pandas as pd
|
||||
import pyzh
|
||||
zh = pyzh.zh()
|
||||
|
||||
con = pyzh.con
|
||||
sch = 'zones_humides'
|
||||
lst_site_old = ['38RD0146','38RD0147']
|
||||
lst_site_new = ['38RD0164','38RD0165']
|
||||
df = zh.get_regHydro(id_site=lst_site_old)
|
||||
geomsite = zh.get_sitesGeom(id_site=lst_site_new)
|
||||
aut = pyzh.pers.get_auteur()
|
||||
|
||||
|
||||
tab = 'r_site_reghydro'
|
||||
rtab_aut = 'r_rsitehydro_auteur'
|
||||
id_rsiteaut = 'id_sitehydro'
|
||||
date_champ = 'date_reghy'
|
||||
nom_champ1 = 'regime_hydri'
|
||||
nom_champ2 = 'permanence'
|
||||
id_champ1 = 'id_reg_hydro'
|
||||
id_champ2 = 'id_permanence'
|
||||
delim1 = zh._get_param(param_table='param_reg_hydro', type_court=False)
|
||||
delim2 = zh._get_param(param_table='param_permanence', type_court=False)
|
||||
|
||||
col_desc = df.columns[df.columns.str.contains('desc')]
|
||||
df.drop(
|
||||
columns=['id','valid', 'date_geom', 'auteur_geom', *col_desc,'type','lb_hab_fr'],
|
||||
inplace=True,
|
||||
errors='ignore')
|
||||
|
||||
for i, s in enumerate(lst_site_new):
|
||||
df.loc[df.id_site==lst_site_old[i], 'id_site'] = s
|
||||
df.loc[df.id_site==s, ['id_geom_site']] = geomsite.loc[geomsite.id_site==s,'id'].values[0].astype(str)
|
||||
|
||||
df['id_geom_site'] = df.id_geom_site.astype(int)
|
||||
df[id_champ1] = df[nom_champ1]
|
||||
df[id_champ2] = df[nom_champ2]
|
||||
df[id_champ1] = df[id_champ1].replace(delim1.nom.tolist(),delim1.id.tolist())
|
||||
df[id_champ2] = df[id_champ2].replace(delim2.nom.tolist(),delim2.id.tolist())
|
||||
df['date'] = df[date_champ]
|
||||
df.in_out = df.in_out == 'entree'
|
||||
|
||||
|
||||
df.drop(columns=['id_site',nom_champ1,nom_champ2,date_champ], inplace=True)
|
||||
lst_idgeom = df.id_geom_site.unique().astype(str)
|
||||
|
||||
# check si data in bdd
|
||||
sql = 'select exists(select * from {sch}.{tab} where id_geom_site in ({lst_id}));'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
res = pd.read_sql(sql,con)
|
||||
if res.exists[0]:
|
||||
# delete if exist
|
||||
sql = 'delete from {sch}.{tab} where id_geom_site in ({lst_id});'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
pd.read_sql(sql,con)
|
||||
# insert new line in bdd
|
||||
col = df.columns[~df.columns.str.contains('auteur')]
|
||||
colaut = df.columns[df.columns.str.contains('auteur')]
|
||||
df[col].to_sql(
|
||||
tab, con, schema=sch,
|
||||
if_exists='append',index=False,method='multi'
|
||||
)
|
||||
# Récupération des nouveaux ids insérés
|
||||
sql = 'select * from {sch}.{tab} where id_geom_site in ({lst_id});'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
res = pd.read_sql(sql,con)
|
||||
df = df.merge(res, how='left', on=[*col])
|
||||
|
||||
ins_aut = df[['id',*colaut]].copy()
|
||||
aut['nom_prenom'] = aut.nom + ' ' + aut.prenom
|
||||
aut.loc[aut.nom_prenom.isna(), 'nom_prenom'] = aut.loc[aut.nom_prenom.isna(), 'nom']
|
||||
ins_aut[colaut] = ins_aut[colaut].replace(aut.nom_prenom.tolist(), aut.id.tolist())
|
||||
ins_aut.columns = [id_rsiteaut, 'id_auteur']
|
||||
ins_aut.to_sql(
|
||||
rtab_aut, con, schema=sch,
|
||||
if_exists='append',index=False,method='multi')
|
||||
@@ -0,0 +1,82 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
import pandas as pd
|
||||
import pyzh
|
||||
zh = pyzh.zh()
|
||||
|
||||
con = pyzh.con
|
||||
sch = 'zones_humides'
|
||||
lst_site_old = ['38RD0146','38RD0147']
|
||||
lst_site_new = ['38RD0164','38RD0165']
|
||||
df = zh.get_sub(id_site=lst_site_old)
|
||||
geomsite = zh.get_sitesGeom(id_site=lst_site_new)
|
||||
aut = pyzh.pers.get_auteur()
|
||||
|
||||
tab = 'r_site_sub'
|
||||
rtab_aut = 'r_rsitesub_auteur'
|
||||
id_rsiteaut = 'id_sitesub'
|
||||
date_champ = 'date_sub'
|
||||
nom_champ1 = 'Submersion étendue'
|
||||
nom_champ2 = 'Submersion fréquente'
|
||||
nom_champ3 = 'origine_sub'
|
||||
id_champ1 = 'id_freqsub'
|
||||
id_champ2 = 'id_etendsub'
|
||||
id_champ3 = 'id_origsub'
|
||||
delim = zh._get_param(param_table='param_sub',type_table='type_param_sub', type_court=False)
|
||||
delim1 = delim.loc[delim.type == nom_champ1]
|
||||
delim2 = delim.loc[delim.type == nom_champ2]
|
||||
|
||||
|
||||
df.drop(
|
||||
columns=['id','valid', 'date_geom', 'auteur_geom', 'desc_param','type','lb_hab_fr'],
|
||||
inplace=True,
|
||||
errors='ignore')
|
||||
|
||||
for i, s in enumerate(lst_site_new):
|
||||
df.loc[df.id_site==lst_site_old[i], 'id_site'] = s
|
||||
df.loc[df.id_site==s, ['id_geom_site']] = geomsite.loc[geomsite.id_site==s,'id'].values[0].astype(str)
|
||||
|
||||
df['id_geom_site'] = df.id_geom_site.astype(int)
|
||||
df[id_champ1] = df[nom_champ1]
|
||||
df[id_champ2] = df[nom_champ2]
|
||||
df[id_champ3] = df[nom_champ3]
|
||||
df[id_champ1] = df[id_champ1].replace(delim1.nom.tolist(),delim1.id.tolist())
|
||||
df[id_champ2] = df[id_champ2].replace(delim2.nom.tolist(),delim2.id.tolist())
|
||||
df['date'] = df[date_champ]
|
||||
|
||||
df.drop(columns=['id_site',nom_champ1,nom_champ2,nom_champ3,date_champ], inplace=True)
|
||||
lst_idgeom = df.id_geom_site.unique().astype(str)
|
||||
|
||||
# check si data in bdd
|
||||
sql = 'select exists(select * from {sch}.{tab} where id_geom_site in ({lst_id}));'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
res = pd.read_sql(sql,con)
|
||||
if res.exists[0]:
|
||||
# delete if exist
|
||||
sql = 'delete from {sch}.{tab} where id_geom_site in ({lst_id});'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
pd.read_sql(sql,con)
|
||||
# insert new line in bdd
|
||||
col = df.columns[~df.columns.str.contains('auteur')]
|
||||
colaut = df.columns[df.columns.str.contains('auteur')]
|
||||
df[col].to_sql(
|
||||
tab, con, schema=sch,
|
||||
if_exists='append',index=False,method='multi'
|
||||
)
|
||||
# Récupération des nouveaux ids insérés
|
||||
sql = 'select * from {sch}.{tab} where id_geom_site in ({lst_id});'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
res = pd.read_sql(sql,con)
|
||||
df = df.merge(res, how='left', on=[*col])
|
||||
|
||||
ins_aut = df[['id',*colaut]].copy()
|
||||
aut['nom_prenom'] = aut.nom + ' ' + aut.prenom
|
||||
aut.loc[aut.nom_prenom.isna(), 'nom_prenom'] = aut.loc[aut.nom_prenom.isna(), 'nom']
|
||||
ins_aut[colaut] = ins_aut[colaut].replace(aut.nom_prenom.tolist(), aut.id.tolist())
|
||||
ins_aut.columns = [id_rsiteaut, 'id_auteur']
|
||||
ins_aut.to_sql(
|
||||
rtab_aut, con, schema=sch,
|
||||
if_exists='append',index=False,method='multi')
|
||||
@@ -0,0 +1,83 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
import pandas as pd
|
||||
import pyzh
|
||||
zh = pyzh.zh()
|
||||
|
||||
con = pyzh.con
|
||||
sch = 'zones_humides'
|
||||
lst_site_old = ['38RD0146','38RD0147']
|
||||
lst_site_new = ['38RD0164','38RD0165']
|
||||
df = zh.get_usageprocess(id_site=lst_site_old)
|
||||
geomsite = zh.get_sitesGeom(id_site=lst_site_new)
|
||||
aut = pyzh.pers.get_auteur()
|
||||
|
||||
tab = 'r_site_usageprocess'
|
||||
rtab_aut = 'r_rsiteusage_auteur'
|
||||
id_rsiteaut = 'id_siteusage'
|
||||
date_champ = 'date_usage'
|
||||
nom_champ1 = 'activite_hum'
|
||||
nom_champ2 = 'impact'
|
||||
nom_champ3 = 'position'
|
||||
id_champ1 = 'id_activ_hum'
|
||||
id_champ2 = 'id_impact'
|
||||
id_champ3 = 'id_position'
|
||||
delim1 = zh._get_param(param_table='param_activ_hum', type_court=False)
|
||||
delim2 = zh._get_param(param_table='param_impact', type_court=False)
|
||||
delim3 = zh._get_param(param_table='param_position', type_court=False)
|
||||
|
||||
col_desc = df.columns[df.columns.str.contains('desc')]
|
||||
df.drop(
|
||||
columns=['id','valid', 'date_geom', 'auteur_geom', *col_desc,'description','type','lb_hab_fr'],
|
||||
inplace=True,
|
||||
errors='ignore')
|
||||
|
||||
for i, s in enumerate(lst_site_new):
|
||||
df.loc[df.id_site==lst_site_old[i], 'id_site'] = s
|
||||
df.loc[df.id_site==s, ['id_geom_site']] = geomsite.loc[geomsite.id_site==s,'id'].values[0].astype(str)
|
||||
|
||||
df['id_geom_site'] = df.id_geom_site.astype(int)
|
||||
df[id_champ1] = df[nom_champ1]
|
||||
df[id_champ2] = df[nom_champ2]
|
||||
df[id_champ3] = df[nom_champ3]
|
||||
df[id_champ1] = df[id_champ1].replace(delim1.nom.tolist(),delim1.id.tolist())
|
||||
df[id_champ2] = df[id_champ2].replace(delim2.nom.tolist(),delim2.id.tolist())
|
||||
df[id_champ3] = df[id_champ3].replace(delim3.nom.tolist(),delim3.id.tolist())
|
||||
df['date'] = df[date_champ]
|
||||
|
||||
df.drop(columns=['id_site',nom_champ1,nom_champ2,nom_champ3,date_champ], inplace=True)
|
||||
lst_idgeom = df.id_geom_site.unique().astype(str)
|
||||
|
||||
# check si data in bdd
|
||||
sql = 'select exists(select * from {sch}.{tab} where id_geom_site in ({lst_id}));'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
res = pd.read_sql(sql,con)
|
||||
if res.exists[0]:
|
||||
# delete if exist
|
||||
sql = 'delete from {sch}.{tab} where id_geom_site in ({lst_id});'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
pd.read_sql(sql,con)
|
||||
# insert new line in bdd
|
||||
col = df.columns[~df.columns.str.contains('auteur')]
|
||||
colaut = df.columns[df.columns.str.contains('auteur')]
|
||||
df[col].to_sql(
|
||||
tab, con, schema=sch,
|
||||
if_exists='append',index=False,method='multi'
|
||||
)
|
||||
# Récupération des nouveaux ids insérés
|
||||
sql = 'select * from {sch}.{tab} where id_geom_site in ({lst_id});'.format(
|
||||
sch=sch, tab=tab, lst_id=','.join(lst_idgeom)
|
||||
)
|
||||
res = pd.read_sql(sql,con)
|
||||
df = df.merge(res, how='left', on=[*col])
|
||||
|
||||
ins_aut = df[['id',*colaut]].copy()
|
||||
aut['nom_prenom'] = aut.nom + ' ' + aut.prenom
|
||||
aut.loc[aut.nom_prenom.isna(), 'nom_prenom'] = aut.loc[aut.nom_prenom.isna(), 'nom']
|
||||
ins_aut[colaut] = ins_aut[colaut].replace(aut.nom_prenom.tolist(), aut.id.tolist())
|
||||
ins_aut.columns = [id_rsiteaut, 'id_auteur']
|
||||
ins_aut.to_sql(
|
||||
rtab_aut, con, schema=sch,
|
||||
if_exists='append',index=False,method='multi')
|
||||
Reference in New Issue
Block a user