Skip to content

Commit 62d99f0

Browse files
committed
Fixed a bug in the DownstreamDistanceToStream tool
Fixed a bug in the DownstreamDistanceToStream tool
1 parent f5cc363 commit 62d99f0

2 files changed

Lines changed: 25 additions & 18 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tools/hydro_analysis/downslope_distance_to_stream.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
This tool is part of the WhiteboxTools geospatial analysis library.
33
Authors: Dr. John Lindsay
44
Created: 9/07/2017
5-
Last Modified: 12/10/2018
5+
Last Modified: 04/10/2019
66
License: MIT
77
*/
88

@@ -222,6 +222,9 @@ impl WhiteboxTool for DownslopeDistanceToStream {
222222
));
223223
}
224224

225+
/////////////////////////////////////////////
226+
// Perform the D8 flow pointer calculation //
227+
/////////////////////////////////////////////
225228
let num_procs = num_cpus::get() as isize;
226229
let (tx, rx) = mpsc::channel();
227230
for tid in 0..num_procs {
@@ -248,13 +251,13 @@ impl WhiteboxTool for DownslopeDistanceToStream {
248251
for row in (0..rows).filter(|r| r % num_procs == tid) {
249252
let mut data: Vec<i8> = vec![flow_nodata; columns as usize];
250253
for col in 0..columns {
251-
z = dem[(row, col)];
254+
z = dem.get_value(row, col);
252255
if z != nodata {
253256
dir = 0i8;
254257
max_slope = f64::MIN;
255258
neighbouring_nodata = false;
256259
for i in 0..8 {
257-
z_n = dem[(row + dy[i], col + dx[i])];
260+
z_n = dem.get_value(row + dy[i], col + dx[i]);
258261
if z_n != nodata {
259262
slope = (z - z_n) / grid_lengths[i];
260263
if slope > max_slope && slope > 0f64 {
@@ -294,18 +297,19 @@ impl WhiteboxTool for DownslopeDistanceToStream {
294297
interior_pit_found = true;
295298
}
296299
for col in 0..columns {
297-
if streams[(row, col)] > 0f64 && streams[(row, col)] != streams_nodata {
298-
output[(row, col)] = 0f64;
299-
stack.push((row, col, dem[(row, col)]));
300+
// stream cells get added to the stack; nodata cells get assigned that in the output
301+
if streams.get_value(row, col) > 0f64 && streams.get_value(row, col) != streams_nodata {
302+
output.set_value(row, col, 0f64);
303+
stack.push((row, col, 0f64));
300304
}
301-
if dem[(row, col)] == nodata {
302-
output[(row, col)] = nodata;
305+
if dem.get_value(row, col) == nodata {
306+
output.set_value(row, col, nodata);
303307
num_solved_cells += 1;
304308
}
305-
if flow_dir[(row, col)] == -1 {
306-
if output[(row, col)] != 0f64 {
309+
if flow_dir.get_value(row, col) == -1 {
310+
if output.get_value(row, col) != 0f64 {
307311
stack.push((row, col, nodata));
308-
output[(row, col)] = nodata;
312+
output.set_value(row, col, nodata);
309313
num_solved_cells += 1;
310314
}
311315
}
@@ -318,7 +322,10 @@ impl WhiteboxTool for DownslopeDistanceToStream {
318322
}
319323
}
320324
}
321-
325+
326+
////////////////////////////////////////////////
327+
// Calculate the downslope distance to stream //
328+
////////////////////////////////////////////////
322329
let num_cells = dem.num_cells();
323330
let mut stream_dist: f64;
324331
let mut dist: f64;
@@ -342,15 +349,15 @@ impl WhiteboxTool for DownslopeDistanceToStream {
342349
for n in 0..8 {
343350
row_n = row + dy[n];
344351
col_n = col + dx[n];
345-
if flow_dir[(row_n, col_n)] == inflowing_vals[n]
346-
&& output[(row_n, col_n)] == background_value
352+
if flow_dir.get_value(row_n, col_n) == inflowing_vals[n]
353+
&& output.get_value(row_n, col_n) == background_value
347354
{
348355
if stream_dist != nodata {
349356
dist = stream_dist + grid_lengths[n];
350-
output[(row_n, col_n)] = dist;
357+
output.set_value(row_n, col_n, dist);
351358
stack.push((row_n, col_n, dist));
352359
} else {
353-
output[(row_n, col_n)] = nodata;
360+
output.set_value(row_n, col_n, nodata);
354361
stack.push((row_n, col_n, nodata));
355362
}
356363
}
@@ -402,4 +409,4 @@ impl WhiteboxTool for DownslopeDistanceToStream {
402409

403410
Ok(())
404411
}
405-
}
412+
}

0 commit comments

Comments
 (0)