geopandas.GeoDataFrame.to_postgis#
- GeoDataFrame.to_postgis(name, con, schema=None, if_exists='fail', index=False, index_label=None, chunksize=None, dtype=None)#
将GeoDataFrame上传到PostGIS数据库。
此方法需要安装SQLAlChemy和GeoAlChemy2,并安装PostgreSQL Python驱动程序(例如心理拷贝g2)。
- 参数
- name应力
目标表的名称。
- conSqlalChemy.Eng.Connection或SqlalChemy.Engine
与PostGIS数据库的活动连接。
- if_exists{‘FAIL’,‘REPLACE’,‘APPEND’},默认为‘FAIL’
如果该表已存在,应如何操作:
失败:引发ValueError。
替换:在插入新值之前删除该表。
追加:在现有表中插入新值。
- schema字符串,可选
指定架构。如果没有,则使用默认架构:‘PUBLIC’。
- index布尔值,默认为False
将DataFrame索引作为列写入。用途 index_label 作为表中的列名。
- index_label字符串或序列,默认为无
索引列的列标签。如果未指定(默认),且index为True,则使用索引名。
- chunksize整型,可选
每次将按此大小成批写入行。默认情况下,将一次写入所有行。
- dtype将列名字典转换为SQL类型,默认为无
指定列的数据类型。键应该是列名,值应该是SQLAlChemy类型。
参见
GeoDataFrame.to_file
将GeoDataFrame写入文件
read_postgis
将PostGIS数据库读取到GeoDataFrame
示例
>>> from sqlalchemy import create_engine >>> engine = create_engine("postgresql://myusername:mypassword@myhost:5432/mydatabase") >>> gdf.to_postgis("my_table", engine)