mirror of
https://github.com/rasterio/rasterio.git
synced 2025-12-08 17:36:12 +00:00
Raise WindowError from geometry_window
This commit is contained in:
parent
33232130be
commit
c54e22da24
@ -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
|
||||
@ -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
|
||||
|
||||
@ -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):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user