📄 grid.cpp
字号:
///////////////////////////////////////////////////////////
// //
// SAGA //
// //
// System for Automated Geoscientific Analyses //
// //
// Application Programming Interface //
// //
// Library: SAGA_API //
// //
//-------------------------------------------------------//
// //
// grid.cpp //
// //
// Copyright (C) 2005 by Olaf Conrad //
// //
//-------------------------------------------------------//
// //
// This file is part of 'SAGA - System for Automated //
// Geoscientific Analyses'. //
// //
// This library is free software; you can redistribute //
// it and/or modify it under the terms of the GNU Lesser //
// General Public License as published by the Free //
// Software Foundation, version 2.1 of the License. //
// //
// This library is distributed in the hope that it will //
// be useful, but WITHOUT ANY WARRANTY; without even the //
// implied warranty of MERCHANTABILITY or FITNESS FOR A //
// PARTICULAR PURPOSE. See the GNU Lesser General Public //
// License for more details. //
// //
// You should have received a copy of the GNU Lesser //
// General Public License along with this program; if //
// not, write to the Free Software Foundation, Inc., //
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, //
// USA. //
// //
//-------------------------------------------------------//
// //
// contact: Olaf Conrad //
// Institute of Geography //
// University of Goettingen //
// Goldschmidtstr. 5 //
// 37077 Goettingen //
// Germany //
// //
// e-mail: oconrad@saga-gis.org //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
#include "grid.h"
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
BYTE CSG_Grid::m_Bitmask[8] = { 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80 };
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
CSG_Grid * SG_Create_Grid(void)
{
return( new CSG_Grid );
}
//---------------------------------------------------------
CSG_Grid * SG_Create_Grid(const CSG_Grid &Grid)
{
return( new CSG_Grid(Grid) );
}
//---------------------------------------------------------
CSG_Grid * SG_Create_Grid(const SG_Char *File_Name, TSG_Grid_Type Type, TSG_Grid_Memory_Type Memory_Type)
{
return( new CSG_Grid(File_Name, Type, Memory_Type) );
}
//---------------------------------------------------------
CSG_Grid * SG_Create_Grid(CSG_Grid *pGrid, TSG_Grid_Type Type, TSG_Grid_Memory_Type Memory_Type)
{
return( new CSG_Grid(pGrid, Type, Memory_Type) );
}
//---------------------------------------------------------
CSG_Grid * SG_Create_Grid(const CSG_Grid_System &System, TSG_Grid_Type Type, TSG_Grid_Memory_Type Memory_Type)
{
return( new CSG_Grid(System, Type, Memory_Type) );
}
//---------------------------------------------------------
CSG_Grid * SG_Create_Grid(TSG_Grid_Type Type, int NX, int NY, double Cellsize, double xMin, double yMin, TSG_Grid_Memory_Type Memory_Type)
{
return( new CSG_Grid(Type, NX, NY, Cellsize, xMin, yMin, Memory_Type) );
}
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
/**
* Default constructor.
*/
//---------------------------------------------------------
CSG_Grid::CSG_Grid(void)
: CSG_Data_Object()
{
_On_Construction();
}
//---------------------------------------------------------
/**
* Copy constructor.
*/
//---------------------------------------------------------
CSG_Grid::CSG_Grid(const CSG_Grid &Grid)
: CSG_Data_Object()
{
_On_Construction();
Create(Grid);
}
//---------------------------------------------------------
/**
* Create a grid from file.
*/
//---------------------------------------------------------
CSG_Grid::CSG_Grid(const SG_Char *File_Name, TSG_Grid_Type Type, TSG_Grid_Memory_Type Memory_Type)
: CSG_Data_Object()
{
_On_Construction();
Create(File_Name, Type, Memory_Type);
}
//---------------------------------------------------------
/**
* Create a grid similar to 'pGrid'.
*/
//---------------------------------------------------------
CSG_Grid::CSG_Grid(CSG_Grid *pGrid, TSG_Grid_Type Type, TSG_Grid_Memory_Type Memory_Type)
: CSG_Data_Object()
{
_On_Construction();
Create(pGrid, Type, Memory_Type);
}
//---------------------------------------------------------
/**
* Create a grid using 'System'.
*/
//---------------------------------------------------------
CSG_Grid::CSG_Grid(const CSG_Grid_System &System, TSG_Grid_Type Type, TSG_Grid_Memory_Type Memory_Type)
: CSG_Data_Object()
{
_On_Construction();
Create(System, Type, Memory_Type);
}
//---------------------------------------------------------
/**
* Create a grid with specified parameters.
* This constructor initializes the grid's data space with 'NX' x 'NY' cells of the size indicated by 'Type'.
* If 'DX/DY' are equal or less zero then both will be set to 1.0. 'xMin/yMin' specify the coordinates of the
* lower left corner of the grid.
*/
//---------------------------------------------------------
CSG_Grid::CSG_Grid(TSG_Grid_Type Type, int NX, int NY, double Cellsize, double xMin, double yMin, TSG_Grid_Memory_Type Memory_Type)
: CSG_Data_Object()
{
_On_Construction();
Create(Type, NX, NY, Cellsize, xMin, yMin, Memory_Type);
}
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
void CSG_Grid::_On_Construction(void)
{
m_bCreated = false;
m_Type = GRID_TYPE_Undefined;
m_Memory_Type = GRID_MEMORY_Normal;
m_Memory_bLock = false;
m_Values = NULL;
LineBuffer = NULL;
LineBuffer_Count = 5;
m_zFactor = 1.0;
m_NoData_Value = -99999.0;
m_NoData_hiValue = -999.0;
m_bSorted = false;
m_Sort_2b = NULL;
m_Sort_4b = NULL;
m_bUpdate = true;
}
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
bool CSG_Grid::Create(const CSG_Grid &Grid)
{
if( Create((CSG_Grid *)&Grid, ((CSG_Grid *)&Grid)->Get_Type()) )
{
return( Assign((CSG_Data_Object *)&Grid) );
}
return( false );
}
//---------------------------------------------------------
bool CSG_Grid::Create(CSG_Grid *pGrid, TSG_Grid_Type Type, TSG_Grid_Memory_Type Memory_Type)
{
return( Create(Type, pGrid->Get_NX(), pGrid->Get_NY(), pGrid->Get_Cellsize(), pGrid->Get_XMin(), pGrid->Get_YMin(), Memory_Type) );
}
//---------------------------------------------------------
bool CSG_Grid::Create(const CSG_Grid_System &System, TSG_Grid_Type Type, TSG_Grid_Memory_Type Memory_Type)
{
return( Create(Type, System.Get_NX(), System.Get_NY(), System.Get_Cellsize(), System.Get_XMin(), System.Get_YMin(), Memory_Type) );
}
//---------------------------------------------------------
bool CSG_Grid::Create(const SG_Char *File_Name, TSG_Grid_Type Type, TSG_Grid_Memory_Type Memory_Type)
{
return( _Load(File_Name, Type, Memory_Type) );
}
//---------------------------------------------------------
bool CSG_Grid::Create(TSG_Grid_Type Type, int NX, int NY, double Cellsize, double xMin, double yMin, TSG_Grid_Memory_Type Memory_Type)
{
Destroy();
_Set_Properties(Type, NX, NY, Cellsize, xMin, yMin);
if( _Memory_Create(Memory_Type) )
{
m_bCreated = true;
}
return( m_bCreated );
}
///////////////////////////////////////////////////////////
// //
// Destruction //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
/**
* The destructor.
*/
CSG_Grid::~CSG_Grid(void)
{
Destroy();
}
/**
* Destroys the data space of CSG_Grid.
*/
bool CSG_Grid::Destroy(void)
{
_Memory_Destroy();
m_bCreated = false;
m_Type = GRID_TYPE_Undefined;
m_Memory_Type = GRID_MEMORY_Normal;
m_zFactor = 1.0;
m_Description .Clear();
m_Unit .Clear();
return( CSG_Data_Object::Destroy() );
}
///////////////////////////////////////////////////////////
// //
// Header //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
void CSG_Grid::_Set_Properties(TSG_Grid_Type Type, int NX, int NY, double Cellsize, double xMin, double yMin)
{
m_Type = Type > GRID_TYPE_Undefined && Type < GRID_TYPE_Count ? Type : GRID_TYPE_Float;
m_System.Assign(Cellsize > 0.0 ? Cellsize : 1.0, xMin, yMin, NX, NY);
m_zMin = m_zMax = 0.0;
switch( m_Type )
{
case GRID_TYPE_Bit:
m_NoData_Value = m_NoData_hiValue = 0.0;
break;
case GRID_TYPE_Byte:
m_NoData_Value = m_NoData_hiValue = 255.0;
break;
case GRID_TYPE_Char:
m_NoData_Value = m_NoData_hiValue = -127.0;
break;
case GRID_TYPE_Word:
m_NoData_Value = m_NoData_hiValue = 65535.0;
break;
case GRID_TYPE_Short:
m_NoData_Value = m_NoData_hiValue = -32767.0;
break;
case GRID_TYPE_DWord:
m_NoData_Value = m_NoData_hiValue = 4294967295.0;
break;
case GRID_TYPE_Int:
m_NoData_Value = m_NoData_hiValue = -2147483647.0;
break;
default:
case GRID_TYPE_Float:
case GRID_TYPE_Double:
m_NoData_Value = -99999.0;
m_NoData_hiValue = - 999.0;
break;
}
}
//---------------------------------------------------------
void CSG_Grid::Set_Description(const SG_Char *String)
{
m_Description.Printf(String ? String : SG_T(""));
}
const SG_Char * CSG_Grid::Get_Description(void) const
{
return( m_Description.c_str() );
}
//---------------------------------------------------------
void CSG_Grid::Set_Unit(const SG_Char *String)
{
m_Unit.Printf(String ? String : SG_T(""));
}
const SG_Char * CSG_Grid::Get_Unit(void) const
{
return( m_Unit.c_str() );
}
//---------------------------------------------------------
void CSG_Grid::Set_ZFactor(double Value)
{
m_zFactor = Value;
}
//---------------------------------------------------------
double CSG_Grid::Get_ZFactor(void) const
{
return( m_zFactor );
}
///////////////////////////////////////////////////////////
// //
// No Data Values //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -