Som før nævnt virker @Kartiks svar kun for et enkelt opkald, for at tilføje data fremkalder det en DataError
siden geom
kolonne forventer derefter, at geometrien har et SRID. Du kan bruge GeoAlchemy
at håndtere alle sagerne:
# Imports
from geoalchemy2 import Geometry, WKTElement
from sqlalchemy import *
# Use GeoAlchemy's WKTElement to create a geom with SRID
def create_wkt_element(geom):
return WKTElement(geom.wkt, srid = <your_SRID>)
geodataframe['geom'] = geodataframe['geom'].apply(create_wkt_element)
db_url = 'postgresql://username:[email protected]:socket/database'
engine = create_engine(db_url, echo=False)
# Use 'dtype' to specify column's type
# For the geom column, we will use GeoAlchemy's type 'Geometry'
your_geodataframe.to_sql(table_name, engine, if_exists='append', index=False,
dtype={'geom': Geometry('POINT', srid= <your_srid>)})