1 
2 ///////////////////////////////////////////////////////////
3 //                                                       //
4 //                         SAGA                          //
5 //                                                       //
6 //      System for Automated Geoscientific Analyses      //
7 //                                                       //
8 //                     Tool Library                      //
9 //                      Grid_Tools                       //
10 //                                                       //
11 //-------------------------------------------------------//
12 //                                                       //
13 //                  Grid_Value_NoData.cpp                //
14 //                                                       //
15 //                 Copyright (C) 2016 by                 //
16 //                      Olaf Conrad                      //
17 //                                                       //
18 //-------------------------------------------------------//
19 //                                                       //
20 // This file is part of 'SAGA - System for Automated     //
21 // Geoscientific Analyses'. SAGA is free software; you   //
22 // can redistribute it and/or modify it under the terms  //
23 // of the GNU General Public License as published by the //
24 // Free Software Foundation, either version 2 of the     //
25 // License, or (at your option) any later version.       //
26 //                                                       //
27 // SAGA is distributed in the hope that it will be       //
28 // useful, but WITHOUT ANY WARRANTY; without even the    //
29 // implied warranty of MERCHANTABILITY or FITNESS FOR A  //
30 // PARTICULAR PURPOSE. See the GNU General Public        //
31 // License for more details.                             //
32 //                                                       //
33 // You should have received a copy of the GNU General    //
34 // Public License along with this program; if not, see   //
35 // <http://www.gnu.org/licenses/>.                       //
36 //                                                       //
37 //-------------------------------------------------------//
38 //                                                       //
39 //    e-mail:     oconrad@saga-gis.org                   //
40 //                                                       //
41 //    contact:    Olaf Conrad                            //
42 //                Institute of Geography                 //
43 //                University of Hamburg                  //
44 //                Germany                                //
45 //                                                       //
46 ///////////////////////////////////////////////////////////
47 
48 //---------------------------------------------------------
49 #include "Grid_Value_NoData.h"
50 
51 
52 ///////////////////////////////////////////////////////////
53 //														 //
54 //														 //
55 //														 //
56 ///////////////////////////////////////////////////////////
57 
58 //---------------------------------------------------------
CGrid_Value_NoData(void)59 CGrid_Value_NoData::CGrid_Value_NoData(void)
60 {
61 	Set_Name		(_TL("Change a Grid's No-Data Value"));
62 
63 	Set_Author		("O.Conrad (c) 2016");
64 
65 	Set_Description	(_TW(
66 		"This tool allows changing a grid's no-data value or value range "
67 		"definition. It does not change the cell values of the grid, "
68 		"unless you check the 'Change Values' option. "
69 		"Its main purpose is to support this type of operation for tool "
70 		"chains and scripting environments. "
71 		"If the 'Change Values' option is checked all no-data cells will be "
72 		"changed to the new no-data value. "
73 	));
74 
75 	//-----------------------------------------------------
76 	Parameters.Add_Grid("",
77 		"GRID"	, _TL("Grid"),
78 		_TL(""),
79 		PARAMETER_INPUT
80 	);
81 
82 	Parameters.Add_Grid("",
83 		"OUTPUT", _TL("Changed Grid"),
84 		_TL(""),
85 		PARAMETER_OUTPUT_OPTIONAL
86 	);
87 
88 	Parameters.Add_Choice("",
89 		"TYPE"	, _TL("Type"),
90 		_TL(""),
91 		CSG_String::Format("%s|%s",
92 			_TL("single value"),
93 			_TL("value range")
94 		), 0
95 	);
96 
97 	Parameters.Add_Double("",
98 		"VALUE"	, _TL("No-Data Value"),
99 		_TL(""),
100 		-99999.
101 	);
102 
103 	Parameters.Add_Range("",
104 		"RANGE"	, _TL("No-Data Value Range"),
105 		_TL(""),
106 		-99999., -99999.
107 	);
108 
109 	Parameters.Add_Bool("",
110 		"CHANGE", _TL("Change Values"),
111 		_TL(""),
112 		false
113 	);
114 }
115 
116 
117 ///////////////////////////////////////////////////////////
118 //														 //
119 ///////////////////////////////////////////////////////////
120 
121 //---------------------------------------------------------
On_Parameter_Changed(CSG_Parameters * pParameters,CSG_Parameter * pParameter)122 int CGrid_Value_NoData::On_Parameter_Changed(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
123 {
124 	if( pParameter->Cmp_Identifier("GRID") && pParameter->asGrid() && has_GUI() )
125 	{
126 		CSG_Grid	*pGrid	= pParameter->asGrid();
127 
128 		pParameters->Set_Parameter("VALUE",
129 			pGrid->Get_NoData_Value()
130 		);
131 
132 		pParameters->Get_Parameter("RANGE")->asRange()->Set_Range(
133 			pGrid->Get_NoData_Value(), pGrid->Get_NoData_Value(true)
134 		);
135 
136 		pParameters->Set_Parameter("TYPE",
137 			pGrid->Get_NoData_Value() < pGrid->Get_NoData_Value(true) ? 1 : 0
138 		);
139 
140 		On_Parameters_Enable(pParameters, (*pParameters)("TYPE"));
141 	}
142 
143 	return( CSG_Tool_Grid::On_Parameter_Changed(pParameters, pParameter) );
144 }
145 
146 //---------------------------------------------------------
On_Parameters_Enable(CSG_Parameters * pParameters,CSG_Parameter * pParameter)147 int CGrid_Value_NoData::On_Parameters_Enable(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
148 {
149 	if( pParameter->Cmp_Identifier("TYPE") )
150 	{
151 		pParameters->Set_Enabled("VALUE", pParameter->asInt() == 0);
152 		pParameters->Set_Enabled("RANGE", pParameter->asInt() == 1);
153 	}
154 
155 	return( CSG_Tool_Grid::On_Parameters_Enable(pParameters, pParameter) );
156 }
157 
158 
159 ///////////////////////////////////////////////////////////
160 //														 //
161 ///////////////////////////////////////////////////////////
162 
163 //---------------------------------------------------------
On_Execute(void)164 bool CGrid_Value_NoData::On_Execute(void)
165 {
166 	CSG_Grid	*pGrid	= Parameters("GRID")->asGrid();
167 
168 	if( Parameters("OUTPUT")->asGrid() && Parameters("OUTPUT")->asGrid() != pGrid )
169 	{
170 		Parameters("OUTPUT")->asGrid()->Create(*pGrid);
171 
172 		pGrid	= Parameters("OUTPUT")->asGrid();
173 
174 		pGrid->Fmt_Name("%s [%s]", pGrid->Get_Name(), _TL("No-Data Changed"));
175 	}
176 
177 	//-----------------------------------------------------
178 	double	nodata_min	= Parameters("TYPE")->asInt() == 0
179 		? Parameters("VALUE"    )->asDouble()
180 		: Parameters("RANGE.MIN")->asDouble();
181 
182 	double	nodata_max	= Parameters("TYPE")->asInt() == 0
183 		? Parameters("VALUE"    )->asDouble()
184 		: Parameters("RANGE.MAX")->asDouble();
185 
186 	if( nodata_min == pGrid->Get_NoData_Value(false)
187 	&&  nodata_max == pGrid->Get_NoData_Value(true ) )
188 	{
189 		Message_Fmt("\n%s\n%s", _TL("Nothing to do!"), _TL("Targeted no-data value (range) is already present."));
190 
191 		return( true );
192 	}
193 
194 	//-----------------------------------------------------
195 	if( Parameters("CHANGE")->asBool() )
196 	{
197 		#pragma omp parallel for
198 		for(int y=0; y<Get_NY(); y++)
199 		{
200 			for(int x=0; x<Get_NX(); x++)
201 			{
202 				if( pGrid->is_NoData(x, y) )
203 				{
204 					pGrid->Set_Value(x, y, nodata_min);
205 				}
206 			}
207 		}
208 	}
209 
210 	//-----------------------------------------------------
211 	pGrid->Set_NoData_Value_Range(nodata_min, nodata_max);
212 
213 	if( pGrid == Parameters("GRID")->asGrid() )
214 	{
215 		DataObject_Update(pGrid);
216 	}
217 
218 	return( true );
219 }
220 
221 
222 ///////////////////////////////////////////////////////////
223 //														 //
224 //														 //
225 //														 //
226 ///////////////////////////////////////////////////////////
227 
228 //---------------------------------------------------------
229