Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the bbox bug in ViewFinder #251

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pysheds [![Build Status](https://github.com/mdbartos/pysheds/actions/workflows/python-package.yml/badge.svg)](https://github.com/mdbartos/pysheds/actions) [![Coverage Status](https://coveralls.io/repos/github/mdbartos/pysheds/badge.svg?branch=master&service=github)](https://coveralls.io/github/mdbartos/pysheds?branch=master) [![Python Versions](https://img.shields.io/badge/python-3.7%7C3.8%7C3.9%7C3.10%7C3.11-blue.svg)](https://www.python.org/downloads/)
# pysheds [![Build Status](https://github.com/mdbartos/pysheds/actions/workflows/python-package.yml/badge.svg)](https://github.com/mdbartos/pysheds/actions) [![Coverage Status](https://coveralls.io/repos/github/mdbartos/pysheds/badge.svg?branch=master&service=github)](https://coveralls.io/github/mdbartos/pysheds?branch=master) [![Python Versions](https://img.shields.io/pypi/pyversions/pysheds)](https://pypi.python.org/pypi/pysheds)
🌎 Simple and fast watershed delineation in python.

## Documentation
Expand Down
2 changes: 1 addition & 1 deletion pysheds/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.3.5"
__version__ = "0.4"
27 changes: 22 additions & 5 deletions pysheds/sview.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name="pysheds",
version="0.3.5",
version="0.4",
description="🌎 Simple and fast watershed delineation in python.",
long_description="🌎 Simple and fast watershed delineation in python.",
long_description_content_type="text/x-rst",
Expand Down
11 changes: 11 additions & 0 deletions tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pyproj
import pytest
from pysheds.grid import Grid
from pysheds.view import ViewFinder


# Initialize parameters
Expand Down Expand Up @@ -557,3 +558,13 @@ def test_misc(d, grid):
dem = d.dem
l, r, t, b = grid._pop_rim(dem, nodata=0)
grid._replace_rim(dem, l, r, t, b)


def test_viewfinder_bbox():
from affine import Affine

affine = Affine(0.9641494421429059, 0.0, 503066.3552400001, 0.0, 0.986615463859816, 170296.54693900005)
shape = (23229, 14821)
viewfinder = ViewFinder(affine, shape)
expected = (503066.3552400001, 170296.54693900005, 517356.01412200014, 193214.63754899972)
assert viewfinder.bbox == expected