113 lines
3.3 KiB
Python
113 lines
3.3 KiB
Python
from pycen import con_gn
|
|
import numpy as np
|
|
import pandas as pd
|
|
import os
|
|
|
|
def get_status(lst):
|
|
sql = """
|
|
SELECT
|
|
t.cd_nom,
|
|
t.cd_ref,
|
|
t.regne,
|
|
t.phylum,
|
|
t.classe,
|
|
t.ordre,
|
|
t.famille,
|
|
t.group1_inpn,
|
|
t.group2_inpn,
|
|
t.nom_vern,
|
|
t.nom_complet,
|
|
t.nom_valide,
|
|
t.lb_nom,
|
|
--s.*
|
|
s.rq_statut,
|
|
s.code_statut,
|
|
s.cd_type_statut,
|
|
s.label_statut,
|
|
s.full_citation,
|
|
s.doc_url
|
|
FROM taxonomie.taxref t
|
|
JOIN taxonomie.v_bdc_status s USING (cd_nom)
|
|
WHERE t.cd_nom IN {cd_nom}
|
|
;""".format(cd_nom = tuple(lst))
|
|
return pd.read_sql_query(sql,con_gn)
|
|
|
|
dict_dep = {
|
|
'38':'Isère',
|
|
'42':'Loire',
|
|
'07':'Ardèche',
|
|
'26':'Drôme',
|
|
}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
PATH = '/media/colas/SRV/FICHIERS/SITES/SITES GERES/ROLA_ROLANDE-MAUPAS/ROLA_PPI/ROLA_2025-2034_PG/donneesnaturalistes'
|
|
file = 'liste sp_ROLA.xlsx'
|
|
sheet = 'liste sp'
|
|
|
|
# Liste des CD_NOM en entrée
|
|
cd_col = 'cd_ref'
|
|
taxlist = pd.read_excel(os.path.join(PATH,file),sheet,usecols=[cd_col],header=0)
|
|
tab_sp = pd.read_excel(os.path.join(PATH,file),sheet,index_col=cd_col)
|
|
df = get_status(taxlist[cd_col].astype(str))
|
|
|
|
for c in ['cd_ref','cd_nom','lb_nom']:
|
|
if c in tab_sp.columns:
|
|
# if 'cd_nom' not in df.columns and c == 'cd_ref': continue
|
|
tab_sp.drop(c,axis=1,inplace=True)
|
|
|
|
# df.to_csv('/media/colas/SRV/FICHIERS/TRANSFERTS-EQUIPE/LC/BOCA_CD_NOM_STATUS.csv')
|
|
|
|
pivot = pd.pivot_table(
|
|
df,
|
|
values='code_statut',
|
|
index=['cd_nom', 'cd_ref','lb_nom'#,'niveau_admin','lb_adm_tr'
|
|
],
|
|
columns=['cd_type_statut'],
|
|
aggfunc=list,fill_value=None)
|
|
|
|
for c in pivot.columns:
|
|
pivot[c] = [x[0] if x is not np.NaN and len(x)==1 else x for x in pivot[c]]
|
|
if 'DH' in pivot.columns:
|
|
pivot['DH'] = [','.join(x) if (x is not np.NaN) and (len(x)==2) else x for x in pivot['DH']]
|
|
pivot.DH.replace({'CDH':''},regex=True,inplace=True)
|
|
|
|
pivot = tab_sp.merge(pivot,on=[cd_col],how='left')
|
|
|
|
|
|
pivlib = pd.pivot_table(
|
|
df,
|
|
values='label_statut',
|
|
index=['cd_nom', 'cd_ref','lb_nom'#,'niveau_admin','lb_adm_tr'
|
|
],
|
|
columns=['cd_type_statut'],
|
|
aggfunc=list,fill_value=None)
|
|
for c in pivlib.columns:
|
|
pivlib[c] = [x[0] if x is not np.NaN and len(x)==1 else x for x in pivlib[c]]
|
|
if 'DH' in pivot.columns:
|
|
pivlib['DH'] = [','.join(x) if (x is not np.NaN) and (len(x)==2) else x for x in pivlib['DH']]
|
|
pivlib.DH.replace({'CDH':''},regex=True,inplace=True)
|
|
|
|
pivlib = tab_sp.merge(pivlib,on=[cd_col],how='left')
|
|
|
|
print('INIT writer')
|
|
NAME_OUT = os.path.join(PATH,sheet+'_status.xlsx')
|
|
with pd.ExcelWriter(NAME_OUT) as writer:
|
|
df.to_excel(
|
|
writer,sheet_name='v_bdc_status',index=False
|
|
)
|
|
# writer.save()
|
|
print('v_bdc_status OK !')
|
|
pivot.to_excel(
|
|
writer,sheet_name='pivot_table'
|
|
)
|
|
# writer.save()
|
|
print('pivot_table OK !')
|
|
pivlib.to_excel(
|
|
writer,sheet_name='pivot_libel'
|
|
)
|
|
# writer.save()
|
|
print('pivot_libel OK !')
|
|
|