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_crs_writer__missing():
test_da = xarray.DataArray(
numpy.zeros((5, 5)),
dims=("y", "x"),
coords={"y": numpy.arange(1, 6), "x": numpy.arange(2, 7)},
)
with pytest.raises(MissingCRS):
test_da.rio.write_crs()
with pytest.raises(MissingCRS):
test_da.to_dataset(name="test").rio.write_crs()
def test_crs_writer__missing():
test_da = xarray.DataArray(
numpy.zeros((5, 5)),
dims=("y", "x"),
coords={"y": numpy.arange(1, 6), "x": numpy.arange(2, 7)},
)
with pytest.raises(MissingCRS):
test_da.rio.write_crs()
with pytest.raises(MissingCRS):
test_da.to_dataset(name="test").rio.write_crs()
else:
data_obj = self._get_obj(inplace=inplace)
# get original transform
transform = self._cached_transform()
# remove old grid maping coordinate if exists
grid_mapping_name = (
self.grid_mapping if grid_mapping_name is None else grid_mapping_name
)
try:
del data_obj.coords[grid_mapping_name]
except KeyError:
pass
if data_obj.rio.crs is None:
raise MissingCRS(
"CRS not found. Please set the CRS with 'set_crs()' or 'write_crs()'."
)
# add grid mapping coordinate
data_obj.coords[grid_mapping_name] = xarray.Variable((), 0)
grid_map_attrs = pyproj.CRS.from_user_input(data_obj.rio.crs).to_cf()
# spatial_ref is for compatibility with GDAL
crs_wkt = crs_to_wkt(data_obj.rio.crs)
grid_map_attrs["spatial_ref"] = crs_wkt
grid_map_attrs["crs_wkt"] = crs_wkt
if transform is not None:
grid_map_attrs["GeoTransform"] = " ".join(
[str(item) for item in transform.to_gdal()]
)
data_obj.coords[grid_mapping_name].rio.set_attrs(grid_map_attrs, inplace=True)
return data_obj.rio.write_grid_mapping(
together with resolution.
transform: optional
The destination transform.
resampling: Resampling method, optional
See rasterio.warp.reproject for more details.
Returns
-------
:obj:`xarray.DataArray`:
The reprojected DataArray.
"""
if resolution is not None and (shape is not None or transform is not None):
raise RioXarrayError("resolution cannot be used with shape or transform.")
if self.crs is None:
raise MissingCRS(
"CRS not found. Please set the CRS with 'set_crs()' or 'write_crs()'."
f"{_get_data_var_message(self._obj)}"
)
src_affine = self.transform(recalc=True)
if transform is None:
dst_affine, dst_width, dst_height = _make_dst_affine(
self._obj, self.crs, dst_crs, resolution, shape
)
else:
dst_affine = transform
if shape is not None:
dst_height, dst_width = shape
else:
dst_height, dst_width = self.shape
extra_dim = self._check_dimensions()
drop: bool, optional
If True, drop the data outside of the extent of the mask geoemtries
Otherwise, it will return the same raster with the data masked.
Default is True.
invert: boolean, optional
If False, pixels that do not overlap shapes will be set as nodata.
Otherwise, pixels that overlap the shapes will be set as nodata.
False by default.
Returns
-------
:obj:`xarray.DataArray`:
The clipped object.
"""
if self.crs is None:
raise MissingCRS(
"CRS not found. Please set the CRS with 'set_crs()' or 'write_crs()'."
f"{_get_data_var_message(self._obj)}"
)
crs = CRS.from_wkt(crs_to_wkt(crs)) if crs is not None else self.crs
if self.crs != crs:
geometries = [
rasterio.warp.transform_geom(crs, self.crs, geometry)
for geometry in geometries
]
clip_mask_arr = geometry_mask(
geometries=geometries,
out_shape=(int(self.height), int(self.width)),
transform=self.transform(recalc=True),
invert=not invert,
all_touched=all_touched,