SAGA API  v9.5
grid_pyramid.cpp
Go to the documentation of this file.
1 /**********************************************************
2  * Version $Id$
3  *********************************************************/
4 
6 // //
7 // SAGA //
8 // //
9 // System for Automated Geoscientific Analyses //
10 // //
11 // Application Programming Interface //
12 // //
13 // Library: SAGA_API //
14 // //
15 //-------------------------------------------------------//
16 // //
17 // grid_pyramids.cpp //
18 // //
19 // Copyright (C) 2009 by Olaf Conrad //
20 // //
21 //-------------------------------------------------------//
22 // //
23 // This file is part of 'SAGA - System for Automated //
24 // Geoscientific Analyses'. //
25 // //
26 // This library is free software; you can redistribute //
27 // it and/or modify it under the terms of the GNU Lesser //
28 // General Public License as published by the Free //
29 // Software Foundation, either version 2.1 of the //
30 // License, or (at your option) any later version. //
31 // //
32 // This library is distributed in the hope that it will //
33 // be useful, but WITHOUT ANY WARRANTY; without even the //
34 // implied warranty of MERCHANTABILITY or FITNESS FOR A //
35 // PARTICULAR PURPOSE. See the GNU Lesser General Public //
36 // License for more details. //
37 // //
38 // You should have received a copy of the GNU Lesser //
39 // General Public License along with this program; if //
40 // not, see <http://www.gnu.org/licenses/>. //
41 // //
42 //-------------------------------------------------------//
43 // //
44 // contact: Olaf Conrad //
45 // Institute of Geography //
46 // University of Hamburg //
47 // Bundesstr. 55 //
48 // 20146 Hamburg //
49 // Germany //
50 // //
51 // e-mail: oconrad@saga-gis.org //
52 // //
54 
55 //---------------------------------------------------------
56 
57 
59 // //
60 // //
61 // //
63 
64 //---------------------------------------------------------
65 #include "grid_pyramid.h"
66 
67 
69 // //
70 // //
71 // //
73 
74 //---------------------------------------------------------
76 {
77  m_nLevels = 0;
78  m_pLevels = NULL;
79  m_pGrid = NULL;
80 }
81 
82 //---------------------------------------------------------
84 {
85  m_nLevels = 0;
86  m_pLevels = NULL;
87  m_pGrid = NULL;
88 
89  Create(pGrid, Grow, Generalisation, Grow_Type);
90 }
91 
92 //---------------------------------------------------------
93 CSG_Grid_Pyramid::CSG_Grid_Pyramid(CSG_Grid *pGrid, double Grow, double Start, int nMaxLevels, TSG_Grid_Pyramid_Generalisation Generalisation, TSG_Grid_Pyramid_Grow_Type Grow_Type)
94 {
95  m_nLevels = 0;
96  m_pLevels = NULL;
97  m_pGrid = NULL;
98 
99  Create(pGrid, Grow, Start, nMaxLevels, Generalisation, Grow_Type);
100 }
101 
102 //---------------------------------------------------------
104 {
105  Destroy();
106 }
107 
108 
110 // //
111 // //
112 // //
114 
115 //---------------------------------------------------------
117 {
118  if( pGrid && pGrid->is_Valid() && Grow > 1.0 && (pGrid->Get_NX() > Grow || pGrid->Get_NY() > Grow) )
119  {
120  Destroy();
121 
122  m_Grow_Type = Grow_Type;
123  m_nMaxLevels = 0;
124  m_pGrid = pGrid;
125  m_Grow = Grow;
126  m_Generalisation = Generalisation;
127 
128  _Get_Next_Level(pGrid);
129 
130  return( true );
131  }
132 
133  return( false );
134 }
135 
136 //---------------------------------------------------------
137 bool CSG_Grid_Pyramid::Create(CSG_Grid *pGrid, double Grow, double Start, int nMaxLevels, TSG_Grid_Pyramid_Generalisation Generalisation, TSG_Grid_Pyramid_Grow_Type Grow_Type)
138 {
139  if( pGrid && pGrid->is_Valid() && Grow > 0.0 && (pGrid->Get_NX() > Grow || pGrid->Get_NY() > Grow) )
140  {
141  Destroy();
142 
143  m_Grow_Type = Grow_Type;
144  m_nMaxLevels = nMaxLevels;
145  m_pGrid = pGrid;
146  m_Grow = Grow;
147  m_Generalisation = Generalisation;
148 
149  if( Start > 0.0 )
150  {
151  _Get_Next_Level(pGrid, Start);
152  }
153  else
154  {
155  _Get_Next_Level(pGrid);
156  }
157 
158  return( true );
159  }
160 
161  return( false );
162 }
163 
164 //---------------------------------------------------------
166 {
167  if( m_pLevels )
168  {
169  for(int i=0; i<m_nLevels; i++)
170  {
171  delete(m_pLevels[i]);
172  }
173 
174  SG_Free(m_pLevels);
175 
176  m_nLevels = 0;
177  m_pLevels = NULL;
178  m_pGrid = NULL;
179  }
180 
181  return( true );
182 }
183 
184 
186 // //
187 // //
188 // //
190 
191 //---------------------------------------------------------
192 bool CSG_Grid_Pyramid::_Get_Next_Level(CSG_Grid *pGrid)
193 {
194  if( (m_nMaxLevels <= 0 || m_nLevels < m_nMaxLevels) )
195  {
196  int nx, ny;
197  double Cellsize;
198 
199  switch( m_Grow_Type )
200  {
201  case GRID_PYRAMID_Arithmetic: Cellsize = pGrid->Get_Cellsize() + m_Grow; break;
202  case GRID_PYRAMID_Geometric: Cellsize = pGrid->Get_Cellsize() * m_Grow; break;
203  default: Cellsize = pGrid->Get_Cellsize() * m_Grow; break;
204  }
205 
206  nx = (int)(1.5 + m_pGrid->Get_XRange() / Cellsize); if( nx < 1 ) nx = 1;
207  ny = (int)(1.5 + m_pGrid->Get_YRange() / Cellsize); if( ny < 1 ) ny = 1;
208 
209  if( nx > 1 || ny > 1 )
210  {
211  CSG_Grid *pNext = SG_Create_Grid(SG_DATATYPE_Float, nx, ny, Cellsize, pGrid->Get_XMin(), pGrid->Get_YMin());
212 
213  pNext->Set_NoData_Value(pGrid->Get_NoData_Value());
214  pNext->Assign(pGrid);
215 
216  m_pLevels = (CSG_Grid **)SG_Realloc(m_pLevels, (m_nLevels + 1) * sizeof(CSG_Grid *));
217  m_pLevels[m_nLevels++] = pNext;
218 
219  _Get_Next_Level(pNext);
220 
221  return( true );
222  }
223  }
224 
225  return( false );
226 }
227 
228 //---------------------------------------------------------
229 bool CSG_Grid_Pyramid::_Get_Next_Level(CSG_Grid *pGrid, double Cellsize)
230 {
231  if( (m_nMaxLevels <= 0 || m_nLevels < m_nMaxLevels) )
232  {
233  int nx, ny;
234 
235  nx = (int)(1.5 + m_pGrid->Get_XRange() / Cellsize); if( nx < 1 ) nx = 1;
236  ny = (int)(1.5 + m_pGrid->Get_YRange() / Cellsize); if( ny < 1 ) ny = 1;
237 
238  if( nx > 1 || ny > 1 )
239  {
240  CSG_Grid *pNext = SG_Create_Grid(SG_DATATYPE_Float, nx, ny, Cellsize, pGrid->Get_XMin(), pGrid->Get_YMin());
241 
242  pNext->Set_NoData_Value(pGrid->Get_NoData_Value());
243  pNext->Assign(pGrid);
244 
245  m_pLevels = (CSG_Grid **)SG_Realloc(m_pLevels, (m_nLevels + 1) * sizeof(CSG_Grid *));
246  m_pLevels[m_nLevels++] = pNext;
247 
248  _Get_Next_Level(pNext);
249 
250  return( true );
251  }
252  }
253 
254  return( false );
255 }
256 
257 
259 // //
260 // //
261 // //
263 
264 //---------------------------------------------------------
CSG_Grid_Pyramid::~CSG_Grid_Pyramid
virtual ~CSG_Grid_Pyramid(void)
Definition: grid_pyramid.cpp:103
GRID_PYRAMID_Geometric
@ GRID_PYRAMID_Geometric
Definition: grid_pyramid.h:102
CSG_Data_Object::Get_NoData_Value
double Get_NoData_Value(bool bUpper=false) const
Definition: dataobject.h:253
TSG_Grid_Pyramid_Generalisation
TSG_Grid_Pyramid_Generalisation
Definition: grid_pyramid.h:90
SG_Free
SAGA_API_DLL_EXPORT void SG_Free(void *memblock)
Definition: api_memory.cpp:83
CSG_Grid::Get_XRange
double Get_XRange(bool bCells=false) const
Definition: grid.h:546
CSG_Grid::Get_YMin
double Get_YMin(bool bCells=false) const
Definition: grid.h:548
CSG_Grid::Get_Cellsize
double Get_Cellsize(void) const
Definition: grid.h:539
TSG_Grid_Pyramid_Grow_Type
TSG_Grid_Pyramid_Grow_Type
Definition: grid_pyramid.h:100
CSG_Grid::is_Valid
virtual bool is_Valid(void) const
Definition: grid.cpp:433
CSG_Grid::Get_NY
int Get_NY(void) const
Definition: grid.h:536
SG_DATATYPE_Float
@ SG_DATATYPE_Float
Definition: api_core.h:1003
CSG_Grid::Assign
virtual bool Assign(double Value=0.0)
Definition: grid_operation.cpp:77
CSG_Grid::Get_XMin
double Get_XMin(bool bCells=false) const
Definition: grid.h:544
grid_pyramid.h
CSG_Grid_Pyramid::Create
bool Create(class CSG_Grid *pGrid, double Grow=2.0, TSG_Grid_Pyramid_Generalisation Generalisation=GRID_PYRAMID_Mean, TSG_Grid_Pyramid_Grow_Type Grow_Type=GRID_PYRAMID_Geometric)
Definition: grid_pyramid.cpp:116
CSG_Grid::Get_YRange
double Get_YRange(bool bCells=false) const
Definition: grid.h:550
CSG_Grid::Get_NX
int Get_NX(void) const
Definition: grid.h:535
CSG_Grid
Definition: grid.h:473
CSG_Grid_Pyramid::Destroy
bool Destroy(void)
Definition: grid_pyramid.cpp:165
CSG_Data_Object::Set_NoData_Value
virtual bool Set_NoData_Value(double Value)
Definition: dataobject.cpp:572
SG_Create_Grid
CSG_Grid * SG_Create_Grid(void)
Definition: grid.cpp:72
SG_Realloc
SAGA_API_DLL_EXPORT void * SG_Realloc(void *memblock, size_t size)
Definition: api_memory.cpp:77
CSG_Grid_Pyramid::CSG_Grid_Pyramid
CSG_Grid_Pyramid(void)
Definition: grid_pyramid.cpp:75
GRID_PYRAMID_Arithmetic
@ GRID_PYRAMID_Arithmetic
Definition: grid_pyramid.h:101