首页 > 解决方案 > 调用 .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 方法不起作用?

标签: pythongisshapefilegeopandas

解决方案


推荐阅读