{
"cells": [
{
"cell_type": "markdown",
"id": "e9045413",
"metadata": {},
"source": [
"## Exploring Xarray-spatial Local Tools Functions"
]
},
{
"cell_type": "markdown",
"id": "32b5e1bf",
"metadata": {},
"source": [
"Local tools operate at the cell level, where values with the same position from a set of input rasters are used to calculate the values of the cells at the output raster.\n",
"Some examples of the application of local tools are:\n",
"- Change over time: You can use local tools to identify places where a value like land use or temperature changed over time.\n",
"- Aggregate over time: You can use local tools to aggregate values over time, for example calculating the average rainfall or hours of sunshine for each cell.\n",
"- Threshold over time: You can use local tools to identify places where a value is above or below a specified threshold, for example where the temperature was below a 0 °C.\n",
"- Data aggregation: You can use local tools to calculate the [cost surface](http://wiki.gis.com/wiki/index.php/Cost_surface) of an area, summing up different types of cost over the same cell in different layers."
]
},
{
"cell_type": "markdown",
"id": "ea55ffed",
"metadata": {},
"source": [
"In this notebook, we'll demonstrate how to use the [Xarray-spatial](http://xarray-spatial.readthedocs.io/en/latest/index.html) local tools functions supported by [Numpy](https://numpy.org/). The spatial functions available are:\n",
"- [Cell Statistics](#Cell-Statistics)\n",
"- [Combine](#Combine)\n",
"- [Lesser Frequency](#Lesser-Frequency)\n",
"- [Equal Frequency](#Equal-Frequency)\n",
"- [Greater Frequency](#Greater-Frequency)\n",
"- [Lowest Position](#Lowest-Position)\n",
"- [Highest Position](#Highest-Position)\n",
"- [Popularity](#Popularity)\n",
"- [Rank](#Rank)"
]
},
{
"cell_type": "markdown",
"id": "b9c4ed8c",
"metadata": {},
"source": [
"### Creating the sample data"
]
},
{
"cell_type": "markdown",
"id": "af3c5207",
"metadata": {},
"source": [
"In order to present the functions in a very simple and easy to understand way, we'll use 4x4 data arrays and create an `xarray.Dataset`."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "c7945275",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import xarray as xr\n",
"\n",
"arr = xr.DataArray([[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]], name=\"arr\")\n",
"\n",
"arr1 = xr.DataArray(\n",
" [[np.nan, 4, 2, 0], [2, 3, np.nan, 1], [5, 1, 2, 0], [1, 3, 2, np.nan]], name=\"arr1\"\n",
")\n",
"\n",
"arr2 = xr.DataArray(\n",
" [[3, 1, 1, 2], [4, 1, 2, 5], [0, 0, 0, 0], [np.nan, 1, 1, 1]], name=\"arr2\"\n",
")\n",
"\n",
"arr3 = xr.DataArray(\n",
" [[3, 3, 2, 0], [4, 1, 3, 1], [6, 1, 2, 2], [0, 0, 1, 1]], name=\"arr3\"\n",
")\n",
"\n",
"raster_ds = xr.merge([arr, arr1, arr2, arr3])"
]
},
{
"cell_type": "markdown",
"id": "d12080a6",
"metadata": {},
"source": [
"### Helping function"
]
},
{
"cell_type": "markdown",
"id": "17d54999",
"metadata": {},
"source": [
"This function will be used to plot the arrays for all the examples in this notebook."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9ce0bf94",
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"\n",
"def plot_arrays(arr_list, title_list):\n",
" fig, ax = plt.subplots(nrows=1, ncols=len(arr_list), figsize=(15, 10))\n",
"\n",
" for idx, arr in zip(range(0, len(arr_list)), arr_list):\n",
" for i in range(arr.shape[0]):\n",
" for j in range(arr.shape[1]):\n",
" ax[idx].text(\n",
" j,\n",
" i,\n",
" int(arr.data[i, j]) if str(arr.data[i, j]) != \"nan\" else np.nan,\n",
" ha=\"center\",\n",
" va=\"center\",\n",
" color=\"black\",\n",
" )\n",
"\n",
" ax[idx].imshow(arr.values, cmap=\"tab20c_r\")\n",
" ax[idx].set_xticks([])\n",
" ax[idx].set_yticks([])\n",
" ax[idx].set_title(title_list[idx])\n",
"\n",
" plt.show()"
]
},
{
"cell_type": "markdown",
"id": "00300315",
"metadata": {},
"source": [
"### Cell Statistics"
]
},
{
"cell_type": "markdown",
"id": "01ab159c",
"metadata": {},
"source": [
"[`xrspatial.local.cell_stats`](https://xarray-spatial.readthedocs.io/en/latest/reference/_autosummary/xrspatial.local.cell_stats.html) calculates statistics from a raster dataset on a cell-by-cell basis."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "86353494",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
"