|
SAGA API v2.0.8
|
00001 /********************************************************** 00002 * Version $Id: grid.h 1105 2011-06-21 14:11:47Z oconrad $ 00003 *********************************************************/ 00004 00006 // // 00007 // SAGA // 00008 // // 00009 // System for Automated Geoscientific Analyses // 00010 // // 00011 // Application Programming Interface // 00012 // // 00013 // Library: SAGA_API // 00014 // // 00015 //-------------------------------------------------------// 00016 // // 00017 // grid.h // 00018 // // 00019 // Copyright (C) 2005 by Olaf Conrad // 00020 // // 00021 //-------------------------------------------------------// 00022 // // 00023 // This file is part of 'SAGA - System for Automated // 00024 // Geoscientific Analyses'. // 00025 // // 00026 // This library is free software; you can redistribute // 00027 // it and/or modify it under the terms of the GNU Lesser // 00028 // General Public License as published by the Free // 00029 // Software Foundation, version 2.1 of the License. // 00030 // // 00031 // This library is distributed in the hope that it will // 00032 // be useful, but WITHOUT ANY WARRANTY; without even the // 00033 // implied warranty of MERCHANTABILITY or FITNESS FOR A // 00034 // PARTICULAR PURPOSE. See the GNU Lesser General Public // 00035 // License for more details. // 00036 // // 00037 // You should have received a copy of the GNU Lesser // 00038 // General Public License along with this program; if // 00039 // not, write to the Free Software Foundation, Inc., // 00040 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, // 00041 // USA. // 00042 // // 00043 //-------------------------------------------------------// 00044 // // 00045 // contact: Olaf Conrad // 00046 // Institute of Geography // 00047 // University of Goettingen // 00048 // Goldschmidtstr. 5 // 00049 // 37077 Goettingen // 00050 // Germany // 00051 // // 00052 // e-mail: oconrad@saga-gis.org // 00053 // // 00055 00056 //--------------------------------------------------------- 00057 00058 00060 // // 00061 // // 00062 // // 00064 00065 //--------------------------------------------------------- 00066 #ifndef HEADER_INCLUDED__SAGA_API__grid_H 00067 #define HEADER_INCLUDED__SAGA_API__grid_H 00068 00069 00071 // // 00072 // // 00073 // // 00075 00076 //--------------------------------------------------------- 00077 #include "dataobject.h" 00078 #include "table.h" 00079 #include "grid_pyramid.h" 00080 00081 00083 // // 00084 // Memory Handling // 00085 // // 00087 00088 //--------------------------------------------------------- 00089 typedef enum ESG_Grid_Memory_Type 00090 { 00091 GRID_MEMORY_Normal = 0, 00092 GRID_MEMORY_Cache, 00093 GRID_MEMORY_Compression 00094 } 00095 TSG_Grid_Memory_Type; 00096 00097 00099 // // 00100 // Grid File // 00101 // // 00103 00104 //--------------------------------------------------------- 00105 typedef enum ESG_Grid_File_Format 00106 { 00107 GRID_FILE_FORMAT_Undefined = 0, 00108 GRID_FILE_FORMAT_Binary, 00109 GRID_FILE_FORMAT_ASCII 00110 } 00111 TSG_Grid_File_Format; 00112 00113 //--------------------------------------------------------- 00114 typedef enum ESG_Grid_File_Key 00115 { 00116 GRID_FILE_KEY_NAME = 0, 00117 GRID_FILE_KEY_DESCRIPTION, 00118 GRID_FILE_KEY_UNITNAME, 00119 GRID_FILE_KEY_DATAFILE_NAME, 00120 GRID_FILE_KEY_DATAFILE_OFFSET, 00121 GRID_FILE_KEY_DATAFORMAT, 00122 GRID_FILE_KEY_BYTEORDER_BIG, 00123 GRID_FILE_KEY_POSITION_XMIN, 00124 GRID_FILE_KEY_POSITION_YMIN, 00125 GRID_FILE_KEY_CELLCOUNT_X, 00126 GRID_FILE_KEY_CELLCOUNT_Y, 00127 GRID_FILE_KEY_CELLSIZE, 00128 GRID_FILE_KEY_Z_FACTOR, 00129 GRID_FILE_KEY_NODATA_VALUE, 00130 GRID_FILE_KEY_TOPTOBOTTOM, 00131 GRID_FILE_KEY_Count 00132 } 00133 TSG_Grid_File_Key; 00134 00135 //--------------------------------------------------------- 00136 const SG_Char gSG_Grid_File_Key_Names[GRID_FILE_KEY_Count][32] = 00137 { 00138 SG_T("NAME"), 00139 SG_T("DESCRIPTION"), 00140 SG_T("UNIT"), 00141 SG_T("DATAFILE_NAME"), 00142 SG_T("DATAFILE_OFFSET"), 00143 SG_T("DATAFORMAT"), 00144 SG_T("BYTEORDER_BIG"), 00145 SG_T("POSITION_XMIN"), 00146 SG_T("POSITION_YMIN"), 00147 SG_T("CELLCOUNT_X"), 00148 SG_T("CELLCOUNT_Y"), 00149 SG_T("CELLSIZE"), 00150 SG_T("Z_FACTOR"), 00151 SG_T("NODATA_VALUE"), 00152 SG_T("TOPTOBOTTOM") 00153 }; 00154 00155 //--------------------------------------------------------- 00156 #define GRID_FILE_KEY_TRUE SG_T("TRUE") 00157 #define GRID_FILE_KEY_FALSE SG_T("FALSE") 00158 00159 00161 // // 00162 // // 00163 // // 00165 00166 //--------------------------------------------------------- 00167 typedef enum ESG_Grid_Interpolation 00168 { 00169 GRID_INTERPOLATION_NearestNeighbour = 0, 00170 GRID_INTERPOLATION_Bilinear, 00171 GRID_INTERPOLATION_InverseDistance, 00172 GRID_INTERPOLATION_BicubicSpline, 00173 GRID_INTERPOLATION_BSpline, 00174 00175 GRID_INTERPOLATION_Mean_Nodes, 00176 GRID_INTERPOLATION_Mean_Cells, 00177 GRID_INTERPOLATION_Minimum, 00178 GRID_INTERPOLATION_Maximum, 00179 GRID_INTERPOLATION_Majority, 00180 00181 GRID_INTERPOLATION_Undefined 00182 } 00183 TSG_Grid_Interpolation; 00184 00185 //--------------------------------------------------------- 00186 typedef enum ESG_Grid_Operation 00187 { 00188 GRID_OPERATION_Addition = 0, 00189 GRID_OPERATION_Subtraction, 00190 GRID_OPERATION_Multiplication, 00191 GRID_OPERATION_Division 00192 } 00193 TSG_Grid_Operation; 00194 00195 00197 // // 00198 // CSG_Grid_System // 00199 // // 00201 00202 //--------------------------------------------------------- 00211 //--------------------------------------------------------- 00212 class SAGA_API_DLL_EXPORT CSG_Grid_System 00213 { 00214 public: 00215 CSG_Grid_System(void); 00216 CSG_Grid_System(const CSG_Grid_System &System); 00217 CSG_Grid_System(double Cellsize, const CSG_Rect &Extent); 00218 CSG_Grid_System(double Cellsize, double xMin, double yMin, double xMax, double yMax); 00219 CSG_Grid_System(double Cellsize, double xMin, double yMin, int NX, int NY); 00220 00221 ~CSG_Grid_System(void); 00222 00223 00224 //----------------------------------------------------- 00225 bool is_Valid (void) const; 00226 00227 const SG_Char * Get_Name (bool bShort = true); 00228 00229 double Get_Cellsize (void) const { return( m_Cellsize ); } 00230 double Get_Cellarea (void) const { return( m_Cellarea ); } 00231 00232 int Get_NX (void) const { return( m_NX ); } 00233 int Get_NY (void) const { return( m_NY ); } 00234 long Get_NCells (void) const { return( m_NCells ); } 00235 00236 const CSG_Rect & Get_Extent (bool bCells = false) const { return( bCells ? m_Extent_Cells : m_Extent ); } 00237 00238 double Get_XMin (bool bCells = false) const { return( bCells ? m_Extent_Cells.Get_XMin () : m_Extent.Get_XMin () ); } 00239 double Get_XMax (bool bCells = false) const { return( bCells ? m_Extent_Cells.Get_XMax () : m_Extent.Get_XMax () ); } 00240 double Get_XRange (bool bCells = false) const { return( bCells ? m_Extent_Cells.Get_XRange() : m_Extent.Get_XRange() ); } 00241 00242 double Get_YMin (bool bCells = false) const { return( bCells ? m_Extent_Cells.Get_YMin () : m_Extent.Get_YMin () ); } 00243 double Get_YMax (bool bCells = false) const { return( bCells ? m_Extent_Cells.Get_YMax () : m_Extent.Get_YMax () ); } 00244 double Get_YRange (bool bCells = false) const { return( bCells ? m_Extent_Cells.Get_YRange() : m_Extent.Get_YRange() ); } 00245 00246 00247 //----------------------------------------------------- 00248 bool operator == (const CSG_Grid_System &System) const; 00249 void operator = (const CSG_Grid_System &System); 00250 00251 bool Assign (const CSG_Grid_System &System); 00252 bool Assign (double Cellsize, const CSG_Rect &Extent); 00253 bool Assign (double Cellsize, double xMin, double yMin, double xMax, double yMax); 00254 bool Assign (double Cellsize, double xMin, double yMin, int NX, int NY); 00255 00256 bool is_Equal (const CSG_Grid_System &System) const; 00257 bool is_Equal (double Cellsize, const TSG_Rect &Extent) const; 00258 00259 00260 //----------------------------------------------------- 00262 double Fit_xto_Grid_System (double x) const { return( Get_XMin() + m_Cellsize * (int)(0.5 + (x - Get_XMin()) / m_Cellsize) ); } 00263 00265 double Fit_yto_Grid_System (double y) const { return( Get_YMin() + m_Cellsize * (int)(0.5 + (y - Get_YMin()) / m_Cellsize) ); } 00266 00268 TSG_Point Fit_to_Grid_System (TSG_Point ptWorld) const 00269 { 00270 ptWorld.x = Fit_xto_Grid_System(ptWorld.x); 00271 ptWorld.y = Fit_yto_Grid_System(ptWorld.y); 00272 00273 return( ptWorld ); 00274 } 00275 00276 00277 //----------------------------------------------------- 00278 double Get_xGrid_to_World (int xGrid) const { return( Get_XMin() + xGrid * m_Cellsize ); } 00279 double Get_yGrid_to_World (int yGrid) const { return( Get_YMin() + yGrid * m_Cellsize ); } 00280 00281 TSG_Point Get_Grid_to_World (int xGrid, int yGrid) const 00282 { 00283 TSG_Point pt; 00284 00285 pt.x = Get_xGrid_to_World(xGrid); 00286 pt.y = Get_yGrid_to_World(yGrid); 00287 00288 return( pt ); 00289 } 00290 00291 00292 //----------------------------------------------------- 00293 int Get_xWorld_to_Grid (double xWorld) const { return( (int)(0.5 + (xWorld - Get_XMin()) / m_Cellsize) ); } 00294 int Get_yWorld_to_Grid (double yWorld) const { return( (int)(0.5 + (yWorld - Get_YMin()) / m_Cellsize) ); } 00295 00296 bool Get_World_to_Grid (int &xGrid, int &yGrid, double xWorld, double yWorld) const 00297 { 00298 return( is_InGrid(xGrid = Get_xWorld_to_Grid(xWorld), yGrid = Get_yWorld_to_Grid(yWorld)) ); 00299 } 00300 00301 bool Get_World_to_Grid (int &xGrid, int &yGrid, TSG_Point ptWorld) const 00302 { 00303 return( is_InGrid(xGrid = Get_xWorld_to_Grid(ptWorld.x), yGrid = Get_yWorld_to_Grid(ptWorld.y)) ); 00304 } 00305 00306 00307 //----------------------------------------------------- 00308 bool Get_Neighbor_Pos (int Direction, int x, int y, int &xPos, int &yPos) const 00309 { 00310 return( is_InGrid(xPos = Get_xTo(Direction, x), yPos = Get_yTo(Direction, y)) ); 00311 } 00312 00313 static int Get_xTo (int Direction, int x = 0) 00314 { 00315 static int ix[8] = { 0, 1, 1, 1, 0,-1,-1,-1 }; 00316 00317 Direction %= 8; 00318 00319 if( Direction < 0 ) 00320 { 00321 Direction += 8; 00322 } 00323 00324 return( x + ix[Direction] ); 00325 } 00326 00327 static int Get_yTo (int Direction, int y = 0) 00328 { 00329 static int iy[8] = { 1, 1, 0,-1,-1,-1, 0, 1 }; 00330 00331 Direction %= 8; 00332 00333 if( Direction < 0 ) 00334 { 00335 Direction += 8; 00336 } 00337 00338 return( y + iy[Direction] ); 00339 } 00340 00341 static int Get_xFrom (int Direction, int x = 0) { return( Get_xTo(Direction + 4, x) ); } 00342 static int Get_yFrom (int Direction, int y = 0) { return( Get_yTo(Direction + 4, y) ); } 00343 00344 int Get_xToSave (int Direction, int x) const { return( (x = Get_xTo (Direction, x)) < 0 ? 0 : (x >= m_NX ? m_NX - 1 : x) ); } 00345 int Get_yToSave (int Direction, int y) const { return( (y = Get_yTo (Direction, y)) < 0 ? 0 : (y >= m_NY ? m_NY - 1 : y) ); } 00346 int Get_xFromSave (int Direction, int x) const { return( (x = Get_xFrom(Direction, x)) < 0 ? 0 : (x >= m_NX ? m_NX - 1 : x) ); } 00347 int Get_yFromSave (int Direction, int y) const { return( (y = Get_yFrom(Direction, y)) < 0 ? 0 : (y >= m_NY ? m_NY - 1 : y) ); } 00348 00349 bool is_InGrid (int x, int y) const { return( x >= 0 && x < m_NX && y >= 0 && y < m_NY ); } 00350 bool is_InGrid (int x, int y, int Rand) const { return( x >= Rand && x < m_NX - Rand && y >= Rand && y < m_NY - Rand ); } 00351 00352 double Get_Length (int Direction) const { return( Direction % 2 ? m_Diagonal : m_Cellsize ); } 00353 static double Get_UnitLength (int Direction) { return( Direction % 2 ? sqrt(2.0) : 1.0 ); } 00354 00355 00356 private: 00357 00358 int m_NX, m_NY; 00359 long m_NCells; 00360 00361 double m_Cellsize, m_Cellarea, m_Diagonal; 00362 00363 CSG_Rect m_Extent, m_Extent_Cells; 00364 00365 CSG_String m_Name; 00366 00367 }; 00368 00369 00371 // // 00372 // CSG_Grid // 00373 // // 00375 00376 //--------------------------------------------------------- 00380 //--------------------------------------------------------- 00381 class SAGA_API_DLL_EXPORT CSG_Grid : public CSG_Data_Object 00382 { 00383 //--------------------------------------------------------- 00384 public: 00385 00386 //----------------------------------------------------- 00387 CSG_Grid(void); 00388 00389 CSG_Grid (const CSG_Grid &Grid); 00390 bool Create (const CSG_Grid &Grid); 00391 00392 CSG_Grid (const CSG_String &File_Name, TSG_Data_Type Type = SG_DATATYPE_Undefined, TSG_Grid_Memory_Type Memory_Type = GRID_MEMORY_Normal); 00393 bool Create (const CSG_String &File_Name, TSG_Data_Type Type = SG_DATATYPE_Undefined, TSG_Grid_Memory_Type Memory_Type = GRID_MEMORY_Normal); 00394 00395 CSG_Grid (CSG_Grid *pGrid, TSG_Data_Type Type = SG_DATATYPE_Undefined, TSG_Grid_Memory_Type Memory_Type = GRID_MEMORY_Normal); 00396 bool Create (CSG_Grid *pGrid, TSG_Data_Type Type = SG_DATATYPE_Undefined, TSG_Grid_Memory_Type Memory_Type = GRID_MEMORY_Normal); 00397 00398 CSG_Grid (const CSG_Grid_System &System, TSG_Data_Type Type = SG_DATATYPE_Undefined, TSG_Grid_Memory_Type Memory_Type = GRID_MEMORY_Normal); 00399 bool Create (const CSG_Grid_System &System, TSG_Data_Type Type = SG_DATATYPE_Undefined, TSG_Grid_Memory_Type Memory_Type = GRID_MEMORY_Normal); 00400 00401 CSG_Grid (TSG_Data_Type Type, int NX, int NY, double Cellsize = 0.0, double xMin = 0.0, double yMin = 0.0, TSG_Grid_Memory_Type Memory_Type = GRID_MEMORY_Normal); 00402 bool Create (TSG_Data_Type Type, int NX, int NY, double Cellsize = 0.0, double xMin = 0.0, double yMin = 0.0, TSG_Grid_Memory_Type Memory_Type = GRID_MEMORY_Normal); 00403 00404 00405 //----------------------------------------------------- 00406 virtual ~CSG_Grid(void); 00407 00408 virtual bool Destroy (void); 00409 00410 00411 //----------------------------------------------------- 00414 virtual TSG_Data_Object_Type Get_ObjectType (void) const { return( DATAOBJECT_TYPE_Grid ); } 00415 00416 00417 //----------------------------------------------------- 00418 // Data-Info... 00419 00420 TSG_Data_Type Get_Type (void) const { return( m_Type ); } 00421 00422 int Get_nValueBytes (void) const { return( (int)SG_Data_Type_Get_Size(m_Type) ); } 00423 int Get_nLineBytes (void) const { return( m_Type != SG_DATATYPE_Bit ? (int)SG_Data_Type_Get_Size(m_Type) * Get_NX() : 1 + Get_NX() / 8 ); } 00424 00425 void Set_Unit (const SG_Char *String); 00426 const SG_Char * Get_Unit (void) const; 00427 00428 const CSG_Grid_System & Get_System (void) const { return( m_System ); } 00429 00430 int Get_NX (void) const { return( m_System.Get_NX() ); } 00431 int Get_NY (void) const { return( m_System.Get_NY() ); } 00432 long Get_NCells (void) const { return( m_System.Get_NCells() ); } 00433 00434 double Get_Cellsize (void) const { return( m_System.Get_Cellsize() ); } 00435 double Get_Cellarea (void) const { return( m_System.Get_Cellarea() ); } 00436 00437 const CSG_Rect & Get_Extent (bool bCells = false) const { return( m_System.Get_Extent(bCells) ); } 00438 00439 double Get_XMin (bool bCells = false) const { return( m_System.Get_XMin (bCells) ); } 00440 double Get_XMax (bool bCells = false) const { return( m_System.Get_XMax (bCells) ); } 00441 double Get_XRange (bool bCells = false) const { return( m_System.Get_XRange(bCells) ); } 00442 00443 double Get_YMin (bool bCells = false) const { return( m_System.Get_YMin (bCells) ); } 00444 double Get_YMax (bool bCells = false) const { return( m_System.Get_YMax (bCells) ); } 00445 double Get_YRange (bool bCells = false) const { return( m_System.Get_YRange(bCells) ); } 00446 00447 double Get_ZMin (bool bZFactor = false); 00448 double Get_ZMax (bool bZFactor = false); 00449 double Get_ZRange (bool bZFactor = false); 00450 00451 void Set_ZFactor (double Value); 00452 double Get_ZFactor (void) const; 00453 00454 double Get_ArithMean (bool bZFactor = false); 00455 double Get_StdDev (bool bZFactor = false); 00456 double Get_Variance (void); 00457 00458 long Get_NoData_Count (void); 00459 00460 virtual bool Save (const CSG_String &File_Name, int Format = GRID_FILE_FORMAT_Binary); 00461 virtual bool Save (const CSG_String &File_Name, int Format, int xA, int yA, int xN, int yN); 00462 00463 00464 //----------------------------------------------------- 00465 // Checks... 00466 00467 virtual bool is_Valid (void) const; 00468 00469 TSG_Intersection is_Intersecting (const CSG_Rect &Extent) const; 00470 TSG_Intersection is_Intersecting (const TSG_Rect &Extent) const; 00471 TSG_Intersection is_Intersecting (double xMin, double yMin, double xMax, double yMax) const; 00472 00473 bool is_Compatible (CSG_Grid *pGrid) const; 00474 bool is_Compatible (const CSG_Grid_System &System) const; 00475 bool is_Compatible (int NX, int NY, double Cellsize, double xMin, double yMin) const; 00476 00477 bool is_InGrid(int x, int y, bool bCheckNoData = true) const { return( m_System.is_InGrid(x, y) && (!bCheckNoData || (bCheckNoData && !is_NoData(x, y))) ); } 00478 bool is_InGrid_byPos (double xPos, double yPos) const { return( xPos >= Get_XMin() && xPos <= Get_XMax() && yPos >= Get_YMin() && yPos <= Get_YMax() ); } 00479 bool is_InGrid_byPos (TSG_Point Position) const { return( is_InGrid_byPos(Position.x, Position.y) ); } 00480 00481 00482 //----------------------------------------------------- 00483 // Memory... 00484 00485 int Get_Buffer_Size (void) { return( LineBuffer_Count * Get_nLineBytes() ); } 00486 bool Set_Buffer_Size (int Size); 00487 00488 bool Set_Cache (bool bOn); 00489 bool is_Cached (void); 00490 00491 bool Set_Compression (bool bOn); 00492 bool is_Compressed (void); 00493 double Get_Compression_Ratio (void); 00494 00495 00496 //----------------------------------------------------- 00497 // Operations... 00498 00499 void Assign_NoData (void); 00500 00501 virtual bool Assign (double Value = 0.0); 00502 virtual bool Assign (CSG_Data_Object *pObject); 00503 virtual bool Assign (CSG_Grid *pGrid, TSG_Grid_Interpolation Interpolation); 00504 00505 void Flip (void); 00506 void Mirror (void); 00507 void Invert (void); 00508 00509 bool Normalise (void); 00510 bool DeNormalise (double Minimum, double Maximum); 00511 00512 bool Standardise (void); 00513 bool DeStandardise (double Mean, double StdDev); 00514 00515 int Get_Gradient_NeighborDir (int x, int y, bool bMustBeLower = true) const; 00516 bool Get_Gradient (int x, int y, double &Decline, double &Azimuth) const; 00517 00518 00519 //----------------------------------------------------- 00520 // Set update flag when modified... 00521 00522 virtual void Set_Modified (bool bFlag = true) 00523 { 00524 CSG_Data_Object::Set_Modified(bFlag); 00525 00526 if( bFlag ) 00527 { 00528 Set_Update_Flag(); 00529 00530 m_bIndexed = false; 00531 } 00532 } 00533 00534 00535 //----------------------------------------------------- 00536 // Index... 00537 00538 bool Set_Index (bool bOn = false); 00539 00540 long Get_Sorted (long Position, bool bDown = true, bool bCheckNoData = true) 00541 { 00542 if( Position >= 0 && Position < Get_NCells() && (m_bIndexed || Set_Index(true)) ) 00543 { 00544 Position = m_Index[bDown ? Get_NCells() - Position - 1 : Position]; 00545 00546 if( !bCheckNoData || !is_NoData(Position) ) 00547 { 00548 return( Position ); 00549 } 00550 } 00551 00552 return( -1 ); 00553 } 00554 00555 bool Get_Sorted (long Position, long &n, bool bDown = true, bool bCheckNoData = true) 00556 { 00557 return( (n = Get_Sorted(Position, bDown, bCheckNoData)) >= 0 ); 00558 } 00559 00560 bool Get_Sorted (long Position, int &x, int &y, bool bDown = true, bool bCheckNoData = true) 00561 { 00562 if( (Position = Get_Sorted(Position, bDown, bCheckNoData)) >= 0 ) 00563 { 00564 x = (int)(Position % Get_NX()); 00565 y = (int)(Position / Get_NX()); 00566 00567 return( true ); 00568 } 00569 00570 return( false ); 00571 } 00572 00573 double Get_Percentile (double Percent, bool bZFactor = false); 00574 00575 00576 //----------------------------------------------------- 00577 // No Data Value... 00578 00579 virtual bool is_NoData (int x, int y) const { return( is_NoData_Value(asDouble(x, y)) ); } 00580 virtual bool is_NoData (long n) const { return( is_NoData_Value(asDouble( n)) ); } 00581 00582 virtual void Set_NoData (int x, int y) { Set_Value(x, y, Get_NoData_Value() ); } 00583 virtual void Set_NoData (long n) { Set_Value( n, Get_NoData_Value() ); } 00584 00585 00586 //----------------------------------------------------- 00587 // Operators... 00588 00589 virtual CSG_Grid & operator = (const CSG_Grid &Grid); 00590 virtual CSG_Grid & operator = (double Value); 00591 00592 virtual CSG_Grid operator + (const CSG_Grid &Grid) const; 00593 virtual CSG_Grid operator + (double Value) const; 00594 virtual CSG_Grid & operator += (const CSG_Grid &Grid); 00595 virtual CSG_Grid & operator += (double Value); 00596 virtual CSG_Grid & Add (const CSG_Grid &Grid); 00597 virtual CSG_Grid & Add (double Value); 00598 00599 virtual CSG_Grid operator - (const CSG_Grid &Grid) const; 00600 virtual CSG_Grid operator - (double Value) const; 00601 virtual CSG_Grid & operator -= (const CSG_Grid &Grid); 00602 virtual CSG_Grid & operator -= (double Value); 00603 virtual CSG_Grid & Subtract (const CSG_Grid &Grid); 00604 virtual CSG_Grid & Subtract (double Value); 00605 00606 virtual CSG_Grid operator * (const CSG_Grid &Grid) const; 00607 virtual CSG_Grid operator * (double Value) const; 00608 virtual CSG_Grid & operator *= (const CSG_Grid &Grid); 00609 virtual CSG_Grid & operator *= (double Value); 00610 virtual CSG_Grid & Multiply (const CSG_Grid &Grid); 00611 virtual CSG_Grid & Multiply (double Value); 00612 00613 virtual CSG_Grid operator / (const CSG_Grid &Grid) const; 00614 virtual CSG_Grid operator / (double Value) const; 00615 virtual CSG_Grid & operator /= (const CSG_Grid &Grid); 00616 virtual CSG_Grid & operator /= (double Value); 00617 virtual CSG_Grid & Divide (const CSG_Grid &Grid); 00618 virtual CSG_Grid & Divide (double Value); 00619 00620 virtual double operator () (int x, int y) const { return( asDouble(x, y) ); } 00621 00622 00623 //----------------------------------------------------- 00624 // Get Value... 00625 00626 double Get_Value(double xPos, double yPos, int Interpolation = GRID_INTERPOLATION_BSpline, bool bZFactor = false, bool bByteWise = false, bool bOnlyValidCells = false) const; 00627 double Get_Value(TSG_Point Position , int Interpolation = GRID_INTERPOLATION_BSpline, bool bZFactor = false, bool bByteWise = false, bool bOnlyValidCells = false) const; 00628 bool Get_Value(double xPos, double yPos, double &Value, int Interpolation = GRID_INTERPOLATION_BSpline, bool bZFactor = false, bool bByteWise = false, bool bOnlyValidCells = false) const; 00629 bool Get_Value(TSG_Point Position , double &Value, int Interpolation = GRID_INTERPOLATION_BSpline, bool bZFactor = false, bool bByteWise = false, bool bOnlyValidCells = false) const; 00630 00631 virtual BYTE asByte (int x, int y, bool bZFactor = false) const { return( (BYTE )asDouble(x, y, bZFactor) ); } 00632 virtual BYTE asByte ( long n, bool bZFactor = false) const { return( (BYTE )asDouble( n, bZFactor) ); } 00633 virtual char asChar (int x, int y, bool bZFactor = false) const { return( (char )asDouble(x, y, bZFactor) ); } 00634 virtual char asChar ( long n, bool bZFactor = false) const { return( (char )asDouble( n, bZFactor) ); } 00635 virtual short asShort (int x, int y, bool bZFactor = false) const { return( (short)asDouble(x, y, bZFactor) ); } 00636 virtual short asShort ( long n, bool bZFactor = false) const { return( (short)asDouble( n, bZFactor) ); } 00637 virtual int asInt (int x, int y, bool bZFactor = false) const { return( (int )asDouble(x, y, bZFactor) ); } 00638 virtual int asInt ( long n, bool bZFactor = false) const { return( (int )asDouble( n, bZFactor) ); } 00639 virtual float asFloat (int x, int y, bool bZFactor = false) const { return( (float)asDouble(x, y, bZFactor) ); } 00640 virtual float asFloat ( long n, bool bZFactor = false) const { return( (float)asDouble( n, bZFactor) ); } 00641 00642 //----------------------------------------------------- 00643 virtual double asDouble( long n, bool bZFactor = false) const 00644 { 00645 return( asDouble(n % Get_NX(), n / Get_NX(), bZFactor) ); 00646 } 00647 00648 virtual double asDouble(int x, int y, bool bZFactor = false) const 00649 { 00650 double Result; 00651 00652 if( m_Memory_Type == GRID_MEMORY_Normal ) 00653 { 00654 switch( m_Type ) 00655 { 00656 default: Result = 0.0; break; 00657 case SG_DATATYPE_Byte: Result = ((BYTE **)m_Values)[y][x]; break; 00658 case SG_DATATYPE_Char: Result = ((char **)m_Values)[y][x]; break; 00659 case SG_DATATYPE_Word: Result = ((WORD **)m_Values)[y][x]; break; 00660 case SG_DATATYPE_Short: Result = ((short **)m_Values)[y][x]; break; 00661 case SG_DATATYPE_DWord: Result = ((DWORD **)m_Values)[y][x]; break; 00662 case SG_DATATYPE_Int: Result = ((int **)m_Values)[y][x]; break; 00663 case SG_DATATYPE_Float: Result = ((float **)m_Values)[y][x]; break; 00664 case SG_DATATYPE_Double: Result = ((double **)m_Values)[y][x]; break; 00665 case SG_DATATYPE_Bit: Result =(((BYTE **)m_Values)[y][x / 8] & m_Bitmask[x % 8]) == 0 ? 0.0 : 1.0; break; 00666 } 00667 } 00668 else 00669 { 00670 Result = _LineBuffer_Get_Value(x, y); 00671 } 00672 00673 if( bZFactor ) 00674 { 00675 Result *= m_zFactor; 00676 } 00677 00678 return( Result ); 00679 } 00680 00681 00682 //----------------------------------------------------- 00683 // Set Value... 00684 00685 virtual void Add_Value(int x, int y, double Value) { Set_Value(x, y, asDouble(x, y) + Value ); } 00686 virtual void Add_Value( long n, double Value) { Set_Value( n, asDouble( n) + Value ); } 00687 00688 virtual void Mul_Value(int x, int y, double Value) { Set_Value(x, y, asDouble(x, y) * Value ); } 00689 virtual void Mul_Value( long n, double Value) { Set_Value( n, asDouble( n) * Value ); } 00690 00691 //----------------------------------------------------- 00692 virtual void Set_Value( long n, double Value) 00693 { 00694 Set_Value(n % Get_NX(), n / Get_NX(), Value); 00695 } 00696 00697 virtual void Set_Value(int x, int y, double Value) 00698 { 00699 if( m_Memory_Type == GRID_MEMORY_Normal ) 00700 { 00701 switch( m_Type ) 00702 { 00703 default: break; 00704 case SG_DATATYPE_Byte: ((BYTE **)m_Values)[y][x] = (BYTE )Value; break; 00705 case SG_DATATYPE_Char: ((char **)m_Values)[y][x] = (char )Value; break; 00706 case SG_DATATYPE_Word: ((WORD **)m_Values)[y][x] = (WORD )Value; break; 00707 case SG_DATATYPE_Short: ((short **)m_Values)[y][x] = (short )Value; break; 00708 case SG_DATATYPE_DWord: ((DWORD **)m_Values)[y][x] = (DWORD )Value; break; 00709 case SG_DATATYPE_Int: ((int **)m_Values)[y][x] = (int )Value; break; 00710 case SG_DATATYPE_Float: ((float **)m_Values)[y][x] = (float )Value; break; 00711 case SG_DATATYPE_Double: ((double **)m_Values)[y][x] = (double)Value; break; 00712 case SG_DATATYPE_Bit: ((BYTE **)m_Values)[y][x / 8] = Value != 0.0 00713 ? ((BYTE **)m_Values)[y][x / 8] | m_Bitmask[x % 8] 00714 : ((BYTE **)m_Values)[y][x / 8] & (~m_Bitmask[x % 8]); 00715 break; 00716 } 00717 } 00718 else 00719 { 00720 _LineBuffer_Set_Value(x, y, Value); 00721 } 00722 00723 Set_Modified(); 00724 } 00725 00726 virtual void Set_Value_And_Sort( long n, double Value); 00727 virtual void Set_Value_And_Sort(int x, int y, double Value); 00728 00729 00730 //--------------------------------------------------------- 00731 protected: 00732 00733 virtual bool On_Update (void); 00734 00735 00736 //--------------------------------------------------------- 00737 private: 00738 00739 void **m_Values; 00740 00741 bool m_bCreated, m_bIndexed, m_Memory_bLock, 00742 Cache_bTemp, Cache_bSwap, Cache_bFlip; 00743 00744 int LineBuffer_Count; 00745 00746 long *m_Index, Cache_Offset; 00747 00748 double m_zFactor; 00749 00750 CSG_Simple_Statistics m_zStats; 00751 00752 CSG_File Cache_Stream; 00753 00754 TSG_Data_Type m_Type; 00755 00756 TSG_Grid_Memory_Type m_Memory_Type; 00757 00758 CSG_Grid_System m_System; 00759 00760 CSG_String m_Unit, Cache_Path; 00761 00762 00763 //----------------------------------------------------- 00764 static BYTE m_Bitmask[8]; 00765 00766 //----------------------------------------------------- 00767 typedef struct 00768 { 00769 bool bModified; 00770 int y; 00771 char *Data; 00772 } 00773 TSG_Grid_Line; 00774 00775 TSG_Grid_Line *LineBuffer; 00776 00777 00778 //----------------------------------------------------- 00779 void _On_Construction (void); 00780 00781 void _Set_Properties (TSG_Data_Type m_Type, int NX, int NY, double Cellsize, double xMin, double yMin); 00782 00783 bool _Set_Index (void); 00784 00785 00786 //----------------------------------------------------- 00787 // Memory handling... 00788 00789 int _Get_nLineBytes (void) { return( m_Type == SG_DATATYPE_Bit ? Get_NX() / 8 + 1 : Get_NX() * Get_nValueBytes() ); } 00790 00791 bool _Memory_Create (TSG_Grid_Memory_Type aMemory_Type); 00792 void _Memory_Destroy (void); 00793 00794 void _LineBuffer_Create (void); 00795 void _LineBuffer_Destroy (void); 00796 void _LineBuffer_Flush (void); 00797 TSG_Grid_Line * _LineBuffer_Get_Line (int y) const; 00798 void _LineBuffer_Set_Value (int x, int y, double Value); 00799 double _LineBuffer_Get_Value (int x, int y) const; 00800 00801 bool _Array_Create (void); 00802 void _Array_Destroy (void); 00803 00804 bool _Cache_Create (const SG_Char *FilePath, TSG_Data_Type File_Type, long Offset, bool bSwap, bool bFlip); 00805 bool _Cache_Create (void); 00806 bool _Cache_Destroy (bool bMemory_Restore); 00807 void _Cache_LineBuffer_Save (TSG_Grid_Line *pLine) const; 00808 void _Cache_LineBuffer_Load (TSG_Grid_Line *pLine, int y) const; 00809 00810 bool _Compr_Create (void); 00811 bool _Compr_Destroy (bool bMemory_Restore); 00812 void _Compr_LineBuffer_Save (TSG_Grid_Line *pLine) const; 00813 void _Compr_LineBuffer_Load (TSG_Grid_Line *pLine, int y) const; 00814 00815 00816 //----------------------------------------------------- 00817 // File access... 00818 00819 void _Swap_Bytes (char *Bytes, int nBytes) const; 00820 00821 bool _Load (const CSG_String &File_Name, TSG_Data_Type m_Type, TSG_Grid_Memory_Type aMemory_Type); 00822 00823 bool _Load_Binary (CSG_File &Stream, TSG_Data_Type File_Type, bool bFlip, bool bSwapBytes); 00824 bool _Save_Binary (CSG_File &Stream, int xA, int yA, int xN, int yN, TSG_Data_Type File_Type, bool bFlip, bool bSwapBytes); 00825 bool _Load_ASCII (CSG_File &Stream, TSG_Grid_Memory_Type aMemory_Type, bool bFlip = false); 00826 bool _Save_ASCII (CSG_File &Stream, int xA, int yA, int xN, int yN, bool bFlip = false); 00827 bool _Load_Native (const CSG_String &File_Name, TSG_Grid_Memory_Type aMemory_Type); 00828 bool _Save_Native (const CSG_String &File_Name, int xA, int yA, int xN, int yN, bool bBinary = true); 00829 00830 int _Load_Native_Get_Key (CSG_File &Stream, CSG_String &Line); 00831 00832 bool _Load_Surfer (const CSG_String &File_Name, TSG_Grid_Memory_Type aMemory_Type); 00833 00834 //----------------------------------------------------- 00835 CSG_Grid & _Operation_Arithmetic (const CSG_Grid &Grid, TSG_Grid_Operation Operation); 00836 CSG_Grid & _Operation_Arithmetic (double Value , TSG_Grid_Operation Operation); 00837 00838 00839 //----------------------------------------------------- 00840 // Interpolation subroutines... 00841 00842 double _Get_ValAtPos_NearestNeighbour (int x, int y, double dx, double dy) const; 00843 double _Get_ValAtPos_BiLinear (int x, int y, double dx, double dy, bool bByteWise) const; 00844 double _Get_ValAtPos_InverseDistance (int x, int y, double dx, double dy, bool bByteWise) const; 00845 double _Get_ValAtPos_BiCubicSpline (int x, int y, double dx, double dy, bool bByteWise) const; 00846 double _Get_ValAtPos_BiCubicSpline (double dx, double dy, double z_xy[4][4]) const; 00847 double _Get_ValAtPos_BSpline (int x, int y, double dx, double dy, bool bByteWise) const; 00848 double _Get_ValAtPos_BSpline (double dx, double dy, double z_xy[4][4]) const; 00849 bool _Get_ValAtPos_Fill4x4Submatrix (int x, int y, double z_xy[4][4]) const; 00850 bool _Get_ValAtPos_Fill4x4Submatrix (int x, int y, double z_xy[4][4][4]) const; 00851 00852 bool _Assign_Interpolated (CSG_Grid *pSource, TSG_Grid_Interpolation Interpolation); 00853 bool _Assign_MeanValue (CSG_Grid *pSource, bool bAreaProportional); 00854 bool _Assign_ExtremeValue (CSG_Grid *pSource, bool bMaximum); 00855 bool _Assign_Majority (CSG_Grid *pSource); 00856 00857 }; 00858 00859 00861 // // 00862 // Functions // 00863 // // 00865 00866 //--------------------------------------------------------- 00868 SAGA_API_DLL_EXPORT CSG_Grid * SG_Create_Grid (void); 00869 00871 SAGA_API_DLL_EXPORT CSG_Grid * SG_Create_Grid (const CSG_Grid &Grid); 00872 00874 SAGA_API_DLL_EXPORT CSG_Grid * SG_Create_Grid (const CSG_String &File_Name, TSG_Data_Type Type = SG_DATATYPE_Undefined, TSG_Grid_Memory_Type Memory_Type = GRID_MEMORY_Normal); 00875 00877 SAGA_API_DLL_EXPORT CSG_Grid * SG_Create_Grid (CSG_Grid *pGrid, TSG_Data_Type Type = SG_DATATYPE_Undefined, TSG_Grid_Memory_Type Memory_Type = GRID_MEMORY_Normal); 00878 00880 SAGA_API_DLL_EXPORT CSG_Grid * SG_Create_Grid (const CSG_Grid_System &System, TSG_Data_Type Type = SG_DATATYPE_Undefined, TSG_Grid_Memory_Type Memory_Type = GRID_MEMORY_Normal); 00881 00883 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, TSG_Grid_Memory_Type Memory_Type = GRID_MEMORY_Normal); 00884 00885 //--------------------------------------------------------- 00887 SAGA_API_DLL_EXPORT const SG_Char * SG_Grid_Cache_Get_Directory (void); 00888 00890 SAGA_API_DLL_EXPORT void SG_Grid_Cache_Set_Directory (const SG_Char *Directory); 00891 00892 SAGA_API_DLL_EXPORT void SG_Grid_Cache_Set_Automatic (bool bOn); 00893 SAGA_API_DLL_EXPORT bool SG_Grid_Cache_Get_Automatic (void); 00894 00895 SAGA_API_DLL_EXPORT void SG_Grid_Cache_Set_Confirm (int Confirm); 00896 SAGA_API_DLL_EXPORT int SG_Grid_Cache_Get_Confirm (void); 00897 00898 SAGA_API_DLL_EXPORT void SG_Grid_Cache_Set_Threshold (int nBytes); 00899 SAGA_API_DLL_EXPORT void SG_Grid_Cache_Set_Threshold_MB (double nMegabytes); 00900 SAGA_API_DLL_EXPORT long SG_Grid_Cache_Get_Threshold (void); 00901 SAGA_API_DLL_EXPORT double SG_Grid_Cache_Get_Threshold_MB (void); 00902 00903 00905 // // 00906 // // 00907 // // 00909 00910 //--------------------------------------------------------- 00911 class CSG_Grid_Stack : public CSG_Stack 00912 { 00913 public: 00914 CSG_Grid_Stack(void) : CSG_Stack(sizeof(TSG_Point_Int)) {} 00915 00916 //----------------------------------------------------- 00917 virtual void Push (int x, int y) 00918 { 00919 TSG_Point_Int *pPoint = (TSG_Point_Int *)Get_Record_Push(); 00920 00921 if( pPoint ) 00922 { 00923 pPoint->x = x; 00924 pPoint->y = y; 00925 } 00926 } 00927 00928 //----------------------------------------------------- 00929 virtual void Pop (int &x, int &y) 00930 { 00931 TSG_Point_Int *pPoint = (TSG_Point_Int *)Get_Record_Pop(); 00932 00933 if( pPoint ) 00934 { 00935 x = pPoint->x; 00936 y = pPoint->y; 00937 } 00938 } 00939 00940 }; 00941 00942 00944 // // 00945 // // 00946 // // 00948 00949 //--------------------------------------------------------- 00950 class SAGA_API_DLL_EXPORT CSG_Grid_Cell_Addressor 00951 { 00952 public: 00953 CSG_Grid_Cell_Addressor(void); 00954 00955 bool Destroy (void); 00956 00957 CSG_Distance_Weighting & Get_Weighting (void) { return( m_Weighting ); } 00958 00959 bool Set_Radius (double Radius); 00960 bool Set_Annulus (double inner_Radius, double outer_Radius); 00961 bool Set_Sector (double Radius, double Direction, double Tolerance); 00962 00963 int Get_Count (void) const { return( m_Cells.Get_Count() ); } 00964 00965 int Get_X (int Index, int Offset = 0) const { return( Offset + (Index >= 0 && Index < Get_Count() ? m_Cells.Get_Record_byIndex(Index)->asInt(0) : 0) ); } 00966 int Get_Y (int Index, int Offset = 0) const { return( Offset + (Index >= 0 && Index < Get_Count() ? m_Cells.Get_Record_byIndex(Index)->asInt(1) : 0) ); } 00967 00968 double Get_Distance (int Index) const { return( Index >= 0 && Index < Get_Count() ? m_Cells.Get_Record_byIndex(Index)->asDouble(2) : -1.0 ); } 00969 double Get_Weight (int Index) const { return( Index >= 0 && Index < Get_Count() ? m_Cells.Get_Record_byIndex(Index)->asDouble(3) : -1.0 ); } 00970 00971 bool Get_Values (int Index, int &x, int &y, double &Distance, double &Weight, bool bOffset = false) const 00972 { 00973 if( Index >= 0 && Index < Get_Count() ) 00974 { 00975 CSG_Table_Record *pCell = m_Cells.Get_Record_byIndex(Index); 00976 00977 if( bOffset ) 00978 { 00979 x += pCell->asInt(0); 00980 y += pCell->asInt(1); 00981 } 00982 else 00983 { 00984 x = pCell->asInt(0); 00985 y = pCell->asInt(1); 00986 } 00987 00988 Distance = pCell->asDouble(2); 00989 Weight = pCell->asDouble(3); 00990 00991 return( true ); 00992 } 00993 00994 return( false ); 00995 } 00996 00997 00998 private: 00999 01000 CSG_Distance_Weighting m_Weighting; 01001 01002 CSG_Table m_Cells; 01003 01004 }; 01005 01006 01008 // // 01009 // // 01010 // // 01012 01013 //--------------------------------------------------------- 01014 #endif // #ifndef HEADER_INCLUDED__SAGA_API__grid_H