Skip to main content

AttributeError: 'Polygon' object has no attribute 'total_bounds' when trying to get bbox for seperate polygons

  • April 26, 2024
  • 1 reply
  • 113 views

I’m tring to get bbox for polygon I have . The polygons are originaly in geodataframe and I change it into the sentinelhub geometry object.

The problem is that when I try to get the bbox for that polygon. I get error:

AttributeError: ‘Polygon’ object has no attribute ‘total_bounds’

This is how I have tried to do this:

#function to get the bbox- coordinates, bbox and size:
def get_bbox_from_shape(shape,resolution):
    minx, miny, maxx, maxy = shape.geometry.total_bounds
    bbox_coords_wgs84=[minx, miny, maxx, maxy]
    bbox = BBox(bbox=bbox_coords_wgs84, crs=CRS.WGS84)
    bbox_size = bbox_to_dimensions(bbox, resolution=resolution)
    return bbox_size,bbox,bbox_coords_wgs84

#get the bbox for each 
for name,shape in zip(shapes['name'],shapes['geometry']):
    shape=Geometry(shape,CRS.WGS84)
    bbox_size,bbox,bbox_coords_wgs84=get_bbox_from_shape(shape,10)

and that returns the attribute error.

I have used the Geoemtry in the past in order to get it as the correct object needed, so I have no idea why it doesn’t give me the bbox of the plot polygon.

My end goal is to be able to get bbox for each polygon in my geodataframe.

1 reply

Hi,

total_bounds is not an attribute of a sentinelhub geometry object, that is why you get the error

AttributeError: ‘Polygon’ object has no attribute ‘total_bounds

total_bounds will work with your geodataframe and return a list of [minx, miny, maxx, maxy] see example

My end goal is to be able to get bbox for each polygon in my geodataframe.

One alternative is: dont convert your geodataframe to sentinelhub geometry object. Just work with your geodataframe . So in your function:

def get_bbox_from_shape(shape,resolution):

shape parameter should be your gdf with the polygons. Sentinel hub bbox () function will automatically convert this list bbox_coords_wgs84=[minx, miny, maxx, maxy] to a sentinelhub.geometry.BBox object.

give it a try , let me know if this solves your problem

Best,