-
Notifications
You must be signed in to change notification settings - Fork 7
feat: Support Zarr Conventions #68
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: main
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 | ||||
|---|---|---|---|---|---|---|
| @@ -1,6 +1,7 @@ | ||||||
| """Shared library utilities for rasterix.""" | ||||||
|
|
||||||
| import logging | ||||||
| from typing import NotRequired, TypedDict | ||||||
|
|
||||||
| from affine import Affine | ||||||
|
|
||||||
|
|
@@ -104,3 +105,70 @@ def affine_from_stac_proj_metadata(metadata: dict) -> Affine | None: | |||||
|
|
||||||
| a, b, c, d, e, f = transform[:6] | ||||||
| return Affine(a, b, c, d, e, f) | ||||||
|
|
||||||
|
|
||||||
| _ZarrConventionRegistration = TypedDict("_ZarrConventionRegistration", {"spatial:": str}) | ||||||
|
|
||||||
| _ZarrSpatialMetadata = TypedDict( | ||||||
| "_ZarrSpatialMetadata", | ||||||
| { | ||||||
| "zarr_conventions": NotRequired[list[_ZarrConventionRegistration | dict]], | ||||||
| "spatial:transform": NotRequired[list[float]], | ||||||
| "spatial:transform_type": NotRequired[str], | ||||||
| "spatial:registration": NotRequired[str], | ||||||
| }, | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| def _has_spatial_zarr_convention(metadata: _ZarrSpatialMetadata) -> bool: | ||||||
| zarr_conventions = metadata.get("zarr_conventions") | ||||||
| if not zarr_conventions: | ||||||
| return False | ||||||
| for entry in zarr_conventions: | ||||||
| if isinstance(entry, dict) and entry.get("name") == "spatial:": | ||||||
|
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.
Suggested change
If a convention specifies a uuid, it is meant to be the "primary" identifier. I'll check at the GeoZarr meeting tomorrow if implemented have been following this in practice
Collaborator
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. o lordy. that's unreadable! |
||||||
| return True | ||||||
| return False | ||||||
|
|
||||||
|
|
||||||
| def affine_from_spatial_zarr_convention(metadata: dict) -> Affine | None: | ||||||
| """Extract Affine transform from Zarr spatial convention metadata. | ||||||
|
|
||||||
| See https://github.com/zarr-conventions/spatial for the full specification. | ||||||
|
|
||||||
| Parameters | ||||||
| ---------- | ||||||
| metadata : dict | ||||||
| Dictionary containing Zarr spatial convention metadata. | ||||||
|
|
||||||
| Returns | ||||||
| ------- | ||||||
| Affine or None | ||||||
| Affine transformation matrix if minimal Zarr spatial metadata is found, None otherwise. | ||||||
|
|
||||||
| Examples | ||||||
| -------- | ||||||
| >>> ds: xr.Dataset = ... | ||||||
| >>> affine = affine_from_spatial_zarr_convention(ds.attrs) | ||||||
| """ | ||||||
| possibly_spatial_metadata: _ZarrSpatialMetadata = metadata # type: ignore[assignment] | ||||||
|
|
||||||
| if _has_spatial_zarr_convention(possibly_spatial_metadata): | ||||||
| if transform := possibly_spatial_metadata.get("spatial:transform"): | ||||||
| if len(transform) < 6: | ||||||
| raise ValueError(f"spatial:transform must have at least 6 elements, got {len(transform)}") | ||||||
|
|
||||||
| transform_type = possibly_spatial_metadata.get("spatial:transform_type", "affine") | ||||||
| if transform_type != "affine": | ||||||
| raise NotImplementedError( | ||||||
| f"Unsupported spatial:transform_type {transform_type!r}; only 'affine' is supported." | ||||||
| ) | ||||||
|
|
||||||
| registration = possibly_spatial_metadata.get("spatial:registration", "pixel") | ||||||
| if registration != "pixel": | ||||||
| raise NotImplementedError( | ||||||
| f"Unsupported spatial:registration {registration!r}; only 'pixel' is supported." | ||||||
| ) | ||||||
|
|
||||||
| return Affine(*map(float, transform[:6])) | ||||||
|
|
||||||
| return None | ||||||
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.
some fancy types there!