[GRASS-SVN] r42460 - grass-addons/raster/r.stream.pos

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jun 3 07:33:50 EDT 2010


Author: mmetz
Date: 2010-06-03 07:33:50 -0400 (Thu, 03 Jun 2010)
New Revision: 42460

Modified:
   grass-addons/raster/r.stream.pos/global.h
   grass-addons/raster/r.stream.pos/io.c
   grass-addons/raster/r.stream.pos/main.c
   grass-addons/raster/r.stream.pos/pos.c
Log:
format and clean up code

Modified: grass-addons/raster/r.stream.pos/global.h
===================================================================
--- grass-addons/raster/r.stream.pos/global.h	2010-06-03 11:33:08 UTC (rev 42459)
+++ grass-addons/raster/r.stream.pos/global.h	2010-06-03 11:33:50 UTC (rev 42460)
@@ -40,7 +40,7 @@
 STREAM {
     int stream;			/* index */
     int init_r, init_c;
-  };
+};
 
 	
 
@@ -60,7 +60,7 @@
 int trib_nums(int r, int c);
 int init_streams(void);
 int find_inits(void);
-int calculate (void);
+int calculate(void);
 
 
 	  /* variables */

Modified: grass-addons/raster/r.stream.pos/io.c
===================================================================
--- grass-addons/raster/r.stream.pos/io.c	2010-06-03 11:33:08 UTC (rev 42459)
+++ grass-addons/raster/r.stream.pos/io.c	2010-06-03 11:33:50 UTC (rev 42460)
@@ -4,7 +4,6 @@
 
 int open_raster(char *mapname)
 {
-
     int fd = 0;
     char *mapset;
     struct Cell_head cellhd;	/* it stores region information, and header information of rasters */
@@ -14,8 +13,8 @@
     if (mapset == NULL)
 	G_fatal_error(_("Raster map <%s> not found"), mapname);
 
-  /* checking if the input maps type is CELL */ 
- 		if (G_raster_map_type(mapname, mapset) != CELL_TYPE)	
+    /* checking if the input maps type is CELL */
+    if (G_raster_map_type(mapname, mapset) != CELL_TYPE)
 	G_fatal_error(_("<%s> is not of type CELL"), mapname);
 
     if ((fd = G_open_cell_old(mapname, mapset)) < 0)	/* file descriptor */
@@ -35,22 +34,21 @@
 int create_base_maps(void)
 {
     int r, c;
-    CELL *r_dirs=NULL, *r_streams=NULL, *r_mask=NULL;
-    
-    int in_dir_fd=0, in_stm_fd=0, in_mask_fd=0;	/* input file descriptors: indir_fd - direction.... etc */
+    CELL *r_dirs = NULL, *r_streams = NULL;
+    int in_dir_fd = 0, in_stm_fd = 0;	/* input file descriptors: indir_fd - direction.... etc */
 
     in_dir_fd = open_raster(in_dirs);
     in_stm_fd = open_raster(in_streams);
-		
+
     dirs = (CELL **) G_malloc(sizeof(CELL *) * nrows);
-		streams = (CELL **) G_malloc(sizeof(CELL *) * nrows);
-		
-		if(out_streams_length)
-		streams_length = (FCELL **) G_malloc(sizeof(FCELL *) * nrows);
+    streams = (CELL **) G_malloc(sizeof(CELL *) * nrows);
 
+    if (out_streams_length)
+	streams_length = (FCELL **) G_malloc(sizeof(FCELL *) * nrows);
+
     r_dirs = (CELL *) G_malloc(sizeof(CELL) * ncols);
     r_streams = (CELL *) G_malloc(sizeof(CELL) * ncols);
-	
+
     G_message("Reading maps...");
 
     for (r = 0; r < nrows; ++r) {
@@ -58,65 +56,62 @@
 	/* dirs & streams */
 	dirs[r] = G_malloc(sizeof(CELL) * ncols);
 	streams[r] = G_malloc(sizeof(CELL) * ncols);
-	
-		if(out_streams_length)
-	streams_length[r] = G_malloc(sizeof(FCELL *) * ncols);
 
-	
+	if (out_streams_length)
+	    streams_length[r] = G_malloc(sizeof(FCELL *) * ncols);
+
+
 	if (G_get_c_raster_row(in_dir_fd, r_dirs, r) < 0 ||
-	    G_get_c_raster_row(in_stm_fd, r_streams, r) < 0 )
-	     {
+	    G_get_c_raster_row(in_stm_fd, r_streams, r) < 0) {
 	    G_close_cell(in_dir_fd);
 	    G_close_cell(in_stm_fd);
 
 	    G_fatal_error(_("Unable to read raster maps at row <%d>"), r);
 	}
-	
 
 	for (c = 0; c < ncols; ++c) {
-	    if (G_is_c_null_value(&r_dirs[c])) 
+	    if (G_is_c_null_value(&r_dirs[c]))
 		dirs[r][c] = 0;
-	    else 
+	    else
 		dirs[r][c] = r_dirs[c];
-	   
+
 	    if (G_is_c_null_value(&r_streams[c]))
 		streams[r][c] = 0;
-	    else 
-	   streams[r][c] = r_streams[c];
-	   
-		if(out_streams_length)
-	streams_length[r][c] = 0.;
-	   
-	   
+	    else
+		streams[r][c] = r_streams[c];
+
+	    if (out_streams_length)
+		streams_length[r][c] = 0.;
+
 	}			/* end for c */
 
-    } /* end for r */
+    }				/* end for r */
     G_free(r_streams);
     G_free(r_dirs);
     G_close_cell(in_dir_fd);
     G_close_cell(in_stm_fd);
-		G_percent(r, nrows, 2);
+    G_percent(r, nrows, 2);
     return 0;
 }				/* end create base maps */
 
 /*
-int stream_number(void)
-{
-	char *cur_mapset;
-	CELL c_min, c_max;
-	struct Range stream_range;
-	G_init_range(&stream_range);
-	cur_mapset = G_find_cell2(in_streams, "");
-	G_read_range(in_streams,cur_mapset,&stream_range);
-	G_get_range_min_max(&stream_range, &c_min, &c_max);
-	stream_num=c_max;
-	return 0;
-}
-*/
+   int stream_number(void)
+   {
+   char *cur_mapset;
+   CELL c_min, c_max;
+   struct Range stream_range;
+   G_init_range(&stream_range);
+   cur_mapset = G_find_cell2(in_streams, "");
+   G_read_range(in_streams,cur_mapset,&stream_range);
+   G_get_range_min_max(&stream_range, &c_min, &c_max);
+   stream_num=c_max;
+   return 0;
+   }
+ */
 
 int write_maps_f(char *mapname, FCELL ** map)
 {
-    int r, c;
+    int r;
     int fd = 0;
 
     if ((fd = G_open_raster_new(mapname, FCELL_TYPE)) < 0)
@@ -136,7 +131,7 @@
 
 int write_maps_c(char *mapname, CELL ** map)
 {
-    int r, c;
+    int r;
     int fd = 0;
 
     if ((fd = G_open_raster_new(mapname, CELL_TYPE)) < 0)

Modified: grass-addons/raster/r.stream.pos/main.c
===================================================================
--- grass-addons/raster/r.stream.pos/main.c	2010-06-03 11:33:08 UTC (rev 42459)
+++ grass-addons/raster/r.stream.pos/main.c	2010-06-03 11:33:50 UTC (rev 42460)
@@ -25,15 +25,15 @@
 int main(int argc, char *argv[])
 {
     struct GModule *module;	/* GRASS module for parsing arguments */
-    struct Option *in_dir_opt, *in_stm_opt, *in_multipier, *in_order, *out_str_length, *out_str;	/* options */
-		struct Flag *out_seq;	/* flags */
+    struct Option *in_dir_opt, *in_stm_opt, *in_multipier, *out_str_length, *out_str;	/* options */
+    struct Flag *out_seq;	/* flags */
+
     /* initialize GIS environment */
     G_gisinit(argv[0]);		/* reads grass env, stores program name to G_program_name() */
 
     /* initialize module */
     module = G_define_module();
-    module->keywords =
-	_("stream, order, route azimuth, route direction");
+    module->keywords = _("stream, order, route azimuth, route direction");
     module->description =
 	_("Route azimuth, direction and relation to streams of higher order");
 
@@ -54,71 +54,70 @@
     in_dir_opt->gisprompt = "old,cell,raster";
     in_dir_opt->description =
 	"Name of direction input map (r.watershed or r.stream.extract)";
-			  
+
     in_multipier = G_define_option();
     in_multipier->key = "multipier";
     in_multipier->label = _("Multipier to store stream index value");
     in_multipier->description = _("Must be > 0");
-		in_multipier->required = YES;
-		in_multipier->answer = "1000";
-		in_multipier->type = TYPE_INTEGER;
-		
-		
-		out_str = G_define_option();
-		out_str->key = "cells";
-		out_str->label = _("File to store pixel's position");
-		out_str->required = NO;
-		out_str->answer = NULL;
-		out_str->type = TYPE_STRING;
-		out_str->gisprompt = "new,cell,raster";
-		
-		
-		out_str_length = G_define_option();
-		out_str_length->key = "lengths";
-		out_str_length->label = _("File to store current stream length");
-		out_str_length->required = NO;
-		out_str_length->answer = NULL;
-		out_str_length->type = TYPE_STRING;
-		out_str_length->gisprompt = "new,cell,raster";
-	
-		out_seq = G_define_flag();
-		out_seq->key = 's';
-		out_seq->description = _("Create new stream category sequence");	     
-      
+    in_multipier->required = YES;
+    in_multipier->answer = "1000";
+    in_multipier->type = TYPE_INTEGER;
+
+    out_str = G_define_option();
+    out_str->key = "cells";
+    out_str->label = _("File to store pixel's position");
+    out_str->required = NO;
+    out_str->answer = NULL;
+    out_str->type = TYPE_STRING;
+    out_str->gisprompt = "new,cell,raster";
+
+    out_str_length = G_define_option();
+    out_str_length->key = "lengths";
+    out_str_length->label = _("File to store current stream length");
+    out_str_length->required = NO;
+    out_str_length->answer = NULL;
+    out_str_length->type = TYPE_STRING;
+    out_str_length->gisprompt = "new,cell,raster";
+
+    out_seq = G_define_flag();
+    out_seq->key = 's';
+    out_seq->description = _("Create new stream category sequence");
+
     if (G_parser(argc, argv))	/* parser */
 	exit(EXIT_FAILURE);
 
-      if (!out_str->answer && !out_str_length->answer)
-	G_fatal_error("You must select one or more output maps: cells or lengths or both ");
-    
+    if (!out_str->answer && !out_str_length->answer)
+	G_fatal_error
+	    ("You must select one or more output maps: cells or lengths or both ");
+
     G_get_window(&window);
-    
+
     /* stores input options to variables */
     in_dirs = in_dir_opt->answer;
     in_streams = in_stm_opt->answer;
     out_streams_length = out_str_length->answer;
     out_streams = out_str->answer;
     seq_cats = (out_seq->answer == 0);
-    
+
     multipier = atoi(in_multipier->answer);
     if (multipier <= 0)
 	G_fatal_error("Multipier must be > 0");
-    
+
     /* stores output options to variables */
- 		
+
     nrows = G_window_rows();
     ncols = G_window_cols();
 
     create_base_maps();
     find_inits();
     calculate();
- 
- 	 if (out_streams_length) {
+
+    if (out_streams_length) {
 	set_null_f(streams_length);
 	write_maps_f(out_streams_length, streams_length);
     }
- 
-     if (out_streams) {
+
+    if (out_streams) {
 	set_null_c(streams);
 	write_maps_c(out_streams, streams);
     }

Modified: grass-addons/raster/r.stream.pos/pos.c
===================================================================
--- grass-addons/raster/r.stream.pos/pos.c	2010-06-03 11:33:08 UTC (rev 42459)
+++ grass-addons/raster/r.stream.pos/pos.c	2010-06-03 11:33:50 UTC (rev 42460)
@@ -4,158 +4,158 @@
 #include "global.h"
 
 int trib_nums(int r, int c)
-{				/* calcualte number of tributuaries */
-
+{				/* calculate 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 &&
-				dirs[r + nextr[i]][c + nextc[i]] == j)
-			trib++;
+
+	j = (i + 4) > 8 ? i - 4 : i + 4;
+
+	if (streams[r + nextr[i]][c + nextc[i]] > 0 &&
+	    dirs[r + nextr[i]][c + nextc[i]] == j)
+	    trib++;
     }
 
-		if(trib>1) {
-			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;
-		
-			j = (i + 4) > 8 ? i - 4 : i + 4;
-			
-			if (streams[r + nextr[i]][c + nextc[i]] == streams[r][c] &&
-				dirs[r + nextr[i]][c + nextc[i]] == j)
-			trib--;
-			}
-		}
+    if (trib > 1) {
+	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;
 
+	    j = (i + 4) > 8 ? i - 4 : i + 4;
+
+	    if (streams[r + nextr[i]][c + nextc[i]] == streams[r][c] &&
+		dirs[r + nextr[i]][c + nextc[i]] == j)
+		trib--;
+	}
+    }
+
     if (trib > 5)
-	G_fatal_error("Error finding inits. Stream and direction maps probably do not match...");
+	G_fatal_error
+	    ("Error finding inits. Stream and direction maps probably do not match...");
     if (trib > 3)
 	G_warning("Stream network may be too dense...");
 
-   return trib;
+    return trib;
 }				/* end trib_num */
 
 
 int find_inits(void)
 {
     int r, c;
-    stream_num=0;
 
-    s_streams = (STREAM *) G_malloc((nrows+ncols) * sizeof(STREAM));
+    stream_num = 0;
+
+    s_streams = (STREAM *) G_malloc((nrows + ncols) * sizeof(STREAM));
     G_message("Finding inits...");
 
-   for (r = 0; r < nrows; ++r) {
-		for (c = 0; c < ncols; ++c) {
-	   	if (streams[r][c] > 0) {
-	
-				if (trib_nums(r, c) != 1) {	/* adding inits */
-					
-					if (stream_num>(2*(nrows+ncols)))
-						G_fatal_error("Error finding inits. Stream and direction maps probably do not match...");
-					if (stream_num>(nrows+ncols)) /* almost not possible */
-						s_streams = G_realloc(s_streams,2*(nrows+ncols)*sizeof(STREAM));
-						
-						s_streams[stream_num].stream = streams[r][c];
-		 				s_streams[stream_num].init_r = r;
-						s_streams[stream_num++].init_c = c;
-					}	
-	    		}			/* end if streams */
-			}			/* c */
+    for (r = 0; r < nrows; ++r) {
+	for (c = 0; c < ncols; ++c) {
+	    if (streams[r][c] > 0) {
+
+		if (trib_nums(r, c) != 1) {	/* adding inits */
+
+		    if (stream_num > (2 * (nrows + ncols)))
+			G_fatal_error
+			    ("Error finding inits. Stream and direction maps probably do not match...");
+		    if (stream_num > (nrows + ncols))	/* almost not possible */
+			s_streams =
+			    G_realloc(s_streams,
+				      2 * (nrows + ncols) * sizeof(STREAM));
+
+		    s_streams[stream_num].stream = streams[r][c];
+		    s_streams[stream_num].init_r = r;
+		    s_streams[stream_num++].init_c = c;
+		}
+	    }			/* end if streams */
+	}			/* c */
     }				/* r */
     return 0;
 }
 
-int calculate (void) {
-	
-	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 calculate(void)
+{
+    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 s, d;		/* s - streams index; d - direction */
+    int done = 1;
+    int r, c;
+    int next_r, next_c;
+    int cur_stream;
+    int cur_cat;
+    float cur_northing, cur_easting;
+    float next_northing, next_easting;
+    int cur_pixel;
+    float cur_length = 0.;
+    float cur_accum = 0.;
 
-  int i, s, d;		/* s - streams index; d - direction */
-  int done = 1;
-  int r, c;
-  int next_r, next_c;
-  int cur_stream;
-  int cur_cat;
-  float cur_northing, cur_easting;
-  float next_northing, next_easting;
-  
-	int cur_pixel;
-	float cur_length=0.;
-  float cur_accum=0.;
-	
-	G_message("Calculating...");
-	G_begin_distance_calculations();
-	
-	for (s=0; s<stream_num+1; ++s) { /* main loop on treams */
+    G_message("Calculating...");
+    G_begin_distance_calculations();
 
-			if(s_streams[s].stream<0)
-		continue;
-		
-		r = s_streams[s].init_r;
-		c = s_streams[s].init_c;
-		
-		cur_stream=streams[r][c];
-		cur_cat= (seq_cats) ? cur_stream : s;
+    for (s = 0; s < stream_num + 1; ++s) {	/* main loop on treams */
 
-		cur_length=0.;
-		cur_accum=0.;
-		cur_pixel=0;
-		done=1;
+	if (s_streams[s].stream < 0)
+	    continue;
 
-		while(done) {
+	r = s_streams[s].init_r;
+	c = s_streams[s].init_c;
 
-		 cur_northing = window.north - (r + .5) * window.ns_res;
-	   cur_easting = window.west + (c + .5) * window.ew_res;
-		 d=dirs[r][c];
-		 next_r=r+nextr[d];
-	   next_c=c+nextc[d];
-			
-		if (d<1|| /* end of route: sink */
-		r + nextr[d] < 0 || r + nextr[d] > (nrows - 1) || /*border*/
-		c + nextc[d] < 0 || c + nextc[d] > (ncols - 1) ||
-		streams[next_r][next_c]<1) { /* mask */
-			
-			cur_accum += (window.ns_res+window.ew_res)/2;	   
-			cur_pixel++;
-			
-				if(out_streams_length)
-			streams_length[r][c] = cur_accum;
-				if(out_streams)
-			streams[r][c] = cur_cat * multipier + cur_pixel;
- 			break;
-		}
+	cur_stream = streams[r][c];
+	cur_cat = (seq_cats) ? cur_stream : s;
 
-		 next_northing = window.north - (next_r + .5) * window.ns_res;
-		 next_easting = 	window.west + (next_c + .5) * window.ew_res;
-		 cur_length =	G_distance(next_easting, next_northing, cur_easting,  cur_northing);
-		 cur_accum += cur_length;
-		 cur_pixel++;
-		 
-			
-			if(out_streams_length)
-		 streams_length[r][c] = cur_accum;
-			if(out_streams)
-		 streams[r][c] = cur_cat * multipier + cur_pixel;
-		 
-		 r=next_r;
-		 c=next_c;
+	cur_length = 0.;
+	cur_accum = 0.;
+	cur_pixel = 0;
+	done = 1;
 
-				if (streams[next_r][next_c] != cur_stream) 
-			break;
-		
-		} /* end while */
-	} /* end for s*/
-return 0;
-}
+	while (done) {
 
+	    cur_northing = window.north - (r + .5) * window.ns_res;
+	    cur_easting = window.west + (c + .5) * window.ew_res;
+	    d = dirs[r][c];
+	    next_r = r + nextr[d];
+	    next_c = c + nextc[d];
 
+	    if (d < 1 ||	/* end of route: sink */
+		r + nextr[d] < 0 || r + nextr[d] > (nrows - 1) ||	/*border */
+		c + nextc[d] < 0 || c + nextc[d] > (ncols - 1) || streams[next_r][next_c] < 1) {	/* mask */
+
+		cur_accum += (window.ns_res + window.ew_res) / 2;
+		cur_pixel++;
+
+		if (out_streams_length)
+		    streams_length[r][c] = cur_accum;
+		if (out_streams)
+		    streams[r][c] = cur_cat * multipier + cur_pixel;
+		break;
+	    }
+
+	    next_northing = window.north - (next_r + .5) * window.ns_res;
+	    next_easting = window.west + (next_c + .5) * window.ew_res;
+	    cur_length =
+		G_distance(next_easting, next_northing, cur_easting,
+			   cur_northing);
+	    cur_accum += cur_length;
+	    cur_pixel++;
+
+	    if (out_streams_length)
+		streams_length[r][c] = cur_accum;
+	    if (out_streams)
+		streams[r][c] = cur_cat * multipier + cur_pixel;
+
+	    r = next_r;
+	    c = next_c;
+
+	    if (streams[next_r][next_c] != cur_stream)
+		break;
+
+	}			/* end while */
+    }				/* end for s */
+    return 0;
+}



More information about the grass-commit mailing list