17 lines
756 B
Python
17 lines
756 B
Python
|
|
import geopandas as gpd
|
||
|
|
import pandas as pd
|
||
|
|
from database.connection import engine
|
||
|
|
from sqlalchemy import text
|
||
|
|
|
||
|
|
def save_dataframe_dynamic(df: pd.DataFrame, table_name: str):
|
||
|
|
"""Save pandas DataFrame to Postgres (non-geo)."""
|
||
|
|
df.to_sql(table_name, engine, if_exists="replace", index=False, method='multi', chunksize=1000)
|
||
|
|
|
||
|
|
def save_geodataframe(gdf: gpd.GeoDataFrame, table_name: str):
|
||
|
|
"""Save GeoDataFrame to PostGIS (requires geoalchemy/geopandas)."""
|
||
|
|
# ensure geometry column exists and CRS set
|
||
|
|
if gdf.crs is None:
|
||
|
|
gdf = gdf.set_crs("EPSG:4326", allow_override=True)
|
||
|
|
# geopandas >= 0.10 has to_postgis in some installs; fallback using SQLAlchemy + GeoAlchemy2:
|
||
|
|
gdf.to_postgis(table_name, engine, if_exists="replace")
|