init depot renew
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
import geopandas as gpd
|
||||
import pandas as pd
|
||||
from sqlalchemy.engine import URL
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
# Parametres bdd HOMER - sicen2
|
||||
from pycen import con_sicen
|
||||
schema_hom = 'saisie'
|
||||
table_hom = 'saisie_observation'
|
||||
|
||||
# Parametres bdd HOMER - geonature
|
||||
from pycen import con_gn as con_geo
|
||||
schema_geo = 'taxonomie'
|
||||
table_geo = 'taxref'
|
||||
|
||||
|
||||
file = True
|
||||
path_file = '/home/colas/Documents/9_PROJETS/6_GEONATURE/MODULES/Macrophytes/Module_Macrophytes_Liste_Espèces.xlsx'
|
||||
|
||||
taxref = False
|
||||
args = {
|
||||
'sous_famille': 'Zygaeninae'
|
||||
}
|
||||
|
||||
|
||||
if file:
|
||||
if path_file.split(r'.',1)[-1] == 'xlsx':
|
||||
f = pd.read_excel(path_file)
|
||||
f.columns = f.colufmns.str.lower()
|
||||
lst_cdnom = f.cd_nom.astype(str).unique()
|
||||
elif path_file.rsplit(r'.',1)[-1] == 'csv':
|
||||
f = pd.read_csv(path_file)
|
||||
f.columns = f.columns.str.lower()
|
||||
lst_cdnom = f.cd_nom.astype(str).unique()
|
||||
elif taxref:
|
||||
sql = 'SELECT * FROM {sch}.{tab}'.format(sch = schema_geo,tab = table_geo)
|
||||
sql += ' WHERE %s'%' AND '.join([
|
||||
x+'='+"'%s'"%args[x] if isinstance(args[x],str) else x+' in %s'%str(tuple(args[x])) for x in args
|
||||
])
|
||||
df_tax = pd.read_sql_query(sql,con_geo)
|
||||
lst_cdnom = df_tax.cd_nom.astype(str).unique()
|
||||
else:
|
||||
sql = 'SELECT * FROM {sch}.{tab}'.format(sch=schema_hom, tab=table_hom)
|
||||
gdf_saisie = gpd.read_postgis(
|
||||
sql = sql,
|
||||
con = con_sicen,
|
||||
geom_col = 'geometrie'
|
||||
)
|
||||
lst_cdnom = gdf_saisie.cd_nom.unique()
|
||||
|
||||
# Récupération de la liste taxref non présents
|
||||
# dans la table bib_noms
|
||||
sql = """
|
||||
SELECT DISTINCT cd_nom,cd_ref,nom_vern,id_rang FROM {sch}.{tab}
|
||||
WHERE cd_nom IN ('{lst}')
|
||||
AND cd_nom NOT IN (SELECT cd_nom FROM {sch}.bib_noms WHERE cd_nom IN ('{lst}'))
|
||||
""".format(
|
||||
sch = schema_geo,
|
||||
tab = table_geo,
|
||||
lst = "','".join(lst_cdnom)
|
||||
)
|
||||
df_taxref = pd.read_sql_query(sql,con_geo)
|
||||
df_taxref.rename(columns={'nom_vern':'nom_francais'}, inplace=True)
|
||||
|
||||
# Envoie de la liste dans la table taxonomie.bib_noms
|
||||
df_taxref.drop(columns=['id_rang']).to_sql(
|
||||
name = 'bib_noms',
|
||||
con = con_geo,
|
||||
schema=schema_geo,
|
||||
if_exists='append',
|
||||
index=False
|
||||
)
|
||||
|
||||
# récupération des IDs la table taxonomie.bib_noms
|
||||
sql = 'SELECT distinct cd_nom FROM {sch}.{tab} WHERE cd_nom IN ({id_nom_max})'.format(
|
||||
sch = schema_geo,
|
||||
tab = 'bib_noms',
|
||||
id_nom_max = ",".join(lst_cdnom))
|
||||
|
||||
id_liste = 112 # id_liste de la liste présente dans la table taxonomie.bib_listes
|
||||
sql = """
|
||||
SELECT id_nom FROM {sch}.{tab} WHERE cd_nom IN ({id_nom_max})
|
||||
AND id_nom NOT IN (SELECT id_nom FROM {sch}.cor_nom_liste WHERE id_liste = '{id_liste}')
|
||||
""".format(
|
||||
sch = schema_geo,
|
||||
tab = 'bib_noms',
|
||||
id_nom_max = ",".join(lst_cdnom),
|
||||
id_liste = id_liste
|
||||
)
|
||||
_df = pd.read_sql_query(sql,con_geo)
|
||||
# _df = pd.read_sql_table(
|
||||
# table_name='bib_noms',
|
||||
# con= con_geo,
|
||||
# schema=schema_geo
|
||||
# )
|
||||
_df['id_liste'] = id_liste # id_liste de la liste présente dans la table taxonomie.bib_listes
|
||||
df = _df[['id_liste','id_nom']]
|
||||
# envoie de la liste dans la table taxonomie.bib_noms
|
||||
df.to_sql(
|
||||
name = 'cor_nom_liste',
|
||||
con = con_geo,
|
||||
schema=schema_geo,
|
||||
if_exists='append',
|
||||
index=False
|
||||
)
|
||||
|
||||
sql = 'SELECT * FROM {sch}.cor_nom_liste'.format(
|
||||
sch = schema_geo)
|
||||
cor_nom_liste = pd.read_sql_query(sql,con_geo)
|
||||
Reference in New Issue
Block a user