Skip to content

Commit

Permalink
Add crop/expand method to GeoBox
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill888 committed Aug 29, 2023
1 parent e294d0e commit 0bc9e6c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions odc/geo/geobox.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,17 @@ def pad_wh(self, alignx: int = 16, aligny: MaybeInt = None) -> "GeoBox":

return GeoBox((ny, nx), self._affine, self._crs)

def crop(self, shape: SomeShape) -> "GeoBox":
"""
Crop or expand to a given shape.
:returns: New :py:class:`~odc.geo.geobox.GeoBox` with a new shape,
top left pixel remains at the same location and scale.
"""
return GeoBox(shape, self._affine, self._crs)

expand = crop

def zoom_out(self, factor: float) -> "GeoBox":
"""
Compute :py:class:`~odc.geo.geobox.GeoBox` with changed resolution.
Expand Down
18 changes: 18 additions & 0 deletions tests/test_geobox.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,3 +601,21 @@ def test_qr2sample(n, with_edges):
assert (gbox.project(xx1) - gbox.extent).is_empty
assert (xx1 - gbox.project(gbox.extent)).is_empty
assert (xx2 - gbox.project(gbox.extent)).is_empty


_gbox = GeoBox((3, 17), mkA(scale=(10, -10), translation=(1000, -20)), epsg3857)


@pytest.mark.parametrize(
("gbox", "shape"),
[
(_gbox, (1, 1)),
(_gbox, (100, 200)),
(_gbox, (200, 100)),
],
)
def test_crop(gbox, shape):
assert gbox.crop(shape).shape == shape
assert gbox.crop(shape).crs == gbox.crs
assert gbox.crop(shape)[:1, :1] == gbox[:1, :1]
assert gbox.crop(shape) == gbox.expand(shape)

0 comments on commit 0bc9e6c

Please sign in to comment.