栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

geopandas写入postgis

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

geopandas写入postgis

1、直接通过to_sql写入
file_name = os.path.split(file)[1]
print(‘正在写入:’+file)
tbl_name = file_name.split(‘.’)[0] # 表名
map_data = gpd.GeoDataFrame.from_file(file)
spatial_ref = map_data.crs.srs.split(‘:’)[-1]
map_data[‘geometry’] = map_data[‘geometry’].apply(
lambda x: WKTElement(x.wkt, spatial_ref))
# geopandas 的to_sql()方法继承自pandas, 将GeoDataFrame中的数据写入数据库
map_data.to_sql(
name=tbl_name,
con=engine,
if_exists=‘replace’, # 如果表存在,则替换原有表
chunksize=1000, # 设置一次入库大小,防止数据量太大卡顿
# 指定geometry的类型,这里直接指定geometry_type=‘GEOMETRY’,防止MultiPolygon无法写入
dtype={‘geometry’: Geometry(
geometry_type=‘GEOMETRY’, srid=spatial_ref)},
method=‘multi’
)
2、通过to_postgis写入(数据表名称一定要小写)
为了能在geopandas中与postgresql和PostGIS建立连接,请确保以下3个库已经安装:
pip install sqlalchemy psycopg2-binary geoalchemy2

CREATE EXTENSION postgis;并执行,成功之后我们的数据库就变成了空间数据库,支持空间相关的各种功能:
3、读取postgis数据
从PostGIS中读取数据要用到另一个API,对应geopandas的read_postgis(),其主要参数如下:

sql:字符型,对应从空间数据库中提取数据的SQL语句

con:同to_postgis()

geom_col:字符型,用于指定将哪一列作为GeoDataFrame的矢量列

crs:用于指定坐标参考系,同GeoDataFrame的坐标参考系设定方式

index_col:字符型或列表,用于指定将哪些列作为索引

parse_dates:列表,用于预解析时间类型数据

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/870424.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号