Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Set the CRS value for the Dataset/DataArray without modifying
the dataset/data array.
Parameters
----------
input_crs: object
Anything accepted by `rasterio.crs.CRS.from_user_input`.
inplace: bool, optional
If True, it will write to the existing dataset. Default is False.
Returns
-------
:obj:`xarray.Dataset` | :obj:`xarray.DataArray`:
Dataset with crs attribute.
"""
crs = CRS.from_wkt(crs_to_wkt(input_crs))
obj = self._get_obj(inplace=inplace)
obj.rio._crs = crs
return obj
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(
grid_mapping_name=grid_mapping_name, inplace=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,
)
clip_mask_xray = xarray.DataArray(
clip_mask_arr,
coords={
Parameters
----------
match_data_array: :obj:`xarray.DataArray` | :obj:`xarray.Dataset`
DataArray of the target resolution and projection.
resampling: Resampling method, optional
See rasterio.warp.reproject for more details.
Returns
--------
:obj:`xarray.DataArray`:
Contains the data from the src_data_array, reprojected to match
match_data_array.
"""
dst_crs = crs_to_wkt(match_data_array.rio.crs)
return self.reproject(
dst_crs,
transform=match_data_array.rio.transform(recalc=True),
shape=match_data_array.rio.shape,
resampling=resampling,
)