Maj code zh avec la nomenclature CEN38
This commit is contained in:
parent
2a61e71914
commit
70b17f9495
37
5_GEONATURE/GN_ZH/tools/update_zh_name.py
Normal file
37
5_GEONATURE/GN_ZH/tools/update_zh_name.py
Normal file
@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
import geopandas as gpd
|
||||
from pycen import update_to_sql, con_gn, con as con_aza
|
||||
|
||||
def get_sdr_eau(con=con_aza):
|
||||
return gpd.read_postgis('SELECT * FROM ref_territoire.sdr_ressources_en_eau', con)
|
||||
|
||||
def get_zh_to_update(con=con_gn):
|
||||
return gpd.read_postgis("SELECT id_zh,code,ST_TRANSFORM(geom,2154) geom FROM pr_zh.t_zh WHERE code ilike '38CENIS%%'", con)
|
||||
|
||||
def change_code(zh,sdr,con=con_gn):
|
||||
res = zh.sjoin(sdr[['code_tr','geom']]).drop(columns='index_right')
|
||||
code_max = gpd.pd.read_sql(
|
||||
"""SELECT MAX(SUBSTRING(code,5,4)::int) val_max, SUBSTRING(code,3,2) code_tr
|
||||
FROM pr_zh.t_zh WHERE code ilike ANY(ARRAY['%%{}%%'])
|
||||
GROUP BY SUBSTRING(code,3,2)""".format(
|
||||
"%%','%%".join(res.code_tr.unique().tolist())
|
||||
),con_gn
|
||||
)
|
||||
|
||||
for i,r in code_max.iterrows():
|
||||
res.loc[res.code_tr==r.code_tr,'code_new'] = ['38'+row.code_tr+str(r.val_max+i+1).zfill(4) for i,row in res.iterrows()]
|
||||
|
||||
update_to_sql(
|
||||
res[['id_zh','code_new']].rename(columns={'code_new':'code'}),
|
||||
con_gn,
|
||||
't_zh',
|
||||
'pr_zh',
|
||||
'id_zh'
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
sdr = get_sdr_eau(con=con_aza)
|
||||
zh = get_zh_to_update(con=con_gn)
|
||||
change_code(zh,sdr,con=con_gn)
|
||||
Loading…
x
Reference in New Issue
Block a user