Focal

Apply

xrspatial.focal.apply(raster, kernel, func=CPUDispatcher(<function _calc_mean>))[source]

Returns custom function applied array using a user-created window.

Parameters
  • raster (xarray.DataArray) – 2D array of input values to be filtered.

  • kernel (numpy.array) – 2D array where values of 1 indicate the kernel.

  • func (callable, default=xrspatial.focal._calc_mean) – Function which takes an input array and returns an array.

Returns

agg – 2D aggregate array of filtered values.

Return type

xarray.DataArray of same type as raster

Examples

>>> print(terrain_agg[200:203, 200:202])
<xarray.DataArray 'Elevation' (lat: 3, lon: 2)>
array([[1264.02296597, 1261.947921  ],
       [1285.37105519, 1282.48079719],
       [1306.02339636, 1303.4069579 ]])
Coordinates:
* lon      (lon) float64 -3.96e+06 -3.88e+06
* lat      (lat) float64 6.733e+06 6.867e+06 7e+06
Attributes:
    res:            (80000.0, 133333.3333333333)
    Description:    Example Terrain
    units:          km
    Max Elevation:  4000

>>> print(agg[200:203, 200:202])
<xarray.DataArray (lat: 3, lon: 2)>
array([[1307.19395948, 1302.69168271],
       [1323.55815161, 1318.75959349],
       [1342.33133041, 1336.93821534]])
Coordinates:
* lon      (lon) float64 -3.96e+06 -3.88e+06
* lat      (lat) float64 6.733e+06 6.867e+06 7e+06
Attributes:
    res:            (80000.0, 133333.3333333333)
    Description:    Example Filtered Terrain
    units:          km
    Max Elevation:  4000

Hotspots

xrspatial.focal.hotspots(raster, kernel)[source]

Identify statistically significant hot spots and cold spots in an input raster. To be a statistically significant hot spot, a feature will have a high value and be surrounded by other features with high values as well. Neighborhood of a feature defined by the input kernel, which currently support a shape of circle, annulus, or custom kernel.

The result should be a raster with the following 7 values:
  • 90 for 90% confidence high value cluster

  • 95 for 95% confidence high value cluster

  • 99 for 99% confidence high value cluster

  • 90 for 90% confidence low value cluster

  • 95 for 95% confidence low value cluster

  • 99 for 99% confidence low value cluster

  • 0 for no significance

Parameters
  • raster (xarray.DataArray) – 2D Input raster image with raster.shape = (height, width).

  • kernel (Numpy Array) – 2D array where values of 1 indicate the kernel.

Returns

hotspots_agg – 2D array of hotspots with values indicating confidence level.

Return type

xarray.DataArray of same type as raster

Examples

>>> print(terrain_agg[200:203, 200:202])
<xarray.DataArray 'Elevation' (lat: 3, lon: 2)>
array([[1264.02296597, 1261.947921  ],
       [1285.37105519, 1282.48079719],
       [1306.02339636, 1303.4069579 ]])
Coordinates:
* lon      (lon) float64 -3.96e+06 -3.88e+06
* lat      (lat) float64 6.733e+06 6.867e+06 7e+06
Attributes:
    res:            (80000.0, 133333.3333333333)
    Description:    Example Terrain
    units:          km
    Max Elevation:  4000

>>> print(hotspots_agg[200:203, 200:202])
<xarray.DataArray 'Significance' (lat: 3, lon: 2)>
array([[0, 0],
       [0, 0],
       [0, 0]], dtype=int8)
Coordinates:
* lon      (lon) float64 -3.96e+06 -3.88e+06
* lat      (lat) float64 6.733e+06 6.867e+06 7e+06
Attributes:
    res:            (80000.0, 133333.3333333333)
    Description:    Example Hotspots
    units:          %
    Max Elevation:  4000

Mean

xrspatial.focal.mean(agg, passes=1, excludes=[nan], name='mean')[source]

Returns Mean filtered array using a 3x3 window. Default behaviour to ‘mean’ is to pad the borders with nans

Parameters
  • agg (xarray.DataArray) – 2D array of input values to be filtered.

  • passes (int, default=1) – Number of times to run mean.

  • name (str, default='mean') – Output xr.DataArray.name property.

Returns

mean_agg – 2D aggregate array of filtered values.

Return type

xarray.DataArray of same type as agg

Examples

Focal Statistics

xrspatial.focal.custom_kernel(kernel)[source]

Validates a custom kernel. If the kernel is valid, returns itself.