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__nodata_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__"
with pytest.raises(
NoDataInBounds, match=var_match,
):
xdi.rio.clip_box(
minx=-8272735.53951584, # xdi.x[5].values
miny=8048371.187465771, # xdi.y[7].values
maxx=-8272967.195874103, # xdi.x[4].values
maxy=8048834.500182299, # xdi.y[5].values
)
def _internal_bounds(self):
"""Determine the internal bounds of the `xarray.DataArray`"""
if self.x_dim not in self._obj.coords:
raise DimensionMissingCoordinateError(f"{self.x_dim} missing coordinates.")
elif self.y_dim not in self._obj.coords:
raise DimensionMissingCoordinateError(f"{self.y_dim} missing coordinates.")
try:
left = float(self._obj[self.x_dim][0])
right = float(self._obj[self.x_dim][-1])
top = float(self._obj[self.y_dim][0])
bottom = float(self._obj[self.y_dim][-1])
except IndexError:
raise NoDataInBounds(
"Unable to determine bounds from coordinates."
f"{_get_data_var_message(self._obj)}"
)
return left, bottom, right, top
cl_array = self.isel_window(window)
# check that the window has data in it
if cl_array.rio.width <= 1 or cl_array.rio.height <= 1:
if auto_expand and auto_expand < auto_expand_limit:
resolution_x, resolution_y = self.resolution()
return self.clip_box(
minx=minx - abs(resolution_x) / 2.0,
miny=miny - abs(resolution_y) / 2.0,
maxx=maxx + abs(resolution_x) / 2.0,
maxy=maxy + abs(resolution_y) / 2.0,
auto_expand=int(auto_expand) + 1,
auto_expand_limit=auto_expand_limit,
)
if cl_array.rio.width < 1 or cl_array.rio.height < 1:
raise NoDataInBounds(
f"No data found in bounds.{_get_data_var_message(self._obj)}"
)
elif cl_array.rio.width == 1 or cl_array.rio.height == 1:
raise OneDimensionalRaster(
"At least one of the clipped raster x,y coordinates"
" has only one point."
f"{_get_data_var_message(self._obj)}"
)
# make sure correct attributes preserved & projection added
_add_attrs_proj(cl_array, self._obj)
return cl_array
)
cropped_ds = cropped_ds.rio.isel_window(
rasterio.windows.get_data_window(
np.ma.masked_array(clip_mask_arr, ~clip_mask_arr)
)
)
if self.nodata is not None and not np.isnan(self.nodata):
cropped_ds = cropped_ds.fillna(self.nodata)
cropped_ds = cropped_ds.astype(self._obj.dtype)
if (
cropped_ds.coords[self.x_dim].size < 1
or cropped_ds.coords[self.y_dim].size < 1
):
raise NoDataInBounds(
f"No data found in bounds.{_get_data_var_message(self._obj)}"
)
# make sure correct attributes preserved & projection added
_add_attrs_proj(cropped_ds, self._obj)
return cropped_ds