Proximity to raster or vector

Proximity to raster or vector#

This example demonstrates the calculation of proximity distances to a raster or vector using proximity().

We open an example raster, and a vector for which we select a single feature

import geoutils as gu

filename_rast = gu.examples.get_path("everest_landsat_b4")
filename_vect = gu.examples.get_path("everest_rgi_outlines")
rast = gu.open_raster(filename_rast)
vect = gu.open_vector(filename_vect)
vect = vect[vect["RGIId"] == "RGI60-15.10055"]
rast = rast.rst.crop(vect)

# Plot the raster and vector
rast.rst.plot(cmap="Blues")
vect.vct.reproject(rast).vct.plot(fc="none", ec="k", lw=2)
proximity metric

We select the vector boundary, then use the raster as a reference to match for rasterizing the proximity distances with proximity(). See Match-reference operations for more details.

boundary = vect.set_geometry(vect.boundary)
proximity = boundary.vct.proximity(rast.rst)
proximity.rst.plot(cmap="viridis")
proximity metric

Proximity can also be computed to target pixels of a raster, or that of a mask

# Get mask of pixels within 30 of 200 infrared
import numpy as np

mask_200 = np.abs(rast - 200) < 30
mask_200.rst.plot()
proximity metric

Because a mask is bool, no need to pass target pixels

proximity metric

By default, proximity is computed using the raster’s georeferenced resolution, here meters. It can also be computed in pixels.

proximity_mask = mask_200.rst.proximity(distance_unit="pixel")
proximity_mask.rst.plot(cmap="viridis")
proximity metric

Total running time of the script: (0 minutes 5.050 seconds)

Gallery generated by Sphinx-Gallery