From 4f549143f04c4606870451c54d227c746f5eccd8 Mon Sep 17 00:00:00 2001 From: Huidae Cho Date: Tue, 17 Oct 2023 12:42:28 -0600 Subject: [PATCH] r.flowaccumulate: Handle null values properly --- src/raster/r.flowaccumulation/accumulate.c | 7 +++---- src/raster/r.flowaccumulation/global.h | 8 ++++++++ src/raster/r.flowaccumulation/main.c | 11 ++++++++--- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/raster/r.flowaccumulation/accumulate.c b/src/raster/r.flowaccumulation/accumulate.c index fc0c9ca114..e36105a910 100644 --- a/src/raster/r.flowaccumulation/accumulate.c +++ b/src/raster/r.flowaccumulation/accumulate.c @@ -1,7 +1,6 @@ #include #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) | \ @@ -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 @@ -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); } @@ -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; diff --git a/src/raster/r.flowaccumulation/global.h b/src/raster/r.flowaccumulation/global.h index 7c4539664e..cf7b52f229 100644 --- a/src/raster/r.flowaccumulation/global.h +++ b/src/raster/r.flowaccumulation/global.h @@ -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 diff --git a/src/raster/r.flowaccumulation/main.c b/src/raster/r.flowaccumulation/main.c index 7c7931dbb3..eee5ae329a 100644 --- a/src/raster/r.flowaccumulation/main.c +++ b/src/raster/r.flowaccumulation/main.c @@ -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; @@ -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);