Python_scripts/5_GEONATURE/insert_lareas.py
2024-05-15 20:37:41 +02:00

196 lines
5.1 KiB
Python

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import geopandas as gpd
from geoalchemy2 import Geometry
import pycen
import pycen.wfs
# con = conn
con = pycen.con_gn
def update_larea(df, cols_updt=[]):
from pycen import update_to_sql
table = 'l_areas'
idtyp = tuple(df.id_type.unique())
if len(idtyp) > 1:
where = 'in %s'%tuple(df.id_type.unique())
else :
where = '=%s'%tuple(df.id_type.unique())
sql = 'SELECT id_area, area_name FROM ref_geo.l_areas WHERE id_type %s;'%where
larea = gpd.pd.read_sql_query(sql,con)
to_updt = df.merge(larea, on='area_name')
update_to_sql(
df=to_updt[['id_area',*cols_updt]],
con=con,
table_name=table,
schema_name='ref_geo',
key_name='id_area',
)
dic = {
'ID_ENS':'area_code',
'ENS_Id':'area_code',
'Id':'area_code',
'ID':'area_code',
'NOM':'area_name',
'Nom_ens':'area_name',
# 'URL':'source',
}
# pycen.wfs.list_layer('http://ws.carmencarto.fr/WFS/94/ENS_CG38')
df = pycen.wfs.get_wfs('http://ws.carmencarto.fr/WFS/94/ENS_CG38', 'zone_d_intervention_02')
df.rename(columns=dic,inplace=True)
df = df[[*set(dic.values()),'geometry']]
df.rename_geometry('geom', inplace=True)
df['id_type'] = 35
df['geojson_4326'] = df.to_crs(4326).geom.__geo_interface__['features']
df['geojson_4326'] = [x['geometry'] for x in df['geojson_4326']]
# df['geojson_4326'] = df['geojson_4326'].astype(str)
df['centroid'] = df.geom.centroid.to_wkt()
df['enable'] = True
df['source'] = 'http://ws.carmencarto.fr/WFS/94/ENS_CG38'
df.to_postgis(
name='l_areas',
con=con,
schema='ref_geo',
if_exists='append',
index=False,
index_label=None,
chunksize=None,
dtype={
'centroid': Geometry(geometry_type='POINT',srid=2154)
},
)
# path0 = '/home/colas/Documents/5_BDD/BASSIN_VERSANT/'
path0 = '/home/colas/Documents/9_PROJETS/6_GEONATURE/AREA/'
dic = {
# 'ID_MNHN':'area_code',
'NOM':'area_name',
'URL':'source',
}
# path = 'BD_Topage_FXX_2022-shp/BD_TOPAGE_2022-shp/BassinVersantTopographique_FXX-shp/'
# file = 'BassinVersantTopographique_FXX.shp'
path = 'IN DATA BASE/znieff1/'
file = 'znieff1.shp'
id_type = 3 # id_type in bdd geonature : ref_geo.bib_areas_types
# 3 : 'ZNIEFF1',
# 4 : 'APB',
# 5 : 'RNN',
# 6 : 'RNR',
# 7 : 'ZPS',
# 8 : 'SIC',
# 9 : 'ZICO',
# 10 : 'RNCFS',
# 12 : 'SCEN',
# 15 : 'PNR',
# 16 : 'RBIOL',
# 20 : 'AA',
# 32 : 'Zones biogéographiques',
# DIC for pr_zh
# dic = {
# # 'index' : 'id_hydro',
# 'index' : 'id_rb',
# 'TopoOH' : 'name',
# 'lib_ssbv' : 'name',
# }
# sql = '''
# SELECT a.* FROM ref_geo.l_areas a
# WHERE a.id_type = 25
# '''
sql = '''
SELECT a.* FROM ref_geo.l_areas a
JOIN ref_geo.bib_areas_types b USING (id_type)
WHERE b.type_code = 'DEP' AND a.area_code = '38'
'''
reg = gpd.read_postgis(sql,con)
df = gpd.read_file(path0+path+file)
if file == 'N_ENP_PN_S_000.shp':
if id_type == 20:
df = df[df.CODE_R_ENP == 'AAPN']
elif id_type == 1:
df = df[df.CODE_R_ENP == 'CPN']
else:
raise Exception('ERROR N_ENP_PN_S_000.shp')
if file == 'N_ENP_SCEN_S_000.shp':
df = df[df.ID_LOCAL.str.contains('CENIS')]
if file != 'N_ENP_SCEN_S_000.shp':
intersect = gpd.sjoin(df,reg,op='intersects').index
df = df[df.index.isin(intersect)]
# SPEC t_river_basin
# df.reset_index(drop=True,inplace=True)
# df.reset_index(drop=False,inplace=True)
# df.loc[df['index']==0,'index'] = 28
# df.loc[df['index']==1,'index'] = 29
# df['lib_ssbv'] = df['lib_ssbv']+' - '+df['code_ssbv']
df.rename_geometry('geom', inplace=True)
df.dropna(how='all',axis=1,inplace=True)
df = pycen.tools.Polygons_to_MultiPolygon(df)
df.rename(columns=dic, inplace=True)
# df['geom'] = df.geometry.to_wkt().copy()
del_col = df.columns[~df.columns.isin(['geom',*[*dic.values()]])]
del_col = df.columns[~df.columns.isin([*[*dic.values()]])]
df.drop(columns=del_col, inplace=True)
df['id_type'] = id_type
update_larea(df,cols_updt=['area_code'])
# SPEC t_river_basin / t_hydro_area
# df.to_crs(4326,inplace=True)
# df.geom = df.geom.to_wkt().copy()
# df.to_wkt().to_sql(
# name='t_river_basin', con=con, schema='pr_zh',
# if_exists='append', index=False,
# dtype={
# 'geom': Geometry(srid=4326)
# # 'id_lim_list':uuid.SafeUUID
# }
# )
df['geojson_4326'] = df.to_crs(4326).geom.__geo_interface__['features']
df['geojson_4326'] = [x['geometry'] for x in df['geojson_4326']]
# df['geojson_4326'] = df['geojson_4326'].astype(str)
df['centroid'] = df.geom.centroid.to_wkt()
df['enable'] = True
df['source'] = 'IGN'
df.to_postgis(
name='l_areas',
con=con,
schema='ref_geo',
if_exists='append',
index=False,
index_label=None,
chunksize=None,
dtype={
'centroid': Geometry(geometry_type='POINT',srid=2154)
},
)
sql = '''
SELECT a.id_area,a.area_name,a.geom FROM ref_geo.l_areas a
JOIN ref_geo.bib_areas_types b USING (id_type)
WHERE b.type_code = 'COM'
'''
tu = gpd.read_postgis(sql,con_gn)
sql = '''
SELECT a.id_zh,a.code,
ST_MakeValid(ST_Transform(ST_SetSRID(a.geom,4326),2154)) geom
FROM pr_zh.t_zh a
--JOIN ref_geo.bib_areas_types b USING (id_type)
--WHERE b.type_code = 'COM'
'''
to = gpd.read_postgis(sql,con_gn)