Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_clip_box__one_dimension_error(modis_clip):
with modis_clip["open"](modis_clip["input"]) as xdi:
var_match = ""
if hasattr(xdi, "name") and xdi.name:
var_match = " Data variable: __xarray_dataarray_variable__"
# test exception after raster clipped
with pytest.raises(
OneDimensionalRaster,
match=(
"At least one of the clipped raster x,y coordinates has "
f"only one point.{var_match}"
),
):
xdi.rio.clip_box(
minx=-7272735.53951584, # xdi.x[5].values
miny=5048834.500182299, # xdi.y[5].values
maxx=-7272735.53951584, # xdi.x[5].values
maxy=5048834.500182299, # xdi.y[5].values
)
# test exception before raster clipped
with pytest.raises(OneDimensionalRaster):
xdi.isel(x=slice(5, 6), y=slice(5, 6)).rio.clip_box(
minx=-7272735.53951584, # xdi.x[5].values
miny=5048371.187465771, # xdi.y[7].values
# test exception after raster clipped
with pytest.raises(
OneDimensionalRaster,
match=(
"At least one of the clipped raster x,y coordinates has "
f"only one point.{var_match}"
),
):
xdi.rio.clip_box(
minx=-7272735.53951584, # xdi.x[5].values
miny=5048834.500182299, # xdi.y[5].values
maxx=-7272735.53951584, # xdi.x[5].values
maxy=5048834.500182299, # xdi.y[5].values
)
# test exception before raster clipped
with pytest.raises(OneDimensionalRaster):
xdi.isel(x=slice(5, 6), y=slice(5, 6)).rio.clip_box(
minx=-7272735.53951584, # xdi.x[5].values
miny=5048371.187465771, # xdi.y[7].values
maxx=-7272272.226799311, # xdi.x[7].values
maxy=5048834.500182299, # xdi.y[5].values
)
resolution_y = transform.e
return resolution_x, resolution_y
# if the coordinates of the spatial dimensions are missing
# use the cached transform resolution
try:
left, bottom, right, top = self._internal_bounds()
except DimensionMissingCoordinateError:
if transform is None:
raise
resolution_x = transform.a
resolution_y = transform.e
return resolution_x, resolution_y
if self.width == 1 or self.height == 1:
raise OneDimensionalRaster(
"Only 1 dimenional array found. Cannot calculate the resolution."
f"{_get_data_var_message(self._obj)}"
)
resolution_x = (right - left) / (self.width - 1)
resolution_y = (bottom - top) / (self.height - 1)
return resolution_x, resolution_y
Maximum bound for x coordinate.
maxy: float
Maximum bound for y coordinate.
auto_expand: bool
If True, it will expand clip search if only 1D raster found with clip.
auto_expand_limit: int
maximum number of times the clip will be retried before raising
an exception.
Returns
-------
:obj:`xarray.DataArray`:
The clipped object.
"""
if self.width == 1 or self.height == 1:
raise OneDimensionalRaster(
"At least one of the raster x,y coordinates has only one point."
f"{_get_data_var_message(self._obj)}"
)
# make sure that if the coordinates are
# in reverse order that it still works
resolution_x, resolution_y = self.resolution()
if resolution_y < 0:
top = maxy
bottom = miny
else:
top = miny
bottom = maxy
if resolution_x < 0:
left = maxx
right = minx