这是因为 shapefile 将为生成的地理数据框的给定行指示几何类型,该几何类型可以是多边形或多多边形。
转换为 Well-Known-Binary 十六进制字符串时包含此信息,并在将文本转换为几何图形时产生类型问题。
mhweber 的要点中的爆炸功能将通过将多面体分解为其组件部分来解决此问题。
import geopandas as gpd
from shapely.geometry.polygon import Polygon
from shapely.geometry.multipolygon import MultiPolygon
def explode(indata):
count_mp = 0
indf = gpd.GeoDataFrame.from_file(indata)
outdf = gpd.GeoDataFrame(columns=indf.columns)
for idx, row in indf.iterrows():
if type(row.geometry) == Polygon:
outdf = outdf.append(row,ignore_index=True)
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)
print("There were ", count_mp, "Multipolygons found and exploded")
return outdf
我添加了一个副作用来打印找到的多面体的数量。
奇怪的是,您应该调查这些以确保爆炸功能不会破坏您需要的关系。