2

I have a GeoDataFrame which is a multipolygon and represents boundaries of a country:

0 MULTIPOLYGON (((10.10112 46.23346, 10.10099 46...

Its bounding box/extent is:

minx miny maxx maxy 5.956063 45.817059 10.495112 47.808483

I would like to extend the bounding box using a parameter let's say ext = 1.4 so it will become:

minx miny maxx maxy 8.338488 64.143881 14.693157 66.931876

I'm trying to extend it so when I display the raster, the layer is not cropped too tightly and I don't lose some of the information around it. When I try data.bounds *= 1.4, I get the following error: AttributeError: can't set attribute. It seems like I can't modify the DataFrame directly. What's the proper way of extending the bounding box of a GeoDataFrame?

1 Answer 1

3

Since you have a one-row geodataframe, using bounds is okay but ideally, you'd use total_bounds to get the bounds of the geodataframe as a whole. This returns a tuple of 4 coordinates (documentation). I recommend this approach:

  • Get the bounds by using gdf.total_bounds:
bounds = gdf.total_bounds
  • Use shapely (underlying objects that are used in a geodataframe)
from shapely.geometry import box
country_box = box(*bounds)

  • This country_box is now a shapely geometry that can now be modified.
buffered_box = country_box.buffer(1.4) # Be careful about the units you use. 1.4 in decimal degrees can be a lot!
  • You can then use this geometry towards creating another geodataframe/writing to a file/just look at the wkt (buffered_box.wkt for example)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.