python - 调用 .bounds 方法时 GeoSeries 对象解释为 NoneType
问题描述
我正在尝试将 shapefile 加载到 Geopandas 对象中,然后调用 total_bounds 方法来识别 shapefile 的范围。
我已将 shapefile 加载到 Geopandas 对象中,并且能够使用 .plot 方法对其进行绘制。我能够将 geopandas 对象的内容打印到标准输出。但是,当我尝试调用 .bounds 方法时,出现以下错误...
*** AttributeError: 'NoneType' 对象没有属性 'bounds'
class MyClass:
def __init__(self):
self.my_shapefile = "filepath"
self.display = True
self.wpd_gdf = None
self.run()
def run(self):
self.load_shapefile()
self.my_func()
return
def load_shapefile(self):
gdf = gpd.read_file(self.my_shapefile)
if self.display:
gdf.plot(cmap="OrRd", edgecolor="black")
plt.show()
self.wpd_gdf = gdf
return gdf
def my_func(self):
"""
The function that is throwing the error.
"""
# define extent of meshgrid
# import pdb; pdb.set_trace()
extent = self.wpd_gdf.geometry.total_bounds
在初始化范围变量之前,我尝试过调试代码。
我可以打电话self.wpd_gdf
并返回一个地理数据框。
如果我打电话type(self.wpd_gdf)
,我会得到以下输出:
<class 'geopandas.geodataframe.GeoDataFrame'>
我可以打电话self.wpd_gdf.geometry
,我得到了一系列多边形的返回。
如果我打电话type(self.wpd_gdf.geoometry)
,我会得到以下输出:
<class 'geopandas.geoseries.GeoSeries'>
但是,当我打电话时,self.wpd_gdf.geometry.total_bounds
我得到以下输出:
*** AttributeError: 'NoneType' object has no attribute 'bounds'
如果我打电话,我会得到相同的输出self.wpd_gdf.geometry.bounds
我用不同的 shapefile 运行了这个脚本,它工作正常。我很确定这是 Geopandas 或我的 shapefile 的问题。我无法在线共享 shapefile,因为它与工作相关且保密。我没有创建 shapefile,并且 shapefile 可以很好地加载到 ArcMaps 中,因此从表面上看,shapefile 似乎没有问题。
我可以采取哪些步骤来诊断 Geopandas 或我的 shapefile 的问题?
为什么我的 shapefile 在 python 和 geopandas 中都可以正常加载,但 bounds 方法不起作用?
解决方案
推荐阅读
- c# - 使用 Nuget ComponentModel.Annotations 替换程序集引用 ComponentModel.DataAnnotations 的运行时错误
- r - 使用 geom_boxplot() 在整洁的数据框中更改箱线图的顺序
- python - 为每个超过最大值的 X 堆叠“点”
- http - adding 1 user with htpasswd in 2 different servers using ssh connection
- ansible - 使用 Ansible 2.9.6 更改 VMware VM 端口组
- javascript - 应用“无返回等待”时,函数是否应该保持异步?
- sql - 如何在SQL中查找两次之间不存在的数据
- html - NavBar Bootstrap 4,如何不重复隐藏手机屏幕中的某些项目?
- android - 一个安卓应用程序在我打开时一直崩溃并显示“已停止工作”的消息
- node.js - 如何使用新的 NodeJS @azure/storage-queue 库连接到开发 Azure 存储(本地)?