Skip to content

Refactor CRS-aware index #27

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

Merged
merged 7 commits into from
Jun 11, 2025
Merged

Refactor CRS-aware index #27

merged 7 commits into from
Jun 11, 2025

Conversation

benbovy
Copy link
Member

@benbovy benbovy commented Jun 6, 2025

I have copied here some CRS-specific code from xvec so it can be easily reused elsewhere.

@martinfleis -- to give you a bit more context, we are adding CRS-aware functionality to RasterIndex in https://github.com/dcherian/rasterix/pull/31. The functionality is very similar to the CRS functionality implemented in xvec.GeometryIndex, so instead of copying the CRS stuff into RasterIndex I thought it would be nice to have it implemented in a common place (xproj would nicely fit).

With this PR, we could refactor xvec.GeometryIndex like this:

class GeometryIndex(Index, xproj.ProjIndexMixin):

    @property
    def crs(self) -> pyproj.CRS | None:
        return self._crs

and internally reuse the CRS helper methods that are provided by xproj.ProjIndexMixin (i.e., move the maintenance of most CRS stuff to xproj). Another benefit is better integration between xvec and xproj.

Of course this would mean that xproj becomes a required dependency of xvec, but that's probably fine I guess? xproj only depends on Xarray and Pyproj...

If you agree with this, I'm happy to submit a PR to xvec after merging this PR and making a new release of xproj.

benbovy added 2 commits June 6, 2025 11:59
ProjIndexMixin class:

- replace the ``_proj_get_crs`` abstract method by the ``crs``
abstract (public) property.

- add ``_proj_crs_equals`` helper method

Xproj now identifies an Xarray index as CRS-aware if it has a ``crs``
property that returns either a ``pyproj.CRS`` object or ``None``. A
CRS-aware index may have an undefined CRS (``None``).

Add ``CRSAwareIndex`` protocol class.
Copied and adapted from xvec.
Comment on lines +34 to +35
# TODO: eventually we won't need to copy the xarray.Index interface here?
# (https://github.com/python/typing/issues/213)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately AFAIK there's no current way around copying the xarray Index interface. I didn't copied the whole Index interface, just the main functions to avoid maintenance hassle.

Copy link
Collaborator

@scottyhq scottyhq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of copying the CRS stuff into RasterIndex I thought it would be nice to have it implemented in a common place (xproj would nicely fit)

I think it'd be great to consolidate if possible!

@martinfleis
Copy link
Member

We already soft depend on xproj so having it as a hard dep makes it only easier.

@benbovy
Copy link
Member Author

benbovy commented Jun 10, 2025

If everyone is OK with this, I'm going to proceed like this:

  1. merge this PR
  2. make a quick release of xproj (0.2.0, there's a breaking change introduced here in the CRS-aware index interface, which is OK)
  3. refactor and update the PRs currently open in rasterix
  4. submit a new PR to xvec

@dcherian
Copy link
Contributor

Happy to defer to your judgement, @benbovy !

@benbovy benbovy merged commit 49d61b3 into main Jun 11, 2025
12 checks passed
@benbovy benbovy deleted the refactor-crs-aware-index branch June 11, 2025 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants