From c54e22da2451cefb0e01acb3c6db91cdfafc70dd Mon Sep 17 00:00:00 2001 From: Brendan Ward Date: Tue, 19 Sep 2017 19:34:21 -0700 Subject: [PATCH] Raise WindowError from geometry_window --- rasterio/features.py | 13 +++---------- rasterio/mask.py | 16 +++++++++------- tests/test_features.py | 8 ++++---- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/rasterio/features.py b/rasterio/features.py index 0a6888d3..97652c2d 100644 --- a/rasterio/features.py +++ b/rasterio/features.py @@ -2,14 +2,12 @@ import logging -import warnings import numpy as np from rasterio._features import _shapes, _sieve, _rasterize, _bounds from rasterio.dtypes import validate_dtype, can_cast_dtype, get_minimum_dtype from rasterio.env import ensure_env -from rasterio.errors import WindowError from rasterio.transform import IDENTITY, guard_transform from rasterio.windows import Window @@ -315,8 +313,7 @@ def geometry_window(raster, shapes, pad_x=0, pad_y=0, north_up=True, geometry plus optional padding. The window is the outermost pixel indices that contain the geometry (floor of offsets, ceiling of width and height). - If shapes do not overlap raster, a warning is raised and an empty window - `Window(0, 0, 0, 0)` is returned. + If shapes do not overlap raster, a WindowError is raised. Parameters ---------- @@ -367,12 +364,8 @@ def geometry_window(raster, shapes, pad_x=0, pad_y=0, north_up=True, # Make sure that window overlaps raster raster_window = Window(0, 0, raster.height, raster.width) - try: - window = window.intersection(raster_window) - except WindowError: - window = Window(0, 0, 0, 0) - warnings.warn("shapes are outside bounds of raster. " - "Are they in different coordinate reference systems?") + # This will raise a WindowError if windows do not overlap + window = window.intersection(raster_window) return window \ No newline at end of file diff --git a/rasterio/mask.py b/rasterio/mask.py index 44a3c3bf..b3c5e448 100644 --- a/rasterio/mask.py +++ b/rasterio/mask.py @@ -5,6 +5,7 @@ import warnings import numpy as np +from rasterio.errors import WindowError from rasterio.features import geometry_mask, geometry_window @@ -19,7 +20,7 @@ def raster_geometry_mask(raster, shapes, all_touched=False, invert=False, By default, mask is intended for use as a numpy mask, where pixels that overlap shapes are False. - If shapes do not overlap the raster and crop=True, an exception is + If shapes do not overlap the raster and crop=True, a ValueError is raised. Otherwise, a warning is raised, and a completely True mask is returned (if invert is False). @@ -74,12 +75,13 @@ def raster_geometry_mask(raster, shapes, all_touched=False, invert=False, north_up = raster.transform.e <= 0 - window = geometry_window(raster, shapes, north_up=north_up, pad_x=pad_x, - pad_y=pad_y) + try: + window = geometry_window(raster, shapes, north_up=north_up, pad_x=pad_x, + pad_y=pad_y) - # If shapes do not overlap raster, raise Exception or UserWarning - # depending on value of crop - if window.flatten() == (0, 0, 0, 0): + except WindowError: + # If shapes do not overlap raster, raise Exception or UserWarning + # depending on value of crop if crop: raise ValueError('Input shapes do not overlap raster.') else: @@ -111,7 +113,7 @@ def mask(raster, shapes, all_touched=False, invert=False, nodata=None, Pixels are masked or set to nodata outside the input shapes, unless `invert` is `True`. - If shapes do not overlap the raster and crop=True, an exception is + If shapes do not overlap the raster and crop=True, a ValueError is raised. Otherwise, a warning is raised. Parameters diff --git a/tests/test_features.py b/tests/test_features.py index 0f23a907..092e6a32 100644 --- a/tests/test_features.py +++ b/tests/test_features.py @@ -6,6 +6,7 @@ import pytest from affine import Affine import rasterio +from rasterio.errors import WindowError from rasterio.features import ( bounds, geometry_mask, geometry_window, rasterize, sieve, shapes) @@ -153,13 +154,12 @@ def test_geometry_large_shapes(basic_image_file): def test_geometry_no_overlap(path_rgb_byte_tif, basic_geometry): + """Geometries that do not overlap raster raises WindowError""" + with rasterio.open(path_rgb_byte_tif) as src: - with pytest.warns(UserWarning) as warning: + with pytest.raises(WindowError): window = geometry_window(src, [basic_geometry], north_up=False) - assert 'outside bounds of raster' in warning[0].message.args[0] - - assert window.flatten() == (0, 0, 0, 0) def test_rasterize(basic_geometry, basic_image_2x2):