Local

Sample data

[1]:
import numpy as np
import xarray as xr

arr = xr.DataArray([[2, 2, 2, 2],
                    [2, 2, 2, 2],
                    [2, 2, 2, 2],
                    [2, 2, 2, 2]], name='arr')

arr1 = xr.DataArray([[np.nan, 4, 2, 0],
                     [2, 3, np.nan, 1],
                     [5, 1, 2, 0],
                     [1, 3, 2, np.nan]], name='arr1')

arr2 = xr.DataArray([[3, 1, 1, 2],
                     [4, 1, 2, 5],
                     [0, 0, 0, 0],
                     [np.nan, 1, 1, 1]], name='arr2')

arr3 = xr.DataArray([[3, 3, 2, 0],
                     [4, 1, 3, 1],
                     [6, 1, 2, 2],
                     [0, 0, 1, 1]], name='arr3')

raster_ds = xr.merge([arr, arr1, arr2, arr3])

Helping functions

[2]:
import matplotlib.pyplot as plt

def plot_arrays(arr_list, title_list):
    fig, ax = plt.subplots(nrows=1, ncols=len(arr_list), figsize=(15, 10))

    for idx, arr in zip(range(0, len(arr_list)), arr_list):
        for i in range(arr.shape[0]):
            for j in range(arr.shape[1]):
                text = ax[idx].text(j, i, int(arr.data[i, j]) if str(arr.data[i, j]) != 'nan' else np.nan,
                               ha='center', va='center', color='black')

        ax[idx].imshow(arr.values, cmap='tab20c_r')
        ax[idx].set_xticks([])
        ax[idx].set_yticks([])
        ax[idx].set_title(title_list[idx])

    plt.show()

Cell Stats

xrspatial.local.cell_stats calculates statistics of raster dataset on a cell-by-cell basis.

[3]:
from xrspatial.local import cell_stats

func_list = ['max', 'mean', 'median', 'min', 'std', 'sum']
statistics = [cell_stats(raster_ds[['arr1', 'arr2', 'arr3']], func=func) for func in func_list]

plot_arrays(
    [
        raster_ds['arr1'],
        raster_ds['arr2'],
        raster_ds['arr3'],
    ],
    ['arr1', 'arr2', 'arr3'],
)

plot_arrays(statistics, func_list)
../_images/user_guide_local_7_0.png
../_images/user_guide_local_7_1.png

Combine

xrspatial.local.combine combines raster dataset, a unique output value is assigned to each unique combination of raster values.

[4]:
from xrspatial.local import combine

result_arr = combine(raster_ds[['arr1', 'arr2']])

plot_arrays(
    [
        raster_ds['arr1'],
        raster_ds['arr2'],
        result_arr,
    ],
    ['arr1', 'arr2', 'result']
)
../_images/user_guide_local_10_0.png

Less Frequency

xrspatial.local.lesser_frequency calculates the number of times the raster dataset has a lesser frequency on a cell-by-cell basis.

[5]:
from xrspatial.local import lesser_frequency

result_arr = lesser_frequency(raster_ds, 'arr')

plot_arrays(
    [
        raster_ds['arr'],
        raster_ds['arr1'],
        raster_ds['arr2'],
        raster_ds['arr3'],
        result_arr,
    ],
    ['arr_ref', 'arr1', 'arr2', 'arr3', 'result']
)
../_images/user_guide_local_13_0.png

Equal Frequency

xrspatial.local.equal_frequency calculates the number of times the raster dataset has a equal frequency on a cell-by-cell basis.

[6]:
from xrspatial.local import equal_frequency

result_arr = equal_frequency(raster_ds, 'arr')

plot_arrays(
    [
        raster_ds['arr'],
        raster_ds['arr1'],
        raster_ds['arr2'],
        raster_ds['arr3'],
        result_arr,
    ],
    ['arr_ref', 'arr1', 'arr2', 'arr3', 'result']
)
../_images/user_guide_local_16_0.png

Greater Frequency

xrspatial.local.greater_frequency calculates the number of times the raster dataset has a greater frequency on a cell-by-cell basis.

[7]:
from xrspatial.local import greater_frequency

result_arr = greater_frequency(raster_ds, 'arr')

plot_arrays(
    [
        raster_ds['arr'],
        raster_ds['arr1'],
        raster_ds['arr2'],
        raster_ds['arr3'],
        result_arr,
    ],
    ['arr_ref', 'arr1', 'arr2', 'arr3', 'result']
)
../_images/user_guide_local_19_0.png

Lowest Position

xrspatial.local.lowest_position calculates the data variable index of the lowest value on a cell-by-cell basis.

[8]:
from xrspatial.local import lowest_position

result_arr = lowest_position(raster_ds)

plot_arrays(
    [
        raster_ds['arr1'],
        raster_ds['arr2'],
        raster_ds['arr3'],
        result_arr,
    ],
    ['arr1', 'arr2', 'arr3', 'result']
)
../_images/user_guide_local_22_0.png

Highest Position

xrspatial.local.highest_position calculates the data variable index of the highest value on a cell-by-cell basis.

[9]:
from xrspatial.local import highest_position

result_arr = highest_position(raster_ds)

plot_arrays(
    [
        raster_ds['arr1'],
        raster_ds['arr2'],
        raster_ds['arr3'],
        result_arr,
    ],
    ['arr1', 'arr2', 'arr3', 'result']
)
../_images/user_guide_local_25_0.png

Popularity

xrspatial.local.popularity calculates the popularity, the number of occurrences of each value, of raster dataset on a cell-by-cell basis. The output value is assigned based on the reference data variable nth most popular.

[10]:
from xrspatial.local import popularity


result_arr = popularity(raster_ds, 'arr')

plot_arrays(
    [
        raster_ds['arr'],
        raster_ds['arr1'],
        raster_ds['arr2'],
        raster_ds['arr3'],
        result_arr,
    ],
    ['arr_ref', 'arr1', 'arr2', 'arr3', 'result']
)
../_images/user_guide_local_28_0.png

Rank

xrspatial.local.rank calculates the rank of raster dataset on a cell-by-cell basis. The output value is assigned based on the reference data variable rank.

[11]:
from xrspatial.local import rank

result_arr = rank(raster_ds, 'arr')

plot_arrays(
    [
        raster_ds['arr'],
        raster_ds['arr1'],
        raster_ds['arr2'],
        raster_ds['arr3'],
        result_arr,
    ],
    ['arr_ref', 'arr1', 'arr2', 'arr3', 'result']
)
../_images/user_guide_local_31_0.png