[GRASS-SVN] r65979 - in grass-addons/grass7/raster: . r.wateroutlet.lessmem

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Aug 20 23:51:08 PDT 2015


Author: hcho
Date: 2015-08-20 23:51:08 -0700 (Thu, 20 Aug 2015)
New Revision: 65979

Added:
   grass-addons/grass7/raster/r.wateroutlet.lessmem/
   grass-addons/grass7/raster/r.wateroutlet.lessmem/Makefile
   grass-addons/grass7/raster/r.wateroutlet.lessmem/global.h
   grass-addons/grass7/raster/r.wateroutlet.lessmem/main.c
   grass-addons/grass7/raster/r.wateroutlet.lessmem/over_cells.c
   grass-addons/grass7/raster/r.wateroutlet.lessmem/r.wateroutlet.lessmem.html
Log:
r.wateroutlet.lessmem: Add a new addon; Uses less memory than r.water.outlet

Added: grass-addons/grass7/raster/r.wateroutlet.lessmem/Makefile
===================================================================
--- grass-addons/grass7/raster/r.wateroutlet.lessmem/Makefile	                        (rev 0)
+++ grass-addons/grass7/raster/r.wateroutlet.lessmem/Makefile	2015-08-21 06:51:08 UTC (rev 65979)
@@ -0,0 +1,10 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.wateroutlet.lessmem
+
+LIBES = $(RASTERLIB) $(GISLIB)
+DEPENDENCIES = $(RASTERDEP) $(GISDEP)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd

Added: grass-addons/grass7/raster/r.wateroutlet.lessmem/global.h
===================================================================
--- grass-addons/grass7/raster/r.wateroutlet.lessmem/global.h	                        (rev 0)
+++ grass-addons/grass7/raster/r.wateroutlet.lessmem/global.h	2015-08-21 06:51:08 UTC (rev 65979)
@@ -0,0 +1,34 @@
+#define BITSIZE (((nrows * ncols) >> 3) + ((nrows * ncols) & 7 ? 1 : 0))
+#define SETBIT(b, r, c) \
+    b[((r) * ncols + (c)) >> 3] = \
+    (b[((r) * ncols + (c)) >> 3] & (0xff ^ (1 << (((r) * ncols + (c)) & 7)))) \
+    | (1 << (((r) * ncols + (c)) & 7))
+#define CLEARBIT(b, r, c) \
+    b[((r) * ncols + (c)) >> 3] = \
+    b[((r) * ncols + (c)) >> 3] & (0xff ^ (1 << (((r) * ncols + (c)) & 7)))
+#define GETBIT(b, r, c) \
+    ((b[((r) * ncols + (c)) >> 3] & (1 << (((r) * ncols + (c)) & 7))) \
+     >> (((r) * ncols + (c)) & 7))
+
+#define DIRSIZE (((nrows * ncols) >> 1) + ((nrows * ncols) & 1 ? 1 : 0))
+#define SETDIR(b, r, c, d) \
+    b[((r) * ncols + (c)) >> 1] = \
+    (b[((r) * ncols + (c)) >> 1] & \
+     (0xff ^ (15 << (((r) * ncols + (c)) & 1 ? 4 : 0)))) \
+    | ((d) << (((r) * ncols + (c)) & 1 ? 4 : 0))
+#define GETDIR(b, r, c) \
+    ((b[((r) * ncols + (c)) >> 1] & (15 << (((r) * ncols + (c)) & 1 ? 4 : 0))) \
+     >> (((r) * ncols + (c)) & 1 ? 4 : 0))
+
+/* over_cells.c */
+int overland_cells(int, int);
+
+#ifdef _MAIN_C_
+#define GLOBAL
+#else
+#define GLOBAL extern
+#endif
+
+GLOBAL int nrows, ncols;
+GLOBAL char *drain_ptrs;
+GLOBAL char *bas;

Added: grass-addons/grass7/raster/r.wateroutlet.lessmem/main.c
===================================================================
--- grass-addons/grass7/raster/r.wateroutlet.lessmem/main.c	                        (rev 0)
+++ grass-addons/grass7/raster/r.wateroutlet.lessmem/main.c	2015-08-21 06:51:08 UTC (rev 65979)
@@ -0,0 +1,127 @@
+
+/****************************************************************************
+ *
+ * MODULE:       r.water.outlet
+ * AUTHOR(S):    Charles Ehlschlaeger, USACERL (original contributor)
+ *               Markus Neteler <neteler itc.it>, 
+ *               Roberto Flor <flor itc.it>, 
+ *               Bernhard Reiter <bernhard intevation.de>, 
+ *               Huidae Cho <grass4u gmail.com>, 
+ *               Glynn Clements <glynn gclements.plus.com>, 
+ *               Jan-Oliver Wagner <jan intevation.de>, 
+ *               Soeren Gebbert <soeren.gebbert gmx.de>
+ * PURPOSE:      this program makes a watershed basin raster map using the 
+ *               drainage pointer map, from an outlet point defined by an 
+ *               easting and a northing.
+ * COPYRIGHT:    (C) 1999-2006, 2010, 2013 by the GRASS Development Team
+ *
+ *               This program is free software under the GNU General Public
+ *               License (>=v2). Read the file COPYING that comes with GRASS
+ *               for details.
+ *
+ *****************************************************************************/
+
+#define _MAIN_C_
+#include <stdlib.h>
+#include <string.h>
+#include <grass/raster.h>
+#include <grass/glocale.h>
+#include "global.h"
+
+int main(int argc, char *argv[])
+{
+    struct GModule *module;
+    struct
+    {
+	struct Option *input, *output, *coords;
+    } opt;
+    double N, E;
+    int row, col, basin_fd, drain_fd;
+    CELL *cell_buf;
+    char basin_name[GNAME_MAX], drain_name[GNAME_MAX];
+    struct Cell_head window;
+
+    G_gisinit(argv[0]);
+
+    module = G_define_module();
+    module->description =
+	_("Creates watershed basins from a drainage direction map.");
+    G_add_keyword(_("raster"));
+    G_add_keyword(_("hydrology"));
+    G_add_keyword(_("watershed"));
+
+    opt.input = G_define_standard_option(G_OPT_R_INPUT);
+    opt.input->description = _("Name of input drainage direction map");
+
+    opt.output = G_define_standard_option(G_OPT_R_OUTPUT);
+    opt.output->description = _("Name for output watershed basin map");
+
+    opt.coords = G_define_standard_option(G_OPT_M_COORDS);
+    opt.coords->description = _("Coordinates of outlet point");
+    opt.coords->required = YES;
+
+    /*   Parse command line */
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+
+    G_get_window(&window);
+
+    strcpy(drain_name, opt.input->answer);
+    strcpy(basin_name, opt.output->answer);
+
+    if (!G_scan_easting(opt.coords->answers[0], &E, G_projection()))
+	G_fatal_error(_("Illegal east coordinate '%s'"),
+		      opt.coords->answers[0]);
+    if (!G_scan_northing(opt.coords->answers[1], &N, G_projection()))
+	G_fatal_error(_("Illegal north coordinate '%s'"),
+		      opt.coords->answers[1]);
+
+    G_debug(1, "easting = %.4f northing = %.4f", E, N);
+    if (E < window.west || E > window.east || N < window.south ||
+	N > window.north) {
+	G_warning(_("Ignoring point outside computation region: %.4f,%.4f"),
+		  E, N);
+    }
+
+    G_get_set_window(&window);
+
+    nrows = Rast_window_rows();
+    ncols = Rast_window_cols();
+    drain_fd = Rast_open_old(drain_name, "");
+
+    drain_ptrs = (char *)G_malloc(DIRSIZE);
+    bas = (char *)G_calloc(BITSIZE, 1);
+    cell_buf = Rast_allocate_c_buf();
+
+    for (row = 0; row < nrows; row++) {
+	Rast_get_c_row(drain_fd, cell_buf, row);
+	for (col = 0; col < ncols; col++) {
+	    SETDIR(drain_ptrs, row, col, cell_buf[col]);
+	}
+    }
+    G_free(cell_buf);
+    row = (window.north - N) / window.ns_res;
+    col = (E - window.west) / window.ew_res;
+    if (row >= 0 && col >= 0 && row < nrows && col < ncols)
+	overland_cells(row, col);
+    G_free(drain_ptrs);
+    cell_buf = Rast_allocate_c_buf();
+    basin_fd = Rast_open_c_new(basin_name);
+
+    for (row = 0; row < nrows; row++) {
+	G_percent(row, nrows, 5);
+	for (col = 0; col < ncols; col++) {
+	    cell_buf[col] = GETBIT(bas, row, col);
+	    if (cell_buf[col] == 0)
+		Rast_set_null_value(&cell_buf[col], 1, CELL_TYPE);
+	}
+	Rast_put_row(basin_fd, cell_buf, CELL_TYPE);
+    }
+    G_percent(1, 1, 1);
+
+    G_free(bas);
+    G_free(cell_buf);
+    Rast_close(basin_fd);
+
+    exit(EXIT_SUCCESS);
+}

Added: grass-addons/grass7/raster/r.wateroutlet.lessmem/over_cells.c
===================================================================
--- grass-addons/grass7/raster/r.wateroutlet.lessmem/over_cells.c	                        (rev 0)
+++ grass-addons/grass7/raster/r.wateroutlet.lessmem/over_cells.c	2015-08-21 06:51:08 UTC (rev 65979)
@@ -0,0 +1,49 @@
+#include <grass/raster.h>
+#include "global.h"
+
+#define INCR	64
+#define ONE_CELL struct one_cell
+ONE_CELL {
+    int r, c;
+};
+
+int overland_cells(int row, int col)
+{
+    int r, rr, c, cc, num_cells, size_more;
+    CELL value;
+    ONE_CELL *Acells;
+    int drain[3][3] = { {7, 6, 5}, {8, -17, 4}, {1, 2, 3} };
+
+    if (nrows > ncols)
+	size_more = nrows;
+    else
+	size_more = ncols;
+    Acells = (ONE_CELL *) G_malloc(size_more * sizeof(ONE_CELL));
+    num_cells = 1;
+    Acells[0].r = row;
+    Acells[0].c = col;
+    while (num_cells--) {
+	row = Acells[num_cells].r;
+	col = Acells[num_cells].c;
+	SETBIT(bas, row, col);
+	for (r = row - 1, rr = 0; r <= row + 1; r++, rr++) {
+	    for (c = col - 1, cc = 0; c <= col + 1; c++, cc++) {
+		if (r >= 0 && c >= 0 && r < nrows && c < ncols) {
+		    value = GETDIR(drain_ptrs, r, c);
+		    if ((value == drain[rr][cc]) && (GETBIT(bas, r, c) == 0)) {
+			if (num_cells == size_more) {
+			    size_more += INCR;
+			    Acells = (ONE_CELL *) G_realloc(Acells,
+							    size_more *
+							    sizeof(ONE_CELL));
+			}
+			Acells[num_cells].r = r;
+			Acells[num_cells++].c = c;
+		    }
+		}
+	    }
+	}
+    }
+
+    return 0;
+}

Added: grass-addons/grass7/raster/r.wateroutlet.lessmem/r.wateroutlet.lessmem.html
===================================================================
--- grass-addons/grass7/raster/r.wateroutlet.lessmem/r.wateroutlet.lessmem.html	                        (rev 0)
+++ grass-addons/grass7/raster/r.wateroutlet.lessmem/r.wateroutlet.lessmem.html	2015-08-21 06:51:08 UTC (rev 65979)
@@ -0,0 +1,19 @@
+<h2>DESCRIPTION</h2>
+
+<em>r.wateroutlet.lessmem</em> is a modified version of <em><a href="r.water.outlet.html">r.water.outlet</a></em> and requires about one third of memory compared to the original module. Input drainage direction information is stored as 4-bit segments in the buffer and output basin result is stored as 1-bit data. Because of heavy bitwise operations, there may be some performance penalty depending on the size of the input map. Other than memory management, this module uses the same interface and algorithm in <em><a href="r.water.outlet.html">r.water.outlet</a></em>, so please refer to <em><a href="r.water.outlet.html">r.water.outlet</a></em> for more details.
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="r.water.outlet.html">r.water.outlet</a>,
+</em>
+
+<h2>AUTHOR</h2>
+
+Huidae Cho<br>
+grass4u at gmail.com
+<br>
+based on <a href="r.water.outlet.html">r.water.outlet</a>
+
+<p>
+<i>Last changed: $Date: 2014-12-19 16:17:36 -0500 (Fri, 19 Dec 2014) $</i>



More information about the grass-commit mailing list