[GRASS-SVN] r43578 - grass/branches/releasebranch_6_4/raster/r.neighbors

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Sep 21 08:53:32 EDT 2010


Author: neteler
Date: 2010-09-21 12:53:31 +0000 (Tue, 21 Sep 2010)
New Revision: 43578

Modified:
   grass/branches/releasebranch_6_4/raster/r.neighbors/description.html
   grass/branches/releasebranch_6_4/raster/r.neighbors/local_proto.h
   grass/branches/releasebranch_6_4/raster/r.neighbors/main.c
   grass/branches/releasebranch_6_4/raster/r.neighbors/readweights.c
Log:
backport: Gaussian and range method

Modified: grass/branches/releasebranch_6_4/raster/r.neighbors/description.html
===================================================================
--- grass/branches/releasebranch_6_4/raster/r.neighbors/description.html	2010-09-21 12:48:52 UTC (rev 43577)
+++ grass/branches/releasebranch_6_4/raster/r.neighbors/description.html	2010-09-21 12:53:31 UTC (rev 43578)
@@ -91,6 +91,10 @@
    |----|----|----|          |----|----|----|
 </pre>
 
+<dt><b>range</b>
+
+<dd>The range value within the neighborhood.
+
 <dt><b>stddev</b> 
 
 <dd>The statistical standard deviation of values

Modified: grass/branches/releasebranch_6_4/raster/r.neighbors/local_proto.h
===================================================================
--- grass/branches/releasebranch_6_4/raster/r.neighbors/local_proto.h	2010-09-21 12:48:52 UTC (rev 43577)
+++ grass/branches/releasebranch_6_4/raster/r.neighbors/local_proto.h	2010-09-21 12:53:31 UTC (rev 43578)
@@ -22,3 +22,4 @@
 
 /* read_weights.c */
 extern void read_weights(const char *);
+extern void gaussian_weights(double);

Modified: grass/branches/releasebranch_6_4/raster/r.neighbors/main.c
===================================================================
--- grass/branches/releasebranch_6_4/raster/r.neighbors/main.c	2010-09-21 12:48:52 UTC (rev 43577)
+++ grass/branches/releasebranch_6_4/raster/r.neighbors/main.c	2010-09-21 12:53:31 UTC (rev 43578)
@@ -49,6 +49,7 @@
     {c_mode, w_mode, NO_CATS, 1, 0, "mode", "most frequently occuring value"},
     {c_min, NULL, NO_CATS, 1, 0, "minimum", "lowest value"},
     {c_max, NULL, NO_CATS, 1, 0, "maximum", "highest value"},
+    {c_range, NULL, NO_CATS, 1, 0, "range", "range value"},
     {c_stddev, w_stddev, NO_CATS, 0, 1, "stddev", "standard deviation"},
     {c_sum, w_sum, NO_CATS, 1, 0, "sum", "sum of values"},
     {c_var, w_var, NO_CATS, 0, 1, "variance", "statistical variance"},
@@ -89,6 +90,7 @@
 	struct Option *method, *size;
 	struct Option *title;
 	struct Option *weight;
+	struct Option *gauss;
     } parm;
     struct
     {
@@ -153,6 +155,12 @@
     parm.weight->gisprompt = "old_file,file,input";
     parm.weight->description = _("File containing weights");
 
+    parm.gauss = G_define_option();
+    parm.gauss->key = "gauss";
+    parm.gauss->type = TYPE_DOUBLE;
+    parm.gauss->required = NO;
+    parm.gauss->description = _("Sigma (in cells) for Gaussian filter");
+
     flag.align = G_define_flag();
     flag.align->key = 'a';
     flag.align->description = _("Do not align output with the input");
@@ -189,6 +197,8 @@
 	G_fatal_error(_("<%s> is an illegal file name"), p);
     }
     ncb.newcell.mapset = G_mapset();
+    if (parm.weight->answer && parm.gauss->answer)
+	G_fatal_error(_("weight= and gauss= are mutually exclusive"));
 
     if (!flag.align->answer) {
 	if (G_get_cellhd(ncb.oldcell.name, ncb.oldcell.mapset, &cellhd) < 0)
@@ -240,6 +250,11 @@
 	if (!newvalue_w)
 	    weights_mask();
     }
+    else if (parm.gauss->answer) {
+	if (!newvalue_w)
+	    G_fatal_error(_("Method %s not compatible with Gaussian filter"), parm.method->answer);
+	gaussian_weights(atof(parm.gauss->answer));
+    }
     else
 	newvalue_w = NULL;
 

Modified: grass/branches/releasebranch_6_4/raster/r.neighbors/readweights.c
===================================================================
--- grass/branches/releasebranch_6_4/raster/r.neighbors/readweights.c	2010-09-21 12:48:52 UTC (rev 43577)
+++ grass/branches/releasebranch_6_4/raster/r.neighbors/readweights.c	2010-09-21 12:53:31 UTC (rev 43578)
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <math.h>
 #include <grass/gis.h>
 #include <grass/glocale.h>
 #include "ncb.h"
@@ -23,3 +24,21 @@
 
     fclose(fp);
 }
+
+void gaussian_weights(double sigma)
+{
+    double sigma2 = sigma * sigma;
+    int i, j;
+
+    ncb.weights = G_malloc(ncb.nsize * sizeof(DCELL *));
+    for (i = 0; i < ncb.nsize; i++)
+	ncb.weights[i] = G_malloc(ncb.nsize * sizeof(DCELL));
+
+    for (i = 0; i < ncb.nsize; i++) {
+	double y = i - ncb.dist;
+	for (j = 0; j < ncb.nsize; j++) {
+	    double x = j - ncb.dist;
+	    ncb.weights[i][j] = exp(-(x*x+y*y)/(2*sigma2))/(2*M_PI*sigma2);
+	}
+    }
+}



More information about the grass-commit mailing list