Write GeoDataFrame into SQL Database

前端 未结 3 1820
盖世英雄少女心
盖世英雄少女心 2020-12-05 20:37

I hope that my question is not ridiculous since, surprisingly, this question has apparently not really been asked yet (to the best of my knowledge) on the popular websites.<

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-05 21:15

    A version of Hamri Said's answer, but using a lambda, which in my opinion is a bit nicer because it is such a short function:

    # Imports
    from geoalchemy2 import Geometry, WKTElement
    from sqlalchemy import *
    
    geodataframe['geom'] = geodataframe['geom'].apply(lambda geom: WKTElement(geom.wkt, srid = ))
    
    db_url = 'postgresql://username:password@host: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= )})
    

提交回复
热议问题