Skip to content

Commit f9d453d

Browse files
committed
Expose multi_stop_search on the Dataset accessor via supports_dataset (#3563)
1 parent b0478a6 commit f9d453d

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

xrspatial/accessor.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,6 +1947,12 @@ def surface_direction(self, elevation, **kwargs):
19471947
from .surface_distance import surface_direction
19481948
return surface_direction(self._obj, elevation, **kwargs)
19491949

1950+
# ---- Pathfinding ----
1951+
1952+
def multi_stop_search(self, waypoints, **kwargs):
1953+
from .pathfinding import multi_stop_search
1954+
return multi_stop_search(self._obj, waypoints, **kwargs)
1955+
19501956
# ---- Preview ----
19511957

19521958
def preview(self, **kwargs):

xrspatial/pathfinding.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
dask = None
1616

1717
from xrspatial.cost_distance import _heap_push, _heap_pop
18+
from xrspatial.dataset_support import supports_dataset
1819
from xrspatial.utils import (
1920
_validate_raster,
2021
get_dataarray_resolution, ngjit,
@@ -1324,6 +1325,7 @@ def _optimize_waypoint_order(surface, waypoints, barriers, x, y,
13241325
return [waypoints[i] for i in order]
13251326

13261327

1328+
@supports_dataset
13271329
def multi_stop_search(surface: xr.DataArray,
13281330
waypoints: list,
13291331
barriers: list = [],
@@ -1344,8 +1346,10 @@ def multi_stop_search(surface: xr.DataArray,
13441346
13451347
Parameters
13461348
----------
1347-
surface : xr.DataArray
1348-
2-D elevation / cost surface.
1349+
surface : xr.DataArray or xr.Dataset
1350+
2-D elevation / cost surface. A Dataset routes each data
1351+
variable through the same waypoints independently and returns
1352+
a Dataset of the per-variable results.
13491353
waypoints : list of array-like
13501354
Sequence of ``(y, x)`` coordinate pairs to visit. Must contain
13511355
at least two points.
@@ -1368,9 +1372,10 @@ def multi_stop_search(surface: xr.DataArray,
13681372
13691373
Returns
13701374
-------
1371-
xr.DataArray
1375+
xr.DataArray or xr.Dataset
13721376
Cumulative path cost surface. Attributes include
13731377
``waypoint_order``, ``segment_costs``, and ``total_cost``.
1378+
A Dataset input returns a Dataset of per-variable results.
13741379
13751380
Raises
13761381
------

xrspatial/tests/test_accessor.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ def test_dataset_accessor_has_expected_methods():
117117
'morph_erode', 'morph_dilate', 'morph_opening', 'morph_closing',
118118
'morph_gradient', 'morph_white_tophat', 'morph_black_tophat',
119119
'proximity', 'allocation', 'direction', 'cost_distance',
120+
'multi_stop_search',
120121
'ndvi', 'evi', 'arvi', 'savi', 'nbr', 'sipi',
121122
'rasterize',
122123
'validate',
@@ -299,6 +300,17 @@ def test_da_multi_stop_search_kwargs(elevation):
299300
xr.testing.assert_identical(result, expected)
300301

301302

303+
def test_ds_multi_stop_search(elevation):
304+
from xrspatial.pathfinding import multi_stop_search
305+
ds = xr.Dataset({'a': elevation, 'b': elevation + 100})
306+
waypoints = [(0, 0), (5, 5), (9, 9)]
307+
expected = multi_stop_search(ds, waypoints)
308+
result = ds.xrs.multi_stop_search(waypoints)
309+
xr.testing.assert_identical(result, expected)
310+
# supports_dataset routes each variable through its own surface
311+
assert set(result.data_vars) == {'a', 'b'}
312+
313+
302314
# ---------------------------------------------------------------------------
303315
# 5. Dataset single-input — accessor matches direct call
304316
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)