[GRASS-SVN] r39504 - grass-addons/raster/r.stream.basins

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Oct 13 06:51:56 EDT 2009


Author: martinl
Date: 2009-10-13 06:51:54 -0400 (Tue, 13 Oct 2009)
New Revision: 39504

Modified:
   grass-addons/raster/r.stream.basins/Makefile
   grass-addons/raster/r.stream.basins/description.html
   grass-addons/raster/r.stream.basins/global.h
   grass-addons/raster/r.stream.basins/io.c
   grass-addons/raster/r.stream.basins/main.c
Log:
wrong commit reverted (r39503)


Modified: grass-addons/raster/r.stream.basins/Makefile
===================================================================
--- grass-addons/raster/r.stream.basins/Makefile	2009-10-13 09:58:02 UTC (rev 39503)
+++ grass-addons/raster/r.stream.basins/Makefile	2009-10-13 10:51:54 UTC (rev 39504)
@@ -2,7 +2,7 @@
 # or use absolute path to the GRASS source code
 MODULE_TOPDIR = ../..
 
-PGM = r.stream.del
+PGM = r.stream.basins
 
 LIBES = $(GISLIB)
 DEPENDENCIES = $(GISDEP)

Modified: grass-addons/raster/r.stream.basins/description.html
===================================================================
--- grass-addons/raster/r.stream.basins/description.html	2009-10-13 09:58:02 UTC (rev 39503)
+++ grass-addons/raster/r.stream.basins/description.html	2009-10-13 10:51:54 UTC (rev 39504)
@@ -3,73 +3,106 @@
 <DT><b>-z</b></DT>
 <DD>Creates zero-value background instead of NULL. For some reason (like map algebra calculation) zero-valued background may be required. This flag produces zero-filled background instead of null (default).</DD>
 <p>
+<DT><b>-c</b></DT>
+<DD>By default r.stream.basins uses streams category as basin category. In some cases - for example if
+stream map is product of map algebra and separete streams may not have unique values this option will create
+new category sequence for each basin 
+</DD>
+<p>
+<DT><b>-l</b></DT>
+<DD>By default r.strean.basins create basins for all unique streams. This option delinate basins only for last streams ignoring upstreams.
+</DD>
+<p>
 
-
 <DT><b>stream</b></DT>
-<DD>Stream network: name of input stream map on which reduction will be performed produced by <a href="r.watershed.html">r.watershed</a> or <a href="r.stream.extract.html">r.stream.extract</a>. Because streams network produced by r.watershed and r.stream.extract may slighty differ in detail it is required to use both stream and direction map produced by the same module. Stream background shall have NULL value or zero value. Background values of NULL are by default produced by r.watershed and r.stream.extract. If not 0 or NULL use <a href="r.mapcalc.html">r.mapcalc</a> to set background values to null.  
+<DD>Stream network: name of input stream map on which ordering will be performed produced by r.watershed or r.stream.extract. Because streams network produced by r.watershed and r.stream.extract may slighty differ in detail it is required to use both stream and direction map produced by the same module. Stream background shall have NULL value or zero value. Background values of NULL are by default produced by r.watershed and r.stream.extract. If not 0 or NULL use <a href="r.mapcalc.html">r.mapcalc</a> to set background values to null.  
 </DD>
 <p>
 <DT><b>dir</b></DT>
 <DD>Flow direction: name of input direction map produced by r.watershed or r.stream.extract. If r.stream.extract output map is used, it only has non-NULL values in places where streams occur. NULL (nodata) cells are ignored, zero and negative values are valid direction data if they vary from -8 to 8 (CCW from East in steps of 45 degrees). Direction map shall be of type CELL values. Region resolution and map resoultion must be the same. 
 Also <em>stream</em> network map must have the same resolution. It is checked by default. If resolutions differ the module informs about it and stops. Region boundary and maps boundary may be differ but it may lead to unexpected results.</DD>
-<DT><b>threshold</b></DT>
-<DD>Integer vaule indicating the minimum number of cell in first-order streams (sensu Strhahler) to leeve it in the network.</DD>
+
 <h2>OUTPUTS</h2>
-<P>The module produces new stream map where streams shortrer than threshold are deleted. To keep file consistent, when short stream is deleted the next stream segment is joined with previous one by asigning its category.
-
-<PRE>
-BEFORE:
-
-      |2
-___1__|____3____
-
-AFTER:
-      
-___1______1____
-
-</PRE>
-</P>
+<P>The module produces one raster map with basins acording user's rules</p>
 </DL>
 
 <h2>DESCRIPTION</h2>
 <P>
-Module r.stream.del is prepared to removing short streams from stream network. The short streams are undesired outcome of networks delineated from DEMS in stream ordering and basin delinating. In stream ordering very short stream may increase network order, when two short stream of first order will increase order of next streams. The second situation is connected with network delineated with more complex criteria than only accumulation threeshold like Montgomery's method or weighted method (<a href="r.stream.extract.html">r.stream.extract</a>). It may  produce a scope of very short streams on vallyes slopes. Such streams may lead to wrong results in stream ordering and basin delination so removing short streams to simplify  the network. It also may heve great impact on Horton's statistics: <a href="r.stream.stats.html">r.stream.stats</a>.
+Module r.stream.basins is prepared to delineate basins and subasins according user rules. Module is prepared to delineate unrestricted number of basins in one step. It requires two maps: direction and streams. In stream map we can store all information required to proper delineation, so it may be necessary to modify it before run. Module is prepared to work with output data of <em>r.watershed, r.stream.extract, r.stream.order</em> also with modification done by <em>r.recalss</em> and <em>r.mapcalc</em>. r.stream.basin can delineate basins according outlets marked by raster streams, points and polygons. If outlets are marked by points it delineate basins which cells contribute to that points, if outlets are marked by streams it delineate cells which contribute to the last (downstream) cell of the every stream. If outlets are marked by polygon it delineate cells contributing to most downstream cell of the polygon. If polygon covers more outlets than of one basins it will create collective basin for all outlets  with common category.
 
 
 <h2>NOTES</h2>
 <P>
-Module can work only if direction map, stream map and region map has same settings. It is also required that stream map and direction map come from the same source. For lots of reason this limitation probably cannot be omitted.   this means if stream map comes from r.stream.extract also direction map from r.stream.extract must be used. If stream network was generated with MFD method also MFD direction map must be used.
+Nowadays user's input must be in raster format. Vector format is planned in the feature. To receive good results outlets markers created by user shall overlapping with streams. On the other way basins could results with very small area. Input maps must be in CELL format (default output of r.watershed, r.stream.order  and r.stream.extract)<P>
+Module can work only if direction map, stream map and region map has same settings. It is also required that stream map and direction map come from the same source. For lots of reason this limitation probably cannot be omitted.   this means if stream map comes from r.stream.extract also direction map from r.stream.extract must be used. If stream network was generated with MFD method also MFD direction map must be used. Nowadays f direction map comes from r.stream.extract  must be patched by direction map from r.watershed. (with r.patch). 
 
 <h2>EXAMPLES</h2>
-
 <P>
-Strahler stream ordering before and after removing short streams
+To delineate all basins with categories of streams:
+<P>
+<CODE>r.stream.basins dir=dirs stream=streams basins=bas_basins_elem</CODE>
+<P>
+To determine major and minor basins in area, definied by outlets, ignoring subbasins use  - l flag. That flag ignores all nodes and uses only real outlets (in most cases that on map border):
+<P>
+<CODE>r.stream.basins -l dir=dirs stream=streams basins=bas_basins_last</CODE>
+<P>
+To delinaeate one or more particular basins defined by given streams, stream map must be re-classed first, to leave only desired streams:
 <CODE>
 <PRE>
-r.stream.extract elevation=elev_ned_30m at PERMANENT threshold=100 d8cut=infinity mexp=2 stream_rast=stream_mont direction=stream_dir
-r.stream.order stream=stream_mont dir=stream_dir strahler=streahler_before 
+echo '42=42
+* = NULL' > tmp #for one output
 
-r.stream.del stream=stream_mont dir=stream_dir threshold=10 reduced=stream_out
-r.stream.order stream=stream_out dir=stream_dir strahler=streahler_after
+echo '42 = 42
+252 = 252
+188 = 188
+* = NULL' >tmp #for multiple outputs
 
+r.reclass input=streams output=sel_streams_1 <tmp
+</PRE>
+</CODE>
+<P>
+The usage of polygons as outlets markers is very useful when exact stream course cannot be cleary determined before running analysis, but the area of its occurrence can be determined (mostly in iterative simulations) Example uses r.circle but can be substituted by any polygon created for example  with v.digit:
+<CODE>
+<PRE>
+r.circle -b output=circle coordinate=639936.623832,216939.836449 max=200
+r.stream.basins -c dir=dirs stream=circle basins=bas_simul
+</PRE>
+</CODE>
+<P>
+To determine areas of contribution to streams of particular order  use as streams the result of ordering:
+<P>
+<CODE>r.stream.basins dir=dirs stream=ord_strahler basins=bas_basin_strahler</CODE>
+<P>
+Determination of areas of potential source of pollution. The example will be done for lake marked with FULL_HYDR 8056 in North Carolina sample dataset. The lake shall be extracted and converted to binary raster map.
 
+<CODE>
+<PRE>
+v.extract -d input=lakes at PERMANENT output=lake8056 type=area layer=1 'where=FULL_HYDRO = 8056' new=-1 
+v.to.rast input=lake8056 output=lake8056 use=val type=area layer=1 value=1
+echo 'sel_streams_lake=if(lake8056,streams,null())'|r.mapcalc
+r.stream.basins dir=dirs stream=sel_streams_lake basins=bas_basin_lake
 </PRE>
 </CODE>
 <P>
+See aslo tutorial: <a href="http://heretic.livenet.pl/heretic">http://heretic.livenet.pl/heretic</a>
 
-
 <h2>SEE ALSO</h2>
 
 <em>
 <a href="r.watershed.html">r.watershed</a>,
 <a href="r.stream.extract.html">r.stream.extract</a>,
 <a href="r.stream.order.html">r.stream.order</a>,
-<a href="r.stream.basins.html">r.stream.basins</a>,
 <a href="r.stream.stats.html">r.stream.stats</a>,
+<a href="r.mapcalc.html">r.mapcalc</a>,
+<a href="r.reclass.html">r.reclass</a>,
+<a href="r.patch.html">r.patch</a>
 </em>
 
 <h2>AUTHOR</h2>
 Jarek  Jasiewicz
 
+<HR>
+<P><a href="index.html">Main index</a> - <a href="raster.html">raster index</a> - <a href="full_index.html">Full index</a></P>
+<P>&copy; 2003-2009 <a href="http://grass.osgeo.org">GRASS Development Team</a></p>
 </body>
 </html>

Modified: grass-addons/raster/r.stream.basins/global.h
===================================================================
--- grass-addons/raster/r.stream.basins/global.h	2009-10-13 09:58:02 UTC (rev 39503)
+++ grass-addons/raster/r.stream.basins/global.h	2009-10-13 10:51:54 UTC (rev 39504)
@@ -6,9 +6,23 @@
 
 
 					/* define */
+
+/*define directions for code simplicity
+
+directions according to r.watershed: MUST check all directions
+|3|2|1|
+|4| |8|
+|5|6|7|
+
+*/
+
+#define POINT struct points	
+POINT {
+	int r, c;
+	};
 	
-#define SPRING struct sprs
-SPRING { 
+#define OUTLET struct outs
+OUTLET { 
 	int r, c;
 	int val;
 	};	
@@ -16,15 +30,20 @@
 					/* functions.c */ 
 
 /* io.c */
+int open_raster(char *mapname);
 int create_maps(void);
 int max_link(void);
-int write_map(void);
+int write_chatchment(void);
 int set_null(void);
 
-/* delete */
-int find_springs(int max_link);
-int delete_join(int springs_num);
+/* cachments */
+int find_outlets(void);
+int reset_catchments(void);
+int fill_catchments (OUTLET outlet);
+int fifo_insert (POINT point);
+POINT fifo_return_del (void);
 
+
 				/* variables */
 
 #ifdef MAIN
@@ -35,15 +54,20 @@
 
 GLOBAL struct Cell_head window;
 GLOBAL char *in_dirs, *in_streams;	/* input dirrection and accumulation raster names*/
-GLOBAL char *out_streams;
-GLOBAL int zeros; /* flags */
-GLOBAL int threshold;
+GLOBAL char *name_catchments;
+GLOBAL int zeros, cats, lasts; /* flags */
 
 GLOBAL CELL **dirs, **streams; /* matrix with input data streams is used as output data*/
 
 GLOBAL int nrows, ncols; 
 
-SPRING *springs;
+POINT *fifo_outlet;
+int tail, head;
+int outlets_num;
+int fifo_max;
+	
+GLOBAL int out; /* number of strahler and horton outlets: index */
+OUTLET *outlets;
 
 GLOBAL struct History history;	/* holds meta-data (title, comments,..) */
 

Modified: grass-addons/raster/r.stream.basins/io.c
===================================================================
--- grass-addons/raster/r.stream.basins/io.c	2009-10-13 09:58:02 UTC (rev 39503)
+++ grass-addons/raster/r.stream.basins/io.c	2009-10-13 10:51:54 UTC (rev 39504)
@@ -108,27 +108,26 @@
 }				/* end_max_link       */
 
 
-int write_map(void)
+int write_chatchment(void)
 {
     int r;
     int fdo = 0;
 
-    if ((fdo = G_open_raster_new(out_streams, CELL_TYPE)) < 0)
-	G_fatal_error(_("Unable to create raster map <%s>"), out_streams);
+    if ((fdo = G_open_raster_new(name_catchments, CELL_TYPE)) < 0)
+	G_fatal_error(_("Unable to create raster map <%s>"), name_catchments);
 
     for (r = 0; r < nrows; ++r)
 	G_put_c_raster_row(fdo, streams[r]);
     G_close_cell(fdo);
-    G_short_history(out_streams, "raster", &history);
-    G_write_history(out_streams, &history);
-    G_message(_("%s Done!"), out_streams);
+    G_short_history(name_catchments, "raster", &history);
+    G_write_history(name_catchments, &history);
+    G_message(_("%s Done!"), name_catchments);
 
     G_free(streams);
 
     return 0;
 }
 
-
 int set_null(void)
 {
     int r, c;

Modified: grass-addons/raster/r.stream.basins/main.c
===================================================================
--- grass-addons/raster/r.stream.basins/main.c	2009-10-13 09:58:02 UTC (rev 39503)
+++ grass-addons/raster/r.stream.basins/main.c	2009-10-13 10:51:54 UTC (rev 39504)
@@ -32,10 +32,10 @@
 {
 
     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 */
+    struct Option *in_dir_opt, *in_stm_opt, *out_opt;	/* options */
+    struct Flag *out_back, *out_cat, *out_last;	/* flags */
 
-    int num_spring, link_max;
+    int link_max;
 
     /* initialize GIS environment */
     G_gisinit(argv[0]);		/* reads grass env, stores program name to G_program_name() */
@@ -59,20 +59,15 @@
     in_dir_opt->gisprompt = "old,cell,raster";
     in_dir_opt->description = "Name of flow direction input map";
 
-    in_threshold = G_define_option();
-    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;
+    /*  output option - at least one is reqired  */
 
     out_opt = G_define_option();
-    out_opt->key = "reduced";
+    out_opt->key = "basins";
     out_opt->type = TYPE_STRING;
     out_opt->required = YES;
     out_opt->answer = NULL;
     out_opt->gisprompt = "new,cell,raster";
-    out_opt->description = "Output reduced stream map";
+    out_opt->description = "Output basin map";
 
 
     /* Define the different flags */
@@ -80,7 +75,16 @@
     out_back->key = 'z';
     out_back->description = _("Create zero-value background instead of NULL");
 
+    /* Define the different flags */
+    out_cat = G_define_flag();
+    out_cat->key = 'c';
+    out_cat->description =
+	_("Use unique category sequence instead of input streams");
 
+    /* Define the different flags */
+    out_last = G_define_flag();
+    out_last->key = 'l';
+    out_last->description = _("Create basins only for last stream links");
 
     if (G_parser(argc, argv))	/* parser */
 	exit(EXIT_FAILURE);
@@ -88,27 +92,37 @@
     /* stores input options to variables */
     in_dirs = in_dir_opt->answer;
     in_streams = in_stm_opt->answer;
-    out_streams = out_opt->answer;
+    name_catchments = out_opt->answer;
     zeros = (out_back->answer != 0);
+    cats = (out_cat->answer != 0);
+    lasts = (out_last->answer != 0);
 
-    threshold = atoi(in_threshold->answer);
-    if (threshold <= 0)
-	G_fatal_error(_("Threshold must be > 0"));
+    if (G_legal_filename(name_catchments) < 0)
+	G_fatal_error(_("<%s> is an illegal file name"), name_catchments);
 
-
-    if (G_legal_filename(out_streams) < 0)
-	G_fatal_error(_("<%s> is an illegal file name"), out_streams);
-
     G_get_window(&window);
     nrows = G_window_rows();
     ncols = G_window_cols();
     create_maps();
     link_max = max_link();
-    num_spring=find_springs(link_max);
-		delete_join(num_spring);
-		if (!zeros)
-	set_null();
-	write_map();
+    find_outlets();
 
+    {
+	int j;
+
+	reset_catchments();
+	G_message(_("Calculate basins..."));
+	fifo_max = 4 * (nrows + ncols);
+	fifo_outlet = (POINT *) G_malloc((fifo_max + 1) * sizeof(POINT));
+	/* fifo queue for contributing cell */
+	for (j = 0; j < outlets_num; ++j) {
+	    fill_catchments(outlets[j]);
+	}
+	G_free(fifo_outlet);
+	if (!zeros)
+	    set_null();
+	write_chatchment();
+    }
+
     exit(EXIT_SUCCESS);
 }



More information about the grass-commit mailing list