déplacement des fct get_"attrs" des tables et adaptation de la fonction _get_tables
This commit is contained in:
parent
e95ef38021
commit
a8c8b1a483
@ -10,6 +10,22 @@ from pandas import Series, Index, read_sql, merge
|
||||
|
||||
|
||||
|
||||
#####################################
|
||||
### Fonctions connexion ###
|
||||
#####################################
|
||||
def __get_pkey__(engine,table_name,schema):
|
||||
pk = engine.dialect.get_pk_constraint(engine,table_name=table_name,schema=schema)
|
||||
return pk
|
||||
|
||||
|
||||
def __get_dtype__(engine,table_name,schema):
|
||||
cols = engine.dialect.get_columns(engine,table_name=table_name,schema=schema)
|
||||
# name_cols = [i['name'] for i in cols]
|
||||
# type_cols = [i['type'] for i in cols]
|
||||
type_cols = {i['name']:i['type'] for i in cols}
|
||||
return type_cols
|
||||
|
||||
|
||||
#####################################
|
||||
### Fonctions générales ###
|
||||
#####################################
|
||||
@ -87,6 +103,13 @@ def _get_table(con, schema, table, ids=None, nom=None, cols=None, params_col={},
|
||||
'history'; A une Date_fin
|
||||
'all'; Tous les Sites
|
||||
'''
|
||||
pkey = __get_pkey__(con,table,schema)
|
||||
if pkey['constrained_columns']:
|
||||
pk = pkey['constrained_columns']
|
||||
if len(pk)==1: pk = pk[0]
|
||||
else: raise ValueError(pk)
|
||||
else : pk = 'site_code'
|
||||
|
||||
sql = 'SELECT * FROM {sch}.{tab}'.format(sch=schema, tab=table)
|
||||
if params_col:
|
||||
params_col = { k: v for k, v in params_col.items() if v }
|
||||
@ -94,7 +117,7 @@ def _get_table(con, schema, table, ids=None, nom=None, cols=None, params_col={},
|
||||
if cols : sql = sql.replace('*', to_colStringSQL(cols) )
|
||||
# Si arg (ids|nom|params_col), ajout de 'WHERE'
|
||||
if ids or nom or params_col or (statut!='all' and table=='sites') : sql = sql + ' WHERE '
|
||||
if ids : sql = sql + 'id IN %(ids)s'
|
||||
if ids : sql = sql + pk +' IN %(ids)s'
|
||||
if ids and (nom or params_col or (statut!='all' and table=='sites')) : sql = sql + ' AND '
|
||||
if nom : sql = sql + 'nom IN %(nom)s'
|
||||
if nom and (params_col or (statut!='all' and table=='sites')) : sql = sql + ' AND '
|
||||
@ -114,7 +137,7 @@ def _get_table(con, schema, table, ids=None, nom=None, cols=None, params_col={},
|
||||
df = _set_geom(df)
|
||||
return df
|
||||
|
||||
def _set_geom(df, hex=True):
|
||||
def _set_geom(df, hex=True, crs=2154):
|
||||
from shapely.wkb import loads
|
||||
import geopandas as gpd # set_geometry
|
||||
|
||||
@ -122,10 +145,10 @@ def _set_geom(df, hex=True):
|
||||
# df['geometry'] = [(loads(geom, hex=hex)) for geom in df['geom']]
|
||||
geometry = [(loads(geom, hex=hex)) for geom in df['geom']]
|
||||
df.drop(columns=['geom'], inplace=True)
|
||||
df = gpd.GeoDataFrame(df,geometry=geometry,crs='EPSG:2154')
|
||||
df = gpd.GeoDataFrame(df,geometry=geometry,crs='EPSG:%s'%crs)
|
||||
df.rename_geometry('geom', inplace=True)
|
||||
else:
|
||||
df = df.set_geometry('geom', crs='EPSG:2154')
|
||||
df = df.set_geometry('geom', crs='EPSG:%s'%crs)
|
||||
|
||||
return df
|
||||
|
||||
@ -369,13 +392,17 @@ def calc_recouvrmt(df1,df2):
|
||||
return df1
|
||||
|
||||
|
||||
def Polygons_to_MultiPolygon(df):
|
||||
def Polygons_to_MultiPolygon(df,geom_col=None):
|
||||
from shapely.geometry import MultiPolygon
|
||||
from pandas import concat
|
||||
|
||||
if not geom_col:
|
||||
geom_col = df.geometry.name
|
||||
|
||||
df = df.copy()
|
||||
multi = df.loc[df.geom_type=='MultiPolygon'].copy()
|
||||
poly = df.loc[df.geom_type=='Polygon'].copy()
|
||||
poly['geom'] = [MultiPolygon([geom]) for geom in df.loc[df.geom_type=='Polygon','geom'] ]
|
||||
poly[geom_col] = [MultiPolygon([geom]) for geom in df.loc[df.geom_type=='Polygon',geom_col] ]
|
||||
df = concat([multi,poly])
|
||||
df.sort_index(inplace=True)
|
||||
return df
|
||||
Loading…
x
Reference in New Issue
Block a user