Skip to content

Commit 735f80c

Browse files
dcherianmartinfleispre-commit-ci[bot]
authored
Add xvec (#6)
Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 3348a73 commit 735f80c

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

docs/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@
111111
"numpy": ("https://numpy.org/doc/stable", None),
112112
"xarray": ("https://docs.xarray.dev/en/stable/", None),
113113
"rasterix": ("https://rasterix.readthedocs.io/en/latest/", None),
114+
"shapely": ("https://shapely.readthedocs.io/en/latest/", None),
114115
"xvec": ("https://xvec.readthedocs.io/en/stable/", None),
116+
"geopandas": ("https://geopandas.readthedocs.io/en/stable/", None),
115117
"pint-xarray": ("https://pint-xarray.readthedocs.io/en/latest/", None),
116118
"pint": ("https://pint.readthedocs.io/en/stable/", None),
117119
}

docs/earth/xvec.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,107 @@
1+
---
2+
jupytext:
3+
text_representation:
4+
format_name: myst
5+
kernelspec:
6+
display_name: Python 3
7+
name: python
8+
---
9+
110
# xvec: GeometryIndex
11+
12+
````{grid} 12
13+
```{grid-item}
14+
:columns: 4
15+
```{image} https://xvec.readthedocs.io/en/stable/_images/logo.svg
16+
---
17+
alt: Alt text
18+
width: 600px
19+
align: center
20+
---
21+
```
22+
```{grid-item}
23+
:columns: 8
24+
```{seealso}
25+
Learn more at the [xvec](https://xvec.readthedocs.io) documentation.
26+
```
27+
````
28+
29+
## Highlights
30+
31+
```{margin}
32+
A more general definition is "a data cube that contains geometries as variables (e.g. moving features or time-evolving shapes)".
33+
```
34+
35+
Xvec's use of custom indexes is exciting because it illustrates how a new Index can help define a new data model --- _vector data cube_: "an n-D array that has either at least one dimension indexed by a 2-D array of vector geometries".
36+
37+
1. Indexing using geometries and associated predicates is supported using `.sel`
38+
1. A new `.xvec` accessor exposes additional querying functionality.
39+
1. Exposes complex functionality from other full-featured packages (e.g. shapely) to Xarray.
40+
41+
## Example
42+
43+
First we create a data cube with geometries
44+
45+
```{code-cell}
46+
---
47+
tags: [hide-input]
48+
---
49+
import geopandas as gpd
50+
from geodatasets import get_path
51+
import shapely
52+
import xarray as xr
53+
import xvec
54+
55+
xr.set_options(display_expand_indexes=True, display_expand_data=False, display_expand_attrs=False)
56+
57+
counties = gpd.read_file(get_path("geoda.natregimes"))
58+
59+
cube = xr.Dataset(
60+
data_vars=dict(
61+
population=(["county", "year"], counties[["PO60", "PO70", "PO80", "PO90"]]),
62+
unemployment=(["county", "year"], counties[["UE60", "UE70", "UE80", "UE90"]]),
63+
divorce=(["county", "year"], counties[["DV60", "DV70", "DV80", "DV90"]]),
64+
age=(["county", "year"], counties[["MA60", "MA70", "MA80", "MA90"]]),
65+
),
66+
coords=dict(county=counties.geometry, year=[1960, 1970, 1980, 1990]),
67+
)
68+
cube
69+
```
70+
71+
Note how the `county` dimension is associated with a {py:class}`geopandas.GeometryArray`.
72+
73+
### Assigning
74+
75+
Now we can assign a {py:class}`xvec.GeometryIndex` to `county`.
76+
77+
```{code-cell}
78+
cube = cube.xvec.set_geom_indexes("county")
79+
cube
80+
```
81+
82+
### Indexing
83+
84+
#### Geometries as labels
85+
86+
```{code-cell}
87+
cube.sel(county=cube.county[0])
88+
```
89+
90+
#### Complex spatial predicates
91+
92+
Lets index to counties that intersect the provided bounding box
93+
94+
```{code-cell}
95+
box = shapely.box(-97, 45, -99, 48)
96+
97+
subset = cube.sel(county=box, method="intersects")
98+
subset
99+
```
100+
101+
Notice how we did that with {py:meth}`xarray.DataArray.sel`?!
102+
103+
```{code-cell}
104+
f, axes = subset.population.xvec.plot(col="year")
105+
for ax in axes.flat:
106+
ax.plot(*box.boundary.xy, color='w')
107+
```

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ sphinxext-opengraph[social_cards]
1515
xdggs
1616
rasterix
1717
rioxarray
18+
geopandas
19+
geodatasets
1820
xvec
1921
git+https://github.com/dcherian/rolodex
2022
pint-xarray

0 commit comments

Comments
 (0)