Skip to main content
Ctrl+K
xarray_spatial 0.10.3 documentation - Home xarray_spatial 0.10.3 documentation - Home
  • Getting started
  • User Guide
  • Reference
  • Contributing
  • GitHub
  • Getting started
  • User Guide
  • Reference
  • Contributing
  • GitHub

Section Navigation

  • Dask backend behavior
  • Classification
    • xrspatial.classify.equal_interval
    • xrspatial.classify.natural_breaks
    • xrspatial.classify.reclassify
    • xrspatial.classify.quantile
    • xrspatial.classify.binary
    • xrspatial.classify.box_plot
    • xrspatial.classify.head_tail_breaks
    • xrspatial.classify.maximum_breaks
    • xrspatial.classify.percentiles
    • xrspatial.classify.std_mean
  • Dasymetric
    • xrspatial.dasymetric.disaggregate
    • xrspatial.dasymetric.pycnophylactic
    • xrspatial.dasymetric.validate_disaggregation
  • Diffusion
    • xrspatial.diffusion.diffuse
  • Fire
    • xrspatial.fire.dnbr
    • xrspatial.fire.rdnbr
    • xrspatial.fire.burn_severity_class
    • xrspatial.fire.fireline_intensity
    • xrspatial.fire.flame_length
    • xrspatial.fire.rate_of_spread
    • xrspatial.fire.kbdi
  • Flood
    • xrspatial.flood.flood_depth
    • xrspatial.flood.inundation
    • xrspatial.flood.curve_number_runoff
    • xrspatial.flood.travel_time
  • Focal
    • xrspatial.focal.apply
    • xrspatial.focal.hotspots
    • xrspatial.focal.mean
    • xrspatial.bilateral.bilateral
    • xrspatial.glcm.glcm_texture
    • xrspatial.edge_detection.sobel_x
    • xrspatial.edge_detection.sobel_y
    • xrspatial.edge_detection.laplacian
    • xrspatial.edge_detection.prewitt_x
    • xrspatial.edge_detection.prewitt_y
    • xrspatial.convolution.convolution_2d
    • xrspatial.convolution.annulus_kernel
    • xrspatial.convolution.calc_cellsize
    • xrspatial.convolution.circle_kernel
    • xrspatial.focal.custom_kernel
    • xrspatial.focal.focal_stats
  • GeoTIFF / COG
    • xrspatial.geotiff.open_geotiff
    • xrspatial.geotiff.read_vrt
    • xrspatial.geotiff.to_geotiff
    • xrspatial.geotiff.write_geotiff_gpu
    • xrspatial.geotiff.write_vrt
  • GeoTIFF release contract
  • GeoTIFF backend internals: contract inventory
  • GeoTIFF release gate / audit checklist
  • Hydrology
    • xrspatial.hydro.flow_direction_d8.flow_direction_d8
    • xrspatial.hydro.flow_direction_dinf.flow_direction_dinf
    • xrspatial.hydro.flow_direction_mfd.flow_direction_mfd
    • xrspatial.hydro.flow_accumulation_d8.flow_accumulation_d8
    • xrspatial.hydro.flow_accumulation_dinf.flow_accumulation_dinf
    • xrspatial.hydro.flow_accumulation_mfd.flow_accumulation_mfd
    • xrspatial.hydro.flow_length_d8.flow_length_d8
    • xrspatial.hydro.flow_length_dinf.flow_length_dinf
    • xrspatial.hydro.flow_length_mfd.flow_length_mfd
    • xrspatial.hydro.flow_path_d8.flow_path_d8
    • xrspatial.hydro.flow_path_dinf.flow_path_dinf
    • xrspatial.hydro.flow_path_mfd.flow_path_mfd
    • xrspatial.hydro.fill_d8.fill_d8
    • xrspatial.hydro.sink_d8.sink_d8
    • xrspatial.hydro.basin_d8.basin_d8
    • xrspatial.hydro.watershed_d8.watershed_d8
    • xrspatial.hydro.watershed_d8.basins_d8
    • xrspatial.hydro.watershed_dinf.watershed_dinf
    • xrspatial.hydro.watershed_mfd.watershed_mfd
    • xrspatial.hydro.snap_pour_point_d8.snap_pour_point_d8
    • xrspatial.hydro.stream_link_d8.stream_link_d8
    • xrspatial.hydro.stream_link_dinf.stream_link_dinf
    • xrspatial.hydro.stream_link_mfd.stream_link_mfd
    • xrspatial.hydro.stream_order_d8.stream_order_d8
    • xrspatial.hydro.stream_order_dinf.stream_order_dinf
    • xrspatial.hydro.stream_order_mfd.stream_order_mfd
    • xrspatial.hydro.hand_d8.hand_d8
    • xrspatial.hydro.hand_dinf.hand_dinf
    • xrspatial.hydro.hand_mfd.hand_mfd
    • xrspatial.hydro.twi_d8.twi_d8
  • Interpolation
    • xrspatial.interpolate.idw
    • xrspatial.interpolate.kriging
    • xrspatial.interpolate.spline
  • KDE
    • xrspatial.kde.kde
    • xrspatial.kde.line_density
  • Multi-Criteria Decision Analysis (MCDA)
    • xrspatial.mcda.standardize.standardize
    • xrspatial.mcda.weights.ahp_weights
    • xrspatial.mcda.weights.rank_weights
    • xrspatial.mcda.combine.wlc
    • xrspatial.mcda.combine.wpm
    • xrspatial.mcda.combine.owa
    • xrspatial.mcda.combine.fuzzy_overlay
    • xrspatial.mcda.combine.boolean_overlay
    • xrspatial.mcda.constrain.constrain
    • xrspatial.mcda.sensitivity.sensitivity
  • Morphology
    • xrspatial.morphology.morph_erode
    • xrspatial.morphology.morph_dilate
    • xrspatial.morphology.morph_opening
    • xrspatial.morphology.morph_closing
    • xrspatial.morphology.morph_gradient
    • xrspatial.morphology.morph_white_tophat
    • xrspatial.morphology.morph_black_tophat
    • xrspatial.morphology._circle_kernel
  • Multispectral
    • xrspatial.multispectral.arvi
    • xrspatial.multispectral.bai
    • xrspatial.multispectral.ebbi
    • xrspatial.multispectral.evi
    • xrspatial.multispectral.gci
    • xrspatial.multispectral.msavi2
    • xrspatial.multispectral.nbr
    • xrspatial.multispectral.nbr2
    • xrspatial.multispectral.ndbi
    • xrspatial.multispectral.ndmi
    • xrspatial.multispectral.ndsi
    • xrspatial.multispectral.ndwi
    • xrspatial.multispectral.mndwi
    • xrspatial.multispectral.ndvi
    • xrspatial.multispectral.osavi
    • xrspatial.multispectral.savi
    • xrspatial.multispectral.sipi
    • xrspatial.multispectral.true_color
  • Pathfinding
    • xrspatial.pathfinding.a_star_search
  • Proximity
    • xrspatial.proximity.allocation
    • xrspatial.proximity.direction
    • xrspatial.proximity.euclidean_distance
    • xrspatial.proximity.great_circle_distance
    • xrspatial.proximity.manhattan_distance
    • xrspatial.proximity.proximity
    • xrspatial.cost_distance.cost_distance
    • xrspatial.corridor.least_cost_corridor
    • xrspatial.balanced_allocation.balanced_allocation
    • xrspatial.surface_distance.surface_distance
    • xrspatial.surface_distance.surface_allocation
    • xrspatial.surface_distance.surface_direction
  • Reproject
    • xrspatial.reproject.reproject
    • xrspatial.reproject.merge
  • Resample
    • xrspatial.resample.resample
  • Surface
    • xrspatial.aspect.aspect
    • xrspatial.aspect.northness
    • xrspatial.aspect.eastness
    • xrspatial.curvature.curvature
    • xrspatial.hillshade.hillshade
    • xrspatial.slope.slope
    • xrspatial.terrain.generate_terrain
    • xrspatial.sky_view_factor.sky_view_factor
    • xrspatial.viewshed.viewshed
    • xrspatial.visibility.cumulative_viewshed
    • xrspatial.visibility.visibility_frequency
    • xrspatial.visibility.line_of_sight
    • xrspatial.perlin.perlin
    • xrspatial.bump.bump
    • xrspatial.erosion.erode
  • Terrain Metrics
    • xrspatial.terrain_metrics.roughness
    • xrspatial.terrain_metrics.tpi
    • xrspatial.terrain_metrics.tri
    • xrspatial.terrain_metrics.landforms
  • Utilities
    • xrspatial.mahalanobis.mahalanobis
    • xrspatial.emerging_hotspots.emerging_hotspots
    • xrspatial.polygonize.polygonize
    • xrspatial.rasterize.rasterize
    • xrspatial.contour.contours
    • xrspatial.preview.preview
    • xrspatial.normalize.rescale
    • xrspatial.normalize.standardize
    • xrspatial.utils.fused_overlap
    • xrspatial.utils.multi_overlap
    • xrspatial.utils.rechunk_no_shuffle
    • xrspatial.diagnostics.diagnose
  • Zonal
    • xrspatial.zonal.apply
    • xrspatial.polygon_clip.clip_polygon
    • xrspatial.zonal.crop
    • xrspatial.zonal.regions
    • xrspatial.sieve.sieve
    • xrspatial.zonal.trim
    • xrspatial.zonal.get_full_extent
    • xrspatial.zonal.stats
    • xrspatial.zonal.suggest_zonal_canvas
    • xrspatial.zonal.crosstab
  • Reference
  • Pathfinding
  • xrspatial.pathfinding.a_star_search

xrspatial.pathfinding.a_star_search#

xrspatial.pathfinding.a_star_search(surface: DataArray, start: tuple | list | array, goal: tuple | list | array, barriers: list = [], x: str | None = 'x', y: str | None = 'y', connectivity: int = 8, snap_start: bool = False, snap_goal: bool = False, friction: DataArray = None, search_radius: int | None = None) → DataArray[source]#

Calculate the least-cost path from a starting point to a goal through a surface graph, optionally weighted by a friction surface.

A* is a modification of Dijkstra’s Algorithm that is optimized for a single destination. It prioritizes paths that seem to be leading closer to a goal using an admissible heuristic.

When a friction surface is provided, edge costs are geometric_distance * mean_friction_of_endpoints, matching the cost model used by cost_distance(). The heuristic is scaled by the minimum friction value to remain admissible.

The output is an equal-sized xr.DataArray with NaN for non-path pixels and the accumulated cost at each path pixel.

Backend support

Backend

Strategy

NumPy

Numba-jitted kernel (fast, in-memory)

Dask

Sparse Python A* with LRU chunk cache — loads chunks on demand so the full grid never needs to fit in RAM

CuPy

CPU fallback (transfers to numpy, runs numba kernel, transfers back)

Dask + CuPy

Same sparse A* as Dask, with cupy→numpy chunk conversion

Memory safety

Before allocating arrays, the numpy and cupy backends check whether the grid would exceed 80 % of available RAM. If so, a MemoryError is raised suggesting search_radius or dask. When search_radius=None and the grid would exceed 50 % of RAM, an automatic radius is computed. For very long paths (manhattan distance > 1000 pixels) with auto-radius, hierarchical pathfinding (HPA*) is used: the grid is coarsened, a global route is found, then refined segment by segment.

snap_start and snap_goal are not supported with Dask-backed arrays (raises ValueError).

Parameters:
  • surface (xr.DataArray) – 2D array of values to bin.

  • start (array-like object of 2 numeric elements) – (y, x) or (lat, lon) coordinates of the starting point.

  • goal (array like object of 2 numeric elements) – (y, x) or (lat, lon) coordinates of the goal location.

  • barriers (array like object, default=[]) – List of values inside the surface which are barriers (cannot cross).

  • x (str, default='x') – Name of the x coordinate in input surface raster.

  • y (str, default='y') – Name of the y coordinate in input surface raster.

  • connectivity (int, default=8)

  • snap_start (bool, default=False) – Snap the start location to the nearest valid value before beginning pathfinding.

  • snap_goal (bool, default=False) – Snap the goal location to the nearest valid value before beginning pathfinding.

  • friction (xr.DataArray, optional) – 2-D friction (cost) surface. Must have the same shape as surface. Values must be positive and finite for passable cells; NaN or <= 0 marks impassable barriers. When provided, edge costs become geometric_distance * mean_friction_of_endpoints.

  • search_radius (int, optional) – Limit the A* search to a bounding box of ±search_radius pixels around the start and goal. Dramatically reduces memory for large grids when start and goal are relatively close. If None (default) and the full grid would exceed 50 % of available RAM, an automatic radius is computed. Ignored for dask-backed arrays (already memory-safe).

Returns:

path_agg – 2D array of pathfinding values. All other input attributes are preserved.

Return type:

xr.DataArray of the same type as surface.

References

  • Red Blob Games: https://www.redblobgames.com/pathfinding/a-star/implementation.html # noqa

  • Nicholas Swift: https://medium.com/@nicholas.w.swift/easy-a-star-pathfinding-7e6689c7f7b2 # noqa

Examples

… sourcecode:: python

>>> import numpy as np
>>> import xarray as xr
>>> from xrspatial import a_star_search
>>> agg = xr.DataArray(np.array([
...     [0, 1, 0, 0],
...     [1, 1, 0, 0],
...     [0, 1, 2, 2],
...     [1, 0, 2, 0],
...     [0, 2, 2, 2]
... ]), dims=['lat', 'lon'])
>>> height, width = agg.shape
>>> _lon = np.linspace(0, width - 1, width)
>>> _lat = np.linspace(height - 1, 0, height)
>>> agg['lon'] = _lon
>>> agg['lat'] = _lat
>>> barriers = [0]  # set pixels with value 0 as barriers
>>> start = (3, 0)
>>> goal = (0, 1)
>>> path_agg = a_star_search(agg, start, goal, barriers, 'lon', 'lat')

previous

Pathfinding

next

Proximity

On this page
  • a_star_search()
Show Source

© Copyright 2020-2026, makepath.

Created using Sphinx 9.1.0.

Built with the PyData Sphinx Theme 0.18.0.