diff --git a/.gitignore b/.gitignore index 0951de11..2fd6749f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +*.bak + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 239a9732..b745e730 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,7 +10,7 @@ repos: hooks: - id: black - repo: https://github.com/timothycrosley/isort - rev: 5.10.1 + rev: 5.12.0 hooks: - id: isort args: [rioxarray/, test/, docs/] diff --git a/docs/examples/crs_index.ipynb b/docs/examples/crs_index.ipynb new file mode 100644 index 00000000..1985d6fd --- /dev/null +++ b/docs/examples/crs_index.ipynb @@ -0,0 +1,9170 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# CRS Index" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "By default rioxarray stores CRS information as a scalar coordinate in Xarray. This works well because the important metadata persists through all Xarray operations and when it comes time to save an output, we must retrieve this important metadata.\n", + "\n", + "As of Xarray v2022.09.0 it is possible to create custom Indexes (typically a `PandasIndex` is used behind the scenes that determines how dimensional coordinates are queried and aligned). With the new Xindexes interface, we can attach CRS as persistent metadata to a custom `CRSIndex`. Effectively we enhance the default `PandasIndex` by adding associated metadata (CRS) and new functionality like checking CRS compatibility across objects.\n", + "\n", + "This notebook showcases experimental CRSIndex functionality" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "%load_ext autoreload\n", + "%autoreload 2" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import xarray as xr\n", + "import rioxarray\n", + "import geopandas as gpd\n", + "import xvec\n", + "\n", + "xr.set_options(\n", + " display_expand_data=False,\n", + " display_expand_indexes=True,\n", + ");\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
<xarray.DataArray 'band_data' (band: 1, y: 500, x: 500)>\n", + "[250000 values with dtype=float32]\n", + "Coordinates:\n", + " * band (band) int64 1\n", + " * x (x) float64 1.635e+06 1.635e+06 ... 1.784e+06 1.784e+06\n", + " * y (y) float64 2.715e+06 2.714e+06 ... 2.565e+06 2.565e+06\n", + " spatial_ref int64 ...\n", + "Attributes:\n", + " AREA_OR_POINT: Area\n", + " OVR_RESAMPLING_ALG: NEAREST
<xarray.DataArray 'band_data' (band: 1, y: 500, x: 500)>\n", + "[250000 values with dtype=float32]\n", + "Coordinates:\n", + " * band (band) int64 1\n", + " spatial_ref int64 ...\n", + " * x (x) float64 1.635e+06 1.635e+06 ... 1.784e+06 1.784e+06\n", + " * y (y) float64 2.715e+06 2.714e+06 ... 2.565e+06 2.565e+06\n", + "Indexes:\n", + " x CRSIndex (crs=PROJCS["Albers",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS ...)\n", + " y CRSIndex (crs=PROJCS["Albers",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS ...)\n", + "Attributes:\n", + " AREA_OR_POINT: Area\n", + " OVR_RESAMPLING_ALG: NEAREST
<xarray.DataArray 'band_data' (band: 1, y: 500, x: 500)>\n", + "[250000 values with dtype=float32]\n", + "Coordinates:\n", + " * band (band) int64 1\n", + " spatial_ref int64 0\n", + " * x (x) float64 1.635e+06 1.635e+06 ... 1.784e+06 1.784e+06\n", + " * y (y) float64 2.715e+06 2.714e+06 ... 2.565e+06 2.565e+06\n", + "Indexes:\n", + " x CRSIndex (crs=PROJCS["Albers",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS ...)\n", + " y CRSIndex (crs=PROJCS["Albers",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS ...)\n", + "Attributes:\n", + " AREA_OR_POINT: Area\n", + " OVR_RESAMPLING_ALG: NEAREST
<xarray.Dataset>\n", + "Dimensions: (time: 2, x: 10, y: 10)\n", + "Coordinates:\n", + " * time (time) object 2016-12-19 10:27:29.687763 2016-12-29 12:52:42...\n", + " * x (x) float64 4.663e+05 4.663e+05 ... 4.663e+05 4.663e+05\n", + " * y (y) float64 8.085e+06 8.085e+06 ... 8.085e+06 8.085e+06\n", + " spatial_ref int64 ...\n", + "Data variables:\n", + " blue (time, y, x) float64 ...\n", + " green (time, y, x) float64 ...\n", + "Attributes:\n", + " coordinates: spatial_ref
<xarray.Dataset>\n", + "Dimensions: (time: 2, x: 10, y: 10)\n", + "Coordinates:\n", + " * time (time) object 2016-12-19 10:27:29.687763 2016-12-29 12:52:42...\n", + " spatial_ref int64 ...\n", + " * x (x) float64 4.663e+05 4.663e+05 ... 4.663e+05 4.663e+05\n", + " * y (y) float64 8.085e+06 8.085e+06 ... 8.085e+06 8.085e+06\n", + "Data variables:\n", + " blue (time, y, x) float64 ...\n", + " green (time, y, x) float64 ...\n", + "Indexes:\n", + " x CRSIndex (crs=EPSG:32722)\n", + " y CRSIndex (crs=EPSG:32722)\n", + "Attributes:\n", + " coordinates: spatial_ref
<xarray.Dataset>\n", + "Dimensions: (time: 2, x: 10, y: 10)\n", + "Coordinates:\n", + " * time (time) object 2016-12-19 10:27:29.687763 2016-12-29 12:52:42...\n", + " spatial_ref int64 0\n", + " * x (x) float64 4.663e+05 4.663e+05 ... 4.663e+05 4.663e+05\n", + " * y (y) float64 8.085e+06 8.085e+06 ... 8.085e+06 8.085e+06\n", + "Data variables:\n", + " blue (time, y, x) float64 ...\n", + " green (time, y, x) float64 ...\n", + "Indexes:\n", + " x CRSIndex (crs=EPSG:32722)\n", + " y CRSIndex (crs=EPSG:32722)\n", + "Attributes:\n", + " coordinates: spatial_ref
<xarray.Dataset>\n", + "Dimensions: (x: 10, y: 10, time: 2)\n", + "Coordinates:\n", + " * x (x) float64 -51.32 -51.32 -51.32 ... -51.32 -51.32 -51.32\n", + " * y (y) float64 -17.32 -17.32 -17.32 ... -17.32 -17.32 -17.32\n", + " * time (time) object 2016-12-19 10:27:29.687763 2016-12-29 12:52:42...\n", + " spatial_ref int64 0\n", + "Data variables:\n", + " blue (time, y, x) float64 6.611 5.581 0.3996 ... 3.491 5.056 3.368\n", + " green (time, y, x) float64 7.921 66.15 30.1 ... 21.76 27.29 18.41\n", + "Attributes:\n", + " coordinates: spatial_ref
<xarray.Dataset>\n", + "Dimensions: (time: 2, x: 10, y: 10)\n", + "Coordinates:\n", + " * time (time) object 2016-12-19 10:27:29.687763 2016-12-29 12:52:42...\n", + " * x (x) float64 4.663e+05 4.663e+05 ... 4.663e+05 4.663e+05\n", + " * y (y) float64 8.085e+06 8.085e+06 ... 8.085e+06 8.085e+06\n", + " spatial_ref int64 ...\n", + "Data variables:\n", + " blue (time, y, x) float64 6.611 5.581 0.3996 ... 3.491 5.056 3.368\n", + " green (time, y, x) float64 7.921 66.15 30.1 ... 21.76 27.29 18.41\n", + "Attributes:\n", + " coordinates: spatial_ref
<xarray.Dataset>\n", + "Dimensions: (time: 2, x: 0, y: 0)\n", + "Coordinates:\n", + " * time (time) object 2016-12-19 10:27:29.687763 2016-12-29 12:52:42...\n", + " * x (x) float64 \n", + " * y (y) float64 \n", + " spatial_ref int64 0\n", + "Data variables:\n", + " blue (time, y, x) float64 \n", + " green (time, y, x) float64
<xarray.Dataset>\n", + "Dimensions: (x: 10, y: 10, time: 2)\n", + "Coordinates:\n", + " * time (time) object 2016-12-19 10:27:29.687763 2016-12-29 12:52:42...\n", + " spatial_ref int64 0\n", + " * x (x) float64 -51.32 -51.32 -51.32 ... -51.32 -51.32 -51.32\n", + " * y (y) float64 -17.32 -17.32 -17.32 ... -17.32 -17.32 -17.32\n", + "Data variables:\n", + " blue (time, y, x) float64 6.611 5.581 0.3996 ... 3.491 5.056 3.368\n", + " green (time, y, x) float64 7.921 66.15 30.1 ... 21.76 27.29 18.41\n", + "Indexes:\n", + " x CRSIndex (crs=EPSG:4326)\n", + " y CRSIndex (crs=EPSG:4326)\n", + "Attributes:\n", + " coordinates: spatial_ref
<xarray.DataArray 'blue' (time: 2, y: 10, x: 1)>\n", + "5.078 2.736 5.267 2.344 5.195 5.714 6.407 ... 3.473 2.622 5.093 4.75 3.218 1.888\n", + "Coordinates:\n", + " * time (time) object 2016-12-19 10:27:29.687763 2016-12-29 12:52:42...\n", + " spatial_ref int64 0\n", + " * x (x) float64 -51.32\n", + " * y (y) float64 -17.32 -17.32 -17.32 ... -17.32 -17.32 -17.32\n", + "Indexes:\n", + " x CRSIndex (crs=EPSG:4326)\n", + " y CRSIndex (crs=EPSG:4326)\n", + "Attributes:\n", + " NETCDF_DIM_EXTRA: {time}\n", + " NETCDF_DIM_time_DEF: [2. 6.]\n", + " NETCDF_DIM_time_VALUES: [ 0. 872712.659688]\n", + " units: ('DN', 'DN')
<xarray.DataArray (x: 2)>\n", + "nan nan\n", + "Coordinates:\n", + " * x (x) float64 -51.32 -51.32\n", + " spatial_ref int64 0
<xarray.DataArray (x: 2, y: 2)>\n", + "nan nan nan nan\n", + "Coordinates:\n", + " spatial_ref int64 0\n", + " * x (x) float64 -51.32 -51.32\n", + " * y (y) float64 -17.32 -17.32\n", + "Indexes:\n", + " x CRSIndex (crs=EPSG:4326)\n", + " y CRSIndex (crs=EPSG:4326)
<xarray.DataArray (x: 2, y: 2)>\n", + "nan nan nan nan\n", + "Coordinates:\n", + " spatial_ref int64 0\n", + " * x (x) float64 -51.32 -51.32\n", + " * y (y) float64 -17.32 -17.32\n", + "Indexes:\n", + " x CRSIndex (crs=EPSG:4326)\n", + " y CRSIndex (crs=EPSG:4326)
<xarray.DataArray 'x' (x: 2)>\n", + "-51.32 -51.32\n", + "Coordinates:\n", + " spatial_ref int64 0\n", + " x (x) float64 -51.32 -51.32
\n", + " | station | \n", + "geometry | \n", + "
---|---|---|
0 | \n", + "siteA | \n", + "POINT (-51.31734 -17.32280) | \n", + "
1 | \n", + "siteB | \n", + "POINT (-51.31745 -17.32290) | \n", + "
<xarray.DataArray 'station' (station: 2)>\n", + "'siteA' 'siteB'\n", + "Coordinates:\n", + " * station (station) object POINT (-51.31734 -17.3228) POINT (-51.31745 -17...\n", + "Indexes:\n", + " station GeometryIndex (crs=EPSG:4326)
<xarray.Dataset>\n", + "Dimensions: (time: 2, x: 10, y: 10)\n", + "Coordinates:\n", + " * time (time) object 2016-12-19 10:27:29.687763 2016-12-29 12:52:42...\n", + " * x (x) float64 4.663e+05 4.663e+05 ... 4.663e+05 4.663e+05\n", + " * y (y) float64 8.085e+06 8.085e+06 ... 8.085e+06 8.085e+06\n", + " spatial_ref int64 ...\n", + "Data variables:\n", + " blue (time, y, x) float64 6.611 5.581 0.3996 ... 3.491 5.056 3.368\n", + " green (time, y, x) float64 7.921 66.15 30.1 ... 21.76 27.29 18.41\n", + "Attributes:\n", + " coordinates: spatial_ref
<xarray.Dataset>\n", + "Dimensions: (time: 2, geometry: 2)\n", + "Coordinates:\n", + " * time (time) object 2016-12-19 10:27:29.687763 2016-12-29 12:52:42...\n", + " spatial_ref int64 0\n", + " * geometry (geometry) object POINT (-51.31734 -17.3228) POINT (-51.3174...\n", + "Data variables:\n", + " blue (time, geometry) float64 5.48 4.536 0.5377 6.746\n", + " green (time, geometry) float64 57.79 23.8 20.33 6.438\n", + "Indexes:\n", + " geometry GeometryIndex (crs=EPSG:4326)\n", + "Attributes:\n", + " coordinates: spatial_ref
<xarray.Dataset>\n", + "Dimensions: (time: 2, geometry: 2)\n", + "Coordinates:\n", + " * time (time) object 2016-12-19 10:27:29.687763 2016-12-29 12:52:42...\n", + " spatial_ref int64 0\n", + " * geometry (geometry) object POINT (-51.31734 -17.3228) POINT (-51.3174...\n", + "Data variables:\n", + " blue (time, geometry) float64 5.48 4.536 0.5377 6.746\n", + " green (time, geometry) float64 57.79 23.8 20.33 6.438\n", + "Indexes:\n", + " geometry GeometryIndex (crs=EPSG:4326)\n", + "Attributes:\n", + " coordinates: spatial_ref
<xarray.Dataset>\n", + "Dimensions: (time: 2, geometry: 2)\n", + "Coordinates:\n", + " * time (time) object 2016-12-19 10:27:29.687763 2016-12-29 12:52:42...\n", + " spatial_ref int64 0\n", + " * geometry (geometry) object POINT (-51.31734 -17.3228) POINT (-51.3174...\n", + "Data variables:\n", + " blue (time, geometry) float64 0.8441 0.8441 1.844 1.844\n", + " green (time, geometry) float64 37.09 37.09 44.67 44.67\n", + "Indexes:\n", + " geometry GeometryIndex (crs=EPSG:4326)\n", + "Attributes:\n", + " coordinates: spatial_ref