[CG] : add exploit sicen2

This commit is contained in:
Colas Geier 2023-07-04 14:25:19 +02:00
parent 63b76f6710
commit 5b058daca6
2 changed files with 175 additions and 0 deletions

8
pycen/sicen2/__init__.py Normal file
View File

@ -0,0 +1,8 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
from .obs import (
get_obs,
get_obs_serena,
refresh_vm,
etude,lot,protocole
)

167
pycen/sicen2/obs.py Normal file
View File

@ -0,0 +1,167 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
from ..params import con_sicen
def get_obs(date_debut:str=None,date_fin:str=None,etude:str=None,lot:str=None,protocole:str=None, type_table:str='vm'):
'''Téléchargement des données sicen'''
from geopandas import read_postgis
schema = 'saisie'
ttabl = 'saisie_observation'
v_tab = 'v_'+ttabl
vmtab = 'vm_synthese_observations'
condition = {
'etude':{'nom':etude,'cd':None},
'lot':{'nom':lot,'cd':None},
'protocole':{'nom':protocole,'cd':None},
'date_debut':{'nom':date_debut,'cd':None},
'date_fin':{'nom':date_fin,'cd':None}
}
if type_table == 'vm' :
table = vmtab
geom = 'geom'
elif type_table == 'view' :
table = v_tab
geom = 'geometrie'
elif type_table == 'table' :
table = ttabl
geom = 'geometrie'
else : raise(Exception('''Valeur "%s" non valide pour l'argument "type_table" '''%type_table))
sql = 'SELECT * FROM {sch}.{tab}'.format(sch=schema,tab=table)
if isinstance(etude,(str,list)):
if isinstance(etude,str): etude = [etude]
# whetu = 'etude ilike ANY (ARRAY{etu})'.format(etu=str(etude))
condition['etude']['cd'] = 'etude ilike ANY (ARRAY{etu})'.format(etu=str(etude))
if isinstance(lot,(str,list)):
if isinstance(lot,str): lot = [lot]
# whlo = 'lot ilike ANY (ARRAY{etu})'.format(etu=str(lot))
condition['lot']['cd'] = 'lot_donnee ilike ANY (ARRAY{etu})'.format(etu=str(lot))
if isinstance(protocole,(str,list)):
if isinstance(protocole,str): protocole = [protocole]
# whpr = 'protocole ilike ANY (ARRAY{etu})'.format(etu=str(protocole))
condition['protocole']['cd'] = 'protocole ilike ANY (ARRAY{etu})'.format(etu=str(protocole))
if isinstance(date_debut,str):
# whdd = 'date_debut = {etu}'.format(etu=str(date_debut))
condition['date_debut']['cd'] = "(date_debut_obs >= '{etu}' OR date_obs >= '{etu}')".format(etu=str(date_debut))
# condition['date_debut']['cd'] = 'date_debut >= {etu}'.format(etu=str(date_debut))
if isinstance(date_fin,str):
# whdf = 'date_fin = {etu}'.format(etu=str(date_fin))
condition['date_fin']['cd'] = "(date_fin_obs <= '{etu}' OR date_obs <= '{etu}')".format(etu=str(date_fin))
# condition['date_fin']['cd'] = 'date_fin <= {etu}'.format(etu=str(date_fin))
nb_cd = {c for c in condition if condition[c]['nom'] is not None}
if len(nb_cd) > 0: sql += ' WHERE '
if len(nb_cd) == 1 :
arg = next(iter(nb_cd))
sql += condition[arg]['cd']
if len(nb_cd) > 1 :
for i,arg in enumerate(nb_cd):
if i == 0 : sql += condition[arg]['cd']
else : sql += ' AND '+condition[arg]['cd']
# print(sql)
return read_postgis(sql,con_sicen,geom)
def get_obs_serena(insicen:bool=None):
from pandas import read_sql_query
postgis_sch = 'serenabase'
sql = '''
SELECT o.
s.site_nom,
s.site_ref_sig,
CASE WHEN t.taxo_id = 203471 THEN (SELECT cd_nom FROM inpn.taxref WHERE cd_nom = '54213' )
WHEN t.taxo_id = 203491 THEN (SELECT cd_nom FROM inpn.taxref WHERE cd_nom = '521494' )
WHEN tax.cd_nom IS NULL THEN ta.cd_nom::text
ELSE tax.cd_nom
END cd_nom,
CASE WHEN t.taxo_id = 203471 THEN (SELECT phylum FROM inpn.taxref WHERE cd_nom = '54213' )
WHEN t.taxo_id = 203491 THEN (SELECT phylum FROM inpn.taxref WHERE cd_nom = '521494' )
WHEN tax.phylum IS NULL THEN ta.phylum
ELSE tax.phylum
END phylum,
CASE WHEN t.taxo_id = 203471 THEN (SELECT classe FROM inpn.taxref WHERE cd_nom = '54213' )
WHEN t.taxo_id = 203491 THEN (SELECT classe FROM inpn.taxref WHERE cd_nom = '521494' )
WHEN tax.classe IS NULL THEN ta.classe
ELSE tax.classe
END classe,
CASE WHEN t.taxo_id = 203471 THEN (SELECT ordre FROM inpn.taxref WHERE cd_nom = '54213' )
WHEN t.taxo_id = 203491 THEN (SELECT ordre FROM inpn.taxref WHERE cd_nom = '521494' )
WHEN tax.ordre IS NULL THEN ta.ordre
ELSE tax.ordre
END ordre,
CASE WHEN t.taxo_id = 203471 THEN (SELECT famille FROM inpn.taxref WHERE cd_nom = '54213' )
WHEN t.taxo_id = 203491 THEN (SELECT famille FROM inpn.taxref WHERE cd_nom = '521494' )
WHEN tax.famille IS NULL THEN ta.famille
ELSE tax.famille
END famille,
CASE WHEN t.taxo_id = 203471 THEN (SELECT nom_complet FROM inpn.taxref WHERE cd_nom = '54213' )
WHEN t.taxo_id = 203491 THEN (SELECT nom_complet FROM inpn.taxref WHERE cd_nom = '521494' )
WHEN tax.nom_complet IS NULL THEN ta.nom_complet
ELSE tax.nom_complet
END nom_complet,
CASE WHEN t.taxo_id = 203471 THEN (SELECT nom_vern FROM inpn.taxref WHERE cd_nom = '54213' )
WHEN t.taxo_id = 203491 THEN (SELECT nom_vern FROM inpn.taxref WHERE cd_nom = '521494' )
WHEN tax.nom_vern IS NULL THEN ta.nom_vern
ELSE tax.nom_vern
END nom_vern,
CASE WHEN tmpo.ogll_lon = '999' THEN tmps.sgll_lon::float
ELSE tmpo.ogll_lon::float
END lon,
CASE WHEN tmpo.ogll_lat = '999' THEN tmps.sgll_lat::float
ELSE tmpo.ogll_lat::float
END lat
FROM {sch}.rnf_obse o
LEFT JOIN {sch}.rnf_relv r ON r.relv_id = o.obse_relv_id
LEFT JOIN {sch}.rnf_site s ON s.site_id = o.obse_site_id
LEFT JOIN {sch}.tmp_sgll tmps ON tmps.sgll_site_id = o.obse_site_id
LEFT JOIN {sch}.tmp_ogll tmpo ON tmpo.ogll_obse_id = o.obse_id
LEFT JOIN serenarefe.rnf_taxo t ON t.taxo_id = o.obse_taxo_id
LEFT JOIN inpn.taxons_isere_absents_taxref ta ON ta.id_taxon = o.obse_taxo_id
LEFT JOIN inpn.taxref tax ON t.taxo_mnhn_id = tax.cd_nom::int
--LEFT JOIN {sch}.rnf_odat od ON od.odat_obse_id = o.obse_id
'''.format(sch=postgis_sch)
if insicen is not None :
sql_sicen = 'SELECT DISTINCT id_origine FROM saisie.saisie_observation WHERE id_lot=3'
sicen_obs = read_sql_query(sql_sicen,con_sicen)
if insicen is True :
sql += 'WHERE obse_id IN {lst_id}'.format(lst_id=tuple(sicen_obs.id_origine.astype(int)))
if insicen is False :
sql += 'WHERE obse_id NOT IN {lst_id}'.format(lst_id=tuple(sicen_obs.id_origine.astype(int)))
return read_sql_query(sql,con_sicen)\
.dropna(axis=1,how='all')
def refresh_vm():
sql = "REFRESH MATERIALIZED VIEW saisie.vm_synthese_observations WITH DATA;"
with con_sicen.begin() as cnx:
cnx.execute(sql)
def etude():
from pandas import read_sql_query
sql = 'SELECT * FROM md.etude'
return read_sql_query(sql, con_sicen)
def lot():
from pandas import read_sql_query
sql = 'SELECT * FROM md.lot'
return read_sql_query(sql, con_sicen)
def protocole():
from pandas import read_sql_query
sql = 'SELECT * FROM md.protocole'
return read_sql_query(sql, con_sicen)