📄 grid_io.cpp
字号:
///////////////////////////////////////////////////////////
// //
// SAGA //
// //
// System for Automated Geoscientific Analyses //
// //
// Application Programming Interface //
// //
// Library: SAGA_API //
// //
//-------------------------------------------------------//
// //
// grid_io.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 //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
///////////////////////////////////////////////////////////
// //
// Grid: File Operations //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
#include <string.h>
#include "grid.h"
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
bool CSG_Grid::_Load(const SG_Char *File_Name, TSG_Grid_Type Type, TSG_Grid_Memory_Type Memory_Type)
{
bool bResult;
//-----------------------------------------------------
Destroy();
m_Type = Type;
//-----------------------------------------------------
SG_UI_Msg_Add(CSG_String::Format(SG_T("%s: %s..."), LNG("[MSG] Load grid"), File_Name ), true);
if( SG_File_Cmp_Extension(File_Name, SG_T("grd")) )
{
bResult = _Load_Surfer(File_Name, Memory_Type);
}
else
{
bResult = _Load_Native(File_Name, Memory_Type);
}
//-----------------------------------------------------
if( bResult )
{
Set_File_Name(File_Name);
m_bCreated = true;
m_bUpdate = true;
if( !Get_History().Load(File_Name, HISTORY_EXT_GRID) )
{
Get_History().Add_Entry(LNG("[HST] Loaded from file"), File_Name);
}
SG_UI_Msg_Add(LNG("[MSG] okay"), false);
}
else
{
Destroy();
SG_UI_Msg_Add(LNG("[MSG] failed"), false);
SG_UI_Msg_Add_Error(LNG("[ERR] Grid file could not be opened."));
}
//-----------------------------------------------------
return( bResult );
}
//---------------------------------------------------------
bool CSG_Grid::Save(const SG_Char *File_Name, int Format)
{
return( Save(File_Name, Format, 0, 0, Get_NX(), Get_NY()) );
}
bool CSG_Grid::Save(const SG_Char *File_Name, int Format, int xA, int yA, int xN, int yN)
{
bool bResult;
CSG_String sFile_Name = SG_File_Make_Path(NULL, File_Name, SG_T("sgrd"));
//-----------------------------------------------------
if( xA < 0 || xA >= Get_NX() - 1 )
{
xA = 0;
}
if( yA < 0 || yA >= Get_NY() - 1 )
{
yA = 0;
}
if( xN > Get_NX() - xA )
{
xN = Get_NX() - xA;
}
if( yN > Get_NY() - yA )
{
yN = Get_NY() - yA;
}
//-----------------------------------------------------
SG_UI_Msg_Add(CSG_String::Format(SG_T("%s: %s..."), LNG("[MSG] Save grid"), File_Name ), true);
switch( Format )
{
default:
case GRID_FILE_FORMAT_Binary: // 1 - Binary
bResult = _Save_Native(sFile_Name, xA, yA, xN, yN, true);
break;
case GRID_FILE_FORMAT_ASCII: // 2 - ASCII
bResult = _Save_Native(sFile_Name, xA, yA, xN, yN, false);
break;
}
//-----------------------------------------------------
if( bResult )
{
Set_Modified(false);
Set_File_Name(sFile_Name);
Get_History().Save(File_Name, HISTORY_EXT_GRID);
SG_UI_Msg_Add(LNG("[MSG] okay"), false);
}
else
{
SG_UI_Msg_Add(LNG("[MSG] failed"), false);
SG_UI_Msg_Add_Error(LNG("[ERR] Grid file could not be saved."));
}
return( bResult );
}
///////////////////////////////////////////////////////////
// //
// Binary //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
void CSG_Grid::_Swap_Bytes(char *Bytes, int nBytes) const
{
char Byte, *p;
p = Bytes + nBytes - 1;
while( Bytes < p )
{
Byte = *Bytes;
*(Bytes++) = *p;
*(p--) = Byte;
}
}
//---------------------------------------------------------
bool CSG_Grid::_Load_Binary(CSG_File &Stream, TSG_Grid_Type File_Type, bool bFlip, bool bSwapBytes)
{
char *Line, *pValue;
int x, y, i, iy, dy, nxBytes, nValueBytes;
if( Stream.is_Open() && is_Valid() )
{
Set_File_Type(GRID_FILE_FORMAT_Binary);
if( bFlip )
{
y = Get_NY() - 1;
dy = -1;
}
else
{
y = 0;
dy = 1;
}
//-------------------------------------------------
if( File_Type == GRID_TYPE_Bit )
{
nxBytes = Get_NX() / 8 + 1;
if( m_Type == File_Type && m_Memory_Type == GRID_MEMORY_Normal )
{
for(iy=0; iy<Get_NY() && !Stream.is_EOF() && SG_UI_Process_Set_Progress(iy, Get_NY()); iy++, y+=dy)
{
Stream.Read(m_Values[y], sizeof(char), nxBytes);
}
}
else
{
Line = (char *)SG_Malloc(nxBytes);
for(iy=0; iy<Get_NY() && !Stream.is_EOF() && SG_UI_Process_Set_Progress(iy, Get_NY()); iy++, y+=dy)
{
Stream.Read(Line, sizeof(char), nxBytes);
for(x=0, pValue=Line; x<Get_NX(); pValue++)
{
for(i=0; i<8 && x<Get_NX(); i++, x++)
{
Set_Value(x, y, (*pValue & m_Bitmask[i]) == 0 ? 0.0 : 1.0);
}
}
}
SG_Free(Line);
}
}
//-------------------------------------------------
else
{
nValueBytes = gSG_Grid_Type_Sizes[File_Type];
nxBytes = Get_NX() * nValueBytes;
if( m_Type == File_Type && m_Memory_Type == GRID_MEMORY_Normal && !bSwapBytes )
{
for(iy=0; iy<Get_NY() && !Stream.is_EOF() && SG_UI_Process_Set_Progress(iy, Get_NY()); iy++, y+=dy)
{
Stream.Read(m_Values[y], sizeof(char), nxBytes);
}
}
else
{
Line = (char *)SG_Malloc(nxBytes);
for(iy=0; iy<Get_NY() && !Stream.is_EOF() && SG_UI_Process_Set_Progress(iy, Get_NY()); iy++, y+=dy)
{
Stream.Read(Line, sizeof(char), nxBytes);
for(x=0, pValue=Line; x<Get_NX(); x++, pValue+=nValueBytes)
{
if( bSwapBytes )
{
_Swap_Bytes(pValue, nValueBytes);
}
switch( File_Type )
{
default: break;
case GRID_TYPE_Byte: Set_Value(x, y, *(BYTE *)pValue); break;
case GRID_TYPE_Char: Set_Value(x, y, *(char *)pValue); break;
case GRID_TYPE_Word: Set_Value(x, y, *(WORD *)pValue); break;
case GRID_TYPE_Short: Set_Value(x, y, *(short *)pValue); break;
case GRID_TYPE_DWord: Set_Value(x, y, *(DWORD *)pValue); break;
case GRID_TYPE_Int: Set_Value(x, y, *(int *)pValue); break;
case GRID_TYPE_Float: Set_Value(x, y, *(float *)pValue); break;
case GRID_TYPE_Double: Set_Value(x, y, *(double *)pValue); break;
}
}
}
SG_Free(Line);
}
}
//-------------------------------------------------
SG_UI_Process_Set_Ready();
return( true );
}
return( false );
}
//---------------------------------------------------------
bool CSG_Grid::_Save_Binary(CSG_File &Stream, int xA, int yA, int xN, int yN, TSG_Grid_Type File_Type, bool bFlip, bool bSwapBytes)
{
char *Line, *pValue;
int x, y, i, ix, iy, dy, axBytes, nxBytes, nValueBytes;
//-----------------------------------------------------
if( Stream.is_Open() && m_System.is_Valid() && m_Type > 0 && m_Type < GRID_TYPE_Count )
{
Set_File_Type(GRID_FILE_FORMAT_Binary);
if( bFlip )
{
y = yA + yN - 1;
dy = -1;
}
else
{
y = yA;
dy = 1;
}
//-------------------------------------------------
if( File_Type == GRID_TYPE_Bit )
{
nxBytes = xN / 8 + 1;
if( m_Type == File_Type && m_Memory_Type == GRID_MEMORY_Normal && xA % 8 == 0 )
{
axBytes = xA / 8;
for(iy=0; iy<yN && SG_UI_Process_Set_Progress(iy, yN); iy++, y+=dy)
{
Stream.Write((char *)m_Values[y] + axBytes, sizeof(char), nxBytes);
}
}
else
{
Line = (char *)SG_Malloc(nxBytes);
for(iy=0; iy<yN && SG_UI_Process_Set_Progress(iy, yN); iy++, y+=dy)
{
for(ix=0, x=xA, pValue=Line; ix<xN; pValue++)
{
for(i=0; i<8 && ix<xN; i++, ix++, x++)
{
*pValue = asChar(x, y) != 0.0 ? *pValue | m_Bitmask[i] : *pValue & (~m_Bitmask[i]);
}
}
Stream.Write(Line, sizeof(char), nxBytes);
}
SG_Free(Line);
}
}
//-------------------------------------------------
else
{
nValueBytes = gSG_Grid_Type_Sizes[File_Type];
nxBytes = xN * nValueBytes;
if( m_Type == File_Type && m_Memory_Type == GRID_MEMORY_Normal && !bSwapBytes )
{
axBytes = xA * nValueBytes;
for(iy=0; iy<yN && SG_UI_Process_Set_Progress(iy, yN); iy++, y+=dy)
{
Stream.Write((char *)m_Values[y] + axBytes, sizeof(char), nxBytes);
}
}
else
{
Line = (char *)SG_Malloc(nxBytes);
for(iy=0; iy<yN && SG_UI_Process_Set_Progress(iy, yN); iy++, y+=dy)
{
for(ix=0, x=xA, pValue=Line; ix<xN; ix++, x++, pValue+=nValueBytes)
{
switch( File_Type )
{
default: break;
case GRID_TYPE_Byte: *(BYTE *)pValue = asChar (x, y); break;
case GRID_TYPE_Char: *(char *)pValue = asChar (x, y); break;
case GRID_TYPE_Word: *(WORD *)pValue = asShort (x, y); break;
case GRID_TYPE_Short: *(short *)pValue = asShort (x, y); break;
case GRID_TYPE_DWord: *(DWORD *)pValue = asInt (x, y); break;
case GRID_TYPE_Int: *(int *)pValue = asInt (x, y); break;
case GRID_TYPE_Float: *(float *)pValue = asFloat (x, y); break;
case GRID_TYPE_Double: *(double *)pValue = asDouble (x, y); break;
}
if( bSwapBytes )
{
_Swap_Bytes(pValue, nValueBytes);
}
}
Stream.Write(Line, sizeof(char), nxBytes);
}
SG_Free(Line);
}
}
//-------------------------------------------------
SG_UI_Process_Set_Ready();
return( true );
}
return( false );
}
///////////////////////////////////////////////////////////
// //
// ASCII //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
bool CSG_Grid::_Load_ASCII(CSG_File &Stream, TSG_Grid_Memory_Type Memory_Type, bool bFlip)
{
int x, y, iy, dy;
double Value;
if( Stream.is_Open() && m_System.is_Valid() && m_Type > 0 && m_Type < GRID_TYPE_Count && _Memory_Create(Memory_Type) )
{
Set_File_Type(GRID_FILE_FORMAT_ASCII);
if( bFlip )
{
y = Get_NY() - 1;
dy = -1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -