|
| 1 | +--- |
| 2 | +jupytext: |
| 3 | + text_representation: |
| 4 | + format_name: myst |
| 5 | +kernelspec: |
| 6 | + display_name: Python 3 |
| 7 | + name: python |
| 8 | +--- |
| 9 | + |
1 | 10 | # 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 | +``` |
0 commit comments