[GRASS-SVN] r40534 - grass/trunk/raster/r.neighbors

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Jan 18 10:57:05 EST 2010


Author: huhabla
Date: 2010-01-18 10:57:03 -0500 (Mon, 18 Jan 2010)
New Revision: 40534

Modified:
   grass/trunk/raster/r.neighbors/main.c
   grass/trunk/raster/r.neighbors/r.neighbors.html
Log:
Added new option selection. This option specifies a raster map which is used to 
process only input cells which are marked as non-NULL cells in the selection map.

Modified: grass/trunk/raster/r.neighbors/main.c
===================================================================
--- grass/trunk/raster/r.neighbors/main.c	2010-01-18 15:36:38 UTC (rev 40533)
+++ grass/trunk/raster/r.neighbors/main.c	2010-01-18 15:57:03 UTC (rev 40534)
@@ -28,6 +28,7 @@
 #include "ncb.h"
 #include "local_proto.h"
 
+
 typedef int (*ifunc) (void);
 
 struct menu
@@ -71,9 +72,11 @@
     char *p;
     int method;
     int in_fd;
+    int selection_fd;
     int out_fd;
     DCELL *result;
-    RASTER_MAP_TYPE map_type;
+    void *selection_ptr, *selection, *input, *input_ptr;
+    RASTER_MAP_TYPE map_type, selection_type;
     int row, col;
     int readrow;
     int nrows, ncols;
@@ -92,7 +95,7 @@
     struct GModule *module;
     struct
     {
-	struct Option *input, *output;
+	struct Option *input, *output, *selection;
 	struct Option *method, *size;
 	struct Option *title;
 	struct Option *weight;
@@ -119,9 +122,13 @@
 	  "map layer.");
 
     parm.input = G_define_standard_option(G_OPT_R_INPUT);
+    parm.selection = G_define_standard_option(G_OPT_R_INPUT);
+    parm.selection->key = "selection";
+    parm.selection->required = NO;
+    parm.selection->description = "Name of an input raster map to select the cells which should be processed";
 
     parm.output = G_define_standard_option(G_OPT_R_OUTPUT);
-
+    
     parm.method = G_define_option();
     parm.method->key = "method";
     parm.method->type = TYPE_STRING;
@@ -215,7 +222,6 @@
 
     /* open raster maps */
     in_fd = Rast_open_old(ncb.oldcell, "");
-
     map_type = Rast_get_map_type(in_fd);
 
     /* get the method */
@@ -281,8 +287,17 @@
     for (row = 0; row < ncb.dist; row++)
 	readcell(in_fd, readrow++, nrows, ncols);
 
-    /* open raster map */
-    in_fd = Rast_open_old(ncb.oldcell, "");
+    /* open the selection raster map */
+    if(parm.selection->answer) {
+	G_message("Opening selection map %s\n", parm.selection->answer);
+	selection_fd = Rast_open_old(parm.selection->answer, "");
+        selection_type = Rast_get_map_type(selection_fd);
+        selection = Rast_allocate_buf(selection_type);
+        input = Rast_allocate_buf(map_type);
+    } else {
+        selection_fd = -1;
+        selection = NULL;
+    }
 
     /*open the new raster map */
     out_fd = Rast_open_new(ncb.newcell, map_type);
@@ -299,25 +314,42 @@
     for (row = 0; row < nrows; row++) {
 	G_percent(row, nrows, 2);
 	readcell(in_fd, readrow++, nrows, ncols);
+        /* if selection map is enabled read each row of the
+           selection and intput map
+         */
+	if(selection != NULL) {
+            Rast_get_row(selection_fd, selection, row, selection_type);
+            Rast_get_row(in_fd, input, row, map_type);
+            selection_ptr = selection;
+            input_ptr = input;
+        }
+
 	for (col = 0; col < ncols; col++) {
 	    DCELL *rp = &result[col];
-
-	    if (newvalue_w)
-		n = gather_w(values_w, col);
-	    else
-		n = gather(values, col);
-
-	    if (n < 0)
-		Rast_set_d_null_value(rp, 1);
-	    else {
+            if((selection != NULL && Rast_is_null_value(selection_ptr, selection_type) == 1)) {
+		*rp = Rast_get_d_value((const void *)input_ptr, map_type);
+            }else {
 		if (newvalue_w)
-		    newvalue_w(rp, values_w, n, closure);
+		    n = gather_w(values_w, col);
 		else
-		    newvalue(rp, values, n, closure);
+		    n = gather(values, col);
 
-		if (half && !Rast_is_d_null_value(rp))
-		    *rp += 0.5;
-	    }
+		if (n < 0)
+		    Rast_set_d_null_value(rp, 1);
+		else {
+		    if (newvalue_w)
+		        newvalue_w(rp, values_w, n, closure);
+		    else
+		        newvalue(rp, values, n, closure);
+
+		    if (half && !Rast_is_d_null_value(rp))
+		        *rp += 0.5;
+		}
+            }
+            if(selection != NULL) {
+                selection_ptr = G_incr_void_ptr(selection_ptr, Rast_cell_size(selection_type));
+                input_ptr = G_incr_void_ptr(input_ptr, Rast_cell_size(map_type));
+            }
 	}
 	Rast_put_d_row(out_fd, result);
     }
@@ -326,6 +358,10 @@
     Rast_close(out_fd);
     Rast_close(in_fd);
 
+    if(selection != NULL)
+        Rast_close(selection_fd);
+
+
     /* put out category info */
     null_cats();
     if ((cat_names = menu[method].cat_names))
@@ -343,3 +379,4 @@
 
     exit(EXIT_SUCCESS);
 }
+

Modified: grass/trunk/raster/r.neighbors/r.neighbors.html
===================================================================
--- grass/trunk/raster/r.neighbors/r.neighbors.html	2010-01-18 15:36:38 UTC (rev 40533)
+++ grass/trunk/raster/r.neighbors/r.neighbors.html	2010-01-18 15:57:03 UTC (rev 40534)
@@ -28,14 +28,55 @@
 <b>method</b> used to analyze neighborhood
 values (i.e., the neighborhood function or operation to be
 performed), and the <b>size</b> of the neighborhood.
+<p>
+The user can optionally
+specify a <b>selection</b> map, to compute new values only where the raster
+cells of the selection map are not NULL. In case of a NULL cells,
+the values from the input map are copied into the output map.
+This may useful to smooth only parts of an elevation map (pits, peaks, ...).
+
+<p>
+<em>Example how to use a selection map with method=average:</em><br>
+input map:
+<pre>
+1 1  1 1 1
+1 1  1 1 1
+1 1 10 1 1
+1 1  1 1 1
+1 1  1 1 1
+</pre>
+selection map, NULL values are marked as *:
+<pre>
+* * * * *
+* * 1 * *
+* 1 1 1 *
+* * 1 * *
+* * * * *
+</pre>
+The output map:
+<pre>
+1 1 1 1 1
+1 1 2 1 1
+1 2 2 2 1
+1 1 2 1 1
+1 1 1 1 1
+</pre>
+Without using the selection map, the output map would look like this:
+<pre>
+1 1 1 1 1
+1 2 2 2 1
+1 2 2 2 1
+1 2 2 2 1
+1 1 1 1 1
+</pre>
+
+<p>
 Optionally, the user can also specify the <b>TITLE</b> to
 be assigned to the raster map layer <b>output</b>, elect
 to not align the resolution of the output with that of the
 input (the <b>-a</b> option), and run <em><b>r.neighbors</b></em>
 with a custom matrix weights with the <em>weight</em> option.
 These options are described further below.
-
-
 <p>
 
 <em>Neighborhood Operation Methods:</em>
@@ -222,7 +263,7 @@
 with the current mask, if any.  It is recommended, but not required,
 that the resolution of the geographic region be the same as that
 of the raster map layer.  By default, <em><b>r.neighbors</b></em> will align
-these geographic region settings.  However, the user can elect to keep
+these geographic region settings.  However, the user can select to keep
 original input and output resolutions which are not aligned by specifying
 this (e.g., using the <b>-a</b> option).
 <p>



More information about the grass-commit mailing list