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驱动程序(psycopg或psycopg2)。
也可以使用
to_file()
将数据写入数据库。尤其对于像 GeoPackage 或 SpatiaLite 这样的文件地理数据库,这可能更容易。- Parameters:
- namestr
目标表的名称。
- consqlalchemy.engine.Connection or sqlalchemy.engine.Engine
与PostGIS数据库的活动连接。
- if_exists{‘fail’, ‘replace’, ‘append’}, default ‘fail’
如果表已经存在,应该如何处理:
失败:引发ValueError。
替换:在插入新值之前删除表。
append: 将新值插入到现有表中。
- schemastring, optional
指定架构。如果为 None,使用默认架构:‘public’。
- indexbool, default False
将DataFrame索引写入一列。
使用 index_label 作为表中的列名。- index_labelstring or sequence, default None
索引列的列标签。
如果给定 None(默认值)并且索引为 True,则使用索引名称。- chunksizeint, optional
每次将按此大小批量写入行。
默认情况下,所有行将一次性写入。- dtypedict of column name to SQL type, default None
为列指定数据类型。 键应为列名,值应为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)