From a761d6eab3dc98d61075d49789d3e252bff8fb88 Mon Sep 17 00:00:00 2001 From: Taher Chegini Date: Sun, 14 Apr 2024 15:37:26 -0400 Subject: [PATCH] BUG: Fix the issue with getting the bounds wrong when affine has no rotation or the resolution is negative. Fixes mdbartos/pysheds#250 --- pysheds/sview.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/pysheds/sview.py b/pysheds/sview.py index 2b410c4..63b05f8 100644 --- a/pysheds/sview.py +++ b/pysheds/sview.py @@ -417,11 +417,28 @@ def size(self): @property def bbox(self): - shape = self.shape - xmin, ymax = View.affine_transform(self.affine, 0, 0) - xmax, ymin = View.affine_transform(self.affine, shape[-1], shape[-2]) - _bbox = (xmin, ymin, xmax, ymax) - return _bbox + has_rotation = self.affine.b == self.affine.d != 0 + if has_rotation: + resolution_x = np.sqrt(self.affine.a**2 + self.affine.d**2) + resolution_y = np.sqrt(self.affine.b**2 + self.affine.e**2) + else: + resolution_x, resolution_y = self.affine.a, self.affine.e + + left = self.affine.c + top = self.affine.f + right = left + resolution_x * self.shape[-1] + bottom = top + resolution_y * self.shape[-2] + + if resolution_y < 0: + miny, maxy = bottom, top + else: + miny, maxy = top, bottom + + if resolution_x < 0: + minx, maxx = right, left + else: + minx, maxx = left, right + return (minx, miny, maxx, maxy) @property def extent(self):