我正试图在 map 上绘制一些数据,其中县根据特定值进行着色.这之前工作得很好(第一张图片),但现在留下了空白(第二张图片),并有许多Shapely弃用警告.

enter image description here

ScandinaviaEmpty

在阅读了herehere的弃用警告的问题后,我试着使用. geoms,但数据似乎是MultiPolymore和Polyymore的组合.这会使我无法有效地使用. geoms.

有问题的数据的一部分:

[<shapely.geometry.multipolygon.MultiPolygon object at 0x70ac02a82980>]
Length: 1, dtype: geometry
<GeometryArray>
[<shapely.geometry.multipolygon.MultiPolygon object at 0x70ac02a829b0>]
Length: 1, dtype: geometry
<GeometryArray>
[<shapely.geometry.multipolygon.MultiPolygon object at 0x70ac02a82a10>]
Length: 1, dtype: geometry
<GeometryArray>
[<shapely.geometry.polygon.Polygon object at 0x70ac02a82a70>]
Length: 1, dtype: geometry
<GeometryArray>
[<shapely.geometry.polygon.Polygon object at 0x70ac02a82ad0>]
Length: 1, dtype: geometry
<GeometryArray>
[<shapely.geometry.polygon.Polygon object at 0x70ac02a82b30>]
Length: 1, dtype: geometry

如何编辑我的代码,以便我得到我需要的绘图与Shapely 1.8.0及以上版本?

我的代码最初:

poly = [df.loc[df['NAME_0'] == 'Sweden']['geometry'].values[0]]
pad1 = 6  #padding, degrees unit
exts = [poly[0].bounds[0] - pad1, poly[0].bounds[2] + pad1, poly[0].bounds[1] - pad1, poly[0].bounds[3] + pad1];
msk = Polygon(rect_from_bound(*exts)).difference( poly[0].simplify(0.01) )
msk_stm  = st_proj.project_geometry (msk, projection)  # project geometry to the projection used by stamen

cs1 = axs[0].contourf(lons, lats, FWI_sum, 60, transform=projection, levels=levels, cmap=cmap, extend='both')
cs11 = axs[0].contourf(lons2, lats2, forest_output, 60, transform=projection, levels=levels2, cmap=cmap2, extend='both')
axs[0].coastlines()
axs[0].add_feature(feature.OCEAN, facecolor = facecolor_oceanmask, edgecolor=edgecolor_oceanmask, zorder=zorder_oceanmask)
axs[0].add_feature(feature.BORDERS)
axs[0].set_extent(extent, projection)
axs[0].axis('off')
axs[0].add_geometries(poly, crs=projection, facecolor=facecolor_polygon, edgecolor=edgecolor_polygon)
axs[0].add_geometries(msk_stm, st_proj, facecolor=facecolor_landmask, edgecolor=edgecolor_landmask, zorder=zorder_landmask)
for county in counties:
    poly2 = df2.loc[df2['name'] == county]['geometry'].values
    axs[0].add_geometries(poly2, crs=projection, facecolor='None', edgecolor='black', zorder=1)

cs12 = axs[1].contourf(lons2, lats2, forest_output, 60, transform=projection, levels=levels2, cmap=cmap2, extend='both')
axs[1].set_extent(extent, projection)
axs[1].add_feature(feature.BORDERS)
axs[1].add_feature(feature.OCEAN, facecolor = facecolor_oceanmask, edgecolor=edgecolor_oceanmask, zorder=zorder_oceanmask)
axs[1].coastlines(resolution='10m')
axs[1].axis('off')
axs[1].add_geometries(poly, crs=projection, facecolor=facecolor_polygon, edgecolor=edgecolor_polygon)
axs[1].add_geometries(msk_stm, st_proj, facecolor=facecolor_landmask, edgecolor=edgecolor_landmask, zorder=zorder_landmask)

for county, fireextentcounty_norm in zip(counties, fireextentcounties_norm):
    poly3 = df2.loc[df2['name'] == county]['geometry'].values
    rgba2 = cmap3(fireextentcounty_norm)
    axs[1].add_geometries(poly3, crs=projection, facecolor=rgba2, edgecolor='black', zorder=1)

cs13 = axs[2].contourf(lons2, lats2, forest_output, 60, transform=projection, levels=levels2, cmap=cmap2, extend='both')
axs[2].set_extent(extent, projection)
axs[2].add_feature(feature.BORDERS)
axs[2].add_feature(feature.OCEAN, facecolor = facecolor_oceanmask, edgecolor=edgecolor_oceanmask, zorder=zorder_oceanmask)
axs[2].coastlines(resolution='10m')
axs[2].axis('off')
axs[2].add_geometries(poly, crs=projection, facecolor=facecolor_polygon, edgecolor=edgecolor_polygon)
axs[2].add_geometries(msk_stm, st_proj, facecolor=facecolor_landmask, edgecolor=edgecolor_landmask, zorder=zorder_landmask)

for county, firenumbercounty_norm in zip(counties, firenumbercounties_norm):
    poly4 = df2.loc[df2['name'] == county]['geometry'].values
    rgba3 = cmap4(firenumbercounty_norm)
    axs[2].add_geometries(poly4, crs=projection, facecolor=rgba3, edgecolor='black', zorder=1)

它给出了这些警告:

Warning (from warnings module):
  File "/usr/lib/python3/dist-packages/cartopy/feature/__init__.py", line 217
    self._geoms = tuple(geometries)
ShapelyDeprecationWarning: Iteration over multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the `geoms` property to access the constituent parts of a multi-part geometry.

Warning (from warnings module):
  File "/usr/lib/python3/dist-packages/cartopy/feature/__init__.py", line 217
    self._geoms = tuple(geometries)
ShapelyDeprecationWarning: __len__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Check the length of the `geoms` property instead to get the  number of parts of a multi-part geometry.

Warning (from warnings module):
  File "/usr/lib/python3/dist-packages/cartopy/feature/__init__.py", line 217
    self._geoms = tuple(geometries)
ShapelyDeprecationWarning: Iteration over multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the `geoms` property to access the constituent parts of a multi-part geometry.

Warning (from warnings module):
  File "/usr/lib/python3/dist-packages/cartopy/feature/__init__.py", line 217
    self._geoms = tuple(geometries)
ShapelyDeprecationWarning: __len__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Check the length of the `geoms` property instead to get the  number of parts of a multi-part geometry.

Warning (from warnings module):
  File "/usr/lib/python3/dist-packages/cartopy/feature/__init__.py", line 217
    self._geoms = tuple(geometries)
ShapelyDeprecationWarning: Iteration over multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the `geoms` property to access the constituent parts of a multi-part geometry.

Warning (from warnings module):
  File "/usr/lib/python3/dist-packages/cartopy/feature/__init__.py", line 217
    self._geoms = tuple(geometries)
ShapelyDeprecationWarning: __len__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Check the length of the `geoms` property instead to get the  number of parts of a multi-part geometry.

我现在的代码:

poly = [df.loc[df['NAME_0'] == 'Sweden']

['geometry'].values[0]]
pad1 = 6  #padding, degrees unit
exts = [poly[0].bounds[0] - pad1, poly[0].bounds[2] + pad1, poly[0].bounds[1] - pad1, poly[0].bounds[3] + pad1];
msk = Polygon(rect_from_bound(*exts)).difference( poly[0].simplify(0.01) )
msk_stm  = st_proj.project_geometry (msk, projection)  # project geometry to the projection used by stamen

cs1 = axs[0].contourf(lons, lats, FWI_sum, 60, transform=projection, levels=levels, cmap=cmap, extend='both')
cs11 = axs[0].contourf(lons2, lats2, forest_output, 60, transform=projection, levels=levels2, cmap=cmap2, extend='both')
axs[0].coastlines()
axs[0].add_feature(feature.OCEAN, facecolor = facecolor_oceanmask, edgecolor=edgecolor_oceanmask, zorder=zorder_oceanmask)
axs[0].add_feature(feature.BORDERS)
axs[0].set_extent(extent, projection)
axs[0].axis('off')
axs[0].add_geometries(poly[0].geoms, crs=projection, facecolor=facecolor_polygon, edgecolor=edgecolor_polygon)
axs[0].add_geometries(msk_stm, st_proj, facecolor=facecolor_landmask, edgecolor=edgecolor_landmask, zorder=zorder_landmask)
for county in counties:
    poly2 = df2.loc[df2['name'] == county]['geometry'].values
    print(poly2[0].geoms)
    axs[0].add_geometries(poly2[0].geoms, crs=projection, facecolor='None', edgecolor='black', zorder=1)

cs12 = axs[1].contourf(lons2, lats2, forest_output, 60, transform=projection, levels=levels2, cmap=cmap2, extend='both')
axs[1].set_extent(extent, projection)
axs[1].add_feature(feature.BORDERS)
axs[1].add_feature(feature.OCEAN, facecolor = facecolor_oceanmask, edgecolor=edgecolor_oceanmask, zorder=zorder_oceanmask)
axs[1].coastlines(resolution='10m')
axs[1].axis('off')
axs[1].add_geometries(poly[0].geoms, crs=projection, facecolor=facecolor_polygon, edgecolor=edgecolor_polygon)
axs[1].add_geometries(msk_stm, st_proj, facecolor=facecolor_landmask, edgecolor=edgecolor_landmask, zorder=zorder_landmask)

for county, fireextentcounty_norm in zip(counties, fireextentcounties_norm):
    poly3 = df2.loc[df2['name'] == county]['geometry'].values
    rgba2 = cmap3(fireextentcounty_norm)
    axs[1].add_geometries(poly3[0].geoms, crs=projection, facecolor=rgba2, edgecolor='black', zorder=1)

cs13 = axs[2].contourf(lons2, lats2, forest_output, 60, transform=projection, levels=levels2, cmap=cmap2, extend='both')
axs[2].set_extent(extent, projection)
axs[2].add_feature(feature.BORDERS)
axs[2].add_feature(feature.OCEAN, facecolor = facecolor_oceanmask, edgecolor=edgecolor_oceanmask, zorder=zorder_oceanmask)
axs[2].coastlines(resolution='10m')
axs[2].axis('off')
axs[2].add_geometries(poly[0].geoms, crs=projection, facecolor=facecolor_polygon, edgecolor=edgecolor_polygon)
axs[2].add_geometries(msk_stm, st_proj, facecolor=facecolor_landmask, edgecolor=edgecolor_landmask, zorder=zorder_landmask)

for county, firenumbercounty_norm in zip(counties, firenumbercounties_norm):
    poly4 = df2.loc[df2['name'] == county]['geometry'].values
    rgba3 = cmap4(firenumbercounty_norm)
    axs[2].add_geometries(poly4[0].geoms, crs=projection, facecolor=rgba3, edgecolor='black', zorder=1)

它给出了这些错误:

Warning (from warnings module):
  File "/usr/lib/python3/dist-packages/cartopy/feature/__init__.py", line 217
    self._geoms = tuple(geometries)
ShapelyDeprecationWarning: Iteration over multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the `geoms` property to access the constituent parts of a multi-part geometry.

Warning (from warnings module):
  File "/usr/lib/python3/dist-packages/cartopy/feature/__init__.py", line 217
    self._geoms = tuple(geometries)
ShapelyDeprecationWarning: __len__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Check the length of the `geoms` property instead to get the  number of parts of a multi-part geometry.
<shapely.geometry.base.GeometrySequence object at 0x74aa047dbf40>
<shapely.geometry.base.GeometrySequence object at 0x74aa047dbf10>
<shapely.geometry.base.GeometrySequence object at 0x74aa047fcee0>
<shapely.geometry.base.GeometrySequence object at 0x74aa047fdd50>
<shapely.geometry.base.GeometrySequence object at 0x74aa047fe890>
Traceback (most recent call last):
  File "/usr/lib/python3.10/idlelib/run.py", line 578, in runcode
    exec(code, self.locals)
  File "/home/els/Nextcloud/Documents/PhD/damage_index/FWImodel/Plotting/PlotYearSum.py", line 182, in <module>
    print(poly2[0].geoms)
AttributeError: 'Polygon' object has no attribute 'geoms'. Did you mean: '_geom'?

推荐答案

有计划作出改变,使单一波利尼西亚也有一个.geoms属性,但这还没有准备好(link).

在此变化落地之前,最简单的解决方法是使用shapely.get_parts.用shapely.get_parts(poly[0])代替poly[0].geoms.

Python相关问答推荐

Python中MongoDB的BSON时间戳

根据给定日期的状态过滤查询集

配置Sweetviz以分析对象类型列,而无需转换

Pydantic 2.7.0模型接受字符串日期时间或无

如何让程序打印新段落上的每一行?

使用@ guardlasses. guardlass和注释的Python继承

无法定位元素错误404

Pandas:将多级列名改为一级

如何使用Python以编程方式判断和检索Angular网站的动态内容?

SQLAlchemy Like ALL ORM analog

如何在表中添加重复的列?

字符串合并语法在哪里记录

Python中的变量每次增加超过1

Python Pandas—时间序列—时间戳缺失时间精确在00:00

如何删除重复的文字翻拍?

Tensorflow tokenizer问题.num_words到底做了什么?

如何将泛型类类型与函数返回类型结合使用?

如何为需要初始化的具体类实现依赖反转和接口分离?

时长超过24小时如何从Excel导入时长数据

我可以同时更改多个图像吗?