Skip to content
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

Add conversion between cf and shapely for lines #460

Merged
merged 14 commits into from
Dec 11, 2023

Conversation

jsignell
Copy link
Contributor

I started on this at the SciPy sprints with the idea of pushing forward the work described in this ticket: xarray-contrib/xvec#48. It took me a while to get the logic right 😅 so I wanted to make sure it was somewhere public in case I don't get a chance to come back to it in the near future.

@codecov
Copy link

codecov bot commented Jul 17, 2023

Codecov Report

Attention: 8 lines in your changes are missing coverage. Please review.

Comparison is base (2648109) 83.10% compared to head (bea680c) 83.26%.

Files Patch % Lines
cf_xarray/geometry.py 89.74% 7 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #460      +/-   ##
==========================================
+ Coverage   83.10%   83.26%   +0.15%     
==========================================
  Files          14       14              
  Lines        2676     2749      +73     
  Branches      189      199      +10     
==========================================
+ Hits         2224     2289      +65     
- Misses        402      409       +7     
- Partials       50       51       +1     
Flag Coverage Δ
mypy 42.37% <26.92%> (-0.50%) ⬇️
unittests 94.11% <100.00%> (+0.20%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jsignell jsignell marked this pull request as draft July 17, 2023 12:52
@dcherian
Copy link
Contributor

Thanks! This is great to see

cc @aulemahal

@jsignell
Copy link
Contributor Author

Just to make sure this is visible from this side as well: while working on this PR I was pointed to https://github.com/twhiteaker/CFGeom. I have opened an issue on that repo to see if the contributors over there have opinions about how these efforts relate to each other.

[0, 2, 4, 6, 0, 0, 1, 1.0, 2.0, 9.5], dims=("node",), attrs={"axis": "Y"}
),
part_node_count=xr.DataArray([4, 3, 3], dims=("index",)),
node_count=xr.DataArray([2, 2, 3, 3], dims=("counter",)),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This part caught me up for a bit. As I understand it, the node_count is the number of nodes per line segment. So each line segment in a multiline results in a value in this array. This means that the dims is not index and is not in the shapely version at all. I gave it a kind of random name counter which should probably be changed.

cf_xarray/geometry.py Outdated Show resolved Hide resolved
@dcherian
Copy link
Contributor

@jsignell Any interest in picking this back up?

@martinfleis
Copy link
Member

This could be significantly simplified by using shapely.to_ragged_arrays and from_ragged_arrays. That is used to create a GeoArrow arrays of coordinates, which are nearly the same as CF representation. Just one uses offsets to indicate where a new geom starts and the others counts of coords per geom, which should be an easy conversion.

@jsignell
Copy link
Contributor Author

The motivation for this work was to support IO in xvec, so I kind of stalled after @martinfleis came up with a better solution on that side. If there is still desire for this work then sure I can pick it back up.

@martinfleis
Copy link
Member

@jsignell if you mean the WKB option, there is a bit of disagreement if that is a good idea. If we can get conversion to CF geometries via ragged array IO shapely has, we can use practically the same for GeoArrow option and Zarr (if we decide that is what we want). I still like the elegance of WKB but understand that some folks don't like saving binary objects into Zarr.

@jsignell
Copy link
Contributor Author

Ah yeah I did mean the WKB option. In that case I can pick this back up. Or @dcherian feel free to take it over if you have some urgency around it.

@jsignell
Copy link
Contributor Author

jsignell commented Nov 3, 2023

Thanks for the tip @martinfleis! I looked into from_ragged_array and I agree that it is pretty similar. Actually the logic that is in this PR for creating linestrings is nearly identical: https://github.com/shapely/shapely/blob/9540b77a939ba35d066d618183e67fc905ed9ec4/shapely/_ragged_array.py#L334-L337

I think the complexity in this PR comes from how cf intermingles MULTILINESTRING and LINESTRING. So once you have the line segments you still need to figure out which of them are going to be MULTILINESTRING objects. That is basically what the entirety this section is doing: https://github.com/xarray-contrib/cf-xarray/pull/460/files#diff-6484364cf7fa3216828551b2531094b30d77cf393313226a01d425c22580688aR353-R370

That is probably not the best possible way to do that conversion but I don't see anything similar in the shapely.from_ragged_array implementation

@jsignell
Copy link
Contributor Author

jsignell commented Nov 6, 2023

Ok I added the shapely to CF direction for that one I found it pretty straightforward to use to_ragged_array so that one should read pretty cleanly! I'm going to try again now to see if I can simplify the other direction using from_ragged_array

@jsignell
Copy link
Contributor Author

jsignell commented Nov 6, 2023

Ok this is clearly a case of Monday brain > Friday brain.

@jsignell jsignell marked this pull request as ready for review November 6, 2023 20:56
@jsignell
Copy link
Contributor Author

I'm going to see what I can do to fix CI and improve coverage.

@dcherian
Copy link
Contributor

dcherian commented Dec 1, 2023

I can't really review this so I'm happy to merge when you're ready.

@martinfleis
Copy link
Member

I'll do the review, it is on my to-do list with an increasing priority :D.

@jsignell
Copy link
Contributor Author

jsignell commented Dec 1, 2023

Yeah it's finally green and fairly well tested so it is actually ready for review for the first time 😅

@dcherian dcherian merged commit 02b1d74 into xarray-contrib:main Dec 11, 2023
12 checks passed
@dcherian
Copy link
Contributor

Thanks @jsignell . We can address @martinfleis comments as they come in :)

@jsignell jsignell deleted the lines branch December 11, 2023 14:11
@jsignell
Copy link
Contributor Author

YOLO!

Copy link
Member

@martinfleis martinfleis left a comment

Choose a reason for hiding this comment

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

Approved (even if too late)! Looks good @jsignell!

I suppose that Polygons will look similar but we'll need to deal with holes as one layer on top of this.

@jsignell
Copy link
Contributor Author

I suppose that Polygons will look similar but we'll need to deal with holes as one layer on top of this.

Yeah I will probably start on that one now with all this fresh in my head.

Also I don't think I really considered z, so that might be something to look out for.

@dcherian
Copy link
Contributor

Thanks for taking a look @martinfleis 🙏

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.

3 participants