Skip to content

Commit

Permalink
r.accumulate: Rename map member to cells (#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
HuidaeCho committed Oct 15, 2023
1 parent 428acca commit 4e69b1e
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/raster/r.accumulate/accumulate_iterative.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ static void trace_up(struct cell_map *dir_buf, struct raster_map *weight_buf,
/* if a weight map is specified (no negative accumulation is
* implied), use the weight value at the current cell;
* otherwise use 1 */
accum += weight_buf->map.v
accum += weight_buf->cells.v
? get(weight_buf, cur_up->row, cur_up->col)
: 1.0;

Expand Down Expand Up @@ -213,7 +213,7 @@ static void find_up(struct cell_map *dir_buf, struct raster_map *weight_buf,
if (!*nup) {
/* if a weight map is specified (no negative accumulation is implied),
* use the weight value at the current cell; otherwise use 1 */
double accum = weight_buf->map.v ? get(weight_buf, row, col) : 1.0;
double accum = weight_buf->cells.v ? get(weight_buf, row, col) : 1.0;

/* if negative accumulation is desired and the current cell is
* incomplete, use a negative cell count without weighting; otherwise
Expand Down
2 changes: 1 addition & 1 deletion src/raster/r.accumulate/accumulate_recursive.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static double trace_up(struct cell_map *dir_buf, struct raster_map *weight_buf,

/* if a weight map is specified (no negative accumulation is implied), use
* the weight value at the current cell; otherwise use 1 */
accum = weight_buf->map.v ? get(weight_buf, row, col) : 1.0;
accum = weight_buf->cells.v ? get(weight_buf, row, col) : 1.0;

/* loop through all neighbor cells and see if any of them drains into the
* current cell (are there upstream cells?) */
Expand Down
7 changes: 4 additions & 3 deletions src/raster/r.accumulate/calculate_lfp_iterative.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,10 @@ static void find_up(struct cell_map *dir_buf, struct raster_map *accum_buf,
/* diagonal if i * j == -1 or 1
* horizontal if i == 0
* vertical if j == 0 */
double length = down_length + (i * j ? diag_length
: (i ? window.ns_res
: window.ew_res));
double length =
down_length +
(i && j ? diag_length
: (i ? window.ns_res : window.ew_res));

up[*nup].row = row + i;
up[*nup].col = col + j;
Expand Down
7 changes: 4 additions & 3 deletions src/raster/r.accumulate/calculate_lfp_recursive.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,10 @@ static int trace_up(struct cell_map *dir_buf, struct raster_map *accum_buf,
/* diagonal if i * j == -1 or 1
* horizontal if i == 0
* vertical if j == 0 */
double length = down_length + (i * j ? diag_length
: (i ? window.ns_res
: window.ew_res));
double length =
down_length +
(i && j ? diag_length
: (i ? window.ns_res : window.ew_res));

up[nup].row = row + i;
up[nup].col = col + j;
Expand Down
2 changes: 1 addition & 1 deletion src/raster/r.accumulate/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct raster_map {
CELL **c;
FCELL **f;
DCELL **d;
} map;
} cells;
};

struct point_list {
Expand Down
35 changes: 18 additions & 17 deletions src/raster/r.accumulate/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,23 +491,23 @@ int main(int argc, char *argv[])

accum_buf.nrows = nrows;
accum_buf.ncols = ncols;
accum_buf.map.v = (void **)G_malloc(nrows * sizeof(void *));
accum_buf.cells.v = (void **)G_malloc(nrows * sizeof(void *));

/* optionally, read a weight map */
weight_buf.map.v = NULL;
weight_buf.cells.v = NULL;
if (weight_name) {
int weight_fd = Rast_open_old(weight_name, "");

accum_buf.type = weight_buf.type = Rast_get_map_type(weight_fd);
weight_buf.nrows = nrows;
weight_buf.ncols = ncols;
weight_buf.map.v = (void **)G_malloc(nrows * sizeof(void *));
weight_buf.cells.v = (void **)G_malloc(nrows * sizeof(void *));
G_message(_("Reading weight map..."));
for (row = 0; row < nrows; row++) {
G_percent(row, nrows, 1);
weight_buf.map.v[row] =
weight_buf.cells.v[row] =
(void *)Rast_allocate_buf(weight_buf.type);
Rast_get_row(weight_fd, weight_buf.map.v[row], row,
Rast_get_row(weight_fd, weight_buf.cells.v[row], row,
weight_buf.type);
}
G_percent(1, 1, 1);
Expand All @@ -528,9 +528,9 @@ int main(int argc, char *argv[])
: _("Reading subaccumulation map..."));
for (row = 0; row < nrows; row++) {
G_percent(row, nrows, 1);
accum_buf.map.v[row] =
accum_buf.cells.v[row] =
(void *)Rast_allocate_buf(accum_buf.type);
Rast_get_row(accum_fd, accum_buf.map.v[row], row,
Rast_get_row(accum_fd, accum_buf.cells.v[row], row,
accum_buf.type);
}
G_percent(1, 1, 1);
Expand All @@ -544,7 +544,7 @@ int main(int argc, char *argv[])
for (row = 0; row < nrows; row++) {
G_percent(row, nrows, 1);
done[row] = (char *)G_calloc(ncols, 1);
accum_buf.map.v[row] =
accum_buf.cells.v[row] =
(void *)Rast_allocate_buf(accum_buf.type);
}
G_percent(1, 1, 1);
Expand All @@ -562,10 +562,10 @@ int main(int argc, char *argv[])
}

/* free buffer memory */
if (weight_buf.map.v) {
if (weight_buf.cells.v) {
for (row = 0; row < nrows; row++)
G_free(weight_buf.map.v[row]);
G_free(weight_buf.map.v);
G_free(weight_buf.cells.v[row]);
G_free(weight_buf.cells.v);
}

/* write out buffer to the accumulation map if requested */
Expand All @@ -576,7 +576,7 @@ int main(int argc, char *argv[])
G_message(_("Writing accumulation map..."));
for (row = 0; row < nrows; row++) {
G_percent(row, nrows, 1);
Rast_put_row(accum_fd, accum_buf.map.v[row], accum_buf.type);
Rast_put_row(accum_fd, accum_buf.cells.v[row], accum_buf.type);
}
G_percent(1, 1, 1);
Rast_close(accum_fd);
Expand All @@ -595,7 +595,7 @@ int main(int argc, char *argv[])
}
}
else
accum_buf.map.v = NULL;
accum_buf.cells.v = NULL;

/* delineate stream networks */
if (stream_name) {
Expand Down Expand Up @@ -625,7 +625,8 @@ int main(int argc, char *argv[])
G_message(_("Writing subaccumulation map..."));
for (row = 0; row < nrows; row++) {
G_percent(row, nrows, 1);
Rast_put_row(subaccum_fd, accum_buf.map.v[row], accum_buf.type);
Rast_put_row(subaccum_fd, accum_buf.cells.v[row],
accum_buf.type);
}
G_percent(1, 1, 1);
Rast_close(subaccum_fd);
Expand Down Expand Up @@ -664,10 +665,10 @@ int main(int argc, char *argv[])
}

/* free buffer memory */
if (accum_buf.map.v) {
if (accum_buf.cells.v) {
for (row = 0; row < nrows; row++)
G_free(accum_buf.map.v[row]);
G_free(accum_buf.map.v);
G_free(accum_buf.cells.v[row]);
G_free(accum_buf.cells.v);
}

/* delineate subwatersheds; this process overwrites dir_buf to save memory
Expand Down
24 changes: 12 additions & 12 deletions src/raster/r.accumulate/raster.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ void set(struct raster_map *buf, int row, int col, double value)
{
switch (buf->type) {
case CELL_TYPE:
buf->map.c[row][col] = (CELL)value;
buf->cells.c[row][col] = (CELL)value;
break;
case FCELL_TYPE:
buf->map.f[row][col] = (FCELL)value;
buf->cells.f[row][col] = (FCELL)value;
break;
case DCELL_TYPE:
buf->map.d[row][col] = (DCELL)value;
buf->cells.d[row][col] = (DCELL)value;
break;
}
}
Expand All @@ -22,13 +22,13 @@ double get(struct raster_map *buf, int row, int col)

switch (buf->type) {
case CELL_TYPE:
value = (double)buf->map.c[row][col];
value = (double)buf->cells.c[row][col];
break;
case FCELL_TYPE:
value = (double)buf->map.f[row][col];
value = (double)buf->cells.f[row][col];
break;
case DCELL_TYPE:
value = buf->map.d[row][col];
value = buf->cells.d[row][col];
break;
}

Expand All @@ -41,13 +41,13 @@ int is_null(struct raster_map *buf, int row, int col)

switch (buf->type) {
case CELL_TYPE:
is_null_value = Rast_is_c_null_value(&buf->map.c[row][col]);
is_null_value = Rast_is_c_null_value(&buf->cells.c[row][col]);
break;
case FCELL_TYPE:
is_null_value = Rast_is_f_null_value(&buf->map.f[row][col]);
is_null_value = Rast_is_f_null_value(&buf->cells.f[row][col]);
break;
case DCELL_TYPE:
is_null_value = Rast_is_d_null_value(&buf->map.d[row][col]);
is_null_value = Rast_is_d_null_value(&buf->cells.d[row][col]);
break;
}

Expand All @@ -58,13 +58,13 @@ void set_null(struct raster_map *buf, int row, int col)
{
switch (buf->type) {
case CELL_TYPE:
Rast_set_c_null_value(&buf->map.c[row][col], 1);
Rast_set_c_null_value(&buf->cells.c[row][col], 1);
break;
case FCELL_TYPE:
Rast_set_f_null_value(&buf->map.f[row][col], 1);
Rast_set_f_null_value(&buf->cells.f[row][col], 1);
break;
case DCELL_TYPE:
Rast_set_d_null_value(&buf->map.d[row][col], 1);
Rast_set_d_null_value(&buf->cells.d[row][col], 1);
break;
}
}

0 comments on commit 4e69b1e

Please sign in to comment.