Cost distance function from WhiteboxTools (v.0.11.0) gets stuck in an endless loop after execution. The inputs are 8-bit unsigned interger GeoTIFFs. The source raster has values 1 and Null (size: 16 KB), and cost raster has values from 10-54 (size: 460 KB) (see below for an image of source (grey), cost raster (gradient white-to-red) and white is null/nodata). If necessary, I can provide the GeoTIFFs if needed.

WhiteboxTools - Cost distance code:

import sys
wbtools_dir = r'C:\WhiteboxTools_win_amd64\WBT'
sys.path.insert(0, wbtools_dir)

from whitebox_tools import WhiteboxTools

wbt = WhiteboxTools()

source = "c:/src2.tif"
cost = "c:/cst1.tif"
out_accum = "c:/cd.tif"
out_backlink = "c:/backlink.tif"

wbt.cost_distance(source, cost, out_accum, out_backlink)


* Welcome to CostDistance *
Reading source data...
Reading cost data...
Initializing: 0%
Initializing: 1%
Initializing: 99%
Initializing: 100%
Loop 1: 0%
Loop 1: 1%
Loop 1: 2%
Loop 1: 3%
Loop 4000: 1%

It takes 2-3 seconds to complete each loop and the process was killed after 30-45 minutes. I replicated the cost distance in ArcMap and run time was ~45 seconds.

The code comes directly from the WhiteboxTools user manual and it executes without any issues, so I suspect it is either user error (need to format my source and cost rasters in certain way) or Whitebox bug. Any ideas? Also, I am able to execute other Whitebox tools such as breach_depressions() and create valid outputs.

source and cost surfaces


This tool used an iterative approach to finding the minimum cost surface, which involved multiple directional scans of the input raster. The iteration continued until no further change could be performed on the output cost surface. It seemed that it was getting caught in an infinite loop of modifying a single pixel for certain input data. Nonetheless, I realized when you posted the question that instead of this iterative solution, the tool could use a priority-flood algorithm approach to finding the minimum cost surface. This new modification eliminates the need for iteration and significantly improves the efficiency of the code.

I have committed the new code for the CostDistance tool to the WhiteboxTools GitHub repository here. I have tested the new method and it seems to be a significant improvement in efficiency and robustness. I believe that this will resolve the issue that you are having with running the tool on your data set. I will release the updated tool in WhiteboxTools version 0.12, which will be available from the project homepage. I still have a few tools to complete/refine before releasing the next public version. Until then, if you wish to use the updated CostDistance tool, you may build the project from the source code. Instructions for doing so are found on the download page and the source code repository. If you have Rust installed on your computer, building WhiteboxTools is simply cargo build --release after cd'ing to the source code directory.

| improve this answer | |
  • Appreciate the detailed response! I was able to build the project from source code and execute the code successfully. However, the output looks 'corrupt' see the vertical stripes on the left. Maybe, I am overlooking something when I create my input data. Here's the source raster and cost raster. Since this might be beyond the scope of the original question, would it be okay if I emailed you? – user131921 Nov 19 '18 at 22:10
  • Yep, just send me an email and I'll see if I can figure out the new issue. – WhiteboxDev Nov 20 '18 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.