AttributeError: bounds whentrying to display GeoJson on world map

I’m reading this part of the large area utilities on the documentation and i’m trying to display my geojson on the world map. The problem is that I keep getting error with the bounds :

AttributeError: bounds

This is the script I have used:

#my original file is shapefile so I have changed it into GeoJson:

import geopandas as gpd
shape = gpd.read_file(r'shape/my_shape.shp')
#take only geometry column
shape=shape.iloc[:5,9:10]
shape.to_file('shape/my_shape.geojson',driver='GeoJSON')

import geojson
path_to_file='shape/my_shape.geojson'
with open(path_to_file) as f:
    json_shape = geojson.load(f)
features = gj['features'][0]


#when I print the geojson it seems to  be ok -
 
json_shape['features'][0]
>>>{"geometry": {"coordinates": [[[-42.896028, -3.615297], [-42.884315, -3.613943], [-42.880522, -3.626857], [-42.887634, -3.63392], [-42.890919, -3.632831], [-42.896028, -3.615297]]], "type": "Polygon"}, "properties": {}, "type": "Feature"}

type(json_shape)
>>>geojson.feature.FeatureCollection



#trying to display on world map

def show_area(area_shape, area_buffer=0.3):
    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(111)

    minx, miny, maxx, maxy = area_shape.bounds
    lng, lat = (minx + maxx) / 2, (miny + maxy) / 2

    m = Basemap(projection='ortho',lat_0=lat, lon_0=lng, resolution='l')
    m.drawcoastlines()
    m.bluemarble()

    if isinstance(area_shape, Polygon):
        area_shape = [area_shape]
    for polygon in area_shape:
        x, y = np.array(polygon.boundary)[0]
        m_poly = []
        for x, y in np.array(polygon.boundary):
            m_poly.append(m(x, y))
        ax.add_patch(plt_polygon(np.array(m_poly), closed=True, facecolor='red', edgecolor='red'))

    plt.tight_layout()
    plt.show()

show_area(json_shape)

>>>AttributeError: bounds

I have tried google this error but I found problem with points but i’m working with polygons so i’m not sure why I get this error.

Hi @reutkeller,

The utility function show_area expects area_shape to be an object of type shapely.geometry.Polygon or shapely.geometry.MultiPolygon but you have geojson.feature.FeatureCollection.

Function shapely.geometry.shape can transform your geojson dictionary into a shapely geometry object.

1 Like