python - 几何类型多多边形与列类型多边形不匹配
问题描述
使用 python、sqlalchemy 和 psycopg2 驱动程序,我试图从一个 shapefile 到一个地理数据框,再到一个在这个问题之后安装了 postgis 的 postgres 数据库
我将几何图形转换为 WKB 十六进制字符串,并df.to_sql()
成功使用导入的标准数据框。
运行alter table
查询时出现错误:
sqlalchemy.exc.DataError: (psycopg2.errors.InvalidParameterValue) Geometry type (MultiPolygon) does not match column type (Polygon)
解决方案
重写了@Hugh_Kelley 的函数以更快。
def _explode(indf):
count_mp = 0
outdf = gpd.GeoDataFrame(columns=indf.columns)
outdf = indf[indf.geometry.type == 'Polygon']
indf = indf[indf.geometry.type != 'Polygon']
for idx, row in indf.iterrows():
if type(row.geometry) == MultiPolygon:
count_mp = count_mp + 1
multdf = gpd.GeoDataFrame(columns=indf.columns)
recs = len(row.geometry)
multdf = multdf.append([row]*recs,ignore_index=True)
for geom in range(recs):
multdf.loc[geom,'geometry'] = row.geometry[geom]
outdf = outdf.append(multdf,ignore_index=True)
else:
print(row)
print("There were ", count_mp, "Multipolygons found and exploded")
return outdf
推荐阅读
- python - 如何迭代地在python中手动标记unicode文本
- java - 为什么我不能在任何方法之外修改类成员变量?
- python - 如何在熊猫python中用上一行或下一行填充缺失的行
- mysql - 如何将 dmp 表从 oracle 导入 mysql?
- c - 在二维动态分配的字符串数组中获取单个字符串的长度
- java - 无法在 Java 中使用 TFLite 模型实例化 Tensorflow 解释器
- javascript - 按两个输入值之间的范围过滤元素的菜单
- java - 删除级联不起作用,已经尝试了我找到的所有解决方案
- php - wordpress 将 html 添加到页面的最顶部
- jmeter - JMeter 抛出 oajJMeter:未捕获异常:java.lang.StackOverflowError:空错误