None

Note

This tutorial was generated from an IPython notebook that can be downloaded here.

Usage example

import pyspatialstats.focal as fs
import rasterio as rio
import matplotlib.pyplot as plt
import numpy as np
import os
os.chdir('../../../')

Loading raster (containing water table depth (Fan et al., 2017)).

with rio.open('data/wtd.tif') as f:
    a = f.read(1).astype(np.float64)
    a[a == -999.9] = np.nan

Inspecting the data

plt.imshow(a, cmap='Blues', vmax=100)
plt.title('Water table depth')
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x153e75010>
../_images/focal_stats_6_1.png

Focal statistics

Calculation of the focal mean:

plt.imshow(fs.focal_mean(a, window=15).mean, vmax=100, cmap='Blues')
<matplotlib.image.AxesImage at 0x153fee850>
../_images/focal_stats_9_1.png

This looks quite similar to the input raster, but with smoothing applied. Let’s try a higher window, which should increase the smoothing

plt.imshow(fs.focal_mean(a, window=25).mean, vmax=100, cmap='Blues')
<matplotlib.image.AxesImage at 0x15407a210>
../_images/focal_stats_11_1.png

This same functionality can be used to reduce the shape of the raster based on this window.

x = fs.focal_mean(a, window=108, reduce=True).mean
plt.imshow(x, vmax=100, cmap='Blues')
<matplotlib.image.AxesImage at 0x1541056d0>
../_images/focal_stats_13_1.png

The shape of this new raster is exactly 108 times smaller than the input raster. Note that for this to work both x and y-axes need to be divisible by the window size.