[GRASS-SVN] r72905 - grass-addons/grass7/raster/r.accumulate
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Jun 25 14:28:53 PDT 2018
Author: hcho
Date: 2018-06-25 14:28:53 -0700 (Mon, 25 Jun 2018)
New Revision: 72905
Modified:
grass-addons/grass7/raster/r.accumulate/delineate_streams.c
grass-addons/grass7/raster/r.accumulate/main.c
Log:
r.accumulate: clean up
Modified: grass-addons/grass7/raster/r.accumulate/delineate_streams.c
===================================================================
--- grass-addons/grass7/raster/r.accumulate/delineate_streams.c 2018-06-25 13:29:37 UTC (rev 72904)
+++ grass-addons/grass7/raster/r.accumulate/delineate_streams.c 2018-06-25 21:28:53 UTC (rev 72905)
@@ -2,14 +2,13 @@
#include <grass/vector.h>
#include "global.h"
-static void trace_down(struct cell_map *, int, int, double, double, double,
- double, struct point_list *);
+static void trace_down(struct cell_map *, struct Cell_head *, int, int,
+ struct point_list *);
void delineate_streams(struct Map_info *Map, double thresh,
struct cell_map *dir_buf, struct raster_map *accum_buf)
{
struct Cell_head window;
- double w_off, n_off, ew_res, ns_res;
int rows = accum_buf->rows, cols = accum_buf->cols;
int row, col;
int i, j;
@@ -20,10 +19,6 @@
int stream_id = 0;
G_get_set_window(&window);
- ew_res = window.ew_res;
- ns_res = window.ns_res;
- w_off = window.west + 0.5 * ew_res;
- n_off = window.north - 0.5 * ns_res;
init_point_list(&pl);
Points = Vect_new_line_struct();
@@ -63,8 +58,8 @@
/* if headwater is found, trace down flow directions */
if (!has_thresh_inflow) {
reset_point_list(&pl);
- trace_down(dir_buf, row, col, w_off, n_off, ew_res, ns_res,
- &pl);
+ trace_down(dir_buf, &window, row, col, &pl);
+
/* if tracing is successful, write out the stream */
if (pl.n > 0) {
Vect_reset_line(Points);
@@ -83,9 +78,8 @@
Vect_destroy_cats_struct(Cats);
}
-static void trace_down(struct cell_map *dir_buf, int row, int col,
- double w_off, double n_off, double ew_res,
- double ns_res, struct point_list *pl)
+static void trace_down(struct cell_map *dir_buf, struct Cell_head *window,
+ int row, int col, struct point_list *pl)
{
static int next_cells[8][2] = {
{-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}
@@ -98,13 +92,13 @@
return;
/* add the current cell */
- add_point(pl, w_off + col * ew_res, n_off - row * ns_res);
+ add_point(pl, Rast_col_to_easting(col + 0.5, window),
+ Rast_row_to_northing(row + 0.5, window));
/* if the current cell doesn't flow out of the computational region
* (negative direction from r.watershed flows out), keep tracing */
dir = dir_buf->c[row][col] - 1;
if (dir >= 0 && dir < 8)
- trace_down(dir_buf, row + next_cells[dir][0],
- col + next_cells[dir][1], w_off, n_off, ew_res, ns_res,
- pl);
+ trace_down(dir_buf, window, row + next_cells[dir][0],
+ col + next_cells[dir][1], pl);
}
Modified: grass-addons/grass7/raster/r.accumulate/main.c
===================================================================
--- grass-addons/grass7/raster/r.accumulate/main.c 2018-06-25 13:29:37 UTC (rev 72904)
+++ grass-addons/grass7/raster/r.accumulate/main.c 2018-06-25 21:28:53 UTC (rev 72905)
@@ -48,7 +48,7 @@
} flag;
char *desc;
char *dir_name, *weight_name, *accum_name, *stream_name;
- int dir_fd, weight_fd, accum_fd;
+ int dir_fd;
double dir_format, thresh;
struct Range dir_range;
CELL dir_min, dir_max;
@@ -190,7 +190,8 @@
/* optionally, read a weight map */
if (weight_name) {
- weight_fd = Rast_open_old(weight_name, "");
+ int weight_fd = Rast_open_old(weight_name, "");
+
accum_buf.type = weight_buf.type = Rast_get_map_type(weight_fd);
weight_buf.rows = rows;
weight_buf.cols = cols;
@@ -204,7 +205,6 @@
Rast_close(weight_fd);
}
else {
- weight_fd = -1;
weight_buf.map.v = NULL;
accum_buf.type = CELL_TYPE;
}
@@ -216,14 +216,13 @@
for (row = 0; row < rows; row++)
accum_buf.map.v[row] = (void *)Rast_allocate_buf(accum_buf.type);
- /* create a new accumulation map if requested */
- accum_fd = accum_name ? Rast_open_new(accum_name, accum_buf.type) : -1;
-
/* accumulate flows */
accumulate(&dir_buf, &weight_buf, &accum_buf, done, neg);
- /* write out buffer to the accumulatoin map */
- if (accum_fd >= 0) {
+ /* write out buffer to the accumulatoin map if requested */
+ if (accum_name) {
+ int accum_fd = Rast_open_new(accum_name, accum_buf.type);
+
for (row = 0; row < rows; row++)
Rast_put_row(accum_fd, accum_buf.map.v[row], accum_buf.type);
Rast_close(accum_fd);
@@ -242,11 +241,11 @@
/* free buffer memory */
for (row = 0; row < rows; row++) {
G_free(done[row]);
- if (weight_fd >= 0)
+ if (weight_name)
G_free(weight_buf.map.v[row]);
}
G_free(done);
- if (weight_fd >= 0)
+ if (weight_name)
G_free(weight_buf.map.v);
/* delineate stream networks */
More information about the grass-commit
mailing list