57#include <netinet/in.h>
179 bool bResult =
false;
188 bResult = _Save_Compressed(File);
229bool CSG_Grid::_Load_External(
const CSG_String &File,
bool bCached,
bool bLoadData)
269 if( bResult && Manager.
Grid().
Count() && Manager.
Grid(0).is_Valid() )
283 m_System = pGrid->m_System;
284 m_Type = pGrid->m_Type;
285 m_Values = pGrid->m_Values; pGrid->m_Values = NULL;
287 m_zOffset = pGrid->m_zOffset;
288 m_zScale = pGrid->m_zScale;
289 m_Unit = pGrid->m_Unit;
291 m_nBytes_Value = pGrid->m_nBytes_Value;
292 m_nBytes_Line = pGrid->m_nBytes_Line;
306bool CSG_Grid::_Load_PGSQL(
const CSG_String &File,
bool bCached,
bool bLoadData)
308 bool bResult =
false;
314 s = s.AfterFirst(
':'); CSG_String Host (s.BeforeFirst(
':'));
315 s = s.AfterFirst(
':'); CSG_String Port (s.BeforeFirst(
':'));
316 s = s.AfterFirst(
':'); CSG_String DBase(s.BeforeFirst(
':'));
317 s = s.AfterFirst(
':'); CSG_String Table(s.BeforeFirst(
':'));
318 s = s.AfterFirst(
':'); CSG_String rid (s.BeforeFirst(
':').AfterFirst(
'='));
326 CSG_String Connection(DBase +
" [" + Host +
":" + Port +
"]");
352bool CSG_Grid::_Load_Native(
const CSG_String &File,
bool bCached,
bool bLoadData)
354 CSG_Grid_File_Info Info;
386 return( _Memory_Create(bCached) );
402 return( _Load_ASCII(Stream, bCached, Info.
m_bFlip) );
409 if( bCached || _Cache_Check() )
424 if( _Memory_Create(bCached) )
443#ifdef WORDS_BIGENDIAN
444 bool bBigEndian =
true;
446 bool bBigEndian =
false;
449 CSG_String File(_File);
466 CSG_Grid_File_Info Info(*
this);
468 if( Info.
Save(File, bBinary) )
472 if( bBinary ? _Save_Binary(Stream, m_Type,
false, bBigEndian) : _Save_ASCII (Stream) )
495bool CSG_Grid::_Load_Compressed(
const CSG_String &_File,
bool bCached,
bool bLoadData)
509 if( !Stream.Get_File(File +
"sgrd" )
510 && !Stream.Get_File(File +
"sg-grd") )
514 for(
size_t i=0; i<Stream.Get_File_Count(); i++)
532 CSG_Grid_File_Info Info;
534 if( !Info.
Create(Stream) )
553 if( Stream.Get_File(File +
"prj") )
560 return( _Memory_Create(bCached) );
563 if( Stream.Get_File(File +
"mgrd") )
574 return( Stream.Get_File(File +
"sdat") && _Memory_Create(bCached)
580bool CSG_Grid::_Save_Compressed(
const CSG_String &_File)
582#ifdef WORDS_BIGENDIAN
583 bool bBigEndian =
true;
585 bool bBigEndian =
false;
594 CSG_Grid_File_Info Info(*
this);
596 if( Stream.Add_File(File +
"sgrd") && Info.
Save(Stream,
true)
597 && Stream.Add_File(File +
"sdat") && _Save_Binary(Stream, m_Type,
false, bBigEndian) )
601 Stream.Add_File(File +
"sdat.aux.xml"); Info.
Save_AUX_XML(Stream);
618void CSG_Grid::_Swap_Bytes(
char *Bytes,
int nBytes)
const
622 uint16_t val, valSwapped;
623 memcpy(&val, Bytes, nBytes);
624 valSwapped = ntohs(val);
625 memcpy(Bytes, &valSwapped, nBytes);
627 else if( nBytes == 4 )
629 uint32_t val, valSwapped;
630 memcpy(&val, Bytes, nBytes);
631 valSwapped = ntohl(val);
632 memcpy(Bytes, &valSwapped, nBytes);
649 int nLineBytes =
Get_NX() / 8 + 1;
651 if( m_Type == File_Type && !
is_Cached() )
655 Stream.
Read(m_Values[bFlip ?
Get_NY() - y - 1 : y],
sizeof(
char), nLineBytes);
660 CSG_Array Line(1, nLineBytes);
664 Stream.
Read(Line.Get_Array(), nLineBytes);
666 char *pValue = (
char *)Line.Get_Array();
668 for(
int x=0, yy=bFlip ?
Get_NY()-y-1 : y; x<
Get_NX(); pValue++)
670 for(
int i=0; i<8 && x<
Get_NX(); i++, x++)
672 Set_Value(x, yy, (*pValue & m_Bitmask[i]) == 0 ? 0.0 : 1.0);
683 int nLineBytes =
Get_NX() * nValueBytes;
685 if( m_Type == File_Type && !
is_Cached() && !bSwapBytes )
689 Stream.
Read(m_Values[bFlip ?
Get_NY() - y - 1 : y], nLineBytes);
694 CSG_Array Line(1, nLineBytes);
698 Stream.
Read(Line.Get_Array(), nLineBytes);
700 char *pValue = (
char *)Line.Get_Array();
702 for(
int x=0, yy=bFlip ?
Get_NY()-y-1 : y; x<
Get_NX(); x++, pValue+=nValueBytes)
706 _Swap_Bytes(pValue, nValueBytes);
744 int nLineBytes =
Get_NX() / 8 + 1;
746 if( m_Type == File_Type && !
is_Cached() )
750 Stream.
Write((
char *)m_Values[bFlip ?
Get_NY() - y - 1 : y],
sizeof(
char), nLineBytes);
755 CSG_Array Line(1, nLineBytes);
759 char *pValue = (
char *)Line.Get_Array();
761 for(
int x=0, yy=bFlip ?
Get_NY()-y-1 : y; x<
Get_NX(); pValue++)
763 for(
int i=0; i<8 && x<
Get_NX(); i++, x++)
765 *pValue =
asChar(x, yy) != 0.0 ? *pValue | m_Bitmask[i] : *pValue & (~m_Bitmask[i]);
769 Stream.
Write(Line.Get_Array(), nLineBytes);
778 int nLineBytes =
Get_NX() * nValueBytes;
780 if( m_Type == File_Type && !
is_Cached() && !bSwapBytes )
784 Stream.
Write((
char *)m_Values[bFlip ?
Get_NY() - y - 1 : y], nLineBytes);
789 CSG_Array Line(1, nLineBytes);
793 char *pValue = (
char *)Line.Get_Array();
795 for(
int x=0, yy=bFlip ?
Get_NY()-y-1 : y; x<
Get_NX(); x++, pValue+=nValueBytes)
812 _Swap_Bytes(pValue, nValueBytes);
816 Stream.
Write(Line.Get_Array(), nLineBytes);
833bool CSG_Grid::_Load_ASCII(
CSG_File &Stream,
bool bCached,
bool bFlip)
835 if( !Stream.
is_Reading() || !_Memory_Create(bCached) )
844 for(
int x=0, yy=bFlip ?
Get_NY()-y-1 : y; x<
Get_NX(); x++)
854bool CSG_Grid::_Save_ASCII(
CSG_File &Stream,
bool bFlip)
865 for(
int x=0, yy=bFlip ?
Get_NY()-y-1 : y; x<
Get_NX(); x++)
882bool CSG_Grid::_Load_Surfer(
const CSG_String &File,
bool bCached,
bool bLoadData)
884 const float NoData = 1.70141e38f;
899 char Identifier[4]; Stream.
Read(Identifier,
sizeof(
char), 4);
902 if( !strncmp(Identifier,
"DSBB", 4) )
904 short nx, ny;
double d; CSG_Rect r;
906 Stream.
Read(&nx ,
sizeof(
short ));
907 Stream.
Read(&ny ,
sizeof(
short ));
908 Stream.
Read(&r.
xMin,
sizeof(
double));
909 Stream.
Read(&r.
xMax,
sizeof(
double));
910 Stream.
Read(&r.
yMin,
sizeof(
double));
911 Stream.
Read(&r.
yMax,
sizeof(
double));
912 Stream.
Read(&d ,
sizeof(
double));
913 Stream.
Read(&d ,
sizeof(
double));
927 CSG_Array Line(
sizeof(
float),
Get_NX());
float *Values = (
float *)Line.Get_Array();
933 for(
int x=0; x<
Get_NX(); x++)
935 if( Values[x] == NoData )
951 else if( !strncmp(Identifier,
"DSAA", 4) )
962 dx = (xMax - xMin) / (nx - 1.);
976 for(
int x=0; x<
Get_NX(); x++)
980 if( Stream.
Scan(Value) && Value != NoData )
1022void CSG_Grid_File_Info::_On_Construction(
void)
1107 return(
Create(Stream) );
1120 if( !Stream.
Get_File(Archive_File +
"sgrd" )
1121 && !Stream.
Get_File(Archive_File +
"sg-grd") )
1134 return(
Create(Stream) );
1158 sLong NX = 0, NY = 0;
double Cellsize = 0., xMin = 0., yMin = 0.;
1160 for(
int i=0; i<256 && !Stream.
is_EOF(); i++)
1164 switch( _Get_Key(Stream, Value) )
1213 return(
m_System.Assign(Cellsize, xMin, yMin, (
int)NX, (
int)NY) );
1231 if( s.Find(sLine.
Left(s.Length())) >= 0 )
1260#define GRID_FILE_PRINT(Key, Val) { CSG_String s(gSG_Grid_File_Key_Names[Key]); s += "\t= " + Val + "\n"; Stream.Write(s); }
1267#ifdef WORDS_BIGENDIAN
1290 return( Info.
Save(File, bBinary) );
1298 return( Info.
Save(Stream, bBinary) );
1319 Stream.
Write(
"<PAMDataset>\n<SRS>");
1321 Stream.
Write(
"</SRS>\n</PAMDataset>\n");
void SG_UI_Msg_Add_Error(const char *Message)
void SG_UI_Msg_Add(const char *Message, bool bNewLine, TSG_UI_MSG_STYLE Style)
bool SG_UI_Process_Set_Ready(void)
bool SG_UI_Process_Set_Progress(int Position, int Range)
int SG_UI_Msg_Lock(bool bOn)
void SG_UI_ProgressAndMsg_Lock(bool bOn)
bool SG_Data_Type_is_Numeric(TSG_Data_Type Type)
@ SG_UI_MSG_STYLE_FAILURE
@ SG_UI_MSG_STYLE_SUCCESS
SAGA_API_DLL_EXPORT bool SG_File_Cmp_Extension(const CSG_String &File, const CSG_String &Extension)
SAGA_API_DLL_EXPORT CSG_String SG_File_Get_Path(const CSG_String &full_Path)
SAGA_API_DLL_EXPORT CSG_String SG_File_Make_Path(const CSG_String &Directory, const CSG_String &Name)
SAGA_API_DLL_EXPORT bool SG_File_Delete(const CSG_String &FileName)
SAGA_API_DLL_EXPORT CSG_String SG_File_Get_Name(const CSG_String &full_Path, bool bExtension)
const char gSG_Data_Type_Identifier[][32]
SAGA_API_DLL_EXPORT bool SG_File_Set_Extension(CSG_String &File, const CSG_String &Extension)
size_t SG_Data_Type_Get_Size(TSG_Data_Type Type)
SAGA_API_DLL_EXPORT CSG_String SG_File_Get_Extension(const CSG_String &File)
virtual CSG_String Get_File_Name(size_t Index)
size_t Get_File_Count(void)
bool Get_File(const SG_Char *Name)
CSG_Data_Collection & Grid(void) const
const SG_Char * Get_Name(void) const
void Set_Name(const CSG_String &Name)
bool Save_MetaData(const CSG_String &FileName)
double Get_NoData_Value(bool bUpper=false) const
CSG_MetaData & Get_MetaData(void) const
virtual bool Set_NoData_Value_Range(double Lower, double Upper)
const SG_Char * Get_File_Name(bool bNative=true) const
bool Load_MetaData(const CSG_String &FileName)
void Set_Description(const CSG_String &Description)
void Set_File_Type(int Type)
const SG_Char * Get_Description(void) const
CSG_Projection & Get_Projection(void)
void Set_File_Name(const CSG_String &FileName)
bool Seek(sLong Offset, int Origin=SG_FILE_START) const
virtual bool Open(const SG_Char *FileName, int Mode=SG_FILE_R, bool bBinary=true, int Encoding=SG_FILE_ENCODING_ANSI)
virtual const CSG_String & Get_File_Name(void) const
bool Read_Line(CSG_String &Line) const
size_t Write(void *Buffer, size_t Size, size_t Count=1) const
size_t Read(void *Buffer, size_t Size, size_t Count=1) const
int Printf(const char *Format,...)
bool is_Writing(void) const
bool is_Reading(void) const
bool Scan(int &Value) const
double Scan_Double(void) const
bool Save(const CSG_String &File, bool bBinary=true)
CSG_Projection m_Projection
bool Save_AUX_XML(const CSG_String &File)
bool Create(const CSG_Grid_File_Info &Info)
bool Assign(const CSG_Grid_System &System)
static int Get_Precision(void)
virtual bool On_Reload(void)
virtual int asInt(sLong i, bool bScaled=true) const
bool is_Cached(void) const
double Get_Offset(void) const
virtual void Set_NoData(int x, int y)
double Get_Scaling(void) const
virtual void Set_Modified(bool bModified=true)
virtual float asFloat(sLong i, bool bScaled=true) const
virtual short asShort(sLong i, bool bScaled=true) const
virtual bool is_Valid(void) const
const CSG_String & Get_Unit(void) const
virtual void Set_Value(sLong i, BYTE Value, bool bScaled=true)
virtual BYTE asByte(sLong i, bool bScaled=true) const
virtual bool Save(const char *File, int Format=0)
bool Create(const CSG_Grid &Grid)
const CSG_Grid_System & Get_System(void) const
virtual bool On_Delete(void)
void Set_Unit(const CSG_String &Unit)
virtual char asChar(sLong i, bool bScaled=true) const
TSG_Data_Type Get_Type(void) const
bool Save(const CSG_String &File, ESG_CRS_Format Format=ESG_CRS_Format::WKT) const
bool Load(const CSG_String &File)
CSG_String AfterFirst(char Character) const
bool asInt(int &Value) const
CSG_String & Remove(size_t pos)
int Cmp(const CSG_String &String) const
CSG_String BeforeFirst(char Character) const
static CSG_String Format(const char *Format,...)
int Trim(bool fromRight=false)
int Find(char Character, bool fromEnd=false) const
bool asDouble(double &Value) const
const SG_Char * c_str(void) const
bool is_Empty(void) const
CSG_String Left(size_t count) const
#define GRID_FILE_KEY_TRUE
SAGA_API_DLL_EXPORT CSG_String SG_Grid_Get_File_Extension(int Format)
@ GRID_FILE_KEY_DATAFORMAT
@ GRID_FILE_KEY_CELLCOUNT_Y
@ GRID_FILE_KEY_DESCRIPTION
@ GRID_FILE_KEY_POSITION_YMIN
@ GRID_FILE_KEY_DATAFILE_OFFSET
@ GRID_FILE_KEY_NODATA_VALUE
@ GRID_FILE_KEY_BYTEORDER_BIG
@ GRID_FILE_KEY_POSITION_XMIN
@ GRID_FILE_KEY_DATAFILE_NAME
@ GRID_FILE_KEY_CELLCOUNT_X
@ GRID_FILE_KEY_TOPTOBOTTOM
@ GRID_FILE_FORMAT_Compressed
@ GRID_FILE_FORMAT_Binary_old
@ GRID_FILE_FORMAT_GeoTIFF
@ GRID_FILE_FORMAT_Binary
@ GRID_FILE_FORMAT_Undefined
const SG_Char gSG_Grid_File_Key_Names[GRID_FILE_KEY_Count][32]
#define GRID_FILE_KEY_FALSE
#define GRID_FILE_PRINT(Key, Val)
bool SG_Grid_Set_File_Format_Default(int Format)
CSG_String SG_Grid_Get_File_Extension_Default(void)
CSG_String SG_Grid_Get_File_Extension(int Format)
TSG_Grid_File_Format SG_Grid_Get_File_Format_Default(void)
static TSG_Grid_File_Format gSG_Grid_File_Format_Default