Skip to content

Commit e58a80f

Browse files
committed
added option to buffer BBox
1 parent 098435a commit e58a80f

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Diff for: sentinelhub/geometry.py

+16
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,22 @@ def transform(self, crs):
239239
return BBox((transform_point(self.lower_left, self.crs, new_crs),
240240
transform_point(self.upper_right, self.crs, new_crs)), crs=new_crs)
241241

242+
def buffer(self, buffer):
243+
""" Changes both BBox dimensions (width and height) by a percentage of size of each dimension. If number is
244+
negative, the size will decrease. Returns a new instance of BBox object.
245+
246+
:param buffer: A percentage
247+
:type buffer: float
248+
:return: A new bounding box of buffered size
249+
:rtype: BBox
250+
"""
251+
if buffer < -1:
252+
raise ValueError('Cannot reduce the bounding box to nothing, buffer must be >= -1.0')
253+
ratio = 1 + buffer
254+
mid_x, mid_y = self.middle
255+
return BBox((mid_x - (mid_x - self.min_x) * ratio, mid_y - (mid_y - self.min_y) * ratio,
256+
mid_x + (self.max_x - mid_x) * ratio, mid_y + (self.max_y - mid_y) * ratio), self.crs)
257+
242258
def get_polygon(self, reverse=False):
243259
""" Returns a tuple of coordinates of 5 points describing a polygon. Points are listed in clockwise order, first
244260
point is the same as the last.

Diff for: tests/test_geometry.py

+6
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ def test_geometry(self):
131131
"Expected type {}, got type {}".format(shapely.geometry.Polygon,
132132
type(bbox.get_geometry())))
133133

134+
def test_buffer(self):
135+
bbox = BBox([46.07, 13.23, 46.24, 13.57], CRS.WGS84)
136+
137+
self.assertEqual(bbox, bbox.buffer(0), "Buffer 1 should not change bounding box")
138+
self.assertEqual(bbox, bbox.buffer(1).buffer(-0.5), "Twice buffered bounding box should return to original")
139+
134140

135141
class TestGeometry(TestSentinelHub):
136142

0 commit comments

Comments
 (0)