Skip to content

Commit

Permalink
r.flowaccumulate: Handle null values properly (#957)
Browse files Browse the repository at this point in the history
  • Loading branch information
HuidaeCho committed Oct 17, 2023
1 parent 371f936 commit 59a9207
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
7 changes: 3 additions & 4 deletions src/raster/r.flowaccumulation/accumulate.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <stdlib.h>
#include "global.h"

#define DIR_NULL 0x80000000
#define ACCUM(row, col) accum_map->cells[(size_t)(row)*ncols + (col)]
#define FIND_UP(row, col) \
((row > 0 ? (col > 0 && DIR(row - 1, col - 1) == SE ? NW : 0) | \
Expand Down Expand Up @@ -41,7 +40,7 @@ void accumulate(struct raster_map *dir_map, struct raster_map *accum_map)
#pragma omp parallel for schedule(dynamic) private(col)
for (row = 0; row < nrows; row++) {
for (col = 0; col < ncols; col++)
if (DIR(row, col) != DIR_NULL)
if (DIR(row, col) != cell_null)
UP(row, col) = FIND_UP(row, col);
}
#endif
Expand All @@ -51,7 +50,7 @@ void accumulate(struct raster_map *dir_map, struct raster_map *accum_map)
for (col = 0; col < ncols; col++)
/* if the current cell is not null and has no upstream cells, start
* tracing down */
if (DIR(row, col) != DIR_NULL && !UP(row, col))
if (DIR(row, col) != cell_null && !UP(row, col))
trace_down(dir_map, accum_map, row, col, 1);
}

Expand Down Expand Up @@ -103,7 +102,7 @@ static void trace_down(struct raster_map *dir_map, struct raster_map *accum_map,
/* if the downstream cell is null or any upstream cells of the downstream
* cell have never been visited, stop tracing down */
if (row < 0 || row >= nrows || col < 0 || col >= ncols ||
DIR(row, col) == DIR_NULL || !(up = UP(row, col)) ||
DIR(row, col) == cell_null || !(up = UP(row, col)) ||
!(accum_up = sum_up(accum_map, row, col, up)))
return;

Expand Down
8 changes: 8 additions & 0 deletions src/raster/r.flowaccumulation/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ long long timeval_diff(struct timeval *, struct timeval *, struct timeval *);
/* accumulate.c */
void accumulate(struct raster_map *, struct raster_map *);

#ifdef _MAIN_C_
#define GLOBAL
#else
#define GLOBAL extern
#endif

GLOBAL CELL cell_null;

#endif
11 changes: 8 additions & 3 deletions src/raster/r.flowaccumulation/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ int main(int argc, char *argv[])
nrows = Rast_window_rows();
ncols = Rast_window_cols();

Rast_set_c_null_value(&cell_null, 1);

dir_map = G_malloc(sizeof *dir_map);
dir_map->nrows = nrows;
dir_map->ncols = ncols;
Expand All @@ -186,15 +188,18 @@ int main(int argc, char *argv[])
Rast_get_c_row(dir_fd, dir_map->cells + ncols * row, row);
if (dir_format == DIR_DEG) {
for (col = 0; col < ncols; col++)
DIR(row, col) = pow(2, abs(DIR(row, col) / 45.));
if (DIR(row, col) != cell_null)
DIR(row, col) = pow(2, abs(DIR(row, col) / 45.));
}
else if (dir_format == DIR_DEG45) {
for (col = 0; col < ncols; col++)
DIR(row, col) = pow(2, 8 - abs(DIR(row, col)));
if (DIR(row, col) != cell_null)
DIR(row, col) = pow(2, 8 - abs(DIR(row, col)));
}
else {
for (col = 0; col < ncols; col++)
DIR(row, col) = abs(DIR(row, col));
if (DIR(row, col) != cell_null)
DIR(row, col) = abs(DIR(row, col));
}
}
G_percent(1, 1, 1);
Expand Down

0 comments on commit 59a9207

Please sign in to comment.