-
Notifications
You must be signed in to change notification settings - Fork 95
Generate spatial coords with rasterix.RasterIndex #855
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -121,23 +121,39 @@ def affine_to_coords( | |
|
|
||
| def _generate_spatial_coords( | ||
| *, affine: Affine, width: int, height: int | ||
| ) -> dict[Hashable, Any]: | ||
| ) -> xarray.Coordinates: | ||
| """get spatial coords in new transform""" | ||
| new_spatial_coords = affine_to_coords(affine, width, height) | ||
| if new_spatial_coords["x"].ndim == 1: | ||
| return { | ||
| "x": xarray.IndexVariable("x", new_spatial_coords["x"]), | ||
| "y": xarray.IndexVariable("y", new_spatial_coords["y"]), | ||
| } | ||
| return { | ||
| "xc": (("y", "x"), new_spatial_coords["x"]), | ||
| "yc": (("y", "x"), new_spatial_coords["y"]), | ||
| } | ||
|
|
||
| if get_option("use_raster_index"): | ||
| try: | ||
| from rasterix import RasterIndex | ||
| except (ImportError, ModuleNotFoundError) as err: | ||
| raise ImportError( | ||
| "The rasterix package is needed for generating " | ||
| "the spatial coordinates with a RasterIndex" | ||
| ) from err | ||
|
|
||
| raster_index = RasterIndex.from_transform(affine, width, height) | ||
| return xarray.Coordinates.from_xindex(raster_index) | ||
|
|
||
| else: | ||
| new_spatial_coords = affine_to_coords(affine, width, height) | ||
| if new_spatial_coords["x"].ndim == 1: | ||
| coords = { | ||
| "x": xarray.IndexVariable("x", new_spatial_coords["x"]), | ||
| "y": xarray.IndexVariable("y", new_spatial_coords["y"]), | ||
| } | ||
| else: | ||
| coords = { | ||
| "xc": (("y", "x"), new_spatial_coords["x"]), | ||
| "yc": (("y", "x"), new_spatial_coords["y"]), | ||
| } | ||
| return xarray.Coordinates(coords) | ||
|
|
||
|
|
||
| def _get_nonspatial_coords( | ||
| src_data_array: Union[xarray.DataArray, xarray.Dataset] | ||
| ) -> dict[Hashable, Union[xarray.Variable, xarray.IndexVariable]]: | ||
| ) -> xarray.Coordinates: | ||
| coords: dict[Hashable, Union[xarray.Variable, xarray.IndexVariable]] = {} | ||
| for coord in set(src_data_array.coords) - { | ||
| src_data_array.rio.x_dim, | ||
|
|
@@ -162,7 +178,7 @@ def _get_nonspatial_coords( | |
| src_data_array[coord].values, | ||
| src_data_array[coord].attrs, | ||
| ) | ||
| return coords | ||
| return xarray.Coordinates(coords) | ||
|
|
||
|
|
||
| def _make_coords( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be nice to have an accessor function to set this index even if I opened a Zarr file for example. Is there something like
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think that would be a good fit to add to a rasterix accessor? If you do that, you just set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It needs a bunch of extra info : x dim name, y dim name, transform, and so it's pretty convenient to just do all that with the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inside rasterix, it could look for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is an option but to me it feels a lot cleaner to add a method under the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hmm this wouldn't be ideal IMHO, assuming that the goal of the rasterix (and xproj) project is to provide reusable utilities to downstream packages like rioxarray, odc-geo, etc. even though technically looking for those accessors is possible without having cyclic dependencies. Under the same assumption, rasterix and xproj should also be flexible / not strongly opinionated about the names of the spatial coordinates and dimensions, etc. |
||
|
|
@@ -172,7 +188,7 @@ def _make_coords( | |
| dst_width: int, | ||
| dst_height: int, | ||
| force_generate: bool = False, | ||
| ) -> dict[Hashable, Any]: | ||
| ) -> xarray.Coordinates: | ||
| """Generate the coordinates of the new projected `xarray.DataArray`""" | ||
| coords = _get_nonspatial_coords(src_data_array) | ||
| if ( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should also delete the transform from the grid_mapping variable like so:
following https://github.com/dcherian/rasterix/blob/main/design_notes/raster_index.md#read-time and our discussions. This will trigger some more changes (from a quick search,
_cached_transformwill need to pull the Affine from RasterIndex)