[GRASS-SVN] r41273 - grass-addons/raster/r.fuzzy

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Mar 3 01:53:32 EST 2010


Author: jarekj71
Date: 2010-03-03 01:53:31 -0500 (Wed, 03 Mar 2010)
New Revision: 41273

Added:
   grass-addons/raster/r.fuzzy/Makefile
   grass-addons/raster/r.fuzzy/boundary.png
   grass-addons/raster/r.fuzzy/description.html
   grass-addons/raster/r.fuzzy/fuzzy.c
   grass-addons/raster/r.fuzzy/local_proto.h
   grass-addons/raster/r.fuzzy/main.c
   grass-addons/raster/r.fuzzy/set.png
   grass-addons/raster/r.fuzzy/shape.png
Log:
r.fuzzy.file


Added: grass-addons/raster/r.fuzzy/Makefile
===================================================================
--- grass-addons/raster/r.fuzzy/Makefile	                        (rev 0)
+++ grass-addons/raster/r.fuzzy/Makefile	2010-03-03 06:53:31 UTC (rev 41273)
@@ -0,0 +1,16 @@
+# fix this relative to include/
+# or use absolute path to the GRASS source code
+MODULE_TOPDIR = ../..
+
+PGM = r.fuzzy
+
+LIBES     = $(VECTLIB) $(DBMILIB) $(GISLIB)
+DEPENDENCIES = $(VECTDEP) $(DBMIDEP) $(GISDEP)
+EXTRA_INC = $(VECT_INC)
+EXTRA_CFLAGS = $(VECT_CFLAGS) 
+DEPENDENCIES = $(GISDEP)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd
+


Property changes on: grass-addons/raster/r.fuzzy/Makefile
___________________________________________________________________
Added: svn:executable
   + 

Added: grass-addons/raster/r.fuzzy/boundary.png
===================================================================
(Binary files differ)


Property changes on: grass-addons/raster/r.fuzzy/boundary.png
___________________________________________________________________
Added: svn:executable
   + 
Added: svn:mime-type
   + application/octet-stream

Added: grass-addons/raster/r.fuzzy/description.html
===================================================================
--- grass-addons/raster/r.fuzzy/description.html	                        (rev 0)
+++ grass-addons/raster/r.fuzzy/description.html	2010-03-03 06:53:31 UTC (rev 41273)
@@ -0,0 +1,128 @@
+<h2>OPTIONS</h2>
+
+<p>
+<DT><b>input</b></DT>
+<DD>Name of input raster map to be fuzified. This map may be of any type and may require null values. 
+</DD>
+<p>
+
+<DT><b>points</b></DT>
+<DD>A list containing 4 (A,B,C,D) or 2 A,B) points defining set boundaries. Points must not to be in map range, but it may lead to only 0 o 1 membership for the whole map. For "both" side parameters range between A and D defines base, but range between B and C core of the fuzzy set. Between A and B and C and D are set's boundaries. If side is "both" it require 4 points, else 2 points.</DD>
+<p>
+
+<center>
+<h3>Fuzzy set definition:<h3>
+<img src=set.png border=1><br>
+</center>
+
+<DT><b>side</b></DT>
+<DD>Option indicat if set is fuzzified ob both side s (both), left or right side. See description for details.</DD>
+<p>
+
+<h2>OUTPUTS</h2>
+<DT><b>output</b></DT>
+<DD>Map containing membership value of original map. Map is alvays of type FCELLS and contains values from 0 (no membership) to 1 (full membership). Values between 0 and 1 indicate partial membership</DD>
+<p>
+
+<h2>FUZZY SET PARAMETERS</h2>
+
+<DT><b>boundary</b></DT>
+<DD>Parameter definied the shape of the fuzzy boundary. The default and most popular is S-shaped, linear, J-shaped and G-shaped boundaries are also available. The same boundaires are appled to the both sides.</DD>
+<p>
+
+<center>
+<h3>Boundary definition:<h3>
+<img src=boundary.png border=1><br>
+</center
+
+<DT><b>shape</b></DT>
+<DD>Optional shape modifier. Range from -1 to 1. The default value is 0 and shoud  not be changed in most of the time. The negative values indicate more dilatant set, the positive values more concentrate set. See description for details.</DD>
+<p>
+
+<center>
+<h3>Impact of shape parameter on shape boundary:<h3>
+<img src=shape.png border=1><br>
+</center
+
+<DT><b>height</b></DT>
+<DD>Optional height modifier. Range from 0 to 1. The default value is 1 and indicate full membership beteen points B and C. If height is lesser than one the maximum memebrship is equal to height. See image: Fuzzy set definition.</DD>
+<p>
+
+</DL>
+
+<h2>DESCRIPTION</h2>
+
+<P>
+<H4>Definition of fuzzy set</H4>
+Fuzzy sets are sets whose elements have degrees of membership. Zadeh (1965) introduced Fuzzy sets as an extension of the classical notion of set. Classical  membership of elements in a set are binary terms: an element either belongs or does not belong to the set. Fuzzy set theory use the gradual assessment of the membership of elements in a set. A membership function valued in the real unit interval [0, 1]. Classical sets, are special cases of the membership functions of fuzzy sets, if the latter only take values 0 or 1. Classical sets are in fuzzy set theory usually called crisp sets. The fuzzy set theory can be used in a wide range of domains in which information is  imprecise, such as most of the GIS operations.
+
+
+
+<h2>NOTES</H2>
+<H4>Calculation of boundary shape</H4>
+Depending on type of the boundary different equation are used to determine its shape:
+<P>
+<B>Linear:</B> the membership is calculated according following equation:<br>
+<pre><code>
+value  <=  A -> x = 0
+A< value > B -> x = (value-A)/(B-A)
+B <= value >= C -> x = 1
+C< value > D -> x = (D-value)/(D-C)
+value  >=  D -> x = 0
+
+where x: membership
+</code></pre>
+<P>
+<B>S-shaped:</B> it use following equation:
+<pre><code>
+sin(x * Pi/2)^m (for positive shape parameter)
+1-cos(x * Pi/2)^m (for nagative shape parameter)
+
+where x: membership, and 
+m = 2^exp(2,shape) (for positive shape parameter)
+m = 2^(1+shape) (for negative shape parameter)
+where m: shape parameter.
+</code></pre>
+
+For default shape parameter = 0 m is = 2 whcich is most common parameter for that equation.
+
+<P>
+<B>G-shaped and J shaped:</B> it use following equations: 
+<pre><code>
+tan(x * Pi/4)^m (for J-shaped)
+tan(x * Pi/4)^1/m (for G-shaped)
+
+where x: membership, and 
+m = 2^exp(2,shape) (for positive shape parameter)
+m = 2^(1+shape) (for negative shape parameter)
+where m: shape parameter.
+</code></pre>
+ 
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="r.fuzzy.logic.html">r.fuzzy.logic</a>,
+<a href="r.mapcalc.html">r.mapcalc</a>,
+</em>
+
+
+
+<h2>REFERENCES</h2>
+
+Zadeh, L.A. (1965). "Fuzzy sets". Information and Control 8 (3): 338–353. doi:10.1016/S0019-9958(65)90241-X. ISSN 0019-9958.<P>
+
+Novák, Vilém (1989). Fuzzy Sets and Their Applications. Bristol: Adam Hilger. ISBN 0-85274-583-4.<P>
+
+Klir, George J.; Yuan, Bo (1995). Fuzzy sets and fuzzy logic: theory and applications. Upper Saddle River, NJ: Prentice Hall PTR. ISBN 0-13-101171-5.<P>
+
+Klir, George J.; St Clair, Ute H.; Yuan, Bo (1997). Fuzzy set theory: foundations and applications. Englewood Cliffs, NJ: Prentice Hall. ISBN 0133410587.<P>
+
+Meyer D, Hornik K (2009a). \Generalized and Customizable Sets in R." Journal of Statistical Software, 31(2), 1{27. URL http://www.jstatsoft.org/v31/i02/.<P>
+
+Meyer D, Hornik K (2009b). sets: Sets, Generalized Sets, and Customizable Sets. R~package version~1.0, URL http://CRAN.R-project.org/package=sets.<P>
+
+<h2>AUTHOR</h2>
+Jarek  Jasiewicz
+
+


Property changes on: grass-addons/raster/r.fuzzy/description.html
___________________________________________________________________
Added: svn:executable
   + 

Added: grass-addons/raster/r.fuzzy/fuzzy.c
===================================================================
--- grass-addons/raster/r.fuzzy/fuzzy.c	                        (rev 0)
+++ grass-addons/raster/r.fuzzy/fuzzy.c	2010-03-03 06:53:31 UTC (rev 41273)
@@ -0,0 +1,52 @@
+#include <grass/glocale.h>
+#include "local_proto.h"
+
+float fuzzy (FCELL cell) {
+
+	float x, m;
+	
+	if (!side) { /* both left and right */
+	
+			if (cell <= p[0] || cell >= p[3])
+		return 0.;
+			if (cell >= p[1] && cell <= p[2])
+		return 1.;	
+
+		x = (cell<p[1]) ? (cell-p[0])/(p[1]-p[0])
+				: (p[3]-cell)/(p[3]-p[2]);
+	}
+	
+	if (side) { /* left: 1 OR right: 2 */
+
+			if (cell <= p[0])
+		return (side == 1) ? 0. : 1.;
+			if (cell >= p[1])
+		return (side == 1) ? 1. : 0.;
+	
+		x = (side == 1) ? (cell-p[0])/(p[1]-p[0])
+				: (p[1]-cell)/(p[1]-p[0]);
+	}
+
+		if (type == LINEAR)
+	return x;
+		
+		if (type == JSHAPE) {
+	m = (shape < 0) ? pow(2,1+shape) 
+			: pow(2,exp(2*shape));	
+	return pow(tan(x * PI4),m);
+		}
+		
+		if (type == GSHAPE) {
+	m = (shape > 0) ? pow(2,1-shape) 
+			: pow(2,exp(-2*shape));	
+	return pow(tan(x * PI4),1/m);	
+		}
+	
+			if(type==SSHAPE) {
+		m =  pow(2,exp(2*abs(shape)));
+		return (shape < 0) ? pow(1-cos(x*PI2),m)
+			: pow(sin(x*PI2),m);
+	}
+
+return -1; /* error */
+}


Property changes on: grass-addons/raster/r.fuzzy/fuzzy.c
___________________________________________________________________
Added: svn:executable
   + 

Added: grass-addons/raster/r.fuzzy/local_proto.h
===================================================================
--- grass-addons/raster/r.fuzzy/local_proto.h	                        (rev 0)
+++ grass-addons/raster/r.fuzzy/local_proto.h	2010-03-03 06:53:31 UTC (rev 41273)
@@ -0,0 +1,35 @@
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <math.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+/*
+PI2= PI/2
+PI4= PI/4
+*/
+#ifndef PI2
+	#define PI2 (2*atan(1))
+#endif
+
+#ifndef PI4
+	#define PI4 (atan(1))
+#endif
+	
+
+#define LINEAR 0
+#define SSHAPE 1
+#define JSHAPE 2
+#define GSHAPE 3
+
+#define BOTH 0
+#define LEFT 1
+#define RIGHT 2
+
+char *input, *output;
+float shape, height;
+int type, side;
+double p[4]; /* inflection points */
+int num_points;
+
+float fuzzy (FCELL cell);


Property changes on: grass-addons/raster/r.fuzzy/local_proto.h
___________________________________________________________________
Added: svn:executable
   + 

Added: grass-addons/raster/r.fuzzy/main.c
===================================================================
--- grass-addons/raster/r.fuzzy/main.c	                        (rev 0)
+++ grass-addons/raster/r.fuzzy/main.c	2010-03-03 06:53:31 UTC (rev 41273)
@@ -0,0 +1,246 @@
+/* ***************************************************************************
+ *
+ * MODULE:       r.fuzzy
+ * AUTHOR(S):    Jarek Jasiewicz <jarekj amu.edu.pl>
+ * PURPOSE:      Calculate membership value of any raster map according user's rules
+ * COPYRIGHT:    (C) 1999-2010 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
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include "local_proto.h"
+
+int main(int argc, char *argv[])
+{
+	struct GModule *module;
+	struct Option *par_input,
+								*par_output,
+								*par_points,
+								*par_side,
+								*par_type,
+								*par_height,
+								*par_shape;
+	
+	struct Cell_head cellhd; 
+  struct History history;
+	
+	
+	char *mapset;
+	int nrows, ncols;
+  int row, col;
+  int infd, outfd;
+  void *in_buf;
+	unsigned char *out_buf;
+  RASTER_MAP_TYPE raster_type;
+  FCELL tmp=0;
+	
+	G_gisinit(argv[0]);	
+	
+	par_input = G_define_standard_option(G_OPT_R_INPUT);
+  par_input->description = _("Raster map to be fuzzified");
+  
+  par_output = G_define_standard_option(G_OPT_R_OUTPUT);
+	par_output->description = _("Membership map");
+	
+	par_points=G_define_option();
+	par_points->key = "points";
+	par_points->type = "TYPE_STRING";
+	par_points->answer = "a,b[,c,d]";
+	par_points->multiple = YES;
+	par_points->required = YES;
+	par_points->description = _("Inflection points: a,b[,c,d]");
+	
+	par_side=G_define_option();
+	par_side->key = "side";
+	par_side->type = "TYPE_STRING";
+	par_side->options = "both,left,right";
+	par_side->answer = "both";
+	par_side->multiple = NO;
+	par_side->required = YES;
+	par_side->description = _("Fuzzy range");
+	
+	par_type=G_define_option();
+	par_type->key = "boundary";
+	par_type->type = "TYPE_STRING";
+	par_type->options = "Linear,S-shaped,J-shaped,G-shaped";
+	par_type->answer = "S-shaped";
+	par_type->multiple = NO;
+	par_type->required = YES;
+	par_type->description = _("Type of fuzzy boundaries");
+	par_type->guisection = _("Default options");
+	
+	par_shape=G_define_option();
+	par_shape->key = "shape";
+	par_shape->type = "TYPE_FLOAT";
+	par_shape->options = "-1 to 1";
+	par_shape->answer = "0.";
+	par_shape->multiple = NO;
+	par_shape->required = YES;
+	par_shape->description = _("Shape modifier: -1 to 1");
+	par_shape->guisection = _("Default options");
+	
+	par_height=G_define_option();
+	par_height->key = "height";
+	par_height->type = "TYPE_FLOAT";
+	par_height->options = "0 to 1";
+	par_height->answer = "1";
+	par_height->multiple = NO;
+	par_height->required = YES;
+	par_height->description = _("Membership height: 0 to 1");
+	par_height->guisection = _("Default options");
+	
+	    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+	
+	input=par_input->answer;
+	output=par_output->answer;
+		
+		if (!strcmp(par_type->answer, "Linear"))
+	type=LINEAR;
+		else if (!strcmp(par_type->answer, "S-shaped"))
+	type=SSHAPE;
+		else if (!strcmp(par_type->answer, "J-shaped"))
+	type=JSHAPE;
+		else if (!strcmp(par_type->answer, "G-shaped"))
+	type=GSHAPE;
+	
+			if (!strcmp(par_side->answer, "both"))
+	side=BOTH;
+		else if (!strcmp(par_side->answer, "left"))
+	side=LEFT;
+		else if (!strcmp(par_side->answer, "right"))
+	side=RIGHT;
+	
+	shape = atof(par_shape->answer);
+		if(shape<-1. || shape>1.)
+	G_fatal_error(_("Shape modifier must be between -1 and 1"));
+	
+	height = atof(par_height->answer);
+		if(height>1 || height<0)
+	G_fatal_error(_("Heght modifier must be between 0 and 1"));
+		
+	num_points=sscanf(par_points->answer,
+			"%lf,%lf,%lf,%lf", 
+			&p[0], &p[1], &p[2], &p[3]);
+	
+		if(!side && num_points != 4)
+	G_fatal_error(_("Wrong number of values: got %d but need 4"),
+		      num_points);
+	
+			if(side && num_points != 2)
+	G_fatal_error(_("Wrong number of values: got %d but need 2"),
+		      num_points);
+	
+	if (num_points==2) {
+			if(	p[0]>p[1])
+		G_fatal_error(_("Point sequence must be: a <= b"));
+	} else {
+			if(	p[0]>p[1]
+			||p[1]>p[2]
+			||p[2]>p[3])
+		G_fatal_error(_("Point sequence must be: a <= b; b <= c; c <= d;"));	
+	}
+
+	/* end of interface */
+
+	mapset = G_find_cell2(input, "");
+	
+	    if (mapset == NULL)
+	G_fatal_error(_("Raster map <%s> not found"), input);
+	
+	    if ((infd = G_open_cell_old(input, mapset)) < 0)
+	G_fatal_error(_("Unable to open raster map <%s>"), input);
+	
+	    if (G_get_cellhd(input, mapset, &cellhd) < 0)
+	G_fatal_error(_("Unable to read file header of <%s>"), input);
+	
+	
+	nrows = G_window_rows();
+	ncols = G_window_cols();
+	raster_type = G_get_raster_map_type(infd);
+	
+	   if ((outfd = G_open_raster_new(output, FCELL_TYPE)) < 0)
+	G_fatal_error(_("Unable to create raster map <%s>"), output);
+	
+
+	in_buf = G_allocate_raster_buf(raster_type);
+	out_buf = G_allocate_raster_buf(FCELL_TYPE);
+	
+	/* processing */
+	for (row = 0; row < nrows; row++) {
+		G_percent(row, nrows, 2);
+		CELL c;
+		FCELL f;
+		DCELL d;
+		
+		if (G_get_raster_row(infd, in_buf, row, raster_type)<0) {
+			G_close_cell(infd);
+			G_fatal_error(_("Cannot to read <%s> at row <%d>"), input,row);
+		}
+
+		for (col = 0; col < ncols; col++) {
+				 
+			switch (raster_type) {
+
+				case CELL_TYPE:
+			c = ((CELL *) in_buf)[col];
+				if(G_is_null_value(&c,CELL_TYPE))
+			G_set_f_null_value(&tmp, 1);
+				else {
+					if(0>(tmp = fuzzy ((FCELL) c)))
+			G_warning("Cannot determine memebership at row %d, col %d", row,col);
+			((FCELL *) out_buf)[col] = tmp;
+				}
+			break;
+
+				case FCELL_TYPE:
+			f = ((FCELL *) in_buf)[col];
+				if(G_is_null_value(&f,FCELL_TYPE))
+			G_set_f_null_value(&tmp, 1);
+				else {			
+				tmp = fuzzy((FCELL) f);
+				if(0>(tmp = fuzzy((FCELL) f)))
+			G_warning("Cannot determine memebership at row %d, col %d", row,col);
+			((FCELL *) out_buf)[col] = tmp;
+				}
+			break;
+
+				case DCELL_TYPE:
+			d = ((DCELL *) in_buf)[col];
+				if(G_is_null_value(&d,DCELL_TYPE))
+			G_set_f_null_value(&tmp, 1);
+				else {
+				if(0>(tmp = fuzzy ((FCELL) d)))
+			G_warning("Cannot determine memebership at row %d, col %d", row,col);
+			((FCELL *) out_buf)[col] = tmp;
+				}
+			break;
+	    }
+
+		}
+				if (G_put_raster_row(outfd, out_buf, FCELL_TYPE) < 0)
+	    G_fatal_error(_("Failed writing raster map <%s>"), output);
+	} /* end for */
+	
+  G_free(in_buf);
+  G_free(out_buf);
+
+	G_close_cell(infd);
+	G_close_cell(outfd);
+
+	G_short_history(output, "raster", &history);
+	G_command_history(&history);
+	G_write_history(output, &history);
+	
+	exit(EXIT_SUCCESS);
+}
+
+	
+	


Property changes on: grass-addons/raster/r.fuzzy/main.c
___________________________________________________________________
Added: svn:executable
   + 

Added: grass-addons/raster/r.fuzzy/set.png
===================================================================
(Binary files differ)


Property changes on: grass-addons/raster/r.fuzzy/set.png
___________________________________________________________________
Added: svn:executable
   + 
Added: svn:mime-type
   + application/octet-stream

Added: grass-addons/raster/r.fuzzy/shape.png
===================================================================
(Binary files differ)


Property changes on: grass-addons/raster/r.fuzzy/shape.png
___________________________________________________________________
Added: svn:executable
   + 
Added: svn:mime-type
   + application/octet-stream



More information about the grass-commit mailing list