1 
2 ///////////////////////////////////////////////////////////
3 //                                                       //
4 //                         SAGA                          //
5 //                                                       //
6 //      System for Automated Geoscientific Analyses      //
7 //                                                       //
8 //           Application Programming Interface           //
9 //                                                       //
10 //                  Library: SAGA_API                    //
11 //                                                       //
12 //-------------------------------------------------------//
13 //                                                       //
14 //                        grid.h                         //
15 //                                                       //
16 //          Copyright (C) 2005 by Olaf Conrad            //
17 //                                                       //
18 //-------------------------------------------------------//
19 //                                                       //
20 // This file is part of 'SAGA - System for Automated     //
21 // Geoscientific Analyses'.                              //
22 //                                                       //
23 // This library is free software; you can redistribute   //
24 // it and/or modify it under the terms of the GNU Lesser //
25 // General Public License as published by the Free       //
26 // Software Foundation, either version 2.1 of the        //
27 // License, or (at your option) any later version.       //
28 //                                                       //
29 // This library is distributed in the hope that it will  //
30 // be useful, but WITHOUT ANY WARRANTY; without even the //
31 // implied warranty of MERCHANTABILITY or FITNESS FOR A  //
32 // PARTICULAR PURPOSE. See the GNU Lesser General Public //
33 // License for more details.                             //
34 //                                                       //
35 // You should have received a copy of the GNU Lesser     //
36 // General Public License along with this program; if    //
37 // not, see <http://www.gnu.org/licenses/>.              //
38 //                                                       //
39 //-------------------------------------------------------//
40 //                                                       //
41 //    contact:    Olaf Conrad                            //
42 //                Institute of Geography                 //
43 //                University of Hamburg                  //
44 //                Germany                                //
45 //                                                       //
46 //    e-mail:     oconrad@saga-gis.org                   //
47 //                                                       //
48 ///////////////////////////////////////////////////////////
49 
50 //---------------------------------------------------------
51 #ifndef HEADER_INCLUDED__SAGA_API__grid_H
52 #define HEADER_INCLUDED__SAGA_API__grid_H
53 
54 
55 ///////////////////////////////////////////////////////////
56 //														 //
57 //														 //
58 //														 //
59 ///////////////////////////////////////////////////////////
60 
61 //---------------------------------------------------------
62 /** \file grid.h
63 * Classes for raster data management.
64 * @see CSG_Data_Object
65 * @see CSG_Grid_System
66 * @see CSG_Grid
67 */
68 
69 
70 ///////////////////////////////////////////////////////////
71 //														 //
72 //														 //
73 //														 //
74 ///////////////////////////////////////////////////////////
75 
76 //---------------------------------------------------------
77 #include "table.h"
78 #include "grid_pyramid.h"
79 
80 
81 ///////////////////////////////////////////////////////////
82 //														 //
83 //						Grid File						 //
84 //														 //
85 ///////////////////////////////////////////////////////////
86 
87 //---------------------------------------------------------
88 typedef enum ESG_Grid_File_Format
89 {
90 	GRID_FILE_FORMAT_Undefined			= 0,
91 	GRID_FILE_FORMAT_Binary_old,
92 	GRID_FILE_FORMAT_Binary,
93 	GRID_FILE_FORMAT_ASCII,
94 	GRID_FILE_FORMAT_Compressed,
95 	GRID_FILE_FORMAT_GeoTIFF
96 }
97 TSG_Grid_File_Format;
98 
99 //---------------------------------------------------------
100 typedef enum ESG_Grid_File_Key
101 {
102 	GRID_FILE_KEY_NAME					= 0,
103 	GRID_FILE_KEY_DESCRIPTION,
104 	GRID_FILE_KEY_UNITNAME,
105 	GRID_FILE_KEY_DATAFILE_NAME,
106 	GRID_FILE_KEY_DATAFILE_OFFSET,
107 	GRID_FILE_KEY_DATAFORMAT,
108 	GRID_FILE_KEY_BYTEORDER_BIG,
109 	GRID_FILE_KEY_POSITION_XMIN,
110 	GRID_FILE_KEY_POSITION_YMIN,
111 	GRID_FILE_KEY_CELLCOUNT_X,
112 	GRID_FILE_KEY_CELLCOUNT_Y,
113 	GRID_FILE_KEY_CELLSIZE,
114 	GRID_FILE_KEY_Z_FACTOR,
115 	GRID_FILE_KEY_Z_OFFSET,
116 	GRID_FILE_KEY_NODATA_VALUE,
117 	GRID_FILE_KEY_TOPTOBOTTOM,
118 	GRID_FILE_KEY_Count
119 }
120 TSG_Grid_File_Key;
121 
122 //---------------------------------------------------------
123 const SG_Char	gSG_Grid_File_Key_Names[GRID_FILE_KEY_Count][32]	=
124 {
125 	SG_T("NAME"),
126 	SG_T("DESCRIPTION"),
127 	SG_T("UNIT"),
128 	SG_T("DATAFILE_NAME"),
129 	SG_T("DATAFILE_OFFSET"),
130 	SG_T("DATAFORMAT"),
131 	SG_T("BYTEORDER_BIG"),
132 	SG_T("POSITION_XMIN"),
133 	SG_T("POSITION_YMIN"),
134 	SG_T("CELLCOUNT_X"),
135 	SG_T("CELLCOUNT_Y"),
136 	SG_T("CELLSIZE"),
137 	SG_T("Z_FACTOR"),
138 	SG_T("Z_OFFSET"),
139 	SG_T("NODATA_VALUE"),
140 	SG_T("TOPTOBOTTOM")
141 };
142 
143 //---------------------------------------------------------
144 #define GRID_FILE_KEY_TRUE	SG_T("TRUE")
145 #define GRID_FILE_KEY_FALSE	SG_T("FALSE")
146 
147 
148 ///////////////////////////////////////////////////////////
149 //														 //
150 //														 //
151 //														 //
152 ///////////////////////////////////////////////////////////
153 
154 //---------------------------------------------------------
155 typedef enum ESG_Grid_Resampling
156 {
157 	GRID_RESAMPLING_NearestNeighbour	= 0,
158 	GRID_RESAMPLING_Bilinear,
159 	GRID_RESAMPLING_BicubicSpline,
160 	GRID_RESAMPLING_BSpline,
161 
162 	GRID_RESAMPLING_Mean_Nodes,
163 	GRID_RESAMPLING_Mean_Cells,
164 	GRID_RESAMPLING_Minimum,
165 	GRID_RESAMPLING_Maximum,
166 	GRID_RESAMPLING_Majority,
167 
168 	GRID_RESAMPLING_Undefined
169 }
170 TSG_Grid_Resampling;
171 
172 //---------------------------------------------------------
173 typedef enum ESG_Grid_Operation
174 {
175 	GRID_OPERATION_Addition				= 0,
176 	GRID_OPERATION_Subtraction,
177 	GRID_OPERATION_Multiplication,
178 	GRID_OPERATION_Division
179 }
180 TSG_Grid_Operation;
181 
182 
183 ///////////////////////////////////////////////////////////
184 //														 //
185 //					CSG_Grid_System						 //
186 //														 //
187 ///////////////////////////////////////////////////////////
188 
189 //---------------------------------------------------------
190 /**
191   * CSG_Grid_System is used by the CSG_Grid class to provide
192   * information about the number of rows and columns, the
193   * cell size and the georeference, which define the grid.
194   * It offers various functions, which help when working
195   * with grids.
196   * @see CSG_Grid
197 */
198 //---------------------------------------------------------
199 class SAGA_API_DLL_EXPORT CSG_Grid_System
200 {
201 public:
202 	CSG_Grid_System(void);
203 
204 								CSG_Grid_System		(const CSG_Grid_System &System);
205 	bool						Create				(const CSG_Grid_System &System);
206 
207 								CSG_Grid_System		(double Cellsize, const CSG_Rect &Extent);
208 	bool						Create				(double Cellsize, const CSG_Rect &Extent);
209 
210 								CSG_Grid_System		(double Cellsize, double xMin, double yMin, double xMax, double yMax);
211 	bool						Create				(double Cellsize, double xMin, double yMin, double xMax, double yMax);
212 
213 								CSG_Grid_System		(double Cellsize, double xMin, double yMin, int NX, int NY);
214 	bool						Create				(double Cellsize, double xMin, double yMin, int NX, int NY);
215 
216 	~CSG_Grid_System(void);
217 
218 	bool						Destroy				(void);
219 
220 	//-----------------------------------------------------
221 	bool						Assign				(const CSG_Grid_System &System);
222 	bool						Assign				(double Cellsize, const CSG_Rect &Extent);
223 	bool						Assign				(double Cellsize, double xMin, double yMin, double xMax, double yMax);
224 	bool						Assign				(double Cellsize, double xMin, double yMin, int NX, int NY);
225 
226 	//-----------------------------------------------------
227 	bool						is_Valid			(void)	const;
228 
229 	const SG_Char *				Get_Name			(bool bShort = true);
230 
Get_Cellsize(void)231 	double						Get_Cellsize		(void)	const	{	return( m_Cellsize );	}
Get_Cellarea(void)232 	double						Get_Cellarea		(void)	const	{	return( m_Cellarea );	}
233 
Get_NX(void)234 	int							Get_NX				(void)	const	{	return( m_NX );			}
Get_NY(void)235 	int							Get_NY				(void)	const	{	return( m_NY );			}
Get_NCells(void)236 	sLong						Get_NCells			(void)	const	{	return( m_NCells );		}
237 
238 	const CSG_Rect &			Get_Extent			(bool bCells = false)	const	{	return( bCells ? m_Extent_Cells : m_Extent );	}
239 
240 	double						Get_XMin			(bool bCells = false)	const	{	return( bCells ? m_Extent_Cells.Get_XMin  () : m_Extent.Get_XMin  () );	}
241 	double						Get_XMax			(bool bCells = false)	const	{	return( bCells ? m_Extent_Cells.Get_XMax  () : m_Extent.Get_XMax  () );	}
242 	double						Get_XRange			(bool bCells = false)	const	{	return( bCells ? m_Extent_Cells.Get_XRange() : m_Extent.Get_XRange() );	}
243 
244 	double						Get_YMin			(bool bCells = false)	const	{	return( bCells ? m_Extent_Cells.Get_YMin  () : m_Extent.Get_YMin  () );	}
245 	double						Get_YMax			(bool bCells = false)	const	{	return( bCells ? m_Extent_Cells.Get_YMax  () : m_Extent.Get_YMax  () );	}
246 	double						Get_YRange			(bool bCells = false)	const	{	return( bCells ? m_Extent_Cells.Get_YRange() : m_Extent.Get_YRange() );	}
247 
248 
249 	//-----------------------------------------------------
250 	bool						operator ==			(const CSG_Grid_System &System) const;
251 	void						operator =			(const CSG_Grid_System &System);
252 
253 	bool						is_Equal			(const CSG_Grid_System &System) const;
254 	bool						is_Equal			(double Cellsize, const TSG_Rect &Extent) const;
255 
256 
257 	//-----------------------------------------------------
258 	/// Aligns the world coordinate x to the rows of the grid system and returns it.
Fit_xto_Grid_System(double x)259 	double						Fit_xto_Grid_System	(double x)	const	{	return( Get_XMin() + m_Cellsize * Get_xWorld_to_Grid(x) );	}
260 
261 	/// Aligns the world coordinate y to the columns of the grid system and returns it.
Fit_yto_Grid_System(double y)262 	double						Fit_yto_Grid_System	(double y)	const	{	return( Get_YMin() + m_Cellsize * Get_yWorld_to_Grid(y) );	}
263 
264 	/// Aligns the world coordinate ptWorld to the rows and columns of the grid system and returns it.
Fit_to_Grid_System(TSG_Point ptWorld)265 	TSG_Point					Fit_to_Grid_System	(TSG_Point ptWorld)	const
266 	{
267 		ptWorld.x	= Fit_xto_Grid_System(ptWorld.x);
268 		ptWorld.y	= Fit_yto_Grid_System(ptWorld.y);
269 
270 		return( ptWorld );
271 	}
272 
273 
274 	//-----------------------------------------------------
Get_xGrid_to_World(int xGrid)275 	double						Get_xGrid_to_World	(int xGrid)	const	{	return( Get_XMin() + xGrid * m_Cellsize );	}
Get_yGrid_to_World(int yGrid)276 	double						Get_yGrid_to_World	(int yGrid)	const	{	return( Get_YMin() + yGrid * m_Cellsize );	}
277 
Get_Grid_to_World(int xGrid,int yGrid)278 	TSG_Point					Get_Grid_to_World	(int xGrid, int yGrid)	const
279 	{
280 		TSG_Point	pt;
281 
282 		pt.x	= Get_xGrid_to_World(xGrid);
283 		pt.y	= Get_yGrid_to_World(yGrid);
284 
285 		return( pt );
286 	}
287 
288 
289 	//-----------------------------------------------------
Get_xWorld_to_Grid(double xWorld)290 	int							Get_xWorld_to_Grid	(double xWorld)	const	{	return( (int)floor(0.5 + (xWorld - Get_XMin()) / m_Cellsize) );	}
Get_yWorld_to_Grid(double yWorld)291 	int							Get_yWorld_to_Grid	(double yWorld)	const	{	return( (int)floor(0.5 + (yWorld - Get_YMin()) / m_Cellsize) );	}
292 
Get_World_to_Grid(int & xGrid,int & yGrid,double xWorld,double yWorld)293 	bool						Get_World_to_Grid	(int &xGrid, int &yGrid, double xWorld, double yWorld)	const
294 	{
295 		return( is_InGrid(xGrid = Get_xWorld_to_Grid(xWorld), yGrid = Get_yWorld_to_Grid(yWorld)) );
296 	}
297 
Get_World_to_Grid(int & xGrid,int & yGrid,TSG_Point ptWorld)298 	bool						Get_World_to_Grid	(int &xGrid, int &yGrid, TSG_Point ptWorld)	const
299 	{
300 		return( is_InGrid(xGrid = Get_xWorld_to_Grid(ptWorld.x), yGrid = Get_yWorld_to_Grid(ptWorld.y)) );
301 	}
302 
303 
304 	//-----------------------------------------------------
Get_Neighbor_Pos(int Direction,int x,int y,int & xPos,int & yPos)305 	bool						Get_Neighbor_Pos	(int Direction, int x, int y, int &xPos, int &yPos)	const
306 	{
307 		return( is_InGrid(xPos = Get_xTo(Direction, x), yPos = Get_yTo(Direction, y)) );
308 	}
309 
310 	static int					Get_xTo				(int Direction, int x = 0)
311 	{
312 		static int	ix[8]	= { 0, 1, 1, 1, 0,-1,-1,-1 };
313 
314 		Direction	%= 8;
315 
316 		if( Direction < 0 )
317 		{
318 			Direction	+= 8;
319 		}
320 
321 		return( x + ix[Direction] );
322 	}
323 
324 	static int					Get_yTo				(int Direction, int y = 0)
325 	{
326 		static int	iy[8]	= { 1, 1, 0,-1,-1,-1, 0, 1 };
327 
328 		Direction	%= 8;
329 
330 		if( Direction < 0 )
331 		{
332 			Direction	+= 8;
333 		}
334 
335 		return( y + iy[Direction] );
336 	}
337 
338 	static int					Get_xFrom			(int Direction, int x = 0)			{	return( Get_xTo(Direction + 4, x) );	}
339 	static int					Get_yFrom			(int Direction, int y = 0)			{	return( Get_yTo(Direction + 4, y) );	}
340 
Get_xToSave(int Direction,int x)341 	int							Get_xToSave			(int Direction, int x)		const	{	return( (x = Get_xTo  (Direction, x)) < 0 ? 0 : (x >= m_NX ? m_NX - 1 : x) );	}
Get_yToSave(int Direction,int y)342 	int							Get_yToSave			(int Direction, int y)		const	{	return( (y = Get_yTo  (Direction, y)) < 0 ? 0 : (y >= m_NY ? m_NY - 1 : y) );	}
Get_xFromSave(int Direction,int x)343 	int							Get_xFromSave		(int Direction, int x)		const	{	return( (x = Get_xFrom(Direction, x)) < 0 ? 0 : (x >= m_NX ? m_NX - 1 : x) );	}
Get_yFromSave(int Direction,int y)344 	int							Get_yFromSave		(int Direction, int y)		const	{	return( (y = Get_yFrom(Direction, y)) < 0 ? 0 : (y >= m_NY ? m_NY - 1 : y) );	}
345 
is_InGrid(int x,int y)346 	bool						is_InGrid			(int x, int y)				const	{	return(	x >= 0    && x < m_NX        && y >= 0    && y < m_NY );		}
is_InGrid(int x,int y,int Rand)347 	bool						is_InGrid			(int x, int y, int Rand)	const	{	return(	x >= Rand && x < m_NX - Rand && y >= Rand && y < m_NY - Rand );	}
348 
Get_Length(int Direction)349 	double						Get_Length			(int Direction)				const	{	return( Direction % 2 ? m_Diagonal : m_Cellsize );	}
Get_UnitLength(int Direction)350 	static double				Get_UnitLength		(int Direction)						{	return( Direction % 2 ? sqrt(2.0)  : 1.0 );			}
351 
352 	static int					Set_Precision		(int Decimals);
353 	static int					Get_Precision		(void);
354 
Get_IndexFromRowCol(int x,int y)355 	sLong						Get_IndexFromRowCol	(int  x, int  y)	const
356 	{
357 		if( m_NX > 0 )
358 		{
359 			return( (sLong)y * m_NX + x );
360 		}
361 
362 		return( -1 );
363 	}
364 
Get_RowColFromIndex(int & x,int & y,sLong i)365 	bool						Get_RowColFromIndex	(int &x, int &y, sLong i)	const
366 	{
367 		if( m_NX > 0 )
368 		{
369 			x	= (int)(i % m_NX);
370 			y	= (int)(i / m_NX);
371 
372 			return( true );
373 		}
374 
375 		return( false );
376 	}
377 
378 
379 private:	///////////////////////////////////////////////
380 
381 	int							m_NX, m_NY;
382 
383 	sLong						m_NCells;
384 
385 	double						m_Cellsize, m_Cellarea, m_Diagonal;
386 
387 	CSG_Rect					m_Extent, m_Extent_Cells;
388 
389 	CSG_String					m_Name;
390 
391 	static int					m_Precision;
392 
393 };
394 
395 
396 ///////////////////////////////////////////////////////////
397 //														 //
398 //						CSG_Grid						 //
399 //														 //
400 ///////////////////////////////////////////////////////////
401 
402 //---------------------------------------------------------
403 /**
404   * CSG_Grid is the data object created for raster handling.
405 */
406 //---------------------------------------------------------
407 class SAGA_API_DLL_EXPORT CSG_Grid_File_Info
408 {
409 //---------------------------------------------------------
410 public:		///////////////////////////////////////////////
411 
412 	//-----------------------------------------------------
413 	CSG_Grid_File_Info(void);
414 
415 								CSG_Grid_File_Info		(const CSG_Grid_File_Info &Info);
416 	bool						Create					(const CSG_Grid_File_Info &Info);
417 
418 								CSG_Grid_File_Info		(const CSG_String &FileName);
419 	bool						Create					(const CSG_String &FileName);
420 
421 								CSG_Grid_File_Info		(CSG_File &Stream);
422 	bool						Create					(CSG_File &Stream);
423 
424 								CSG_Grid_File_Info		(const CSG_Grid &Grid);
425 	bool						Create					(const CSG_Grid &Grid);
426 
427 	bool						Save					(const CSG_String &FileName, bool bBinary = true);
428 	bool						Save					(const CSG_File   &Stream  , bool bBinary = true);
429 
430 	static bool					Save					(const CSG_String &FileName, const CSG_Grid &Grid, bool bBinary = true);
431 	static bool					Save					(const CSG_File   &Stream  , const CSG_Grid &Grid, bool bBinary = true);
432 
433 	bool						Save_AUX_XML			(const CSG_String &FileName);
434 	bool						Save_AUX_XML			(CSG_File &Stream);
435 
436 	//-----------------------------------------------------
437 	bool						m_bFlip, m_bSwapBytes;
438 
439 	sLong						m_Offset;
440 
441 	double						m_zScale, m_zOffset, m_NoData[2];
442 
443 	TSG_Data_Type				m_Type;
444 
445 	CSG_String					m_Name, m_Description, m_Unit, m_Data_File;
446 
447 	CSG_Grid_System				m_System;
448 
449 	CSG_Projection				m_Projection;
450 
451 
452 private:
453 
454 	void						_On_Construction		(void);
455 
456 	int							_Get_Key				(CSG_File &Stream, CSG_String &Line);
457 
458 };
459 
460 
461 ///////////////////////////////////////////////////////////
462 //														 //
463 //						CSG_Grid						 //
464 //														 //
465 ///////////////////////////////////////////////////////////
466 
467 //---------------------------------------------------------
468 /**
469   * CSG_Grid is the data object created for raster handling.
470 */
471 //---------------------------------------------------------
472 class SAGA_API_DLL_EXPORT CSG_Grid : public CSG_Data_Object
473 {
474 //---------------------------------------------------------
475 public:		///////////////////////////////////////////////
476 
477 	//-----------------------------------------------------
478 	CSG_Grid(void);
479 
480 									CSG_Grid		(const CSG_Grid &Grid);
481 	bool							Create			(const CSG_Grid &Grid);
482 
483 									CSG_Grid		(const CSG_String &FileName   , TSG_Data_Type Type = SG_DATATYPE_Undefined, bool bCached = false, bool bLoadData = true);
484 	bool							Create			(const CSG_String &FileName   , TSG_Data_Type Type = SG_DATATYPE_Undefined, bool bCached = false, bool bLoadData = true);
485 
486 									CSG_Grid		(CSG_Grid *pGrid              , TSG_Data_Type Type = SG_DATATYPE_Undefined, bool bCached = false);
487 	bool							Create			(CSG_Grid *pGrid              , TSG_Data_Type Type = SG_DATATYPE_Undefined, bool bCached = false);
488 
489 									CSG_Grid		(const CSG_Grid_System &System, TSG_Data_Type Type = SG_DATATYPE_Undefined, bool bCached = false);
490 	bool							Create			(const CSG_Grid_System &System, TSG_Data_Type Type = SG_DATATYPE_Undefined, bool bCached = false);
491 
492 									CSG_Grid		(TSG_Data_Type Type, int NX, int NY, double Cellsize = 0.0, double xMin = 0.0, double yMin = 0.0, bool bCached = false);
493 	bool							Create			(TSG_Data_Type Type, int NX, int NY, double Cellsize = 0.0, double xMin = 0.0, double yMin = 0.0, bool bCached = false);
494 
495 
496 	//-----------------------------------------------------
497 	virtual ~CSG_Grid(void);
498 
499 	virtual bool					Destroy			(void);
500 
501 
502 	//-----------------------------------------------------
503 	virtual bool					Save			(const CSG_String &File, int Format = 0);
504 	virtual bool					Save			(const char       *File, int Format = 0)	{	return( Save(CSG_String(File), Format) );	}
505 	virtual bool					Save			(const wchar_t    *File, int Format = 0)	{	return( Save(CSG_String(File), Format) );	}
506 
507 	//-----------------------------------------------------
508 	/** Data object type information.
509 	*/
Get_ObjectType(void)510 	virtual TSG_Data_Object_Type	Get_ObjectType	(void)	const	{	return( SG_DATAOBJECT_TYPE_Grid );		}
511 
512 	//-----------------------------------------------------
513 	// Data-Info...
514 
Get_Type(void)515 	TSG_Data_Type					Get_Type		(void)	const	{	return( m_Type );					}
516 
Get_nValueBytes(void)517 	int								Get_nValueBytes	(void)	const	{	return( (int)m_nBytes_Value );	}
Get_nLineBytes(void)518 	int								Get_nLineBytes	(void)	const	{	return( (int)m_nBytes_Line  );	}
519 
520 	void							Set_Unit		(const CSG_String &Unit);
Get_Unit(void)521 	const CSG_String &				Get_Unit		(void)	const	{	return( m_Unit );	}
522 
523 
524 	//-----------------------------------------------------
525 	// Georeference...
526 
Get_System(void)527 	const CSG_Grid_System &			Get_System		(void)	const	{	return( m_System );					}
528 
Get_Extent(void)529 	virtual const CSG_Rect &		Get_Extent		(void)			{	return( m_System.Get_Extent() );	}
530 
Get_NX(void)531 	int								Get_NX			(void)	const	{	return( m_System.Get_NX() );		}
Get_NY(void)532 	int								Get_NY			(void)	const	{	return( m_System.Get_NY() );		}
Get_NCells(void)533 	sLong							Get_NCells		(void)	const	{	return( m_System.Get_NCells() );	}
534 
Get_Cellsize(void)535 	double							Get_Cellsize	(void)	const	{	return( m_System.Get_Cellsize() );	}
Get_Cellarea(void)536 	double							Get_Cellarea	(void)	const	{	return( m_System.Get_Cellarea() );	}
537 
538 	const CSG_Rect &				Get_Extent		(bool bCells = false)	const	{	return( m_System.Get_Extent(bCells) );	}
539 
540 	double							Get_XMin		(bool bCells = false)	const	{	return( m_System.Get_XMin  (bCells) );	}
541 	double							Get_XMax		(bool bCells = false)	const	{	return( m_System.Get_XMax  (bCells) );	}
542 	double							Get_XRange		(bool bCells = false)	const	{	return( m_System.Get_XRange(bCells) );	}
543 
544 	double							Get_YMin		(bool bCells = false)	const	{	return( m_System.Get_YMin  (bCells) );	}
545 	double							Get_YMax		(bool bCells = false)	const	{	return( m_System.Get_YMax  (bCells) );	}
546 	double							Get_YRange		(bool bCells = false)	const	{	return( m_System.Get_YRange(bCells) );	}
547 
548 
549 	//-----------------------------------------------------
550 	// Values...
551 
552 	void							Set_Scaling			(double Scale = 1.0, double Offset = 0.0);
553 	double							Get_Scaling			(void)	const;
554 	double							Get_Offset			(void)	const;
is_Scaled(void)555 	bool							is_Scaled			(void)	const	{	return( m_zScale != 1.0 || m_zOffset != 0.0 );	}
556 
557 	double							Get_Mean			(void);
558 	double							Get_Min				(void);
559 	double							Get_Max				(void);
560 	double							Get_Range			(void);
561 	double							Get_StdDev			(void);
562 	double							Get_Variance		(void);
563 	double							Get_Quantile		(double   Quantile, bool bFromHistogram = true);
564 	double							Get_Percentile		(double Percentile, bool bFromHistogram = true);
565 
566 	const CSG_Simple_Statistics &	Get_Statistics		(void);
567 	bool							Get_Statistics		(const CSG_Rect &rWorld, CSG_Simple_Statistics &Statistics, bool bHoldValues = false)	const;
568 
569 	const CSG_Histogram &			Get_Histogram		(size_t nClasses = 0);
570 	bool							Get_Histogram		(const CSG_Rect &rWorld, CSG_Histogram &Histogram, size_t nClasses = 0)	const;
571 
572 	sLong							Get_Data_Count		(void);
573 	sLong							Get_NoData_Count	(void);
574 
575 
576 	//-----------------------------------------------------
577 	// Checks...
578 
579 	virtual bool					is_Valid			(void)	const;
580 
581 	TSG_Intersection				is_Intersecting		(const CSG_Rect &Extent) const;
582 	TSG_Intersection				is_Intersecting		(const TSG_Rect &Extent) const;
583 	TSG_Intersection				is_Intersecting		(double xMin, double yMin, double xMax, double yMax) const;
584 
585 	bool							is_Compatible		(CSG_Grid *pGrid) const;
586 	bool							is_Compatible		(const CSG_Grid_System &System) const;
587 	bool							is_Compatible		(int NX, int NY, double Cellsize, double xMin, double yMin) const;
588 
589 	bool							is_InGrid			(int    x, int    y, bool bCheckNoData = true)	const	{	return( m_System.is_InGrid(x, y) && (!bCheckNoData || !is_NoData(x, y)) );	}
590 	bool							is_InGrid_byPos		(double x, double y, bool bCheckNoData = true)	const	{	return( Get_Extent(true).Contains(x, y) && (!bCheckNoData || !is_NoData(m_System.Get_xWorld_to_Grid(x), m_System.Get_yWorld_to_Grid(y))) );	}
591 	bool							is_InGrid_byPos		(const TSG_Point &p, bool bCheckNoData = true)	const	{	return( is_InGrid_byPos(p.x, p.y, bCheckNoData) );	}
592 
593 
594 	//-----------------------------------------------------
595 	// Memory...
596 
Get_Memory_Size(void)597 	sLong							Get_Memory_Size			(void)		const	{	return( Get_NCells() * Get_nValueBytes() );	}
Get_Memory_Size_MB(void)598 	double							Get_Memory_Size_MB		(void)		const	{	return( (double)Get_Memory_Size() / N_MEGABYTE_BYTES );	}
599 
600 	bool							Set_Cache				(bool bOn);
is_Cached(void)601 	bool							is_Cached				(void)		const	{	return( m_Cache_Stream != NULL );	}
602 
603 
604 	//-----------------------------------------------------
605 	// Operations...
606 
607 	void							Assign_NoData				(void);
608 
609 	virtual bool					Assign						(double Value = 0.0);
610 	virtual bool					Assign						(CSG_Data_Object *pObject);
611 	virtual bool					Assign						(CSG_Grid *pGrid, TSG_Grid_Resampling Interpolation);
612 
613 	void							Flip						(void);
614 	void							Mirror						(void);
615 	void							Invert						(void);
616 
617 	bool							Normalise					(void);
618 	bool							DeNormalise					(double Minimum, double Maximum);
619 
620 	bool							Standardise					(void);
621 	bool							DeStandardise				(double Mean, double StdDev);
622 
623 	int								Get_Gradient_NeighborDir	(int x, int y, bool bDown = true, bool bNoEdges = true)	const;
624 	bool							Get_Gradient				(int x, int y      , double &Slope, double &Aspect)	const;
625 	bool							Get_Gradient				(double x, double y, double &Slope, double &Aspect, TSG_Grid_Resampling Interpolation)	const;
626 	bool							Get_Gradient				(const TSG_Point &p, double &Slope, double &Aspect, TSG_Grid_Resampling Interpolation)	const;
627 
628 
629 	//-----------------------------------------------------
630 	// Set update flag when modified...
631 
632 	virtual void					Set_Modified	(bool bModified = true)
633 	{
634 		CSG_Data_Object::Set_Modified(bModified);
635 
636 		if( bModified )
637 		{
638 			Set_Update_Flag();
639 		}
640 	}
641 
642 
643 	//-----------------------------------------------------
644 	// Index...
645 
646 	bool							Set_Index		(bool bOn = true)
647 	{
648 		if( !bOn )
649 		{
650 			SG_FREE_SAFE(m_Index);
651 
652 			return( true );
653 		}
654 
655 		return( _Get_Index() );
656 	}
657 
658 	sLong							Get_Sorted		(sLong Position, bool bDown = true, bool bCheckNoData = true)
659 	{
660 		if( Position >= 0 && Position < Get_NCells() && _Get_Index() )
661 		{
662 			Position	= m_Index[bDown ? Get_NCells() - Position - 1 : Position];
663 
664 			if( !bCheckNoData || !is_NoData(Position) )
665 			{
666 				return( Position );
667 			}
668 		}
669 
670 		return( -1 );
671 	}
672 
673 	bool							Get_Sorted		(sLong Position, sLong &i, bool bDown = true, bool bCheckNoData = true)
674 	{
675 		return( (i = Get_Sorted(Position, bDown, bCheckNoData)) >= 0 );
676 	}
677 
678 	bool							Get_Sorted		(sLong Position, int &x, int &y, bool bDown = true, bool bCheckNoData = true)
679 	{
680 		if( (Position = Get_Sorted(Position, bDown, bCheckNoData)) >= 0 )
681 		{
682 			x	= (int)(Position % Get_NX());
683 			y	= (int)(Position / Get_NX());
684 
685 			return( true );
686 		}
687 
688 		return( false );
689 	}
690 
691 
692 	//-----------------------------------------------------
693 	// No Data Value...
694 
is_NoData(int x,int y)695 	virtual bool					is_NoData		(int x, int y)	const	{	return( is_NoData_Value(asDouble(x, y, false)) );	}
is_NoData(sLong i)696 	virtual bool					is_NoData		(sLong      i)	const	{	return( is_NoData_Value(asDouble(   i, false)) );	}
697 
Set_NoData(int x,int y)698 	virtual void					Set_NoData		(int x, int y)	{	Set_Value(x, y, Get_NoData_Value(), false);	}
Set_NoData(sLong i)699 	virtual void					Set_NoData		(sLong      i)	{	Set_Value(   i, Get_NoData_Value(), false);	}
700 
701 
702 	//-----------------------------------------------------
703 	// Operators...
704 
705 	virtual CSG_Grid &				operator  =		(const CSG_Grid &Grid);
706 	virtual CSG_Grid &				operator  =		(double Value);
707 
708 	virtual CSG_Grid				operator +		(const CSG_Grid &Grid)	const;
709 	virtual CSG_Grid				operator +		(double Value)			const;
710 	virtual CSG_Grid &				operator +=		(const CSG_Grid &Grid);
711 	virtual CSG_Grid &				operator +=		(double Value);
712 	virtual CSG_Grid &				Add				(const CSG_Grid &Grid);
713 	virtual CSG_Grid &				Add				(double Value);
714 
715 	virtual CSG_Grid				operator -		(const CSG_Grid &Grid)	const;
716 	virtual CSG_Grid				operator -		(double Value)			const;
717 	virtual CSG_Grid &				operator -=		(const CSG_Grid &Grid);
718 	virtual CSG_Grid &				operator -=		(double Value);
719 	virtual CSG_Grid &				Subtract		(const CSG_Grid &Grid);
720 	virtual CSG_Grid &				Subtract		(double Value);
721 
722 	virtual CSG_Grid				operator *		(const CSG_Grid &Grid)	const;
723 	virtual CSG_Grid				operator *		(double Value)			const;
724 	virtual CSG_Grid &				operator *=		(const CSG_Grid &Grid);
725 	virtual CSG_Grid &				operator *=		(double Value);
726 	virtual CSG_Grid &				Multiply		(const CSG_Grid &Grid);
727 	virtual CSG_Grid &				Multiply		(double Value);
728 
729 	virtual CSG_Grid				operator /		(const CSG_Grid &Grid)	const;
730 	virtual CSG_Grid				operator /		(double Value)			const;
731 	virtual CSG_Grid &				operator /=		(const CSG_Grid &Grid);
732 	virtual CSG_Grid &				operator /=		(double Value);
733 	virtual CSG_Grid &				Divide			(const CSG_Grid &Grid);
734 	virtual CSG_Grid &				Divide			(double Value);
735 
operator()736 	virtual double					operator ()		(int x, int y) const	{	return( asDouble(x, y) );	}
737 
738 
739 	//-----------------------------------------------------
740 	// Get Value...
741 
742 	double							Get_Value	(double x, double y,                TSG_Grid_Resampling Resampling = GRID_RESAMPLING_BSpline                      , bool bByteWise = false) const;
743 	double							Get_Value	(const TSG_Point &p,                TSG_Grid_Resampling Resampling = GRID_RESAMPLING_BSpline                      , bool bByteWise = false) const;
744 	bool							Get_Value	(double x, double y, double &Value, TSG_Grid_Resampling Resampling = GRID_RESAMPLING_BSpline, bool bNoData = false, bool bByteWise = false) const;
745 	bool							Get_Value	(const TSG_Point &p, double &Value, TSG_Grid_Resampling Resampling = GRID_RESAMPLING_BSpline, bool bNoData = false, bool bByteWise = false) const;
746 
747 	virtual BYTE					asByte		(int x, int y, bool bScaled = true) const	{	return( SG_ROUND_TO_BYTE (asDouble(x, y, bScaled)) );	}
748 	virtual BYTE					asByte		(     sLong i, bool bScaled = true) const	{	return( SG_ROUND_TO_BYTE (asDouble(   i, bScaled)) );	}
749 	virtual char					asChar		(int x, int y, bool bScaled = true) const	{	return( SG_ROUND_TO_CHAR (asDouble(x, y, bScaled)) );	}
750 	virtual char					asChar		(     sLong i, bool bScaled = true) const	{	return( SG_ROUND_TO_CHAR (asDouble(   i, bScaled)) );	}
751 	virtual short					asShort		(int x, int y, bool bScaled = true) const	{	return( SG_ROUND_TO_SHORT(asDouble(x, y, bScaled)) );	}
752 	virtual short					asShort		(     sLong i, bool bScaled = true) const	{	return( SG_ROUND_TO_SHORT(asDouble(   i, bScaled)) );	}
753 	virtual int						asInt		(int x, int y, bool bScaled = true) const	{	return( SG_ROUND_TO_INT  (asDouble(x, y, bScaled)) );	}
754 	virtual int						asInt		(     sLong i, bool bScaled = true) const	{	return( SG_ROUND_TO_INT  (asDouble(   i, bScaled)) );	}
755 	virtual sLong					asLong		(int x, int y, bool bScaled = true) const	{	return( SG_ROUND_TO_SLONG(asDouble(x, y, bScaled)) );	}
756 	virtual sLong					asLong		(     sLong i, bool bScaled = true) const	{	return( SG_ROUND_TO_SLONG(asDouble(   i, bScaled)) );	}
757 	virtual float					asFloat		(int x, int y, bool bScaled = true) const	{	return( (float)          (asDouble(x, y, bScaled)) );	}
758 	virtual float					asFloat		(     sLong i, bool bScaled = true) const	{	return( (float)          (asDouble(   i, bScaled)) );	}
759 
760 	//-----------------------------------------------------
761 	virtual double					asDouble(     sLong i, bool bScaled = true) const
762 	{
763 		return( asDouble((int)(i % Get_NX()), (int)(i / Get_NX()), bScaled) );
764 	}
765 
766 	virtual double					asDouble(int x, int y, bool bScaled = true) const
767 	{
768 		double	Value;
769 
770 		if( is_Cached() )
771 		{
772 			Value	= _Cache_Get_Value(x, y);
773 		}
774 		else switch( m_Type )
775 		{
776 			case SG_DATATYPE_Float : Value = (double)((float  **)m_Values)[y][x]; break;
777 			case SG_DATATYPE_Double: Value = (double)((double **)m_Values)[y][x]; break;
778 			case SG_DATATYPE_Byte  : Value = (double)((BYTE   **)m_Values)[y][x]; break;
779 			case SG_DATATYPE_Char  : Value = (double)((char   **)m_Values)[y][x]; break;
780 			case SG_DATATYPE_Word  : Value = (double)((WORD   **)m_Values)[y][x]; break;
781 			case SG_DATATYPE_Short : Value = (double)((short  **)m_Values)[y][x]; break;
782 			case SG_DATATYPE_DWord : Value = (double)((DWORD  **)m_Values)[y][x]; break;
783 			case SG_DATATYPE_Int   : Value = (double)((int    **)m_Values)[y][x]; break;
784 			case SG_DATATYPE_Long  : Value = (double)((sLong  **)m_Values)[y][x]; break;
785             case SG_DATATYPE_ULong : Value = (double)((uLong  **)m_Values)[y][x]; break;
786 			case SG_DATATYPE_Bit   : Value = (double)(((BYTE  **)m_Values)[y][x / 8] & m_Bitmask[x % 8]) == 0 ? 0.0 : 1.0;	break;
787 
788 			default:
789 				return( 0.0 );
790 		}
791 
792 		if( bScaled && is_Scaled() )
793 		{
794 			Value	= m_zOffset + m_zScale * Value;
795 		}
796 
797 		return( Value );
798 	}
799 
800 
801 	//-----------------------------------------------------
802 	// Set Value...
803 
Add_Value(int x,int y,double Value)804 	virtual void					Add_Value(int x, int y, double Value)	{	Set_Value(x, y, asDouble(x, y) + Value );	}
Add_Value(sLong i,double Value)805 	virtual void					Add_Value(sLong      i, double Value)	{	Set_Value(   i, asDouble(   i) + Value );	}
806 
Mul_Value(int x,int y,double Value)807 	virtual void					Mul_Value(int x, int y, double Value)	{	Set_Value(x, y, asDouble(x, y) * Value );	}
Mul_Value(sLong i,double Value)808 	virtual void					Mul_Value(sLong      i, double Value)	{	Set_Value(   i, asDouble(   i) * Value );	}
809 
810 	//-----------------------------------------------------
811 	virtual void					Set_Value(sLong      i, double Value, bool bScaled = true)
812 	{
813 		Set_Value((int)(i % Get_NX()), (int)(i / Get_NX()), Value, bScaled);
814 	}
815 
816 	virtual void					Set_Value(int x, int y, double Value, bool bScaled = true)
817 	{
818 		if( bScaled && is_Scaled() )
819 		{
820 			Value	= (Value - m_zOffset) / m_zScale;
821 		}
822 
823 		if( is_Cached() )
824 		{
825 			_Cache_Set_Value(x, y, Value);
826 		}
827 		else switch( m_Type )
828 		{
829 			case SG_DATATYPE_Float : ((float  **)m_Values)[y][x] = (float          )(Value); break;
830 			case SG_DATATYPE_Double: ((double **)m_Values)[y][x] = (double         )(Value); break;
831 			case SG_DATATYPE_Byte  : ((BYTE   **)m_Values)[y][x] = SG_ROUND_TO_BYTE (Value); break;
832 			case SG_DATATYPE_Char  : ((char   **)m_Values)[y][x] = SG_ROUND_TO_CHAR (Value); break;
833 			case SG_DATATYPE_Word  : ((WORD   **)m_Values)[y][x] = SG_ROUND_TO_WORD (Value); break;
834 			case SG_DATATYPE_Short : ((short  **)m_Values)[y][x] = SG_ROUND_TO_SHORT(Value); break;
835 			case SG_DATATYPE_DWord : ((DWORD  **)m_Values)[y][x] = SG_ROUND_TO_DWORD(Value); break;
836 			case SG_DATATYPE_Int   : ((int    **)m_Values)[y][x] = SG_ROUND_TO_INT  (Value); break;
837 			case SG_DATATYPE_Long  : ((sLong  **)m_Values)[y][x] = SG_ROUND_TO_SLONG(Value); break;
838 			case SG_DATATYPE_ULong : ((uLong  **)m_Values)[y][x] = SG_ROUND_TO_ULONG(Value); break;
839 			case SG_DATATYPE_Bit   : ((BYTE   **)m_Values)[y][x / 8] = Value != 0.0
840 					? ((BYTE  **)m_Values)[y][x / 8] |   m_Bitmask[x % 8]
841 					: ((BYTE  **)m_Values)[y][x / 8] & (~m_Bitmask[x % 8]);
842 				break;
843 
844 			default:
845 				return;
846 		}
847 
848 		Set_Modified();
849 	}
850 
851 	//-----------------------------------------------------
852 	CSG_Vector					Get_Row					(int y)	const;
853 	bool						Set_Row					(int y, const CSG_Vector &Values);
854 
855 
856 //---------------------------------------------------------
857 protected:	///////////////////////////////////////////////
858 
859 	virtual bool				On_Update				(void);
860 	virtual bool				On_Reload				(void);
861 	virtual bool				On_Delete				(void);
862 
863 
864 //---------------------------------------------------------
865 private:	///////////////////////////////////////////////
866 
867 	void						**m_Values;
868 
869 	bool						m_Cache_bTemp, m_Cache_bSwap, m_Cache_bFlip;
870 
871 	size_t						m_nBytes_Value, m_nBytes_Line;
872 
873 	sLong						*m_Index, m_Cache_Offset;
874 
875 	double						m_zOffset, m_zScale;
876 
877 	FILE						*m_Cache_Stream;
878 
879 	TSG_Data_Type				m_Type;
880 
881 	CSG_String					m_Unit, m_Cache_File;
882 
883 	CSG_Simple_Statistics		m_Statistics;
884 
885 	CSG_Histogram				m_Histogram;
886 
887 	CSG_Grid_System				m_System;
888 
889 
890 	//-----------------------------------------------------
891 	static	BYTE				m_Bitmask[8];
892 
893 
894 	//-----------------------------------------------------
895 	void						_On_Construction		(void);
896 
897 	void						_Set_Properties			(TSG_Data_Type Type, int NX, int NY, double Cellsize, double xMin, double yMin);
898 
899 	bool						_Set_Index				(void);
_Get_Index(void)900 	bool						_Get_Index				(void)
901 	{
902 		if( Get_Update_Flag() )
903 		{
904 			Update();
905 		}
906 
907 		return( m_Index || _Set_Index() );
908 	}
909 
910 
911 	//-----------------------------------------------------
912 	// Memory handling...
913 
914 	bool						_Memory_Create			(bool bCached);
915 	void						_Memory_Destroy			(void);
916 
917 	bool						_Array_Create			(void);
918 	void						_Array_Destroy			(void);
919 
920 	bool						_Cache_Check			(void);
921 	bool						_Cache_Create			(const CSG_String &File, TSG_Data_Type Data_Type, sLong Offset, bool bSwap, bool bFlip);
922 	bool						_Cache_Create			(void);
923 	bool						_Cache_Destroy			(bool bMemory_Restore);
924 	void						_Cache_Set_Value		(int x, int y, double Value);
925 	double						_Cache_Get_Value		(int x, int y)	const;
926 
927 
928 	//-----------------------------------------------------
929 	// File access...
930 
931 	void						_Swap_Bytes				(char *Bytes, int nBytes)	const;
932 
933 	bool						_Load_External			(const CSG_String &FileName, bool bCached, bool bLoadData);
934 	bool						_Load_PGSQL				(const CSG_String &FileName, bool bCached, bool bLoadData);
935 
936 	bool						_Load_Native			(const CSG_String &FileName, bool bCached, bool bLoadData);
937 	bool						_Save_Native			(const CSG_String &FileName, TSG_Grid_File_Format Format);
938 
939 	bool						_Load_Compressed		(const CSG_String &FileName, bool bCached, bool bLoadData);
940 	bool						_Save_Compressed		(const CSG_String &FileName);
941 
942 	bool						_Load_Binary			(CSG_File &Stream, TSG_Data_Type File_Type, bool bFlip, bool bSwapBytes);
943 	bool						_Save_Binary			(CSG_File &Stream, TSG_Data_Type File_Type, bool bFlip, bool bSwapBytes);
944 	bool						_Load_ASCII				(CSG_File &Stream, bool bCached, bool bFlip = false);
945 	bool						_Save_ASCII				(CSG_File &Stream, bool bFlip = false);
946 
947 	bool						_Load_Surfer			(const CSG_String &FileName, bool bCached, bool bLoadData);
948 
949 
950 	//-----------------------------------------------------
951 	CSG_Grid &					_Operation_Arithmetic	(const CSG_Grid &Grid, TSG_Grid_Operation Operation);
952 	CSG_Grid &					_Operation_Arithmetic	(double Value        , TSG_Grid_Operation Operation);
953 
954 	bool						_Assign_Interpolated	(CSG_Grid *pSource, TSG_Grid_Resampling Interpolation);
955 	bool						_Assign_MeanValue		(CSG_Grid *pSource, bool bAreaProportional);
956 	bool						_Assign_ExtremeValue	(CSG_Grid *pSource, bool bMaximum);
957 	bool						_Assign_Majority		(CSG_Grid *pSource);
958 
959 
960 	//-----------------------------------------------------
961 	// Interpolation subroutines...
962 
963 	bool						_Get_ValAtPos_NearestNeighbour	(double &Value, int x, int y, double dx, double dy                )	const;
964 	bool						_Get_ValAtPos_BiLinear			(double &Value, int x, int y, double dx, double dy, bool bByteWise)	const;
965 	bool						_Get_ValAtPos_BiCubicSpline		(double &Value, int x, int y, double dx, double dy, bool bByteWise)	const;
966 	bool						_Get_ValAtPos_BSpline			(double &Value, int x, int y, double dx, double dy, bool bByteWise)	const;
967 
968 	double						_Get_ValAtPos_BiCubicSpline		(double dx, double dy, double z_xy[4][4])	const;
969 	double						_Get_ValAtPos_BSpline			(double dx, double dy, double z_xy[4][4])	const;
970 
971 	bool						_Get_ValAtPos_Fill4x4Submatrix	(int x, int y, double z_xy[4][4]   )	const;
972 	bool						_Get_ValAtPos_Fill4x4Submatrix	(int x, int y, double z_xy[4][4][4])	const;
973 
974 };
975 
976 
977 ///////////////////////////////////////////////////////////
978 //														 //
979 //						Functions						 //
980 //														 //
981 ///////////////////////////////////////////////////////////
982 
983 //---------------------------------------------------------
984 #define SG_GRID_PTR_SAFE_SET_NODATA(g, x, y)	{ if( g && g->is_InGrid(x, y, false) ) { g->Set_NoData(x, y   ); } }
985 #define SG_GRID_PTR_SAFE_SET_VALUE(g, x, y, z)	{ if( g && g->is_InGrid(x, y, false) ) { g->Set_Value (x, y, z); } }
986 
987 //---------------------------------------------------------
988 /** Safe grid construction */
989 SAGA_API_DLL_EXPORT CSG_Grid *		SG_Create_Grid		(void);
990 
991 /** Safe grid construction */
992 SAGA_API_DLL_EXPORT CSG_Grid *		SG_Create_Grid		(const CSG_Grid &Grid);
993 
994 /** Safe grid construction */
995 SAGA_API_DLL_EXPORT CSG_Grid *		SG_Create_Grid		(const CSG_String &FileName,    TSG_Data_Type Type = SG_DATATYPE_Undefined, bool bCached = false, bool bLoadData = true);
996 
997 /** Safe grid construction */
998 SAGA_API_DLL_EXPORT CSG_Grid *		SG_Create_Grid		(CSG_Grid *pGrid,               TSG_Data_Type Type = SG_DATATYPE_Undefined, bool bCached = false);
999 
1000 /** Safe grid construction */
1001 SAGA_API_DLL_EXPORT CSG_Grid *		SG_Create_Grid		(const CSG_Grid_System &System, TSG_Data_Type Type = SG_DATATYPE_Undefined, bool bCached = false);
1002 
1003 /** Safe grid construction */
1004 SAGA_API_DLL_EXPORT CSG_Grid *		SG_Create_Grid		(TSG_Data_Type Type, int NX, int NY, double Cellsize = 0.0, double xMin = 0.0, double yMin = 0.0, bool bCached = false);
1005 
1006 //---------------------------------------------------------
1007 /** Get default directory for grid caching */
1008 SAGA_API_DLL_EXPORT const SG_Char *	SG_Grid_Cache_Get_Directory		(void);
1009 
1010 /** Set default directory for grid caching */
1011 SAGA_API_DLL_EXPORT void			SG_Grid_Cache_Set_Directory		(const SG_Char *Directory);
1012 
1013 SAGA_API_DLL_EXPORT void			SG_Grid_Cache_Set_Mode			(int Mode);
1014 SAGA_API_DLL_EXPORT int				SG_Grid_Cache_Get_Mode			(void);
1015 
1016 SAGA_API_DLL_EXPORT void			SG_Grid_Cache_Set_Threshold		(int nBytes);
1017 SAGA_API_DLL_EXPORT void			SG_Grid_Cache_Set_Threshold_MB	(double nMegabytes);
1018 SAGA_API_DLL_EXPORT sLong			SG_Grid_Cache_Get_Threshold		(void);
1019 SAGA_API_DLL_EXPORT double			SG_Grid_Cache_Get_Threshold_MB	(void);
1020 
1021 //---------------------------------------------------------
1022 SAGA_API_DLL_EXPORT bool					SG_Grid_Set_File_Format_Default		(int Format);
1023 SAGA_API_DLL_EXPORT TSG_Grid_File_Format	SG_Grid_Get_File_Format_Default		(void);
1024 SAGA_API_DLL_EXPORT CSG_String				SG_Grid_Get_File_Extension_Default	(void);
1025 
1026 
1027 ///////////////////////////////////////////////////////////
1028 //														 //
1029 //                                                       //
1030 //														 //
1031 ///////////////////////////////////////////////////////////
1032 
1033 //---------------------------------------------------------
1034 class CSG_Grid_Stack : public CSG_Stack
1035 {
1036 public:
CSG_Grid_Stack(void)1037 	CSG_Grid_Stack(void) : CSG_Stack(sizeof(TSG_Point_Int))	{}
1038 
1039 	//-----------------------------------------------------
Push(int x,int y)1040 	virtual bool			Push			(int  x, int  y)
1041 	{
1042 		TSG_Point_Int	*pPoint	= (TSG_Point_Int *)Get_Record_Push();
1043 
1044 		if( pPoint )
1045 		{
1046 			pPoint->x	= x;
1047 			pPoint->y	= y;
1048 
1049 			return( true );
1050 		}
1051 
1052 		return( false );
1053 	}
1054 
1055 	//-----------------------------------------------------
Pop(int & x,int & y)1056 	virtual bool			Pop				(int &x, int &y)
1057 	{
1058 		TSG_Point_Int	*pPoint	= (TSG_Point_Int *)Get_Record_Pop();
1059 
1060 		if( pPoint )
1061 		{
1062 			x	= pPoint->x;
1063 			y	= pPoint->y;
1064 
1065 			return( true );
1066 		}
1067 
1068 		return( false );
1069 	}
1070 
1071 };
1072 
1073 
1074 ///////////////////////////////////////////////////////////
1075 //														 //
1076 //														 //
1077 //														 //
1078 ///////////////////////////////////////////////////////////
1079 
1080 //---------------------------------------------------------
1081 #define SG_GRIDCELLADDR_PARM_SQUARE		0x01
1082 #define SG_GRIDCELLADDR_PARM_CIRCLE		0x02
1083 #define SG_GRIDCELLADDR_PARM_ANNULUS	0x04
1084 #define SG_GRIDCELLADDR_PARM_SECTOR		0x08
1085 #define SG_GRIDCELLADDR_PARM_SIZEDBL	0x10
1086 #define SG_GRIDCELLADDR_PARM_MAPUNIT	0x20
1087 #define SG_GRIDCELLADDR_PARM_WEIGHTING	0x40
1088 #define SG_GRIDCELLADDR_PARM_DEFAULT	(SG_GRIDCELLADDR_PARM_SQUARE|SG_GRIDCELLADDR_PARM_CIRCLE)
1089 
1090 //---------------------------------------------------------
1091 class SAGA_API_DLL_EXPORT CSG_Grid_Cell_Addressor
1092 {
1093 public:
1094 	CSG_Grid_Cell_Addressor(void);
1095 
1096 	bool						Destroy				(void);
1097 
1098 	static bool					Enable_Parameters	(class CSG_Parameters &Parameters);
1099 	static bool					Add_Parameters		(class CSG_Parameters &Parameters, const CSG_String &Parent = "", int Style = SG_GRIDCELLADDR_PARM_DEFAULT);
1100 	bool						Set_Parameters		(class CSG_Parameters &Parameters, int Type = 0);
1101 
1102 	bool						Set_Square			(class CSG_Parameters &Parameters);
1103 	bool						Set_Circle			(class CSG_Parameters &Parameters);
1104 	bool						Set_Annulus			(class CSG_Parameters &Parameters);
1105 	bool						Set_Sector			(class CSG_Parameters &Parameters);
1106 
Get_Weighting(void)1107 	CSG_Distance_Weighting &	Get_Weighting		(void)			{	return( m_Weighting );		}
1108 
is_Square(void)1109 	bool						is_Square			(void)	const	{	return( m_Type == 0 );	}
is_Circle(void)1110 	bool						is_Circle			(void)	const	{	return( m_Type == 1 );	}
is_Annulus(void)1111 	bool						is_Annulus			(void)	const	{	return( m_Type == 2 );	}
is_Sector(void)1112 	bool						is_Sector			(void)	const	{	return( m_Type == 3 );	}
1113 
1114 	bool						Set_Radius			(double Radius, bool bSquare = false);
1115 	bool						Set_Square			(double Radius);
1116 	bool						Set_Circle			(double Radius);
1117 	bool						Set_Annulus			(double Radius_Inner, double Radius_Outer);
1118 	bool						Set_Sector			(double Radius, double Direction, double Tolerance);
1119 
1120 	double						Get_Radius			(bool bOuter = true)		const	{	return( bOuter ? m_Radius : m_Radius_0 );	}
Get_Radius_Inner(void)1121 	double						Get_Radius_Inner	(void)						const	{	return( m_Radius_0  );	}
Get_Radius_Outer(void)1122 	double						Get_Radius_Outer	(void)						const	{	return( m_Radius    );	}
Get_Direction(void)1123 	double						Get_Direction		(void)						const	{	return( m_Direction );	}
Get_Tolerance(void)1124 	double						Get_Tolerance		(void)						const	{	return( m_Tolerance );	}
1125 
Get_Count(void)1126 	int							Get_Count			(void)						const	{	return( m_Kernel.Get_Count() );	}
1127 	int							Get_X				(int Index, int Offset = 0)	const	{	return( Index >= 0 && Index < m_Kernel.Get_Count() ? m_Kernel[Index].asInt   (0) + Offset : Offset );	}
1128 	int							Get_Y				(int Index, int Offset = 0)	const	{	return( Index >= 0 && Index < m_Kernel.Get_Count() ? m_Kernel[Index].asInt   (1) + Offset : Offset );	}
Get_Distance(int Index)1129 	double						Get_Distance		(int Index                )	const	{	return( Index >= 0 && Index < m_Kernel.Get_Count() ? m_Kernel[Index].asDouble(2)          : -1.    );	}
Get_Weight(int Index)1130 	double						Get_Weight			(int Index                )	const	{	return( Index >= 0 && Index < m_Kernel.Get_Count() ? m_Kernel[Index].asDouble(3)          :  0.    );	}
1131 	bool						Get_Values			(int Index, int &x, int &y, double &Distance, double &Weight, bool bOffset = false)	const
1132 	{
1133 		if( Index >= 0 && Index < m_Kernel.Get_Count() )
1134 		{
1135 			CSG_Table_Record	&Cell	= m_Kernel[Index];
1136 
1137 			x			= bOffset ? x + Cell.asInt(0) : Cell.asInt(0);
1138 			y			= bOffset ? y + Cell.asInt(1) : Cell.asInt(1);
1139 			Distance	= Cell.asDouble(2);
1140 			Weight		= Cell.asDouble(3);
1141 
1142 			return( true );
1143 		}
1144 
1145 		return( false );
1146 	}
1147 
1148 
1149 private:
1150 
1151 	int							m_Type;
1152 
1153 	double						m_Radius, m_Radius_0, m_Direction, m_Tolerance;
1154 
1155 	CSG_Distance_Weighting		m_Weighting;
1156 
1157 	CSG_Table					m_Kernel;
1158 
1159 
1160 	bool						_Set_Kernel			(int Type, double Radius, double Radius_Inner, double Direction, double Tolerance);
1161 
1162 };
1163 
1164 
1165 ///////////////////////////////////////////////////////////
1166 //														 //
1167 //														 //
1168 //														 //
1169 ///////////////////////////////////////////////////////////
1170 
1171 //---------------------------------------------------------
1172 #endif // #ifndef HEADER_INCLUDED__SAGA_API__grid_H
1173