135 lines
3.8 KiB
Python
135 lines
3.8 KiB
Python
#!/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
|
|
user_hom = 'cen_admin'
|
|
pwd_hom = '#CEN38@venir'
|
|
adr_hom = '91.134.194.221'
|
|
port_hom = '5432'
|
|
base_hom = 'sicen2'
|
|
schema_hom = 'saisie'
|
|
table_hom = 'saisie_observation'
|
|
|
|
url_hom = URL.create('postgresql+psycopg2',
|
|
username = user_hom,
|
|
password = pwd_hom,
|
|
host = adr_hom,
|
|
database = base_hom,
|
|
)
|
|
con_sicen = create_engine(url_hom)
|
|
|
|
# Parametres bdd HOMER - geonature
|
|
user_geo = 'geonatadmin'
|
|
pwd_geo = 'Ge0naT38*aDm1n'
|
|
adr_geo = '91.134.194.221'
|
|
port_geo = '5432'
|
|
base_geo = 'geonature2db'
|
|
schema_geo = 'taxonomie'
|
|
table_geo = 'taxref'
|
|
|
|
url_geo = URL.create('postgresql+psycopg2',
|
|
username = user_geo,
|
|
password = pwd_geo,
|
|
host = adr_geo,
|
|
database = base_geo,
|
|
)
|
|
con_geo = create_engine(url_geo)
|
|
|
|
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) |