1 /**********************************************************
2  * Version $Id: Filter_Resample.cpp 1086 2011-06-08 10:12:02Z reklov_w $
3  *********************************************************/
4 
5 ///////////////////////////////////////////////////////////
6 //                                                       //
7 //                         SAGA                          //
8 //                                                       //
9 //      System for Automated Geoscientific Analyses      //
10 //                                                       //
11 //                     Tool Library                      //
12 //                      Grid_Filter                      //
13 //                                                       //
14 //-------------------------------------------------------//
15 //                                                       //
16 //                  Filter_Resample.cpp                  //
17 //                                                       //
18 //                 Copyright (C) 2012 by                 //
19 //                      Olaf Conrad                      //
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:     oconrad@saga-gis.org                   //
43 //                                                       //
44 //    contact:    Olaf Conrad                            //
45 //                Institute of Geography                 //
46 //                University of Hamburg                  //
47 //                Germany                                //
48 //                                                       //
49 ///////////////////////////////////////////////////////////
50 
51 //---------------------------------------------------------
52 
53 
54 ///////////////////////////////////////////////////////////
55 //														 //
56 //														 //
57 //														 //
58 ///////////////////////////////////////////////////////////
59 
60 //---------------------------------------------------------
61 #include "Filter_Resample.h"
62 
63 
64 ///////////////////////////////////////////////////////////
65 //														 //
66 //														 //
67 //														 //
68 ///////////////////////////////////////////////////////////
69 
70 //---------------------------------------------------------
CFilter_Resample(void)71 CFilter_Resample::CFilter_Resample(void)
72 {
73 	//-----------------------------------------------------
74 	Set_Name		(_TL("Resampling Filter"));
75 
76 	Set_Author		("O.Conrad (c) 2012");
77 
78 	Set_Description	(_TW(
79 		"Resampling filter for grids. Resamples in a first step the "
80 		"given grid to desired resampling cell size, expressed as "
81 		"multiple of the original cell size (scale factor). This is an up-scaling "
82 		"through which cell values are aggregated as cell area weighted "
83 		"means. Second step is the down-scaling to original cell size "
84 		"using spline interpolation. Specially for larger search distances "
85 		"this is a comparably fast alternative for simple low and high "
86 		"pass filter operations. "
87 	));
88 
89 	//-----------------------------------------------------
90 	Parameters.Add_Grid("",
91 		"GRID"		, _TL("Grid"),
92 		_TL(""),
93 		PARAMETER_INPUT
94 	);
95 
96 	Parameters.Add_Grid("",
97 		"LOPASS"	, _TL("Low Pass Filter"),
98 		_TL(""),
99 		PARAMETER_OUTPUT
100 	);
101 
102 	Parameters.Add_Grid("",
103 		"HIPASS"	, _TL("High Pass Filter"),
104 		_TL(""),
105 		PARAMETER_OUTPUT
106 	);
107 
108 	Parameters.Add_Double("",
109 		"SCALE"		, _TL("Scale Factor"),
110 		_TL(""),
111 		10.0, 1.0, true
112 	);
113 }
114 
115 
116 ///////////////////////////////////////////////////////////
117 //														 //
118 ///////////////////////////////////////////////////////////
119 
120 //---------------------------------------------------------
On_Execute(void)121 bool CFilter_Resample::On_Execute(void)
122 {
123 	//-----------------------------------------------------
124 	CSG_Grid	*pGrid		= Parameters("GRID"  )->asGrid();
125 	CSG_Grid	*pLoPass	= Parameters("LOPASS")->asGrid();
126 	CSG_Grid	*pHiPass	= Parameters("HIPASS")->asGrid();
127 
128 	double	Cellsize	= Parameters("SCALE" )->asDouble() * Get_Cellsize();
129 
130 	//-----------------------------------------------------
131 	if( Cellsize > 0.5 * SG_Get_Length(Get_System().Get_XRange(), Get_System().Get_YRange()) )
132 	{
133 		Error_Set(_TL("resampling cell size is too large"));
134 
135 		return( false );
136 	}
137 
138 	//-----------------------------------------------------
139 	CSG_Grid	Grid(CSG_Grid_System(Cellsize, Get_XMin(), Get_YMin(), Get_XMax(), Get_YMax()), SG_DATATYPE_Float);
140 
141 	Grid.Assign(pGrid, GRID_RESAMPLING_Mean_Cells);
142 
143 	//-----------------------------------------------------
144 	pLoPass->Fmt_Name("%s [%s]", pGrid->Get_Name(), _TL("Low Pass" ));
145 	pHiPass->Fmt_Name("%s [%s]", pGrid->Get_Name(), _TL("High Pass"));
146 
147 	CSG_Colors	Colors;
148 
149 	DataObject_Get_Colors(pGrid  , Colors);
150 	DataObject_Set_Colors(pLoPass, Colors);
151 	DataObject_Set_Colors(pHiPass, 11, SG_COLORS_RED_GREY_BLUE);
152 
153 	//-----------------------------------------------------
154 	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
155 	{
156 		double	py	= Get_YMin() + y * Get_Cellsize();
157 
158 		#pragma omp parallel for
159 		for(int x=0; x<Get_NX(); x++)
160 		{
161 			double	z, px	= Get_XMin() + x * Get_Cellsize();
162 
163 			if( !pGrid->is_NoData(x, y) && Grid.Get_Value(px, py, z) )
164 			{
165 				pLoPass->Set_Value(x, y, z);
166 				pHiPass->Set_Value(x, y, pGrid->asDouble(x, y) - z);
167 			}
168 			else
169 			{
170 				pLoPass->Set_NoData(x, y);
171 				pHiPass->Set_NoData(x, y);
172 			}
173 		}
174 	}
175 
176 	//-----------------------------------------------------
177 	return( true );
178 }
179 
180 
181 ///////////////////////////////////////////////////////////
182 //														 //
183 //														 //
184 //														 //
185 ///////////////////////////////////////////////////////////
186 
187 //---------------------------------------------------------
188