104 lines
3.8 KiB
Python
104 lines
3.8 KiB
Python
from pycen import con_gn
|
|
import geopandas as gpd
|
|
import requests
|
|
|
|
def get_list_observateur(code_liste,with_data=False):
|
|
"""
|
|
@code_liste : str. Nom du sous-module monitoring où chercher les sites.
|
|
@with_data : bool.
|
|
"""
|
|
|
|
if with_data is True:
|
|
sql = '''
|
|
SELECT * FROM utilisateurs.t_listes
|
|
JOIN (utilisateurs.cor_role_liste JOIN utilisateurs.t_roles t USING (id_role)) USING(id_liste)
|
|
'''
|
|
elif with_data is False:
|
|
sql = '''
|
|
SELECT code_liste list_name,id_role,nom_role||' '||prenom_role nom_complet FROM utilisateurs.t_listes
|
|
JOIN (utilisateurs.cor_role_liste JOIN utilisateurs.t_roles t USING (id_role)) USING(id_liste)
|
|
'''
|
|
else :
|
|
raise('Argument `with_data` dont accept value "%s"'%with_data)
|
|
|
|
wh = " WHERE (id_liste = {mod} OR code_liste = '{mod}')".format(mod=code_liste)
|
|
|
|
SQL = sql + wh
|
|
return gpd.pd.read_sql_query(sql=SQL,con=con_gn)
|
|
|
|
|
|
|
|
def get_site(module,id_site=None,with_data=False):
|
|
"""
|
|
@module : str. Nom du sous-module monitoring où chercher les sites.
|
|
@id_site : str. Nom du site au sein du sous-monitoring où chercher les sites.
|
|
@with_data : bool.
|
|
"""
|
|
|
|
if with_data is True:
|
|
sql = '''
|
|
SELECT * FROM gn_monitoring.t_base_sites
|
|
JOIN (gn_monitoring.cor_site_module JOIN gn_commons.t_modules t USING (id_module)) USING (id_base_site)
|
|
'''
|
|
elif with_data is False:
|
|
sql = '''
|
|
SELECT id_base_site,base_site_name,
|
|
(ST_Y(geom)::numeric)::text||' '||(ST_X(geom)::numeric)::text geometry
|
|
FROM gn_monitoring.t_base_sites
|
|
JOIN (gn_monitoring.cor_site_module JOIN gn_commons.t_modules t USING (id_module)) USING (id_base_site)
|
|
'''
|
|
else :
|
|
raise('Argument `with_data` dont accept value "%s"'%with_data)
|
|
|
|
wh = " WHERE (t.module_code = '{mod}' OR t.module_label = '{mod}')".format(mod=module)
|
|
|
|
if id_site is not None:
|
|
wh = wh + " AND id_site = '%s'"%id_site
|
|
|
|
SQL = sql + wh
|
|
return gpd.pd.read_sql_query(sql=SQL,con=con_gn)
|
|
# return gpd.read_postgis(sql=SQL,con=con_gn,geom_col='geometry')
|
|
|
|
|
|
def get_jdd(module):
|
|
url = 'https://geonature.cen-isere.fr/geonature/api/monitorings/util/init_data/{mod}'
|
|
r = requests.get(url.format(mod=module), auth=('user', 'pass'))
|
|
if r.status_code == 200:
|
|
d = r.json()
|
|
return gpd.pd.DataFrame(d['dataset'])[['id_dataset','dataset_name']]
|
|
|
|
|
|
def get_taxon(id_list=100,args={}):
|
|
url = 'https://geonature.cen-isere.fr/taxhub/api/taxref/allnamebylist/{list}?limit=100000&offset=0'
|
|
if args is not None:
|
|
formArgs = '&'.join(['='.join([x,args[x]]) for x in args])
|
|
url = 'https://geonature.cen-isere.fr/taxhub/api/taxref/?'+formArgs
|
|
r = requests.get(url.format(list=id_list), auth=('user', 'pass'))
|
|
if r.status_code == 200:
|
|
d = r.json()
|
|
if args is not None:
|
|
d = r.json()['items']
|
|
df = gpd.pd.DataFrame(d)
|
|
return df[['cd_nom','lb_nom','nom_vern']].rename(columns={'lb_nom':'nom_complet'})
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
module_name = "chiro"
|
|
id_tax_list = 100 # taxons occtax
|
|
|
|
PATH = '/media/colas/SRV/homer_geonat/home/geonatureadmin/protocoles_suivi/{mod}/odk_form/'.format(mod=module_name)
|
|
PATH = '/home/colas/Documents/9_PROJETS/6_GEONATURE/MODULES/ODK/test/CHIRO/'
|
|
csv_site_name = 'gn_sites.csv'
|
|
csv_jdd_name = 'gn_jdds.csv'
|
|
csv_tax_name = 'gn_taxons.csv'
|
|
get_site(module_name).to_csv(PATH+csv_site_name,sep=';',index=False)
|
|
get_jdd(module_name).to_csv(PATH+csv_jdd_name,sep=';',index=False)
|
|
get_taxon(args={'ordre':'Chiroptera'}).to_csv(PATH+csv_tax_name,sep=';',index=False)
|
|
|
|
code_liste = '2'
|
|
csv_obs_name = 'gn_observateurs.csv'
|
|
get_list_observateur(code_liste).to_csv(PATH+csv_obs_name,sep=';',index=False)
|
|
|
|
|