1 /**********************************************************
2  * Version $Id$
3  *********************************************************/
4 
5 ///////////////////////////////////////////////////////////
6 //                                                       //
7 //                         SAGA                          //
8 //                                                       //
9 //      System for Automated Geoscientific Analyses      //
10 //                                                       //
11 //                     Tool Library                      //
12 //                     Grid_Calculus                     //
13 //                                                       //
14 //-------------------------------------------------------//
15 //                                                       //
16 //                     FuzzyOR.cpp                       //
17 //                                                       //
18 //                 Copyright (C) 2004 by                 //
19 //           Antonio Boggia and Gianluca Massei          //
20 //                                                       //
21 //-------------------------------------------------------//
22 //                                                       //
23 // This file is part of 'SAGA - System for Automated     //
24 // Geoscientific Analyses'. SAGA is free software; you   //
25 // can redistribute it and/or modify it under the terms  //
26 // of the GNU General Public License as published by the //
27 // Free Software Foundation, either version 2 of the     //
28 // License, or (at your option) any later version.       //
29 //                                                       //
30 // SAGA is distributed in the hope that it will be       //
31 // useful, but WITHOUT ANY WARRANTY; without even the    //
32 // implied warranty of MERCHANTABILITY or FITNESS FOR A  //
33 // PARTICULAR PURPOSE. See the GNU General Public        //
34 // License for more details.                             //
35 //                                                       //
36 // You should have received a copy of the GNU General    //
37 // Public License along with this program; if not, see   //
38 // <http://www.gnu.org/licenses/>.                       //
39 //                                                       //
40 //-------------------------------------------------------//
41 //                                                       //
42 //    e-mail:     boggia@unipg.it						 //
43 //				  g_massa@libero.it				     	 //
44 //                                                       //
45 //    contact:    Antonio Boggia                         //
46 //                Gianluca Massei                        //
47 //                Department of Economics and Appraisal  //
48 //                University of Perugia - Italy			 //
49 //                www.unipg.it                           //
50 //                                                       //
51 ///////////////////////////////////////////////////////////
52 
53 //---------------------------------------------------------
54 
55 
56 ///////////////////////////////////////////////////////////
57 //														 //
58 //														 //
59 //														 //
60 ///////////////////////////////////////////////////////////
61 
62 //---------------------------------------------------------
63 #include "FuzzyOR.h"
64 
65 
66 ///////////////////////////////////////////////////////////
67 //														 //
68 //														 //
69 //														 //
70 ///////////////////////////////////////////////////////////
71 
72 //---------------------------------------------------------
CFuzzyOR(void)73 CFuzzyOR::CFuzzyOR(void)
74 {
75 	Set_Name		(_TL("Fuzzy Union (OR)"));
76 
77 	Set_Author		(SG_T("Antonio Boggia and Gianluca Massei (c) 2004"));
78 
79 	Set_Description	(_TW(
80 		"Calculates the union (max operator) for each grid cell of the selected grids.\n "
81 		"e-mail Gianluca Massei: g_massa@libero.it \n"
82 		"e-mail Antonio Boggia: boggia@unipg.it \n")
83 	);
84 
85 	Parameters.Add_Grid_List(
86 		NULL, "GRIDS"	, _TL("Grids"),
87 		_TL(""),
88 		PARAMETER_INPUT
89 	);
90 
91 	Parameters.Add_Grid(
92 		NULL, "OR"		, _TL("Union"),
93 		_TL(""),
94 		PARAMETER_OUTPUT
95 	);
96 
97 	Parameters.Add_Choice(
98 		NULL, "TYPE"	, _TL("Operator Type"),
99 		_TL(""),
100 		CSG_String::Format(SG_T("%s|%s|%s|"),
101 			_TL("max(a, b) (non-interactive)"),
102 			_TL("a + b - a * b"),
103 			_TL("min(1, a + b)")
104 		), 0
105 	);
106 }
107 
108 
109 ///////////////////////////////////////////////////////////
110 //														 //
111 //														 //
112 //														 //
113 ///////////////////////////////////////////////////////////
114 
115 //---------------------------------------------------------
On_Execute(void)116 bool CFuzzyOR::On_Execute(void)
117 {
118 	int						Type;
119 	CSG_Grid				*pOR;
120 	CSG_Parameter_Grid_List	*pGrids;
121 
122 	//-----------------------------------------------------
123 	pGrids	= Parameters("GRIDS")	->asGridList();
124 	pOR		= Parameters("OR")		->asGrid();
125 	Type	= Parameters("TYPE")	->asInt();
126 
127 	//-----------------------------------------------------
128 	if( pGrids->Get_Grid_Count() < 1 )
129 	{
130 		return( false );
131 	}
132 
133 	//-----------------------------------------------------
134 	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
135 	{
136 		for(int x=0; x<Get_NX(); x++)
137 		{
138 			bool	bNoData	= pGrids->Get_Grid(0)->is_NoData(x, y);
139 			double	OR		= pGrids->Get_Grid(0)->asDouble (x, y);
140 
141 			for(int i=1; i<pGrids->Get_Grid_Count() && !bNoData; i++)
142 			{
143 				if( !(bNoData = pGrids->Get_Grid(i)->is_NoData(x, y)) )
144 				{
145 					double	iz	= pGrids->Get_Grid(i)->asDouble(x, y);
146 
147 					switch( Type )
148 					{
149 					case 0:
150 						if( OR < iz )
151 						{
152 							OR	= iz;
153 						}
154 						break;
155 
156 					case 1:
157 						OR	= OR + iz - OR * iz;
158 						break;
159 
160 					case 2:
161 						if( (OR = OR + iz) > 1.0 )
162 						{
163 							OR	= 1.0;
164 						}
165 						break;
166 					}
167 				}
168 			}
169 
170 			if( bNoData )
171 			{
172 				pOR->Set_NoData(x, y);
173 			}
174 			else
175 			{
176 				pOR->Set_Value(x, y, OR);
177 			}
178 		}
179 	}
180 
181 	//-----------------------------------------------------
182 	return( true );
183 }
184 
185 
186 ///////////////////////////////////////////////////////////
187 //														 //
188 //														 //
189 //														 //
190 ///////////////////////////////////////////////////////////
191 
192 //---------------------------------------------------------
193