[GRASS-SVN] r42458 - grass-addons/raster/r.stream.distance
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Jun 3 07:32:18 EDT 2010
Author: mmetz
Date: 2010-06-03 07:32:18 -0400 (Thu, 03 Jun 2010)
New Revision: 42458
Modified:
grass-addons/raster/r.stream.distance/distance.c
grass-addons/raster/r.stream.distance/global.h
grass-addons/raster/r.stream.distance/inits.c
grass-addons/raster/r.stream.distance/io.c
grass-addons/raster/r.stream.distance/main.c
Log:
format and clean up code
Modified: grass-addons/raster/r.stream.distance/distance.c
===================================================================
--- grass-addons/raster/r.stream.distance/distance.c 2010-06-03 11:31:48 UTC (rev 42457)
+++ grass-addons/raster/r.stream.distance/distance.c 2010-06-03 11:32:18 UTC (rev 42458)
@@ -1,135 +1,138 @@
#include <grass/glocale.h>
#include "global.h"
-int calculate_upstream(void) {
- int r,c;
- int next_r, next_c;
- float easting, northing;
- float cell_easting, cell_northing;
- int i,j,k;
- int done;
- int n_inits=0;
- float cur_dist;
- POINT* d_inits;
- float* tmp_elevation=NULL; /* only for elevation */
- float tmp_dist=0;
- float tmp_elev=0;
- float target_elev=0;
- int tmp_inits=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 };
-
- for (r = 0; r < nrows; ++r) {
- for (c = 0; c < ncols; ++c) {
-
- 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; /* out of border */
-
- j = (i + 4) > 8 ? i - 4 : i + 4;
-
- if (dirs[r + nextr[i]][c + nextc[i]] == j &&
- distance[r][c]!=0) {/* is contributing cell */
- distance[r][c]=-1;
- break;
- }
- }
- if(distance[r][c]==1 && dirs[r][c]>0)
- n_inits++;
- else if (dirs[r][c]>0)
- distance[r][c]=-1;
- }
- }
+int calculate_upstream(void)
+{
+ int r, c;
+ int next_r, next_c;
+ float easting, northing;
+ float cell_easting, cell_northing;
+ int i, j, k;
+ int done;
+ int n_inits = 0;
+ float cur_dist;
+ POINT *d_inits;
+ float *tmp_elevation = NULL; /* only for elevation */
+ float tmp_dist = 0;
+ float target_elev = 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 };
- d_inits=(POINT *) G_malloc(n_inits * sizeof(POINT));
-
- if(out_elev) {
- tmp_elevation=(float *) G_malloc(nrows*ncols*sizeof(float));
- for (r = 0; r < nrows; ++r) {
- for (c = 0; c < ncols; ++c) {
- tmp_elevation[r*ncols+c]=elevation[r][c];
- }
+ for (r = 0; r < nrows; ++r) {
+ for (c = 0; c < ncols; ++c) {
+
+ 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; /* out of border */
+
+ j = (i + 4) > 8 ? i - 4 : i + 4;
+
+ if (dirs[r + nextr[i]][c + nextc[i]] == j && distance[r][c] != 0) { /* is contributing cell */
+ distance[r][c] = -1;
+ break;
}
+ }
+ if (distance[r][c] == 1 && dirs[r][c] > 0)
+ n_inits++;
+ else if (dirs[r][c] > 0)
+ distance[r][c] = -1;
}
-
-
- k=0;
+ }
+
+ d_inits = (POINT *) G_malloc(n_inits * sizeof(POINT));
+
+ if (out_elev) {
+ tmp_elevation = (float *)G_malloc(nrows * ncols * sizeof(float));
for (r = 0; r < nrows; ++r) {
- for (c = 0; c < ncols; ++c) {
-
- if(distance[r][c]==1) {
- distance[r][c]=0;
- if(out_elev)
- elevation[r][c]=0;
-
- if(dirs[r+nextr[dirs[r][c]]][c+nextc[dirs[r][c]]]<0)
- continue;
- if(distance[next_r][next_c]==0)
- continue;
-
- d_inits[k].r=r;
- d_inits[k].c=c;
- d_inits[k].cur_dist=0;
-
- if(out_elev)
- d_inits[k].target_elev=tmp_elevation[r*ncols+c];
-
- k++;
- }
- }
+ for (c = 0; c < ncols; ++c) {
+ tmp_elevation[r * ncols + c] = elevation[r][c];
+ }
}
-n_inits=k;
+ }
-while (n_inits>0) {
- k=0;
+ k = 0;
+ for (r = 0; r < nrows; ++r) {
+ for (c = 0; c < ncols; ++c) {
- for(i=0;i<n_inits;++i) {
- r=d_inits[i].r;
- c=d_inits[i].c;
- next_r=r+nextr[dirs[r][c]];
- next_c=c+nextc[dirs[r][c]];
- tmp_dist=d_inits[i].cur_dist;
-
- if(out_elev)
- target_elev=d_inits[i].target_elev;
-
- easting=window.west + (c + 0.5) * window.ew_res;
- northing=window.north - (r + 0.5) * window.ns_res;
- cell_easting=window.west + (next_c + 0.5) * window.ew_res;
- cell_northing=window.north - (next_r + 0.5) * window.ns_res;
-
- cur_dist=tmp_dist+G_distance(easting,northing,cell_easting,cell_northing);
-
-
- if(near)
- done=(distance[next_r][next_c]>cur_dist||distance[next_r][next_c]==-1) ? 1: 0;
- else
- done=(distance[next_r][next_c]<cur_dist||distance[next_r][next_c]==-1) ? 1: 0;
-
-
- if(done) {
- distance[next_r][next_c]=cur_dist;
- if(out_elev)
- elevation[next_r][next_c]=target_elev-tmp_elevation[next_r*ncols+next_c];
+ if (distance[r][c] == 1) {
+ distance[r][c] = 0;
+ if (out_elev)
+ elevation[r][c] = 0;
- if(dirs[r+nextr[dirs[r][c]]][c+nextc[dirs[r][c]]]<1)
- continue;
+ next_r = r + nextr[dirs[r][c]];
+ next_c = c + nextc[dirs[r][c]];
- d_inits[k].r=next_r;
- d_inits[k].c=next_c;
- d_inits[k].cur_dist=cur_dist;
+ if (dirs[next_r][next_c] < 0)
+ continue;
+ if (distance[next_r][next_c] == 0)
+ continue;
- if(out_elev)
- d_inits[k].target_elev=target_elev;
-
- k++;
- } /* end of if done */
+ d_inits[k].r = r;
+ d_inits[k].c = c;
+ d_inits[k].cur_dist = 0;
+
+ if (out_elev)
+ d_inits[k].target_elev = tmp_elevation[r * ncols + c];
+
+ k++;
+ }
}
- n_inits=k;
+ }
+ n_inits = k;
+
+ while (n_inits > 0) {
+ k = 0;
+
+ for (i = 0; i < n_inits; ++i) {
+ r = d_inits[i].r;
+ c = d_inits[i].c;
+ next_r = r + nextr[dirs[r][c]];
+ next_c = c + nextc[dirs[r][c]];
+ tmp_dist = d_inits[i].cur_dist;
+
+ if (out_elev)
+ target_elev = d_inits[i].target_elev;
+
+ easting = window.west + (c + 0.5) * window.ew_res;
+ northing = window.north - (r + 0.5) * window.ns_res;
+ cell_easting = window.west + (next_c + 0.5) * window.ew_res;
+ cell_northing = window.north - (next_r + 0.5) * window.ns_res;
+
+ cur_dist =
+ tmp_dist + G_distance(easting, northing, cell_easting,
+ cell_northing);
+
+ if (near)
+ done = (distance[next_r][next_c] > cur_dist ||
+ distance[next_r][next_c] == -1) ? 1 : 0;
+ else
+ done = (distance[next_r][next_c] < cur_dist ||
+ distance[next_r][next_c] == -1) ? 1 : 0;
+
+ if (done) {
+ distance[next_r][next_c] = cur_dist;
+ if (out_elev)
+ elevation[next_r][next_c] =
+ target_elev - tmp_elevation[next_r * ncols + next_c];
+
+ if (dirs[r + nextr[dirs[r][c]]][c + nextc[dirs[r][c]]] < 1)
+ continue;
+
+ d_inits[k].r = next_r;
+ d_inits[k].c = next_c;
+ d_inits[k].cur_dist = cur_dist;
+
+ if (out_elev)
+ d_inits[k].target_elev = target_elev;
+
+ k++;
+ } /* end of if done */
+ }
+ n_inits = k;
+ }
+ return 0;
}
-return 0;
-}
/*
@@ -139,11 +142,9 @@
*/
int fill_maps(OUTLET outlet)
{
-
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 r, c, val, i, j;
+ int r, c, i, j;
POINT n_cell;
float cur_dist = 0;
float tmp_dist = 0;
@@ -245,7 +246,7 @@
POINT fifo_return_del(void)
{
- if (head > fifo_max)
+ if (head >= fifo_max)
head = -1;
return fifo_outlet[++head];
}
Modified: grass-addons/raster/r.stream.distance/global.h
===================================================================
--- grass-addons/raster/r.stream.distance/global.h 2010-06-03 11:31:48 UTC (rev 42457)
+++ grass-addons/raster/r.stream.distance/global.h 2010-06-03 11:32:18 UTC (rev 42458)
@@ -22,30 +22,30 @@
#define RELATIVE 2
POINT {
- int r, c;
-float cur_dist;
-float target_elev;
- };
+ int r, c;
+ float cur_dist;
+ float target_elev;
+};
#define OUTLET struct outs
OUTLET {
- int r, c;
- float easting;
- float northing;
- };
+ int r, c;
+ float easting;
+ float northing;
+};
/* functions.c */
/* io.c */
-int open_raster(char *mapname);
+int open_raster(char *);
int create_maps(void);
+int write_maps(char *, FCELL **);
void free_streams (void);
int write_distance(void);
int write_elevation(void);
-int set_null_elev(void);
+int set_null(FCELL **);
/* inits */
-int find_chatchment_outlets(void);
int fill_catchments(OUTLET outlet);
int find_outlets(void);
int reset_distance(void);
Modified: grass-addons/raster/r.stream.distance/inits.c
===================================================================
--- grass-addons/raster/r.stream.distance/inits.c 2010-06-03 11:31:48 UTC (rev 42457)
+++ grass-addons/raster/r.stream.distance/inits.c 2010-06-03 11:32:18 UTC (rev 42458)
@@ -4,11 +4,10 @@
int find_outlets(void)
{
- int d, i, j; /* d: direction, i: iteration */
+ int d; /* d: direction */
int r, c;
int next_stream = -1, cur_stream;
int out_max = ncols + nrows;
-
int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
@@ -100,11 +99,9 @@
int fill_catchments(OUTLET outlet)
{
-
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 r, c, i, j;
+ int r, c, i, j;
float stop, val;
POINT n_cell;
@@ -113,7 +110,7 @@
r = outlet.r;
c = outlet.c;
val = 1;
- stop =0;
+ stop = 0;
distance[r][c] = stop;
@@ -125,14 +122,14 @@
j = (i + 4) > 8 ? i - 4 : i + 4;
if (dirs[r + nextr[i]][c + nextc[i]] == j) { /* countributing cell */
- // if (distance[r + nextr[i]][c + nextc[i]] == 0)
- // continue; /* other outlet */
+ /* if (distance[r + nextr[i]][c + nextc[i]] == 0) */
+ /* continue; */ /* other outlet */
- distance[r + nextr[i]][c + nextc[i]]=
- (distance[r + nextr[i]][c + nextc[i]] == stop) ?
- stop : val;
-
- //distance[r + nextr[i]][c + nextc[i]] = val;
+ distance[r + nextr[i]][c + nextc[i]] =
+ (distance[r + nextr[i]][c + nextc[i]] == stop) ?
+ stop : val;
+
+ /* distance[r + nextr[i]][c + nextc[i]] = val; */
n_cell.r = (r + nextr[i]);
n_cell.c = (c + nextc[i]);
fifo_insert(n_cell);
@@ -146,5 +143,3 @@
return 0;
}
-
-
Modified: grass-addons/raster/r.stream.distance/io.c
===================================================================
--- grass-addons/raster/r.stream.distance/io.c 2010-06-03 11:31:48 UTC (rev 42457)
+++ grass-addons/raster/r.stream.distance/io.c 2010-06-03 11:32:18 UTC (rev 42458)
@@ -86,7 +86,6 @@
G_fatal_error(_("Unable to read raster maps at row <%d>"), r);
}
-
if (in_elev) {
elevation[r] = (FCELL *) G_malloc(sizeof(FCELL) * ncols);
@@ -154,6 +153,7 @@
/* END dirs & streams & accums */
} /*end for r */
+
switch (elev_type) {
case CELL_TYPE:
G_free(r_dem_c);
@@ -163,7 +163,6 @@
break;
}
-
G_free(r_streams);
G_free(r_dirs);
G_percent(r, nrows, 2);
@@ -184,7 +183,7 @@
int write_maps(char *mapname, FCELL ** map)
{
- int r, c;
+ int r;
int fd = 0;
if ((fd = G_open_raster_new(mapname, FCELL_TYPE)) < 0)
Modified: grass-addons/raster/r.stream.distance/main.c
===================================================================
--- grass-addons/raster/r.stream.distance/main.c 2010-06-03 11:31:48 UTC (rev 42457)
+++ grass-addons/raster/r.stream.distance/main.c 2010-06-03 11:32:18 UTC (rev 42458)
@@ -32,20 +32,11 @@
int main(int argc, char *argv[])
{
-
struct GModule *module; /* GRASS module for parsing arguments */
- struct Option *in_dir_opt,
- *in_stm_opt,
- *in_elev_opt,
- *in_method_opt,
- *out_dist_opt,
- *out_elev_opt; /* options */
+ struct Option *in_dir_opt, *in_stm_opt, *in_elev_opt, *in_method_opt, *out_dist_opt, *out_elev_opt; /* options */
struct Flag *out_outs, *out_sub, *out_near; /* flags */
+ char *method_name[] = { "upstream", "downstream" };
- int link_max;
- char* method_name[]={"upstream","downstream"};
-
-
/* initialize GIS environment */
G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name() */
@@ -77,13 +68,13 @@
in_elev_opt->required = NO;
in_elev_opt->gisprompt = "old,cell,raster";
in_elev_opt->description = "Name of elevation map";
-
+
in_method_opt = G_define_option();
- in_method_opt->key = "method";
- in_method_opt->description ="Calculation method";
+ in_method_opt->key = "method";
+ in_method_opt->description = "Calculation method";
in_method_opt->type = TYPE_STRING;
in_method_opt->required = YES;
- in_method_opt->options ="upstream,downstream";
+ in_method_opt->options = "upstream,downstream";
in_method_opt->answer = "downstream";
/* output option - at least one is reqired */
@@ -113,8 +104,8 @@
out_sub->key = 's';
out_sub->description =
_("Calculate parameters for subbasins (ignored in stream mode)");
-
- out_near = G_define_flag();
+
+ out_near = G_define_flag();
out_near->key = 'n';
out_near->description =
_("Calculate nearest local maximum (ignored in downstream calculation)");
@@ -146,12 +137,11 @@
G_fatal_error(_("<%s> is an illegal file name"), out_elev);
}
+ if (!strcmp(in_method_opt->answer, "upstream"))
+ method = UPSTREAM;
+ else if (!strcmp(in_method_opt->answer, "downstream"))
+ method = DOWNSTREAM;
- if (!strcmp(in_method_opt->answer, "upstream"))
- method = UPSTREAM;
- else if (!strcmp(in_method_opt->answer, "downstream"))
- method = DOWNSTREAM;
-
nrows = G_window_rows();
ncols = G_window_cols();
G_get_window(&window);
@@ -161,35 +151,37 @@
find_outlets();
reset_distance();
free_streams();
- G_message(_("Calculate %s distance"),method_name[method]);
-
- if (method==UPSTREAM) {
- int j;
- fifo_max = 4 * (nrows + ncols);
- fifo_outlet = (POINT *) G_malloc((fifo_max + 1) * sizeof(POINT));
-
- for (j = 0; j < outlets_num; ++j) {
- fill_catchments(outlets[j]);
- }
- G_free(fifo_outlet);
- calculate_upstream();
- }
-
- if (method==DOWNSTREAM) {
-
- /* fifo queue for contributing cell */
+ G_message(_("Calculate %s distance"), method_name[method]);
- fifo_max = 4 * (nrows + ncols);
- fifo_outlet = (POINT *) G_malloc((fifo_max + 1) * sizeof(POINT));
- {
- int j;
- for (j = 0; j < outlets_num; ++j) {
- fill_maps(outlets[j]);
- }
- }
- G_free(fifo_outlet);
+ if (method == UPSTREAM) {
+ int j;
+
+ fifo_max = 4 * (nrows + ncols);
+ fifo_outlet = (POINT *) G_malloc((fifo_max + 1) * sizeof(POINT));
+
+ for (j = 0; j < outlets_num; ++j) {
+ fill_catchments(outlets[j]);
}
+ G_free(fifo_outlet);
+ calculate_upstream();
+ }
+ if (method == DOWNSTREAM) {
+
+ /* fifo queue for contributing cell */
+
+ fifo_max = 4 * (nrows + ncols);
+ fifo_outlet = (POINT *) G_malloc((fifo_max + 1) * sizeof(POINT));
+ {
+ int j;
+
+ for (j = 0; j < outlets_num; ++j) {
+ fill_maps(outlets[j]);
+ }
+ }
+ G_free(fifo_outlet);
+ }
+
if (out_elev) {
set_null(elevation);
write_maps(out_elev, elevation);
More information about the grass-commit
mailing list