类型错误:“MultiPolygon”对象不可迭代(VS代码:Python geopandas)

问题描述 投票:0回答:1

在我的 anaconda env 中,我已经安装了 geopandas,但由于错误,我无法在 Visual Studio Code 中进行绘图:

TypeError:“MultiPolygon”对象不可迭代。

我对编程还很陌生,到目前为止,我在 Mac M1 上运行 Visual Code Studio 时遇到了很多困难 - 这是一个常见错误吗?我应该切换到不同的 IDE 吗?

**代码: **

import geopandas as gpd
import matplotlib.pyplot as plt
world_data = gpd.read_file(r'/Users/xxxxx/world.shp')
world_data
world_data.plot()

**错误: **

/Users/lantyynka/opt/anaconda3/envs/datax/lib/python3.12/site-packages/geopandas/plotting.py:48:ShapelyDeprecationWarning:“type”属性已弃用,将来将被删除。您可以改用“geom_type”属性。 如果 geom.type.startswith(前缀): 回溯(最近一次调用最后一次): 文件“”,第 1 行,位于 文件“/Users/lantyynka/opt/anaconda3/envs/datax/lib/python3.12/site-packages/geopandas/geodataframe.py”,第 921 行,在图中 返回plot_dataframe(self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 文件“/Users/lantyynka/opt/anaconda3/envs/datax/lib/python3.12/site-packages/geopandas/plotting.py”,第615行,在plot_dataframe中 返回情节_系列( ^^^^^^^^^^^^^ 文件“/Users/lantyynka/opt/anaconda3/envs/datax/lib/python3.12/site-packages/geopandas/plotting.py”,第413行,在plot_series中 _plot_polygon_collection( 文件“/Users/lantyynka/opt/anaconda3/envs/datax/lib/python3.12/site-packages/geopandas/plotting.py”,第 129 行,在 _plot_polygon_collection 中 几何,多索引 = _flatten_multi_geoms(geoms) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 文件“/Users/lantyynka/opt/anaconda3/envs/datax/lib/python3.12/site-packages/geopandas/plotting.py”,第 49 行,在 _flatten_multi_geoms 中 对于几何中的多边形: 类型错误:“MultiPolygon”对象不可迭代

提前感谢您的帮助!:)

我尝试通过 Jupyter 在终端和交互窗口中运行代码。两次尝试都导致了相同的错误。

我也尝试安装旧版本的 shapely 但它不起作用。

python apple-m1 geopandas multipolygons
1个回答
0
投票

您可以在绘图之前尝试将 MultiPolygon 几何图形转换为单独的 Polygon 对象。

import geopandas as gpd
import matplotlib.pyplot as plt

world_data = gpd.read_file('/Users/xxxxx/world.shp')

# Convert MultiPolygon geometries to Polygon geometries
world_data['geometry'] = world_data['geometry'].explode()

world_data.plot()

plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.