-
Notifications
You must be signed in to change notification settings - Fork 42
Add docs about converting between shapely and cf #512
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eec817b
Add docs about converting between shapely and cf
jsignell d3dab28
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f2cace1
Add shapely to docs env
jsignell b3c45f4
Apply suggestions from code review
jsignell 792def4
Update doc/geometry.md
dcherian eb6ec18
Update doc/geometry.md
dcherian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ dependencies: | |
- pooch | ||
- pint | ||
- regex | ||
- shapely | ||
- furo | ||
- myst-nb | ||
- pip: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,99 @@ | ||
--- | ||
jupytext: | ||
text_representation: | ||
format_name: myst | ||
kernelspec: | ||
display_name: Python 3 | ||
name: python3 | ||
--- | ||
|
||
```{eval-rst} | ||
.. currentmodule:: xarray | ||
``` | ||
|
||
# Geometries | ||
|
||
See {py:func}`cf_xarray.shapely_to_cf`, {py:func}`cf_xarray.cf_to_shapely` | ||
```{seealso} | ||
1. [The CF conventions on Geometries](http://cfconventions.org/Data/cf-conventions/cf-conventions-1.11/cf-conventions.html#geometries) | ||
1. {py:func}`cf_xarray.shapely_to_cf` | ||
1. {py:func}`cf_xarray.cf_to_shapely` | ||
``` | ||
|
||
`cf_xarray` can convert between vector geometries represented as shapely objects | ||
and CF-compliant array representations of those geometries. | ||
|
||
Let's start by creating an xarray object containing some shapely geometries. This example uses | ||
a `xr.DataArray` but these functions also work with a `xr.Dataset` where one of the data variables | ||
contains an array of shapes. | ||
|
||
```{code-cell} | ||
import cf_xarray as cfxr | ||
import xarray as xr | ||
|
||
from shapely.geometry import MultiPoint, Point | ||
|
||
da = xr.DataArray( | ||
[ | ||
MultiPoint([(1.0, 2.0), (2.0, 3.0)]), | ||
Point(3.0, 4.0), | ||
Point(4.0, 5.0), | ||
Point(3.0, 4.0), | ||
], | ||
dims=("index",), | ||
name="geometry" | ||
) | ||
``` | ||
|
||
```{warning} | ||
`cf_xarray` does not support handle multiple types of shapes (Point, Line, Polygon) in one | ||
`xr.DataArray`, but multipart geometries are supported and can be mixed with single-part | ||
geometries of the same type. | ||
``` | ||
|
||
Now we can take that `xr.DataArray` containing shapely geometries and convert it to cf: | ||
|
||
```{code-cell} | ||
ds_cf = cfxr.shapely_to_cf(da) | ||
ds_cf | ||
``` | ||
|
||
This function returns a `xr.Dataset` containing the CF fields needed to reconstruct the | ||
geometries. In particular there are: | ||
|
||
- `'x'`, `'y'` : the node coordinates | ||
- `'crd_x'`, `'crd_y'` : the feature coordinates (might have different names if `grid_mapping` is available). | ||
- `'node_count'` : The number of nodes per feature. Always present for Lines and Polygons. For | ||
Points: only present if there are multipart geometries. | ||
- `'part_node_count'` : The number of nodes per individual geometry. Only for Lines with multipart | ||
geometries and for Polygons with multipart geometries or holes. | ||
- `'interior_ring'` : Integer boolean indicating whether ring is interior or exterior. Only for | ||
Polygons with holes. | ||
- `'geometry_container`' : Empty variable with attributes describing the geometry type. | ||
|
||
Here are the attributes on `geometry_container`. This pattern mimics the convention of | ||
specifying spatial reference information in the attrs of the empty array `spatial_ref`. | ||
|
||
```{code-cell} | ||
ds_cf.geometry_container.attrs | ||
``` | ||
|
||
```{note} | ||
Z axis is not yet supported for any shapes. | ||
``` | ||
|
||
This `xr.Dataset` can be converted back into a `xr.DataArray` of shapely geometries: | ||
|
||
```{code-cell} | ||
cfxr.cf_to_shapely(ds_cf) | ||
``` | ||
|
||
This conversion adds coordinates that aren't in the `xr.DataArray` that we started with. | ||
By default these are called `crd_x` and `crd_y` unless `grid_mapping` is specified. | ||
|
||
## Gotchas | ||
|
||
For MultiPolygons with holes the CF notation is slightly ambiguous on which hole is associated | ||
with which polygon. This is problematic because shapely stores holes within the polygon | ||
object that they are associated with. `cf_xarray` assumes that the the shapes are interleaved | ||
such that the holes (interior rings) are associated with the exteriors (exterior rings) that | ||
immediately precede them. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.