0

I am trying to load a shapefile into a Geopandas object and then call the total_bounds method to identify the extent of the shapefile.

I have loaded the shapefile into a Geopandas object and am able to plot it using the .plot method. I am able to print out the contents of the geopandas object to stdout. However, when I try to call the .bounds method I get the following error...

*** AttributeError: 'NoneType' object has no attribute '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

I have tried debugging the code just before I initialise the extent variable.

I am able to call self.wpd_gdf and I get a geodataframe returned.

If I call type(self.wpd_gdf) I get the following output:

<class 'geopandas.geodataframe.GeoDataFrame'>

I am able to call self.wpd_gdf.geometry and I get a geoseries of Polygons returned.

If I call type(self.wpd_gdf.geoometry) I get the following output:

<class 'geopandas.geoseries.GeoSeries'>

However, when I call self.wpd_gdf.geometry.total_bounds I get the following output:

*** AttributeError: 'NoneType' object has no attribute 'bounds'

I get the same output if I call self.wpd_gdf.geometry.bounds

I have run this script with a different shapefile and it works fine. I am pretty sure it is a problem with either Geopandas or my shapefile. I can't share the shapefile online because it is work-related and confidential. I did not create the shapefile and the shapefile loads fine into ArcMaps so on the face of it it doesn't seem like a problem with the shapefile.

What steps can I take to diagnose the problem with either Geopandas or my shapefile?

Why would my shapefile load ok in both python and geopandas but the bounds method not work?

0 Answers0