[GRASS-SVN] r42457 - grass-addons/raster/r.stream.del
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Jun 3 07:31:49 EDT 2010
Author: mmetz
Date: 2010-06-03 07:31:48 -0400 (Thu, 03 Jun 2010)
New Revision: 42457
Modified:
grass-addons/raster/r.stream.del/delete.c
grass-addons/raster/r.stream.del/global.h
grass-addons/raster/r.stream.del/io.c
grass-addons/raster/r.stream.del/main.c
Log:
format and clean up code
Modified: grass-addons/raster/r.stream.del/delete.c
===================================================================
--- grass-addons/raster/r.stream.del/delete.c 2010-06-03 11:30:57 UTC (rev 42456)
+++ grass-addons/raster/r.stream.del/delete.c 2010-06-03 11:31:48 UTC (rev 42457)
@@ -3,15 +3,14 @@
int trib_nums(int r, int c)
{ /* calcualte number of tributuaries */
-
int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
int trib = 0;
int i, j;
for (i = 1; i < 9; ++i) {
- if (r + nextr[i] < 0 || r + nextr[i] > (nrows - 1) ||
- c + nextc[i] < 0 || c + nextc[i] > (ncols - 1))
+ if (r + nextr[i] < 0 || r + nextr[i] > (nrows - 1) ||
+ c + nextc[i] < 0 || c + nextc[i] > (ncols - 1))
continue;
j = (i + 4) > 8 ? i - 4 : i + 4;
if (streams[r + nextr[i]][c + nextc[i]] > 0 &&
@@ -21,131 +20,126 @@
if (trib > 5)
G_fatal_error(_("Error finding nodes. Stream and direction maps probably do not match..."));
- return trib;
+ return trib;
} /* end trib_num */
int find_springs(int max_link)
{
-
- int d, i, j; /* d: direction, i: iteration */
int r, c;
- int trib_num, trib = 0;
- int cur_stream;
+ int trib_num;
int springs_num = 0;
- int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
- int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
G_message(_("Finding springs..."));
- springs = (SPRING *) G_malloc(max_link * sizeof(SPRING));
+ springs = (SPRING *) G_malloc(max_link * sizeof(SPRING));
- for (r = 0; r < nrows; ++r) {
- for (c = 0; c < ncols; ++c) {
+ for (r = 0; r < nrows; ++r) {
+ for (c = 0; c < ncols; ++c) {
if (streams[r][c] > 0) {
- trib_num = trib_nums(r, c);
- if (trib_num == 0) { /* adding springs */
+ trib_num = trib_nums(r, c);
+ if (trib_num == 0) { /* adding springs */
- if (springs_num > (max_link - 1))
- G_fatal_error(_("Error finding nodes. Stream and direction maps probably do not match..."));
+ if (springs_num > (max_link - 1))
+ G_fatal_error(_("Error finding nodes. Stream and direction maps probably do not match..."));
- springs[springs_num].r=r;
- springs[springs_num].c=c;
- springs[springs_num++].val = streams[r][c]; /* collecting springs */
+ springs[springs_num].r = r;
+ springs[springs_num].c = c;
+ springs[springs_num++].val = streams[r][c]; /* collecting springs */
}
} /* end if streams */
} /* c */
} /* r */
- return springs_num;
+ return springs_num;
}
-int delete_join (int springs_num)
+int delete_join(int springs_num)
{
-
- int i,j, d;
- int r,c,nr,nc;
- int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
- int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
- int cell_num=1;
- int cur_stream,tmp_stream;
- int trib_num;
+ int i, j, d;
+ int r, c, nr, nc;
+ int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
+ int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
+ int cell_num = 1;
+ int cur_stream, tmp_stream;
+ int trib_num;
-G_message(_("Deleting short streams..."));
+ G_message(_("Deleting short streams..."));
-for (j=0;j<springs_num;++j) {
+ for (j = 0; j < springs_num; ++j) {
-r=springs[j].r;
-c=springs[j].c;
-cell_num=1;
+ r = springs[j].r;
+ c = springs[j].c;
+ cell_num = 1;
- while (cell_num<threshold && dirs[r][c]>0) {
- nr=r+nextr[dirs[r][c]];
- nc=c+nextc[dirs[r][c]];
-
- if (streams[nr][nc]==streams[r][c]) {
- cell_num++;
- r=nr;
- c=nc;
- } else {
- break; /* if stream isn't longer due to joining in previous interation */
- }
+ while (cell_num < threshold && dirs[r][c] > 0) {
+ nr = r + nextr[dirs[r][c]];
+ nc = c + nextc[dirs[r][c]];
- } /*end while */
+ if (streams[nr][nc] == streams[r][c]) {
+ cell_num++;
+ r = nr;
+ c = nc;
+ }
+ else {
+ break; /* if stream isn't longer due to joining in previous interation */
+ }
- if(cell_num<threshold) {
+ } /*end while */
+ if (cell_num < threshold) {
- r=springs[j].r;
- c=springs[j].c;
- cur_stream=streams[r][c];
- streams[r][c]=0;
- nr=r+nextr[dirs[r][c]];
- nc=c+nextc[dirs[r][c]];
+ r = springs[j].r;
+ c = springs[j].c;
+ cur_stream = streams[r][c];
+ streams[r][c] = 0;
+ nr = r + nextr[dirs[r][c]];
+ nc = c + nextc[dirs[r][c]];
- while (dirs[r][c]>0 && streams[nr][nc]==cur_stream)
- { /* only when there is more than one cell in stream */
- r=nr;
- c=nc;
- streams[r][c]=0;
- nr=r+nextr[dirs[r][c]];
- nc=c+nextc[dirs[r][c]];
- }
+ while (dirs[r][c] > 0 && streams[nr][nc] == cur_stream) { /* only when there is more than one cell in stream */
+ r = nr;
+ c = nc;
+ streams[r][c] = 0;
+ nr = r + nextr[dirs[r][c]];
+ nc = c + nextc[dirs[r][c]];
+ }
- trib_num = trib_nums(nr, nc); // check if there are no addational tributuaries
+ trib_num = trib_nums(nr, nc); /* check if there are no addational tributuaries */
- if (trib_num != 1) {
- continue; //still is node (>1), or border (==0) finish and go to the next spring
- } else {
- r=nr;
- c=nc;
- cur_stream=0;
- for (i = 1; i < 9; ++i) {
- if (r + nextr[i] < 0 || r + nextr[i] > (nrows - 1) ||
- c + nextc[i] < 0 || c + nextc[i] > (ncols - 1))
- continue; /* border */
- d = (i + 4) > 8 ? i - 4 : i + 4;
- if (streams[r + nextr[i]][c + nextc[i]] > 0 && dirs[r + nextr[i]][c + nextc[i]] == d)
- cur_stream=streams[r + nextr[i]][c + nextc[i]];
- }
- if(cur_stream==0)
- G_fatal_error(_("Problem with joining streams"));
-
- tmp_stream=streams[r][c];
- nr=r+nextr[dirs[r][c]];
- nc=c+nextc[dirs[r][c]];
- streams[r][c]=cur_stream;
+ if (trib_num != 1) {
+ continue; /* still is node (>1), or border (==0) finish and go to the next spring */
+ }
+ else {
+ r = nr;
+ c = nc;
+ cur_stream = 0;
+ for (i = 1; i < 9; ++i) {
+ if (r + nextr[i] < 0 || r + nextr[i] > (nrows - 1) ||
+ c + nextc[i] < 0 || c + nextc[i] > (ncols - 1))
+ continue; /* border */
+ d = (i + 4) > 8 ? i - 4 : i + 4;
+ if (streams[r + nextr[i]][c + nextc[i]] > 0 &&
+ dirs[r + nextr[i]][c + nextc[i]] == d)
+ cur_stream = streams[r + nextr[i]][c + nextc[i]];
+ }
+ if (cur_stream == 0)
+ G_fatal_error(_("Problem with joining streams"));
- while (dirs[r][c]>0 && streams[nr][nc]==tmp_stream) {
- r=nr;
- c=nc;
- nr=r+nextr[dirs[r][c]];
- nc=c+nextc[dirs[r][c]];
- streams[r][c]=cur_stream;
- }
- } /*end if_elese */
- } /* end if cell_num*/
-} /*end for */
-return 0;
-} /*end function */
+ tmp_stream = streams[r][c];
+ nr = r + nextr[dirs[r][c]];
+ nc = c + nextc[dirs[r][c]];
+ streams[r][c] = cur_stream;
+
+ while (dirs[r][c] > 0 && streams[nr][nc] == tmp_stream) {
+ r = nr;
+ c = nc;
+ nr = r + nextr[dirs[r][c]];
+ nc = c + nextc[dirs[r][c]];
+ streams[r][c] = cur_stream;
+ }
+ } /*end if_elese */
+ } /* end if cell_num */
+ } /*end for */
+ return 0;
+} /*end function */
Modified: grass-addons/raster/r.stream.del/global.h
===================================================================
--- grass-addons/raster/r.stream.del/global.h 2010-06-03 11:30:57 UTC (rev 42456)
+++ grass-addons/raster/r.stream.del/global.h 2010-06-03 11:31:48 UTC (rev 42457)
@@ -9,9 +9,9 @@
#define SPRING struct sprs
SPRING {
- int r, c;
- int val;
- };
+ int r, c;
+ int val;
+};
/* functions.c */
Modified: grass-addons/raster/r.stream.del/io.c
===================================================================
--- grass-addons/raster/r.stream.del/io.c 2010-06-03 11:30:57 UTC (rev 42456)
+++ grass-addons/raster/r.stream.del/io.c 2010-06-03 11:31:48 UTC (rev 42457)
@@ -32,7 +32,6 @@
{
int r, c;
int in_dir_fd, in_stm_fd; /* input file descriptors: indir_fd - direction.... etc */
-
CELL *r_dirs, *r_streams;
in_dir_fd = open_raster(in_dirs);
@@ -82,7 +81,6 @@
/* END dirs & streams & accums */
-
} /*end for r */
G_free(r_dirs);
Modified: grass-addons/raster/r.stream.del/main.c
===================================================================
--- grass-addons/raster/r.stream.del/main.c 2010-06-03 11:30:57 UTC (rev 42456)
+++ grass-addons/raster/r.stream.del/main.c 2010-06-03 11:31:48 UTC (rev 42457)
@@ -30,11 +30,9 @@
*/
int main(int argc, char *argv[])
{
-
struct GModule *module; /* GRASS module for parsing arguments */
struct Option *in_dir_opt, *in_stm_opt, *in_threshold, *out_opt; /* options */
struct Flag *out_back; /* flags */
-
int num_spring, link_max;
/* initialize GIS environment */
@@ -63,8 +61,8 @@
in_threshold->key = "threshold";
in_threshold->label = _("Minimum number of cell in stream");
in_threshold->description = _("Must be > 0");
- in_threshold->required = YES;
- in_threshold->type = TYPE_INTEGER;
+ in_threshold->required = YES;
+ in_threshold->type = TYPE_INTEGER;
out_opt = G_define_option();
out_opt->key = "reduced";
@@ -74,14 +72,11 @@
out_opt->gisprompt = "new,cell,raster";
out_opt->description = "Output reduced stream map";
-
/* Define the different flags */
out_back = G_define_flag();
out_back->key = 'z';
out_back->description = _("Create zero-value background instead of NULL");
-
-
if (G_parser(argc, argv)) /* parser */
exit(EXIT_FAILURE);
@@ -95,7 +90,6 @@
if (threshold <= 0)
G_fatal_error(_("Threshold must be > 0"));
-
if (G_legal_filename(out_streams) < 0)
G_fatal_error(_("<%s> is an illegal file name"), out_streams);
@@ -104,11 +98,11 @@
ncols = G_window_cols();
create_maps();
link_max = max_link();
- num_spring=find_springs(link_max);
- delete_join(num_spring);
- if (!zeros)
+ num_spring = find_springs(link_max);
+ delete_join(num_spring);
+ if (!zeros)
set_null();
- write_map();
+ write_map();
exit(EXIT_SUCCESS);
}
More information about the grass-commit
mailing list