Proximity¶
Allocation¶
-
xrspatial.proximity.allocation(raster: xarray.core.dataarray.DataArray, x: str = 'x', y: str = 'y', target_values: list = [], distance_metric: str = 'EUCLIDEAN')[source]¶ Calculates, for all pixels in the input raster, the nearest source based on a set of target values and a distance metric.
This function attempts to produce the value of nearest feature 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.valueswill be considered as “target”, and all allocation will be computed in pixels.- raster: xarray.DataArray
2D image array
- 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, allocation 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’
- allocation: xarray.DataArray
2D proximity allocation array, of the same type as the input All other input attributes are preserved.
- Algorithm References:
https://github.com/OSGeo/gdal/blob/master/gdal/alg/gdalproximity.cpp
Direction¶
-
xrspatial.proximity.direction(raster: xarray.core.dataarray.DataArray, x: str = 'x', y: str = 'y', target_values: list = [], distance_metric: str = 'EUCLIDEAN')[source]¶ Calculates, for all pixels in the input raster, the direction to nearest source based on a set of target values and a distance metric.
This function attempts to calculate for each cell, the the direction, in degrees, to the nearest source. The output values are based on compass directions, where 90 is for the east, 180 for the south, 270 for the west, 360 for the north, and 0 for the source cell itself. The following options are used to define the behavior of the function. By default all non-zero pixels in
raster.valueswill be considered as “target”, and all allocation will be computed in pixels.- 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, allocation 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’
- direction: xarray.DataArray
2D proximity direction array, of the same type as the input All other input attributes are preserved.
- Algorithm References:
https://github.com/OSGeo/gdal/blob/master/gdal/alg/gdalproximity.cpp
Proximity¶
-
xrspatial.proximity.euclidean_distance(x1: float, x2: float, y1: float, y2: float) → float[source]¶ Calculates Euclidean (straight line) distance between (x1, y1) and (x2, y2).
- x1: float
x-coordinate of the first point.
- x2: float
x-coordinate of the second point.
- y1: float
y-coordinate of the first point.
- y2: float
y-coordinate of the second point.
- distance: float
Euclidean distance between two points.
- Algorithm References:
Imports >>> from xrspatial import euclidean_distance >>> point_a = (142.32, 23.23) >>> point_b = (312.54, 432.01)
Calculate Euclidean Distance >>> dist = euclidean_distance(point_a[0], point_b[0], point_a[1], point_b[1]) >>> print(dist) 442.80462599209596
-
xrspatial.proximity.great_circle_distance(x1: float, x2: float, y1: float, y2: float, radius: float = 6378137) → float[source]¶ Calculates great-circle (orthodromic/spherical) distance between (x1, y1) and (x2, y2), assuming each point is a longitude, latitude pair.
- x1: float (between -180 and 180)
x-coordinate (latitude) of the first point.
- x2: float (between -180 and 180)
x-coordinate (latitude) of the second point.
- y1: float (between -90 and 90)
y-coordinate (longitude) of the first point.
- y2: float (between -90 and 90)
y-coordinate (longitude) of the second point.
- radius: float (default = 6378137)
Radius of sphere (earth).
- distance: float
Great-Circle distance between two points.
- Algorithm References:
Imports >>> from xrspatial import great_circle_distance >>> point_a = (123.2, 82.32) >>> point_b = (178.0, 65.09)
Calculate Euclidean Distance >>> dist = great_circle_distance(point_a[0], point_b[0], point_a[1], point_b[1]) >>> print(dist) 2378290.489801402
-
xrspatial.proximity.manhattan_distance(x1: float, x2: float, y1: float, y2: float) → float[source]¶ Calculates Manhattan distance (sum of distance in x and y directions) between (x1, y1) and (x2, y2).
- x1: float
x-coordinate of the first point.
- x2: float
x-coordinate of the second point.
- y1: float
y-coordinate of the first point.
- y2: float
y-coordinate of the second point.
- distance: float
Manhattan distance between two points.
- Algorithm References:
Imports >>> from xrspatial import manhattan_distance >>> point_a = (142.32, 23.23) >>> point_b = (312.54, 432.01)
Calculate Euclidean Distance >>> dist = manhattan_distance(point_a[0], point_b[0], point_a[1], point_b[1]) >>> print(dist) 196075.9368
-
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.valueswill 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:
https://github.com/OSGeo/gdal/blob/master/gdal/alg/gdalproximity.cpp
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 >>> proximity_agg = proximity(points_agg) >>> stack(shade(proximity_agg, cmap=[‘darkturquoise’, ‘black’], how=’linear’), >>> points_shaded)