update code
This commit is contained in:
parent
205e55f6f2
commit
fd0ddaecc4
@ -46,7 +46,7 @@ def revoke_all_table(con,sch):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sch_cad = '38_202501'
|
sch_cad = '26_202504'
|
||||||
sch_old = '42_202207'
|
sch_old = '38_202501'
|
||||||
grant_all_table(con_cad,sch_cad)
|
grant_all_table(con_cad,sch_cad)
|
||||||
# revoke_all_table(con_cad,sch_old)
|
# revoke_all_table(con_cad,sch_old)
|
||||||
@ -106,7 +106,7 @@ def format_date(df):
|
|||||||
df.date = pd.to_datetime(df.date,format='%d%b%Y')
|
df.date = pd.to_datetime(df.date,format='%d%b%Y')
|
||||||
|
|
||||||
if table == '"PS_BIEVRE_CEN38_2016"':
|
if table == '"PS_BIEVRE_CEN38_2016"':
|
||||||
df.date = df.date.astype(str).replace('\.','',regex=True)
|
df.date = df.date.astype(str).replace(r'\.','',regex=True)
|
||||||
df.date = df.date.str.rsplit(' ',1).str[0]\
|
df.date = df.date.str.rsplit(' ',1).str[0]\
|
||||||
.replace(['avr','mai','juin','juil'],['apr','may','jun','jul'],regex=True)
|
.replace(['avr','mai','juin','juil'],['apr','may','jun','jul'],regex=True)
|
||||||
df.date = pd.to_datetime(df.date,format='%d %b %Y')
|
df.date = pd.to_datetime(df.date,format='%d %b %Y')
|
||||||
@ -541,7 +541,7 @@ def normalize_formatIdsite(id_site):
|
|||||||
import re
|
import re
|
||||||
df = pd.DataFrame({'id_site':id_site})
|
df = pd.DataFrame({'id_site':id_site})
|
||||||
df['dept'] = '38'
|
df['dept'] = '38'
|
||||||
df[['org','num','other']] = [re.split('(\d+)',s) for s in [*df.id_site]]
|
df[['org','num','other']] = [re.split(r'(\d+)',s) for s in [*df.id_site]]
|
||||||
del df['other']
|
del df['other']
|
||||||
df.loc[df.num.str.len() == 1,'num'] = '000'+ df.loc[df.num.str.len() == 1,'num']
|
df.loc[df.num.str.len() == 1,'num'] = '000'+ df.loc[df.num.str.len() == 1,'num']
|
||||||
df.loc[df.num.str.len() == 2,'num'] = '00' + df.loc[df.num.str.len() == 2,'num']
|
df.loc[df.num.str.len() == 2,'num'] = '00' + df.loc[df.num.str.len() == 2,'num']
|
||||||
@ -601,7 +601,7 @@ def normalize_pratiques(prat):
|
|||||||
p = normalize_paturage(p)
|
p = normalize_paturage(p)
|
||||||
p = p.replace(dic)
|
p = p.replace(dic)
|
||||||
p.fillna('N.D', inplace=True)
|
p.fillna('N.D', inplace=True)
|
||||||
p[p.str.contains('\?| ou ',na=False)] = 'incertain'
|
p[p.str.contains(r'\?| ou ',na=False)] = 'incertain'
|
||||||
pp = p.str.split(',',expand=True)
|
pp = p.str.split(',',expand=True)
|
||||||
pp.columns = 'p' + pp.columns.astype(str)
|
pp.columns = 'p' + pp.columns.astype(str)
|
||||||
for c in pp.columns:
|
for c in pp.columns:
|
||||||
@ -903,7 +903,7 @@ def insert_attrsPS(df,ps_statut='actif'):
|
|||||||
del df['pratiques']
|
del df['pratiques']
|
||||||
|
|
||||||
for col in params:
|
for col in params:
|
||||||
df[col] = df[col].astype(str).replace(['\.0'],[''],regex=True)
|
df[col] = df[col].astype(str).replace([r'\.0'],[''],regex=True)
|
||||||
|
|
||||||
if 'niv_embrous' in df.columns:
|
if 'niv_embrous' in df.columns:
|
||||||
df.niv_embrous.replace(['nan'],['N.D'],inplace=True)
|
df.niv_embrous.replace(['nan'],['N.D'],inplace=True)
|
||||||
@ -971,9 +971,9 @@ def insert_attrsPS(df,ps_statut='actif'):
|
|||||||
|
|
||||||
def split_codehab(lst_codehab):
|
def split_codehab(lst_codehab):
|
||||||
lst = lst_codehab.copy()
|
lst = lst_codehab.copy()
|
||||||
lst = lst.str.split('x|X|,|&|/|\(|et',expand=True) \
|
lst = lst.str.split(r'x|X|,|&|/|\(|et',expand=True) \
|
||||||
.stack().str.strip() \
|
.stack().str.strip() \
|
||||||
.str.replace('\)','',regex=True)\
|
.str.replace(r'\)','',regex=True)\
|
||||||
.droplevel(-1).reset_index(drop=False)
|
.droplevel(-1).reset_index(drop=False)
|
||||||
return lst
|
return lst
|
||||||
|
|
||||||
@ -994,7 +994,7 @@ def format_codehab2insert(lst_codehab):
|
|||||||
suspect = lst.code_hab.str.split('(',expand=True)
|
suspect = lst.code_hab.str.split('(',expand=True)
|
||||||
if suspect.shape[1] > 1:
|
if suspect.shape[1] > 1:
|
||||||
suspect[0] = suspect[0].str.strip()
|
suspect[0] = suspect[0].str.strip()
|
||||||
suspect[1] = suspect[1].str.replace('\)','',regex=True)
|
suspect[1] = suspect[1].str.replace(r'\)','',regex=True)
|
||||||
suspect.columns = ['code_hab','suspect']
|
suspect.columns = ['code_hab','suspect']
|
||||||
del lst['code_hab']
|
del lst['code_hab']
|
||||||
# raise Exception('habitat suspecté en développement')
|
# raise Exception('habitat suspecté en développement')
|
||||||
@ -1011,7 +1011,7 @@ def insert_cbPS(df):
|
|||||||
drop_col = ['auteur','structure']
|
drop_col = ['auteur','structure']
|
||||||
df = df.copy()
|
df = df.copy()
|
||||||
df.date = df.date.astype(str)
|
df.date = df.date.astype(str)
|
||||||
df.code_hab = df.code_hab.replace(['\*'],[''],regex=True).str.strip()
|
df.code_hab = df.code_hab.replace([r'\*'],[''],regex=True).str.strip()
|
||||||
df.code_hab = df.code_hab.replace(['None','nan','0'],[None,None,None])
|
df.code_hab = df.code_hab.replace(['None','nan','0'],[None,None,None])
|
||||||
df.dropna(subset=['code_hab'],inplace=True)
|
df.dropna(subset=['code_hab'],inplace=True)
|
||||||
ids = select_ID(df[df.columns.drop(['code_hab',*drop_col])],sch=pycen.ps.schema,tab='r_site_habitat')
|
ids = select_ID(df[df.columns.drop(['code_hab',*drop_col])],sch=pycen.ps.schema,tab='r_site_habitat')
|
||||||
@ -1061,7 +1061,7 @@ def insert_habPS(df,ps_statut='actif'):
|
|||||||
# df[t] = df[t].astype(str)
|
# df[t] = df[t].astype(str)
|
||||||
df = df.stack().reset_index(-1)
|
df = df.stack().reset_index(-1)
|
||||||
df.columns = ['param','value']
|
df.columns = ['param','value']
|
||||||
df[['param','index','other']] = [re.split('(\d+)',s) for s in [*df.param]]
|
df[['param','index','other']] = [re.split(r'(\d+)',s) for s in [*df.param]]
|
||||||
del df['other']
|
del df['other']
|
||||||
df['index'] = df['index'].astype(int)
|
df['index'] = df['index'].astype(int)
|
||||||
df['value'] = df['value'].replace(['-'],[None])
|
df['value'] = df['value'].replace(['-'],[None])
|
||||||
@ -1137,7 +1137,7 @@ def filter_saisierror(df):
|
|||||||
tmp.set_index([*cc], inplace=True)
|
tmp.set_index([*cc], inplace=True)
|
||||||
tmp = tmp.stack().reset_index(-1)
|
tmp = tmp.stack().reset_index(-1)
|
||||||
tmp.columns = ['param','value']
|
tmp.columns = ['param','value']
|
||||||
tmp[['param','index','other']] = [re.split('(\d+)',s) for s in [*tmp.param]]
|
tmp[['param','index','other']] = [re.split(r'(\d+)',s) for s in [*tmp.param]]
|
||||||
del tmp['other']
|
del tmp['other']
|
||||||
tmp['index'] = tmp['index'].astype(int)
|
tmp['index'] = tmp['index'].astype(int)
|
||||||
tmp['value'] = tmp['value'].replace(['-'],[None])
|
tmp['value'] = tmp['value'].replace(['-'],[None])
|
||||||
@ -1180,7 +1180,7 @@ def filter_saisierror(df):
|
|||||||
|
|
||||||
|
|
||||||
cb = pycen.ref_hab().get_CB()
|
cb = pycen.ref_hab().get_CB()
|
||||||
tmp.code_hab = tmp.code_hab.replace(['\*'],[''],regex=True).str.strip()
|
tmp.code_hab = tmp.code_hab.replace([r'\*'],[''],regex=True).str.strip()
|
||||||
tmp.code_hab = tmp.code_hab.replace(['0'],[None])
|
tmp.code_hab = tmp.code_hab.replace(['0'],[None])
|
||||||
tmp.dropna(subset=['code_hab'], inplace=True)
|
tmp.dropna(subset=['code_hab'], inplace=True)
|
||||||
tmpp = tmp[['id_site','index','code_hab']].copy()
|
tmpp = tmp[['id_site','index','code_hab']].copy()
|
||||||
@ -1303,8 +1303,9 @@ if __name__ == "__main__":
|
|||||||
]
|
]
|
||||||
# from_table = '"cr_ECRIN_habitats_CBNA_2014"'
|
# from_table = '"cr_ECRIN_habitats_CBNA_2014"'
|
||||||
from_table = None
|
from_table = None
|
||||||
from_file = 'PS38_modifs_AG_2024.gpkg'
|
from_file = 'PS prioritaire.geojson'
|
||||||
path0 = '/home/colas/Documents/9_PROJETS/2_PS/TO IMPORT/2024/'
|
from_tab = 'Tableau_saisie_PS_2026.xlsx'
|
||||||
|
path0 = '/home/colas/Documents/9_PROJETS/2_PS/TO IMPORT/2026/'
|
||||||
|
|
||||||
# org = from_file.split('/')[1]
|
# org = from_file.split('/')[1]
|
||||||
tutu = pd.DataFrame()
|
tutu = pd.DataFrame()
|
||||||
|
|||||||
@ -1,9 +1,6 @@
|
|||||||
from pycen import con_gn
|
from pycen import con_gn
|
||||||
|
|
||||||
sql = '''
|
sql0 = '''
|
||||||
DROP VIEW IF EXISTS gn_exports.v_synthese_zones_humides;
|
|
||||||
|
|
||||||
CREATE VIEW gn_exports.v_synthese_zones_humides AS
|
|
||||||
with habitats as (
|
with habitats as (
|
||||||
SELECT
|
SELECT
|
||||||
zh_uuid,
|
zh_uuid,
|
||||||
@ -25,8 +22,7 @@ with habitats as (
|
|||||||
FROM pr_zh.t_zh
|
FROM pr_zh.t_zh
|
||||||
LEFT JOIN pr_zh.cor_zh_lim_fs USING (id_zh)
|
LEFT JOIN pr_zh.cor_zh_lim_fs USING (id_zh)
|
||||||
group by 1 order by 1
|
group by 1 order by 1
|
||||||
), activ_hum AS (
|
), v_activity AS (
|
||||||
WITH v_activity AS (
|
|
||||||
SELECT
|
SELECT
|
||||||
id_zh,
|
id_zh,
|
||||||
json_agg(ref_nomenclatures.get_nomenclature_label(cor_impact_types.id_impact)) impacts
|
json_agg(ref_nomenclatures.get_nomenclature_label(cor_impact_types.id_impact)) impacts
|
||||||
@ -34,38 +30,60 @@ with habitats as (
|
|||||||
LEFT JOIN pr_zh.cor_impact_list USING (id_impact_list)
|
LEFT JOIN pr_zh.cor_impact_list USING (id_impact_list)
|
||||||
LEFT JOIN pr_zh.cor_impact_types USING (id_cor_impact_types)
|
LEFT JOIN pr_zh.cor_impact_types USING (id_cor_impact_types)
|
||||||
group by 1 order by 1
|
group by 1 order by 1
|
||||||
)
|
), activ_hum AS (
|
||||||
SELECT
|
SELECT
|
||||||
zh_uuid,
|
zh_uuid,
|
||||||
json_agg(json_build_object(
|
json_agg(CASE WHEN t_activity.id_activity IS NULL
|
||||||
'activite', ref_nomenclatures.get_nomenclature_label(t_activity.id_activity),
|
THEN NULL
|
||||||
'position', ref_nomenclatures.get_nomenclature_label(t_activity.id_position),
|
ELSE json_build_object(
|
||||||
'impact', v_activity.impacts,
|
'activite', ref_nomenclatures.get_nomenclature_label(t_activity.id_activity),
|
||||||
'rmq_activ', t_activity.remark_activity
|
'position', ref_nomenclatures.get_nomenclature_label(t_activity.id_position),
|
||||||
)) AS activ_hum
|
'impact', v_activity.impacts,
|
||||||
|
'rmq_activ', t_activity.remark_activity
|
||||||
|
)
|
||||||
|
END) AS activ_hum
|
||||||
FROM pr_zh.t_zh
|
FROM pr_zh.t_zh
|
||||||
LEFT JOIN pr_zh.t_activity USING (id_zh)
|
LEFT JOIN pr_zh.t_activity USING (id_zh)
|
||||||
LEFT JOIN v_activity USING (id_zh)
|
LEFT JOIN v_activity USING (id_zh)
|
||||||
group by zh_uuid order by 1
|
group by zh_uuid order by 1
|
||||||
|
--), activ_hum_test AS (
|
||||||
|
-- SELECT
|
||||||
|
-- zh_uuid,
|
||||||
|
-- CASE WHEN t_activity.id_activity IS NOT NULL
|
||||||
|
-- THEN jsonb_build_object(
|
||||||
|
-- ref_nomenclatures.get_nomenclature_label(t_activity.id_activity), v_activity.impacts
|
||||||
|
-- )
|
||||||
|
-- ELSE NULL
|
||||||
|
-- END AS activ_hum_test
|
||||||
|
-- FROM pr_zh.t_zh
|
||||||
|
-- LEFT JOIN pr_zh.t_activity USING (id_zh)
|
||||||
|
-- LEFT JOIN v_activity USING (id_zh)
|
||||||
|
-- --group by zh_uuid order by 1
|
||||||
), inflow as (
|
), inflow as (
|
||||||
SELECT
|
SELECT
|
||||||
zh_uuid,
|
zh_uuid,
|
||||||
json_agg(json_build_object(
|
json_agg(CASE WHEN t_inflow.id_inflow IS NULL
|
||||||
|
THEN NULL
|
||||||
|
ELSE json_build_object(
|
||||||
'entree_eau', ref_nomenclatures.get_nomenclature_label(t_inflow.id_inflow),
|
'entree_eau', ref_nomenclatures.get_nomenclature_label(t_inflow.id_inflow),
|
||||||
'perm', ref_nomenclatures.get_nomenclature_label(t_inflow.id_permanance),
|
'perm', ref_nomenclatures.get_nomenclature_label(t_inflow.id_permanance),
|
||||||
'topo', t_inflow.topo
|
'topo', t_inflow.topo
|
||||||
)) AS entree_eau
|
)
|
||||||
|
END) AS entree_eau
|
||||||
FROM pr_zh.t_zh
|
FROM pr_zh.t_zh
|
||||||
LEFT JOIN pr_zh.t_inflow USING (id_zh)
|
LEFT JOIN pr_zh.t_inflow USING (id_zh)
|
||||||
group by zh_uuid
|
group by zh_uuid
|
||||||
), outflow as (
|
), outflow as (
|
||||||
SELECT
|
SELECT
|
||||||
zh_uuid,
|
zh_uuid,
|
||||||
json_agg(json_build_object(
|
json_agg(CASE WHEN t_outflow.id_outflow IS NULL
|
||||||
'sortie_eau', ref_nomenclatures.get_nomenclature_label(t_outflow.id_outflow),
|
THEN NULL
|
||||||
'perm', ref_nomenclatures.get_nomenclature_label(t_outflow.id_permanance),
|
ELSE json_build_object(
|
||||||
'topo', t_outflow.topo
|
'sortie_eau', ref_nomenclatures.get_nomenclature_label(t_outflow.id_outflow),
|
||||||
)) AS sortie_eau
|
'perm', ref_nomenclatures.get_nomenclature_label(t_outflow.id_permanance),
|
||||||
|
'topo', t_outflow.topo
|
||||||
|
)
|
||||||
|
END) AS sortie_eau
|
||||||
FROM pr_zh.t_zh
|
FROM pr_zh.t_zh
|
||||||
LEFT JOIN pr_zh.t_outflow USING (id_zh)
|
LEFT JOIN pr_zh.t_outflow USING (id_zh)
|
||||||
group by zh_uuid
|
group by zh_uuid
|
||||||
@ -247,11 +265,13 @@ SELECT
|
|||||||
zh_uuid,
|
zh_uuid,
|
||||||
code,
|
code,
|
||||||
main_name,
|
main_name,
|
||||||
CONCAT(crea_role.nom_role, ' ', crea_role.prenom_role) crea_author,
|
--concat(crea_role.nom_role, ' ', crea_role.prenom_role) AS crea_author,
|
||||||
CONCAT(updt_role.nom_role, ' ', updt_role.prenom_role) updt_author,
|
--bib_org.name AS organisme,
|
||||||
bib_org.name organisme,
|
--concat(updt_role.nom_role, ' ', updt_role.prenom_role) AS updt_author,
|
||||||
|
bib_org_user_updt.nom_organisme as org_updt,
|
||||||
create_date crea_date,
|
create_date crea_date,
|
||||||
update_date updt_date,
|
update_date updt_date,
|
||||||
|
--ST_TRANSFORM(geom,2154)::geometry(geometry,2154) geom,
|
||||||
geom,
|
geom,
|
||||||
area surface,
|
area surface,
|
||||||
rb.river_bassin,
|
rb.river_bassin,
|
||||||
@ -259,27 +279,43 @@ SELECT
|
|||||||
zh_area_com.commune,
|
zh_area_com.commune,
|
||||||
ref_nomenclatures.get_nomenclature_label(id_sdage) sdage,
|
ref_nomenclatures.get_nomenclature_label(id_sdage) sdage,
|
||||||
ref_nomenclatures.get_nomenclature_label(id_sage) sage,
|
ref_nomenclatures.get_nomenclature_label(id_sage) sage,
|
||||||
habitats.habitats,
|
CASE WHEN habitats.habitats::text = '[null]'
|
||||||
|
THEN NULL
|
||||||
|
ELSE habitats.habitats
|
||||||
|
END AS habitats,
|
||||||
remark_pres descript,
|
remark_pres descript,
|
||||||
crit_delim.crit_delim,
|
crit_delim.crit_delim,
|
||||||
remark_lim rmq_delilm,
|
remark_lim rmq_delilm,
|
||||||
esp_fs.crit_esp_fs,
|
CASE WHEN esp_fs.crit_esp_fs::text = '[null]'
|
||||||
|
THEN NULL
|
||||||
|
ELSE esp_fs.crit_esp_fs
|
||||||
|
END AS crit_esp_fs,
|
||||||
remark_lim_fs rmq_esp_fs,
|
remark_lim_fs rmq_esp_fs,
|
||||||
ef_area,
|
--ef_area,
|
||||||
v_habref v_hab,
|
--v_habref v_hab,
|
||||||
activ_hum.activ_hum,
|
CASE WHEN activ_hum.activ_hum::text = '[null]'
|
||||||
|
THEN NULL
|
||||||
|
ELSE activ_hum.activ_hum::text
|
||||||
|
END AS activ_hum,
|
||||||
|
-- activ_hum_test.activ_hum_test,
|
||||||
ref_nomenclatures.get_nomenclature_label(id_thread) bilan_menace,
|
ref_nomenclatures.get_nomenclature_label(id_thread) bilan_menace,
|
||||||
inflow.entree_eau,
|
CASE WHEN inflow.entree_eau::text = '[null]'
|
||||||
outflow.sortie_eau,
|
THEN NULL
|
||||||
|
ELSE inflow.entree_eau::text
|
||||||
|
END AS entree_eau,
|
||||||
|
CASE WHEN outflow.sortie_eau::text = '[null]'
|
||||||
|
THEN NULL
|
||||||
|
ELSE outflow.sortie_eau::text
|
||||||
|
END AS sortie_eau,
|
||||||
ref_nomenclatures.get_nomenclature_label(id_frequency) freq_sub,
|
ref_nomenclatures.get_nomenclature_label(id_frequency) freq_sub,
|
||||||
ref_nomenclatures.get_nomenclature_label(id_spread) extend_sub,
|
ref_nomenclatures.get_nomenclature_label(id_spread) extend_sub,
|
||||||
ref_nomenclatures.get_nomenclature_label(id_connexion) connex,
|
ref_nomenclatures.get_nomenclature_label(id_connexion) connex,
|
||||||
ref_nomenclatures.get_nomenclature_label(id_diag_hydro) diag_hydro,
|
ref_nomenclatures.get_nomenclature_label(id_diag_hydro) diag_hydro,
|
||||||
ref_nomenclatures.get_nomenclature_label(id_diag_bio) diag_bio,
|
ref_nomenclatures.get_nomenclature_label(id_diag_bio) diag_bio,
|
||||||
functions.hydro,
|
functions.hydro::text,
|
||||||
functions.bio,
|
functions.bio::text,
|
||||||
functions.int_patrim,
|
functions.int_patrim::text,
|
||||||
functions.val_socio,
|
functions.val_socio::text,
|
||||||
remark_eval_functions eval_fcts,
|
remark_eval_functions eval_fcts,
|
||||||
remark_eval_heritage eval_heritage,
|
remark_eval_heritage eval_heritage,
|
||||||
remark_eval_thread eval_menace,
|
remark_eval_thread eval_menace,
|
||||||
@ -293,19 +329,21 @@ SELECT
|
|||||||
especes.nb_flora nb_flora_sp,
|
especes.nb_flora nb_flora_sp,
|
||||||
especes.nb_vertebrates nb_vertebrate_sp,
|
especes.nb_vertebrates nb_vertebrate_sp,
|
||||||
especes.nb_invertebrates nb_invertebrate_sp,
|
especes.nb_invertebrates nb_invertebrate_sp,
|
||||||
inventaire.inventaire,
|
inventaire.inventaire::text,
|
||||||
actions.prio_actions,
|
actions.prio_actions::text,
|
||||||
instrument.instrument,
|
instrument.instrument::text,
|
||||||
urba.docs_urba
|
urba.docs_urba::text
|
||||||
--remark_is_other_inventory
|
--remark_is_other_inventory
|
||||||
FROM pr_zh.t_zh
|
FROM pr_zh.t_zh
|
||||||
JOIN utilisateurs.t_roles crea_role ON t_zh.create_author = crea_role.id_role
|
JOIN utilisateurs.t_roles crea_role ON t_zh.create_author = crea_role.id_role
|
||||||
LEFT JOIN utilisateurs.t_roles updt_role ON t_zh.update_author = updt_role.id_role
|
LEFT JOIN utilisateurs.t_roles updt_role ON t_zh.update_author = updt_role.id_role
|
||||||
|
LEFT JOIN utilisateurs.bib_organismes bib_org_user_updt on updt_role.id_organisme = bib_org_user_updt.id_organisme
|
||||||
LEFT JOIN pr_zh.bib_organismes bib_org USING (id_org)
|
LEFT JOIN pr_zh.bib_organismes bib_org USING (id_org)
|
||||||
LEFT JOIN habitats USING (zh_uuid)
|
LEFT JOIN habitats USING (zh_uuid)
|
||||||
LEFT JOIN crit_delim USING (zh_uuid)
|
LEFT JOIN crit_delim USING (zh_uuid)
|
||||||
LEFT JOIN esp_fs USING (zh_uuid)
|
LEFT JOIN esp_fs USING (zh_uuid)
|
||||||
LEFT JOIN activ_hum USING (zh_uuid)
|
LEFT JOIN activ_hum USING (zh_uuid)
|
||||||
|
--LEFT JOIN activ_hum_test USING (zh_uuid)
|
||||||
LEFT JOIN inflow USING (zh_uuid)
|
LEFT JOIN inflow USING (zh_uuid)
|
||||||
LEFT JOIN outflow USING (zh_uuid)
|
LEFT JOIN outflow USING (zh_uuid)
|
||||||
LEFT JOIN functions USING (zh_uuid)
|
LEFT JOIN functions USING (zh_uuid)
|
||||||
@ -317,11 +355,37 @@ LEFT JOIN actions USING (id_zh)
|
|||||||
LEFT JOIN zh_area_com USING (id_zh)
|
LEFT JOIN zh_area_com USING (id_zh)
|
||||||
LEFT JOIN dep USING (id_zh)
|
LEFT JOIN dep USING (id_zh)
|
||||||
LEFT JOIN inventaire USING (id_zh)
|
LEFT JOIN inventaire USING (id_zh)
|
||||||
|
'''
|
||||||
|
|
||||||
|
sql_zh_all = '''
|
||||||
|
DROP VIEW IF EXISTS gn_exports.v_synthese_zones_humides;
|
||||||
|
|
||||||
|
CREATE VIEW gn_exports.v_synthese_zones_humides AS
|
||||||
|
''' + sql0 + '''
|
||||||
--group BY zh_uuid
|
--group BY zh_uuid
|
||||||
WHERE code not like '38CENIS%%'
|
WHERE code not like '38CENIS%%'
|
||||||
|
AND ref_nomenclatures.get_cd_nomenclature(id_sdage)::int > 0
|
||||||
|
--id_sdage > 0
|
||||||
order by code
|
order by code
|
||||||
;
|
;
|
||||||
'''
|
'''
|
||||||
|
|
||||||
with con_gn.begin() as cnx:
|
with con_gn.begin() as cnx:
|
||||||
cnx.execute(sql)
|
cnx.execute(sql_zh_all)
|
||||||
|
|
||||||
|
|
||||||
|
sql_zh_38 = '''
|
||||||
|
DROP VIEW IF EXISTS gn_exports.v_synthese_zones_humides_isere;
|
||||||
|
|
||||||
|
CREATE VIEW gn_exports.v_synthese_zones_humides_isere AS
|
||||||
|
''' + sql0 + '''
|
||||||
|
--group BY zh_uuid
|
||||||
|
WHERE code not like '38CENIS%%'
|
||||||
|
AND dep.departement::jsonb @> '"Isère"'
|
||||||
|
AND ref_nomenclatures.get_cd_nomenclature(id_sdage)::int > 0
|
||||||
|
order by code
|
||||||
|
;
|
||||||
|
'''
|
||||||
|
|
||||||
|
with con_gn.begin() as cnx:
|
||||||
|
cnx.execute(sql_zh_38)
|
||||||
@ -24,6 +24,7 @@ def get_status(lst,con):
|
|||||||
t.nom_valide,
|
t.nom_valide,
|
||||||
t.lb_nom,
|
t.lb_nom,
|
||||||
--s.*
|
--s.*
|
||||||
|
s.cd_sig,
|
||||||
s.rq_statut,
|
s.rq_statut,
|
||||||
s.code_statut,
|
s.code_statut,
|
||||||
s.cd_type_statut,
|
s.cd_type_statut,
|
||||||
@ -202,6 +203,54 @@ def get_taxon_status(lst,api):
|
|||||||
}
|
}
|
||||||
return pd.DataFrame({**phylo,**cd_status})
|
return pd.DataFrame({**phylo,**cd_status})
|
||||||
|
|
||||||
|
|
||||||
|
def filter_bio_geo(df,zone_bio):
|
||||||
|
idNotBioGeo = []
|
||||||
|
# Filtre du dommaine biogeographique sur la plaine Rhodanienne et Alpine
|
||||||
|
test_rhod = df.rq_statut.str.contains('rhodanienne : Non déterminante',na=False)
|
||||||
|
test_alpi = df.rq_statut.str.contains('Alpine : Non déterminante',na=False)
|
||||||
|
|
||||||
|
if zone_bio == 'rhod':
|
||||||
|
idNotBioGeo = df[test_rhod].index
|
||||||
|
elif zone_bio == 'alpi':
|
||||||
|
idNotBioGeo = df[test_alpi].index
|
||||||
|
elif zone_bio == 'all':
|
||||||
|
idNotBioGeo = df[test_rhod&test_alpi].index
|
||||||
|
|
||||||
|
if not idNotBioGeo.empty:
|
||||||
|
df.drop(idNotBioGeo, inplace=True)
|
||||||
|
|
||||||
|
return df
|
||||||
|
|
||||||
|
|
||||||
|
def form_territoire(df,terr):
|
||||||
|
is_dep = df.cd_sig.str.contains('INSEED')
|
||||||
|
dep = (
|
||||||
|
['38','42','07','26'] if terr == 'platiere' else
|
||||||
|
['38','69','01'] if terr == 'negria' else ['38']
|
||||||
|
)
|
||||||
|
keep_not = df[is_dep&(~df.cd_sig.str[-2:].isin(dep))].index
|
||||||
|
df.drop(keep_not,inplace=True)
|
||||||
|
|
||||||
|
is_dep = df.cd_sig.str.contains('INSEED')
|
||||||
|
is_38 = df.cd_sig=='INSEED38'
|
||||||
|
|
||||||
|
if terr == 'isere':
|
||||||
|
filter_id = df[is_dep&(~is_38)].index
|
||||||
|
df.drop(filter_id,inplace=True)
|
||||||
|
else:
|
||||||
|
lst_stat = df[is_dep&(~is_38)].cd_type_statut.unique()
|
||||||
|
# is_lst = df.cd_type_statut.isin(lst_stat)
|
||||||
|
df.loc[is_dep&(~is_38),'cd_type_statut'] = ( df[is_dep&(~is_38)]
|
||||||
|
.cd_type_statut + '_' + df[is_dep&(~is_38)].cd_sig.str.strip('INSEED')
|
||||||
|
)
|
||||||
|
df.loc[is_38,'cd_type_statut'] = ( df[is_38]
|
||||||
|
.cd_type_statut + '_38'
|
||||||
|
)
|
||||||
|
|
||||||
|
return df
|
||||||
|
|
||||||
|
|
||||||
dict_dep = {
|
dict_dep = {
|
||||||
'38':'Isère',
|
'38':'Isère',
|
||||||
'42':'Loire',
|
'42':'Loire',
|
||||||
@ -210,6 +259,11 @@ dict_dep = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
cols_rename = {
|
||||||
|
'nom_vernaculaire': 'nom_vern'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Définition de la connection à la bdd GéoNature
|
# Définition de la connection à la bdd GéoNature
|
||||||
from pycen import con_gn
|
from pycen import con_gn
|
||||||
@ -219,19 +273,33 @@ if __name__ == "__main__":
|
|||||||
# Paramètres de chargement du fichier des taxons
|
# Paramètres de chargement du fichier des taxons
|
||||||
PATH0 = '/home/cgeier/Téléchargements'
|
PATH0 = '/home/cgeier/Téléchargements'
|
||||||
PATH = ''
|
PATH = ''
|
||||||
file = 'CHASS_synthese_observations_2026-01-28.xlsx'
|
file = 'synthese_observations_2026-04-23.xlsx'
|
||||||
sheet = 'liste sp'
|
sheet = 'observations'
|
||||||
|
zone_bio_znieff = 'rhod' # ['rhod', 'alpi', 'all']
|
||||||
|
territoire = 'negria' # ['isere', 'platiere','negria']
|
||||||
|
# [GEOMETRY PARAMS]
|
||||||
|
keep_geomtype = None # ['polygon', 'point', 'ligne', None]
|
||||||
|
geom_col = 'geometrie_wkt_4326'
|
||||||
|
|
||||||
# Liste des CD_NOM en entrée
|
# Liste des CD_NOM en entrée
|
||||||
cd_col = 'cd_ref' # Nom de la colonne à utiliser dans le feuillet ``sheet``
|
cd_col = 'cd_nom' # Nom de la colonne à utiliser dans le feuillet ``sheet``
|
||||||
|
|
||||||
|
|
||||||
# Lecture des données
|
# Lecture des données
|
||||||
taxlist = pd.read_excel(os.path.join(PATH0,PATH,file),sheet,usecols=[cd_col],header=0)
|
taxlist = pd.read_excel(os.path.join(PATH0,PATH,file),sheet,usecols=[cd_col],header=0)
|
||||||
tab_sp = pd.read_excel(os.path.join(PATH0,PATH,file),sheet,index_col=cd_col)
|
tab_sp = pd.read_excel(os.path.join(PATH0,PATH,file),sheet,index_col=cd_col)
|
||||||
|
tab_sp.rename(columns=cols_rename,inplace=True)
|
||||||
|
|
||||||
|
# Exclusion d'un type de géométrie
|
||||||
|
if keep_geomtype is not None:
|
||||||
|
as_geom = tab_sp[geom_col].str.contains(keep_geomtype.upper(),na=False)
|
||||||
|
tab_sp = tab_sp[as_geom]
|
||||||
|
taxlist = taxlist[taxlist.cd_nom.isin(tab_sp.index.tolist())]
|
||||||
|
|
||||||
lst = taxlist[cd_col]
|
lst = taxlist[cd_col]
|
||||||
|
|
||||||
# Récupération des statuts
|
# Récupération des statuts
|
||||||
df = get_status(taxlist[cd_col].astype(str),con_gn)
|
df = get_status(taxlist[cd_col].astype(str).unique(),con_gn)
|
||||||
typ = get_type_status(con_gn)
|
typ = get_type_status(con_gn)
|
||||||
typ = typ[typ.cd_type_statut.isin(df.cd_type_statut.unique())]
|
typ = typ[typ.cd_type_statut.isin(df.cd_type_statut.unique())]
|
||||||
|
|
||||||
@ -241,32 +309,70 @@ if __name__ == "__main__":
|
|||||||
df.loc[is_lrr & (df.niveau_admin == 'Ancienne région'),'cd_type_statut'] = 'LRR_RA'
|
df.loc[is_lrr & (df.niveau_admin == 'Ancienne région'),'cd_type_statut'] = 'LRR_RA'
|
||||||
del df['niveau_admin']
|
del df['niveau_admin']
|
||||||
|
|
||||||
for c in ['cd_ref','cd_nom','lb_nom']:
|
# Filtre du dommaine biogeographique sur la plaine Rhodanienne et Alpine
|
||||||
if c in tab_sp.columns:
|
df = filter_bio_geo(df,zone_bio_znieff)
|
||||||
# if 'cd_nom' not in df.columns and c == 'cd_ref': continue
|
|
||||||
tab_sp.drop(c,axis=1,inplace=True)
|
|
||||||
|
|
||||||
pivot = pd.pivot_table(
|
# Filtre des statuts vis à vis du territoire d'intérêts
|
||||||
|
# Conservation des statuts Adrèche, Drôme et Loire pour la Platière
|
||||||
|
df = form_territoire(df,territoire)
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
|
||||||
|
keep_cols = same_col = df.columns[df.columns.isin(tab_sp.reset_index(drop=False).columns)]
|
||||||
|
as_vern = keep_cols.str.contains('vern').any()
|
||||||
|
as_numb = keep_cols.str.contains('nombre').any()
|
||||||
|
if not as_vern:
|
||||||
|
keep_cols = [*keep_cols,'nom_vern']
|
||||||
|
if not as_numb:
|
||||||
|
keep_cols = [*keep_cols,'nombre_min','nombre_max']
|
||||||
|
else:
|
||||||
|
same_col = [x for x in keep_cols if x not in ['nombre_min','nombre_max']]
|
||||||
|
|
||||||
|
piv = pd.pivot_table(
|
||||||
df,
|
df,
|
||||||
values='code_statut',
|
values='code_statut',
|
||||||
index=['cd_nom', 'cd_ref','lb_nom'#,'niveau_admin','lb_adm_tr'
|
index=['cd_nom', 'cd_ref','lb_nom','nom_vern'#,'niveau_admin','lb_adm_tr'
|
||||||
],
|
],
|
||||||
columns=['cd_type_statut'],
|
columns=['cd_type_statut'],
|
||||||
aggfunc=list,fill_value=None)
|
aggfunc=list,fill_value=None)
|
||||||
|
|
||||||
for c in pivot.columns:
|
for c in piv.columns:
|
||||||
pivot[c] = [x[0] if x is not np.NaN and len(x)==1 else x for x in pivot[c]]
|
piv[c] = [x[0] if x is not np.NaN and len(x)==1 else x for x in piv[c]]
|
||||||
if 'DH' in pivot.columns:
|
if 'DH' in piv.columns:
|
||||||
pivot['DH'] = [','.join(x) if (x is not np.NaN) and (len(x)==2) else x for x in pivot['DH']]
|
piv['DH'] = [','.join(x) if (x is not np.NaN) and (len(x)==2) else x for x in piv['DH']]
|
||||||
pivot.DH.replace({'CDH':''},regex=True,inplace=True)
|
piv.DH.replace({'CDH':''},regex=True,inplace=True)
|
||||||
|
# piv.reset_index(-1, inplace=True)
|
||||||
|
|
||||||
pivot = tab_sp.merge(pivot,on=[cd_col],how='left')
|
# tabsp_nb = tab_sp.reset_index(drop=False)[[*same_col,'nombre_min','nombre_max']].groupby([*same_col]).sum()
|
||||||
|
tabsp_nb = (
|
||||||
|
tab_sp.reset_index(drop=False)[same_col]
|
||||||
|
.drop_duplicates()
|
||||||
|
.merge(
|
||||||
|
tab_sp.reset_index(drop=False)[[cd_col,'nombre_min','nombre_max','date_debut','date_fin']]
|
||||||
|
.groupby([cd_col])
|
||||||
|
.agg({
|
||||||
|
'nombre_min':'sum',
|
||||||
|
'nombre_max':'sum',
|
||||||
|
'date_debut':'min',
|
||||||
|
'date_fin':'max'
|
||||||
|
}),
|
||||||
|
on = cd_col
|
||||||
|
)
|
||||||
|
)
|
||||||
|
tabsp_nb.set_index(cd_col,inplace=True)
|
||||||
|
tabsp_nb.sort_index(inplace=True)
|
||||||
|
|
||||||
|
# pivot = tab_sp.merge(piv,on=[cd_col],how='left')
|
||||||
|
pivot = tabsp_nb.merge(piv,on=[cd_col],how='left')
|
||||||
|
|
||||||
pivlib = pd.pivot_table(
|
pivlib = pd.pivot_table(
|
||||||
df,
|
df,
|
||||||
values='label_statut',
|
values='label_statut',
|
||||||
index=[
|
index=[
|
||||||
'cd_nom', 'cd_ref','lb_nom'#,'niveau_admin','lb_adm_tr'
|
'cd_nom', 'cd_ref','lb_nom','nom_vern'#,'niveau_admin','lb_adm_tr'
|
||||||
],
|
],
|
||||||
columns=['cd_type_statut'],
|
columns=['cd_type_statut'],
|
||||||
aggfunc=list,fill_value=None)
|
aggfunc=list,fill_value=None)
|
||||||
@ -276,10 +382,18 @@ if __name__ == "__main__":
|
|||||||
pivlib['DH'] = [','.join(x) if (x is not np.NaN) and (len(x)==2) else x for x in pivlib['DH']]
|
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.DH.replace({'CDH':''},regex=True,inplace=True)
|
||||||
|
|
||||||
pivlib = tab_sp.merge(pivlib,on=[cd_col],how='left')
|
# pivlib = tab_sp.merge(pivlib,on=[cd_col],how='left')
|
||||||
|
pivotlib = tabsp_nb.merge(pivlib,on=[cd_col],how='left')
|
||||||
|
|
||||||
|
tx_nb_missing = df[~df.cd_nom.isin(tabsp_nb.index)].shape[0]
|
||||||
|
if tx_nb_missing > 0:
|
||||||
|
print('WARNING : %i taxon(s) is MISING !! \n'%tx_nb_missing)
|
||||||
|
|
||||||
print('INIT writer')
|
print('INIT writer')
|
||||||
NAME_OUT = os.path.join('~/',sheet+'_status.xlsx')
|
NAME_OUT = os.path.join('~/',sheet+'_status.xlsx')
|
||||||
|
if keep_geomtype is not None:
|
||||||
|
NAME_OUT = NAME_OUT[:-5]+' (%s only).xlsx'%keep_geomtype.lower()
|
||||||
|
|
||||||
with pd.ExcelWriter(NAME_OUT) as writer:
|
with pd.ExcelWriter(NAME_OUT) as writer:
|
||||||
df.to_excel(
|
df.to_excel(
|
||||||
writer,sheet_name='v_bdc_status',index=False
|
writer,sheet_name='v_bdc_status',index=False
|
||||||
@ -291,7 +405,7 @@ if __name__ == "__main__":
|
|||||||
)
|
)
|
||||||
# writer.save()
|
# writer.save()
|
||||||
print('pivot_table OK !')
|
print('pivot_table OK !')
|
||||||
pivlib.to_excel(
|
pivotlib.to_excel(
|
||||||
writer,sheet_name='pivot_libel'
|
writer,sheet_name='pivot_libel'
|
||||||
)
|
)
|
||||||
# writer.save()
|
# writer.save()
|
||||||
@ -301,3 +415,5 @@ if __name__ == "__main__":
|
|||||||
)
|
)
|
||||||
# writer.save()
|
# writer.save()
|
||||||
print('dic_type_statut OK !')
|
print('dic_type_statut OK !')
|
||||||
|
print('END writing %s'%NAME_OUT)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user