xrspatial.proximity.proximity(raster: xarray.core.dataarray.DataArray, x: str = 'x', y: str = 'y', target_values: list = [], distance_metric: str = 'EUCLIDEAN')xarray.core.dataarray.DataArray[source]

Computes the proximity of all pixels in the image to a set of pixels in the source image based on Euclidean, Great-Circle or Manhattan distance.

This function attempts to compute the proximity of all pixels in the image to a set of pixels in the source image. The following options are used to define the behavior of the function. By default all non-zero pixels in raster.values will be considered the “target”, and all proximities will be computed in pixels. Note that target pixels are set to the value corresponding to a distance of zero.

raster: xarray.DataArray

2D array image with shape = (height, width)

x: str (default = “x”)

Name of x-coordinates.

y: str (default = “y”)

Name of y-coordinates.

target_values: list

Target pixel values to measure the distance from. If this option is not provided, proximity will be computed from non-zero pixel values. Currently pixel values are internally processed as integers.

distance_metric: str (default = “EUCLIDEAN”)

The metric for calculating distance between 2 points. Valid distance_metrics: ‘EUCLIDEAN’, ‘GREAT_CIRCLE’, and ‘MANHATTAN’

proximity: xarray.DataArray

2D proximity array, of the same type as the input All other input attributes are preserved.

Algorithm References: - OSGeo/gdal, https://github.com/OSGeo/gdal/blob/master/gdal/alg/gdalproximity.cpp, Apr. 22, 2021. # noqa

Imports >>> from xrspatial import proximity >>> import pandas as pd >>> from datashader.transfer_functions import shade >>> from datashader.transfer_functions import stack >>> from datashader.transfer_functions import dynspread >>> from datashader.transfer_functions import set_background >>> from datashader.colors import Elevation

Load Data and Create Canvas >>> df = pd.Dataframe({ >>> ‘x’: [-13, -11, -5, 4, 9, 11, 18, 6], >>> ‘y’: [-13, -5, 0, 10, 7, 2, 5, -5] >>> }) >>> cvs = ds.Canvas(plot_width=800, plot_height=600, >>> x_range=(-20, 20), y_range=(-20,20))

Create Proximity Aggregate >>> points_agg = cvs.points(df, x=’x’, y=’y’) >>> points_shaded = dynspread(shade(points_agg, >>> cmap=[‘salmon’, ‘salmon’]), >>> threshold=1, >>> max_px=5) >>> set_background(points_shaded, ‘black’)

Create Proximity Grid for All Non-Zero Values >>> prox_agg = proximity(points_agg) >>> stack(shade(prox_agg, cmap=[‘darkturquoise’, ‘black’], how=’linear’), >>> points_shaded)