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

Ensure that pixel_crop produces correct size for lazy rasters #105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions telluric/georaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,8 +999,8 @@ def pixel_crop(self, bounds, xsize=None, ysize=None, window=None):
else:
window = window or rasterio.windows.Window(bounds[0],
bounds[1],
bounds[2] - bounds[0] + 1,
bounds[3] - bounds[1] + 1)
bounds[2] - bounds[0],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guydou proposed

max(bounds[2] - bounds[0], 1)

but it would be better to first discuss what are we trying to avoid.

bounds[3] - bounds[1])
return self.get_window(window, xsize=xsize, ysize=ysize)

def _crop(self, bounds, xsize=None, ysize=None):
Expand Down
14 changes: 10 additions & 4 deletions tests/test_merge_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


def black_and_white_raster(band_names=[], height=10, width=10, dtype=np.uint16,
crs=WEB_MERCATOR_CRS, affine=None):
crs=WEB_MERCATOR_CRS, affine=None, lazy=False):
if affine is None:
eps = 1e-100
affine = Affine.translation(10, 12) * Affine.scale(1, -1)
Expand All @@ -32,7 +32,12 @@ def black_and_white_raster(band_names=[], height=10, width=10, dtype=np.uint16,

image = np.ma.array(data=array, mask=mask)
raster = GeoRaster2(image=image, affine=affine, crs=crs, band_names=band_names)
return raster

if lazy:
raster.save('test_raster2.tif', overviews=False)
return GeoRaster2.open('test_raster2.tif')
else:
return raster


def test_merge_single_band_single_raster_returns_itself_for_all_strategies():
Expand Down Expand Up @@ -140,8 +145,9 @@ def test_crop_for_merging(main_r, cropping_r):
assert(rr.affine.almost_equals(cropping_r.affine))


def test_pixel_crop():
rr = black_and_white_raster([1, 2, 3], height=1000, width=1000)
@pytest.mark.parametrize("lazy", [False, True])
def test_pixel_crop(lazy):
rr = black_and_white_raster([1, 2, 3], height=1000, width=1000, lazy=lazy)
out = rr.pixel_crop((0, 0, 1000, 1000))
assert(rr == out)
out = rr.pixel_crop((0, 0, 100, 100), 100, 100, 1)
Expand Down