[GRASS-SVN] r56190 - in grass-addons/grass7/raster: . r.traveltime

svn_grass at osgeo.org svn_grass at osgeo.org
Fri May 10 13:46:38 PDT 2013


Author: neteler
Date: 2013-05-10 13:46:38 -0700 (Fri, 10 May 2013)
New Revision: 56190

Added:
   grass-addons/grass7/raster/r.traveltime/
   grass-addons/grass7/raster/r.traveltime/r.traveltime.html
Removed:
   grass-addons/grass7/raster/r.traveltime/description.html
Modified:
   grass-addons/grass7/raster/Makefile
   grass-addons/grass7/raster/r.traveltime/Makefile
   grass-addons/grass7/raster/r.traveltime/main.c
Log:
r.traveltime: attempt to port it to GRASS 7 (using http://trac.osgeo.org/grass/wiki/Grass7/RasterLib/ListOfFunctions)

Modified: grass-addons/grass7/raster/Makefile
===================================================================
--- grass-addons/grass7/raster/Makefile	2013-05-10 18:36:36 UTC (rev 56189)
+++ grass-addons/grass7/raster/Makefile	2013-05-10 20:46:38 UTC (rev 56190)
@@ -27,7 +27,8 @@
 	r.stream.slope \
 	r.stream.snap \
 	r.stream.stats \
-	r.threshold
+	r.threshold \
+	r.traveltime
 
 include $(MODULE_TOPDIR)/include/Make/Dir.make
 

Modified: grass-addons/grass7/raster/r.traveltime/Makefile
===================================================================
--- grass-addons/grass6/raster/r.traveltime/Makefile	2013-05-10 18:36:36 UTC (rev 56189)
+++ grass-addons/grass7/raster/r.traveltime/Makefile	2013-05-10 20:46:38 UTC (rev 56190)
@@ -1,12 +1,9 @@
-# Uncoment and fix MODULE_TOPDIR line OR provide MODULE_TOPDIR variable for make
-# fix this relative to include/
-# or use absolute path to the GRASS source code
-#MODULE_TOPDIR = ../..
+MODULE_TOPDIR = ../..
 
 PGM = r.traveltime
 
-LIBES = $(GISLIB)
-DEPENDENCIES = $(GISDEP)
+LIBES = $(GMATHLIB) $(RASTERLIB) $(GISLIB)
+DEPENDENCIES = $(GMATHDEP) $(RASTERDEP) $(GISDEP)
 
 include $(MODULE_TOPDIR)/include/Make/Module.make
 

Deleted: grass-addons/grass7/raster/r.traveltime/description.html
===================================================================
--- grass-addons/grass6/raster/r.traveltime/description.html	2013-05-10 18:36:36 UTC (rev 56189)
+++ grass-addons/grass7/raster/r.traveltime/description.html	2013-05-10 20:46:38 UTC (rev 56190)
@@ -1,69 +0,0 @@
-<h2>DESCRIPTION</h2>
-<em>r.traveltime</em> computes the travel time of surface runoff to an
-outlet. The program starts at the basin outlet and calculates the travel
-time at each raster cell recursively. A drainage area related threhold
-considers even  surface and also channel runoff. Travel times are
-derived by assuming kinematic wave approximation.<br>
-To derive channel flow velocities an equilibrium discharge for each
-cell is calculated (Q=Area*Excess_Prcipitation, Assumption: storm
-duration >= time of concentration).  This assumption may result
-in overestimated velocities. Therefor a factor is implemented to reduce
-velocities biased towards too large values.<br>
-The results can be used to derive a time-area function. This might be
-usefull for precipitation-runoff calculations (estimation of flood
-predictions) with a lumped hydrologic model (user-specified unit
-hydrograph).
-
-<h2>REMARKS</h2>
-The program ist restricted to SI units (meters). The algorithm is
-recursive. Maybe it will not work with extensive datasets. It is
-assumed that the minimum slope is 0.001. For smaller gradients the
-program uses this value.
-
-<h2>KNOWN ISSUES</h2>
-The program does not work correctly if Manning's roughness grid is
-defined as double (float expected). To define a simple uniform
-roughness distribution try: r.mapcalc 'roughness=0.1f'
-<br>
-The region has to be set one row and column larger than the elevation
-map. See the example below to see how to do that with g.region.
-
-<h2>EXAMPLE</h2>
-<i>This example uses the North Carolina sample dataset.</i>
-<p>
-<div class="code"><pre>
-  g.region rast=elev_lid792_1m n=n+1 s=s-1 w=w-1 e=e+1 -p
-  r.watershed elev_lid792_1m thresh=5000 accum=accum_5K   drain=draindir_5K
-  r.fill.dir elev_lid792_1m elev=elev_filled dir=elev_dir
-  r.mapcalc rough=0.1f
-  r.traveltime --overwrite dir=draindir_5K at user1 accu=accum_5K at user1 \
-        dtm=elev_filled at user1 manningsn=rough out_x=638741.43125 \
-        out_y=220269.7 threshold=1 b=1 nchannel=0.1 ep=40 fdis=1 \
-        out=travel_time
-</pre></div>
-</p>
-
-<h2>SEE ALSO</h2>
-<em><a href="r.watershed.html">r.watershed</a>,
-    <a href="r.fill.dir.html">r.fill.dir</a>
-    <br>
-    <br>
-    <a href="http://jesbergwetter.twoday.net/stories/4845555/">http://jesbergwetter.twoday.net/stories/4845555/</a>
-
-    <h2>REFERENCES</h2>
-    <li>Kilgore, J. L. (1997): <em>Development and evaluation of a GIS-based
-spatially distributed unit hydrograph model</em>, master thesis,
-Virginia
-Polytechnic Institute and State University.</li>
-<li>Melesse, A. M., Graham, W. D. (2004):<em> Storm runoff predicition
-based on a
-spatially distributed travel time method utilizing remote sensing and
-GIS</em>, Journal of the American Water Resources Association, 8,
-863-879.</li>
-<li>Muzik, I. (1996): <em>Flood modelling with GIS-derived distributed
-    unit hydrographs</em>, Hydrological Processes, 10, 1401-1409.</li>
-
-<h2>AUTHOR</h2>
-Kristian Foerster<br>
-<p><i>Last changed: $Date$</i>
-</p>

Modified: grass-addons/grass7/raster/r.traveltime/main.c
===================================================================
--- grass-addons/grass6/raster/r.traveltime/main.c	2013-05-10 18:36:36 UTC (rev 56189)
+++ grass-addons/grass7/raster/r.traveltime/main.c	2013-05-10 20:46:38 UTC (rev 56190)
@@ -27,6 +27,7 @@
 #include <limits.h>
 #include <float.h>
 #include <grass/gis.h>
+#include <grass/raster.h>
 #include <grass/glocale.h>
 #include <grass/config.h>
 
@@ -57,8 +58,7 @@
     int x_exp, y_exp;
     int value;
 
-    if (G_get_raster_row(in_dir, inrast_dir, loc_y, data_type_dir) < 0)
-	G_fatal_error(_("Could not read from map"));
+    Rast_get_row(in_dir, inrast_dir, loc_y, data_type_dir);
     value = ((CELL *) inrast_dir)[loc_x];
 
     // conversions of the flow direction classification to vectors
@@ -150,16 +150,13 @@
     double z1, z2;
     double manningsn;
 
-    if (G_get_raster_row(in_dir, inrast_dir, y, data_type_dir) < 0)
-	G_fatal_error(_("Could not read from map"));
+    Rast_get_row(in_dir, inrast_dir, y, data_type_dir);
     dir = ((CELL *) inrast_dir)[x];
 
-    if (G_get_raster_row(in_accu, inrast_accu, y, data_type_accu) < 0)
-	G_fatal_error(_("Could not read from map"));
+    Rast_get_row(in_accu, inrast_accu, y, data_type_accu);
     accu = ((CELL *) inrast_accu)[x];
 
-    if (G_get_raster_row(in_n, inrast_n, y, data_type_n) < 0)
-	G_fatal_error(_("Could not read from map"));
+    Rast_get_row(in_n, inrast_n, y, data_type_n);
     switch (data_type_n) {
     case DCELL_TYPE:
 	manningsn = (double)((DCELL *) inrast_n)[x];
@@ -169,8 +166,7 @@
 	break;
     }
 
-    if (G_get_raster_row(in_dtm, inrast_dtm, y, data_type_dtm) < 0)
-	G_fatal_error(_("Could not read from map"));
+    Rast_get_row(in_dtm, inrast_dtm, y, data_type_dtm);
     switch (data_type_dtm) {
     case CELL_TYPE:
 	z2 = (double)((CELL *) inrast_dtm)[x];
@@ -183,8 +179,7 @@
 	break;
     }
 
-    if (G_get_raster_row(in_dtm, inrast_dtm, y - dy, data_type_dtm) < 0)
-	G_fatal_error(_("Could not read from map"));
+    Rast_get_row(in_dtm, inrast_dtm, y - dy, data_type_dtm);
     switch (data_type_dtm) {
     case CELL_TYPE:
 	z1 = (double)((CELL *) inrast_dtm)[x - dx];
@@ -197,8 +192,6 @@
 	break;
     }
 
-
-
     for (i = -1; i < 2; i++) {
 	for (j = -1; j < 2; j++) {
 	    if (inflow(x + i, y + j, i, j) > 0) {
@@ -251,17 +244,16 @@
 	*input_outlet_x, *input_outlet_y, *input_thres, *input_nc, *input_b,
 	*input_ep, *input_fdis;
 
-
-
-
     /* initialize GIS environment */
     G_gisinit(argv[0]);		/* reads grass env, stores program name to G_program_name() */
 
-
-
     /* initialize module */
     module = G_define_module();
-    module->description = _("Estimation of travel times/isochrones");
+    G_add_keyword(_("raster"));
+    G_add_keyword(_("hydrology"));
+    module->label = _("Estimation of travel times/isochrones.");
+    module->description =
+	_("Computes the travel time of surface runoff to an outlet");
 
     /* Define the different options as defined in gis.h */
     input_dir = G_define_standard_option(G_OPT_R_INPUT);
@@ -361,102 +353,56 @@
     if (factor > 0 & factor <= 1.0)
 	fdis = factor;
     else {
-	printf("\nWARNING! Reduction factor is not valid!\n");
-	exit(EXIT_FAILURE);
+	   G_fatal_error("Reduction factor is not valid!");
     }
 
-    /* returns NULL if the map was not found in any mapset, 
-     * mapset name otherwise */
-    mapset = G_find_cell2(map_dir, "");
-    if (mapset == NULL)
-	G_fatal_error(_("cell file [%s] not found"), map_dir);
+    mapset = G_find_raster2(map_dir, "");
+    mapset = G_find_raster2(map_accu, "");
+    mapset = G_find_raster2(map_dtm, "");
+    mapset = G_find_raster2(map_n, "");
 
-    mapset = G_find_cell2(map_accu, "");
-    if (mapset == NULL)
-	G_fatal_error(_("cell file [%s] not found"), map_accu);
-
-    mapset = G_find_cell2(map_dtm, "");
-    if (mapset == NULL)
-	G_fatal_error(_("cell file [%s] not found"), map_dtm);
-
-    mapset = G_find_cell2(map_n, "");
-    if (mapset == NULL)
-	G_fatal_error(_("cell file [%s] not found"), map_n);
-
-
-    if (G_legal_filename(result) < 0)
-	G_fatal_error(_("[%s] is an illegal name"), result);
-
-
-
     /* determine the inputmap type (CELL/FCELL/DCELL) */
-    data_type_dir = G_raster_map_type(map_dir, mapset);
-    data_type_accu = G_raster_map_type(map_accu, mapset);
-    data_type_n = G_raster_map_type(map_n, mapset);
-    data_type_dtm = G_raster_map_type(map_dtm, mapset);
+    data_type_dir = Rast_map_type(map_dir, mapset);
+    data_type_accu = Rast_map_type(map_accu, mapset);
+    data_type_n = Rast_map_type(map_n, mapset);
+    data_type_dtm = Rast_map_type(map_dtm, mapset);
 
-    /* G_open_cell_old - returns file destriptor (>0) */
-    if ((in_dir = G_open_cell_old(map_dir, mapset)) < 0)
-	G_fatal_error(_("Cannot open cell file [%s]"), map_dir);
+    in_dir = Rast_open_old(map_dir, mapset);
+    in_accu = Rast_open_old(map_accu, mapset);
+    in_n = Rast_open_old(map_n, mapset);
+    in_dtm = Rast_open_old(map_dtm, mapset);
 
-    if ((in_accu = G_open_cell_old(map_accu, mapset)) < 0)
-	G_fatal_error(_("Cannot open cell file [%s]"), map_accu);
-
-    if ((in_n = G_open_cell_old(map_n, mapset)) < 0)
-	G_fatal_error(_("Cannot open cell file [%s]"), map_n);
-
-    if ((in_dtm = G_open_cell_old(map_dtm, mapset)) < 0)
-	G_fatal_error(_("Cannot open cell file [%s]"), map_dtm);
-
-    /* controlling, if we can open input raster */
-    if (G_get_cellhd(map_accu, mapset, &cellhd_accu) < 0)
-	G_fatal_error(_("Cannot read file header of [%s]"), map_accu);
-
-    if (G_get_cellhd(map_dir, mapset, &cellhd_dir) < 0)
-	G_fatal_error(_("Cannot read file header of [%s]"), map_dir);
-
-    if (G_get_cellhd(map_dtm, mapset, &cellhd_dtm) < 0)
-	G_fatal_error(_("Cannot read file header of [%s]"), map_dtm);
-
-    if (G_get_cellhd(map_n, mapset, &cellhd_n) < 0)
-	G_fatal_error(_("Cannot read file header of [%s]"), map_n);
-
     G_debug(3, "number of rows %d", cellhd_accu.rows);
     G_debug(3, "number of rows %d", cellhd_dir.rows);
     G_debug(3, "number of rows %d", cellhd_dtm.rows);
     G_debug(3, "number of rows %d", cellhd_n.rows);
 
     /* Allocate input buffer */
-    inrast_accu = G_allocate_raster_buf(data_type_accu);
-    inrast_dir = G_allocate_raster_buf(data_type_dir);
-    inrast_n = G_allocate_raster_buf(data_type_n);
-    inrast_dtm = G_allocate_raster_buf(data_type_dtm);
+    inrast_accu = Rast_allocate_buf(data_type_accu);
+    inrast_dir = Rast_allocate_buf(data_type_dir);
+    inrast_n = Rast_allocate_buf(data_type_n);
+    inrast_dtm = Rast_allocate_buf(data_type_dtm);
 
 
     /* Allocate output buffer, use input map data_type */
-    nrows = G_window_rows();
-    ncols = G_window_cols();
-    outrast = G_allocate_raster_buf(FCELL_TYPE);
+    nrows = Rast_window_rows();
+    ncols = Rast_window_cols();
+    outrast = Rast_allocate_buf(FCELL_TYPE);
 
     /* controlling, if we can write the raster */
-    if ((outfd = G_open_raster_new(result, FCELL_TYPE)) < 0)
-	G_fatal_error(_("Could not open <%s>"), result);
+    outfd = Rast_open_new(result, FCELL_TYPE);
 
     /* output array */
 
     array_out = (double **)malloc(nrows * sizeof(double *));
-    if ((NULL == array_out)) {
-	printf("out of memory ... !");
-	exit(EXIT_FAILURE);
-    }
+    if ((NULL == array_out))
+	G_fatal_error("Out of memory ... !");
     int i, j;
 
     for (i = 0; i < nrows; i++) {
 	array_out[i] = (double *)malloc(ncols * sizeof(double));
-	if ((NULL == array_out[i])) {
-	    printf("out of memory ... !");
-	    exit(EXIT_FAILURE);
-	}
+	if ((NULL == array_out[i]))
+	    G_fatal_error("Out of memory ... !");
     }
 
     for (i = 0; i < nrows; i++) {
@@ -469,11 +415,7 @@
      * terrain analysis begins here ...
      */
 
-    if (G_get_window(&window) < 0) {
-	sprintf(map_dir, "can't read current window parameters");
-	G_fatal_error(map_dir);
-	exit(EXIT_FAILURE);
-    }
+    G_get_window(&window);
 
     // map units to matrix locations
 
@@ -532,27 +474,20 @@
 	    ((CELL *) outrast)[col] = array_out[row][col];
 	}
 	/* write raster row to output raster file */
-	if (G_put_raster_row(outfd, outrast, CELL_TYPE) < 0)
-	    G_fatal_error(_("Cannot write to <%s>"), result);
+	Rast_put_row(outfd, outrast, CELL_TYPE);
     }
 
-
-
-
-    /* memory cleanup */
-    G_free(inrast_accu);
-    G_free(inrast_dir);
-    G_free(inrast_dtm);
-    G_free(inrast_n);
-    G_free(outrast);
-
     /* closing raster files */
-    G_close_cell(inrast_accu);
-    G_close_cell(inrast_dir);
-    G_close_cell(inrast_dtm);
-    G_close_cell(inrast_n);
-    G_close_cell(outfd);
+//    Rast_close(inrast_accu);
+//    Rast_close(inrast_dir);
+//    Rast_close(inrast_dtm);
+//    Rast_close(inrast_n);
 
+    Rast_close(in_dir);
+    Rast_close(in_accu); 
+    Rast_close(in_n); 
+    Rast_close(in_dtm); 
+    Rast_close(outfd);
 
     return 0;
 }

Copied: grass-addons/grass7/raster/r.traveltime/r.traveltime.html (from rev 56189, grass-addons/grass6/raster/r.traveltime/description.html)
===================================================================
--- grass-addons/grass7/raster/r.traveltime/r.traveltime.html	                        (rev 0)
+++ grass-addons/grass7/raster/r.traveltime/r.traveltime.html	2013-05-10 20:46:38 UTC (rev 56190)
@@ -0,0 +1,73 @@
+<h2>DESCRIPTION</h2>
+<em>r.traveltime</em> computes the travel time of surface runoff to an
+outlet. The program starts at the basin outlet and calculates the travel
+time at each raster cell recursively. A drainage area related threhold
+considers even surface and also channel runoff. Travel times are
+derived by assuming kinematic wave approximation.<br>
+To derive channel flow velocities an equilibrium discharge for each
+cell is calculated (Q=Area*Excess_Prcipitation, Assumption: storm
+duration >= time of concentration). This assumption may result
+in overestimated velocities. Therefor a factor is implemented to reduce
+velocities biased towards too large values.<br>
+The results can be used to derive a time-area function. This might be
+usefull for precipitation-runoff calculations (estimation of flood
+predictions) with a lumped hydrologic model (user-specified unit
+hydrograph).
+
+<h2>REMARKS</h2>
+The program ist restricted to SI units (meters). The algorithm is
+recursive. Maybe it will not work with extensive datasets. It is
+assumed that the minimum slope is 0.001. For smaller gradients the
+program uses this value.
+
+<h2>KNOWN ISSUES</h2>
+The program does not work correctly if Manning's roughness grid is
+defined as double (float expected). To define a simple uniform
+roughness distribution try: r.mapcalc 'roughness = 0.1f'
+<br>
+The region has to be set one row and column larger than the elevation
+map. See the example below to see how to do that with g.region.
+
+<h2>EXAMPLE</h2>
+<i>This example uses the North Carolina sample dataset.</i>
+<p>
+<div class="code"><pre>
+g.region rast=elev_lid792_1m n=n+1 s=s-1 w=w-1 e=e+1 -p
+r.watershed elev_lid792_1m thresh=5000 accum=accum_5K   drain=draindir_5K
+r.fill.dir elev_lid792_1m output=elev_filled outdir=elev_dir
+
+r.mapcalc "rough = 0.1f"
+r.traveltime dir=draindir_5K at user1 accu=accum_5K at user1 \
+      dtm=elev_filled at user1 manningsn=rough out_x=638741.43125 \
+      out_y=220269.7 threshold=1 b=1 nchannel=0.1 ep=40 fdis=1 \
+      out=travel_time
+r.colors travel_time col=gyr
+</pre></div>
+
+<h2>SEE ALSO</h2>
+<em>
+<a href="r.watershed.html">r.watershed</a>,
+<a href="r.fill.dir.html">r.fill.dir</a>
+</em>
+<br>
+<a href="http://jesbergwetter.twoday.net/stories/4845555/">http://jesbergwetter.twoday.net/stories/4845555/</a>
+
+<h2>REFERENCES</h2>
+
+<ul>
+<li>Kilgore, J. L. (1997): <em>Development and evaluation of a GIS-based
+spatially distributed unit hydrograph model</em>, master thesis,
+Virginia
+Polytechnic Institute and State University.</li>
+<li>Melesse, A. M., Graham, W. D. (2004):<em> Storm runoff predicition
+based on a
+spatially distributed travel time method utilizing remote sensing and
+GIS</em>, Journal of the American Water Resources Association, 8,
+863-879.</li>
+<li>Muzik, I. (1996): <em>Flood modelling with GIS-derived distributed
+    unit hydrographs</em>, Hydrological Processes, 10, 1401-1409.</li>
+</ul>
+
+<h2>AUTHOR</h2>
+Kristian Foerster<br>
+<p><i>Last changed: $Date$</i>



More information about the grass-commit mailing list