Skip to content

Make RasterIndex CRS-aware#31

Merged
dcherian merged 16 commits intoxarray-contrib:mainfrom
benbovy:add-raster-index-crs-property
Jun 30, 2025
Merged

Make RasterIndex CRS-aware#31
dcherian merged 16 commits intoxarray-contrib:mainfrom
benbovy:add-raster-index-crs-property

Conversation

@benbovy
Copy link
Copy Markdown
Member

@benbovy benbovy commented Jun 4, 2025

As a preliminary to #29.

I mostly copied the CRS-handling logic from xvec.GeometryIndex (although instead of doing this, I'm thinking of factoring it out into xproj.ProjIndexMixin and suggest that both xvec.GeometryIndex and rasterix.RasterIndex explicitly inherit from that mixin class so we can maintain that CRS-handling logic in one common place).

Also added a few tests (RasterIndex still needs more tests, in a follow-up PR).

@benbovy benbovy requested review from dcherian and scottyhq June 4, 2025 13:09
Comment thread src/rasterix/raster_index.py Outdated
Comment thread src/rasterix/raster_index.py Outdated
Comment thread src/rasterix/raster_index.py Outdated
Comment thread tests/test_raster_index.py
Comment thread tests/test_raster_index.py Outdated
Copy link
Copy Markdown
Contributor

@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.

Thanks @benbovy! Looks great, especially appreciate all the tests to better understand how things are fitting together.

Comment thread src/rasterix/raster_index.py Outdated
@benbovy
Copy link
Copy Markdown
Member Author

benbovy commented Jun 11, 2025

I integrated xproj (v0.2.0) here in the lasts commits. This should be ready I think.

@scottyhq this PR doesn't fully supersede your PR #29, which adds some valuable instructions in README as well as a nice example of integration with xproj. Would you mind updating your PR (I'm also willing to take over on it) after this one is merged?

Comment thread .github/workflows/test.yml Outdated
@dcherian dcherian requested a review from scottyhq June 12, 2025 03:19
Comment thread src/rasterix/raster_index.py
@dcherian
Copy link
Copy Markdown
Contributor

Seems nice to me. Can we add some magic to assign_index to assign a CRS when available?

@scottyhq
Copy link
Copy Markdown
Contributor

@scottyhq this PR doesn't fully supersede your PR https://github.com/dcherian/rasterix/pull/29, which adds some valuable instructions in README as well as a nice example of integration with xproj. Would you mind updating your PR (I'm also willing to take over on it) after this one is merged?

I probably won't get to it next week, so if you'd like feel free to take it over! Otherwise leave it open and I'll adjust it later

)

def join(self, other: RasterIndex, how: JoinOptions = "inner") -> RasterIndex:
if not self._proj_crs_equals(cast(CRSAwareIndex, other), allow_none=True):
Copy link
Copy Markdown
Contributor

@dcherian dcherian Jun 24, 2025

Choose a reason for hiding this comment

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

confused here, shouldn't alignment check the spatial_ref index and error there too. Why does RasterIndex need the check? Is it because the order in which alignment is checked is unpredictable?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is needed for the case where RasterIndex has a CRS defined but the dataset / dataarray has no spatial_ref coordinate with xproj.CRSIndex.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

do we need to support that case. I was assuming we could just defer to xproj for all that.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Unfortunately we cannot easily detect within the raster index here whether or not there is a related CRSIndex that will be used in the alignment. There's also the possibility that the internal CRS of the raster index is not in-sync with the CRSIndex. By design, xproj.CRSIndex and CRS-aware indexes (like RasterIndex) are not tightly coupled (I haven't found a way to do it nicely).

I guess the big question is: do we need to add a CRS property to RasterIndex at all? We add it here because at some point it will be useful for .sel, etc.?

Copy link
Copy Markdown
Contributor

@dcherian dcherian Jun 27, 2025

Choose a reason for hiding this comment

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

I guess the big question is: do we need to add a CRS property to RasterIndex at all? We add it here because at some point it will be useful for .sel, etc.?

Yeah I agree. My inclination is that we are going to bundle in CRS of some sort, then we may as wrap the Xproj index. So perhaps RasterIndex should stay ignorant of CRS and handle only the coordinate transform pieces.

We can handle the CRS based sel, either as an accessor function that knows about both XProjIndex & RasterIndex; or a new Index that composes the two. SInce they are orthogonal in concept, I'm not sure there's much value to a composed index.

What does XVec use the CRS for? Remapping the geometries passed in .xvec.query?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We don't use CRS much in Xvec. I have implemented as we needed to store it but it kind of hangs around for now. There's certainly scope to make more use of it though.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There's certainly scope to make more use of it though.

Just curious, how would you use it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I have the feeling that we could lost ourselves again in long discussions without making real progress, though.

Yeah I agree. I responded here . But I think we should move forward with this (#22 and #26 are good-enough reasons for me). We should document these choices, and release something that can be experimented with.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just curious, how would you use it?

Depends how much work do we want to do for a user under the hood. If we follow geopandas model, we should use it to check if the CRS of two objects that are being used together (e.g. zonal stats, possibly indexing) match and raise otherwise. If we want to do one more step, we can automatically re-project when they do not align, do the zonal stats using the transformed geom and use the original geom as the resulting index.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I added some discussion of these points to #41

@benbovy benbovy mentioned this pull request Jun 30, 2025
@benbovy
Copy link
Copy Markdown
Member Author

benbovy commented Jun 30, 2025

I just copied the notebook example from #29 here. Now we hit xarray-contrib/xproj#33 but it can be easily fixed. At least trying to align matching transforms but non-matching CRSs still raises an error.

perhaps RasterIndex should stay ignorant of CRS and handle only the coordinate transform pieces.

Although this PR adds a CRS property to RasterIndex, it is opt-in so by default RasterIndex is ignorant of CRS. I suggest to leave it as-is (so that RasterIndex is nicely integrated with the latest version of Xproj) but not advertise too much that RasterIndex has its own CRS property (i.e., in the example notebook we just set an xproj.CRSIndex to the spatial_ref coordinate and check that it is used in CRS-aware alignment... nothing specific to rasterix but good to showcase here as well).

This should be ready then.

@dcherian
Copy link
Copy Markdown
Contributor

Let's merge and iterate.

@dcherian dcherian merged commit 440da19 into xarray-contrib:main Jun 30, 2025
6 checks passed
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