57#include <netinet/in.h>
177 bool bResult =
false;
186 bResult = _Save_Compressed(File);
227bool CSG_Grid::_Load_External(
const CSG_String &File,
bool bCached,
bool bLoadData)
267 if( bResult && Manager.
Grid().
Count() && Manager.
Grid(0).is_Valid() )
281 m_System = pGrid->m_System;
282 m_Type = pGrid->m_Type;
283 m_Values = pGrid->m_Values; pGrid->m_Values = NULL;
285 m_zOffset = pGrid->m_zOffset;
286 m_zScale = pGrid->m_zScale;
287 m_Unit = pGrid->m_Unit;
289 m_nBytes_Value = pGrid->m_nBytes_Value;
290 m_nBytes_Line = pGrid->m_nBytes_Line;
304bool CSG_Grid::_Load_PGSQL(
const CSG_String &File,
bool bCached,
bool bLoadData)
306 bool bResult =
false;
312 s = s.AfterFirst(
':'); CSG_String Host (s.BeforeFirst(
':'));
313 s = s.AfterFirst(
':'); CSG_String Port (s.BeforeFirst(
':'));
314 s = s.AfterFirst(
':'); CSG_String DBase(s.BeforeFirst(
':'));
315 s = s.AfterFirst(
':'); CSG_String Table(s.BeforeFirst(
':'));
316 s = s.AfterFirst(
':'); CSG_String rid (s.BeforeFirst(
':').AfterFirst(
'='));
324 CSG_String Connection(DBase +
" [" + Host +
":" + Port +
"]");
350bool CSG_Grid::_Load_Native(
const CSG_String &File,
bool bCached,
bool bLoadData)
352 CSG_Grid_File_Info Info;
379 return( _Memory_Create(bCached) );
395 return( _Load_ASCII(Stream, bCached, Info.
m_bFlip) );
402 if( bCached || _Cache_Check() )
417 if( _Memory_Create(bCached) )
436#ifdef WORDS_BIGENDIAN
437 bool bBigEndian =
true;
439 bool bBigEndian =
false;
442 CSG_String File(_File);
459 CSG_Grid_File_Info Info(*
this);
461 if( Info.
Save(File, bBinary) )
465 if( bBinary ? _Save_Binary(Stream, m_Type,
false, bBigEndian) : _Save_ASCII (Stream) )
488bool CSG_Grid::_Load_Compressed(
const CSG_String &_File,
bool bCached,
bool bLoadData)
502 if( !Stream.Get_File(File +
"sgrd" )
503 && !Stream.Get_File(File +
"sg-grd") )
507 for(
size_t i=0; i<Stream.Get_File_Count(); i++)
525 CSG_Grid_File_Info Info;
527 if( !Info.
Create(Stream) )
546 if( Stream.Get_File(File +
"prj") )
553 return( _Memory_Create(bCached) );
556 if( Stream.Get_File(File +
"mgrd") )
567 return( Stream.Get_File(File +
"sdat") && _Memory_Create(bCached)
573bool CSG_Grid::_Save_Compressed(
const CSG_String &_File)
575#ifdef WORDS_BIGENDIAN
576 bool bBigEndian =
true;
578 bool bBigEndian =
false;
587 CSG_Grid_File_Info Info(*
this);
589 if( Stream.Add_File(File +
"sgrd") && Info.
Save(Stream,
true)
590 && Stream.Add_File(File +
"sdat") && _Save_Binary(Stream, m_Type,
false, bBigEndian) )
594 Stream.Add_File(File +
"sdat.aux.xml"); Info.
Save_AUX_XML(Stream);
611void CSG_Grid::_Swap_Bytes(
char *Bytes,
int nBytes)
const
615 uint16_t val, valSwapped;
616 memcpy(&val, Bytes, nBytes);
617 valSwapped = ntohs(val);
618 memcpy(Bytes, &valSwapped, nBytes);
620 else if( nBytes == 4 )
622 uint32_t val, valSwapped;
623 memcpy(&val, Bytes, nBytes);
624 valSwapped = ntohl(val);
625 memcpy(Bytes, &valSwapped, nBytes);
642 int nLineBytes =
Get_NX() / 8 + 1;
644 if( m_Type == File_Type && !
is_Cached() )
648 Stream.
Read(m_Values[bFlip ?
Get_NY() - y - 1 : y],
sizeof(
char), nLineBytes);
653 CSG_Array Line(1, nLineBytes);
657 Stream.
Read(Line.Get_Array(), nLineBytes);
659 char *pValue = (
char *)Line.Get_Array();
661 for(
int x=0, yy=bFlip ?
Get_NY()-y-1 : y; x<
Get_NX(); pValue++)
663 for(
int i=0; i<8 && x<
Get_NX(); i++, x++)
665 Set_Value(x, yy, (*pValue & m_Bitmask[i]) == 0 ? 0.0 : 1.0);
676 int nLineBytes =
Get_NX() * nValueBytes;
678 if( m_Type == File_Type && !
is_Cached() && !bSwapBytes )
682 Stream.
Read(m_Values[bFlip ?
Get_NY() - y - 1 : y], nLineBytes);
687 CSG_Array Line(1, nLineBytes);
691 Stream.
Read(Line.Get_Array(), nLineBytes);
693 char *pValue = (
char *)Line.Get_Array();
695 for(
int x=0, yy=bFlip ?
Get_NY()-y-1 : y; x<
Get_NX(); x++, pValue+=nValueBytes)
699 _Swap_Bytes(pValue, nValueBytes);
737 int nLineBytes =
Get_NX() / 8 + 1;
739 if( m_Type == File_Type && !
is_Cached() )
743 Stream.
Write((
char *)m_Values[bFlip ?
Get_NY() - y - 1 : y],
sizeof(
char), nLineBytes);
748 CSG_Array Line(1, nLineBytes);
752 char *pValue = (
char *)Line.Get_Array();
754 for(
int x=0, yy=bFlip ?
Get_NY()-y-1 : y; x<
Get_NX(); pValue++)
756 for(
int i=0; i<8 && x<
Get_NX(); i++, x++)
758 *pValue =
asChar(x, yy) != 0.0 ? *pValue | m_Bitmask[i] : *pValue & (~m_Bitmask[i]);
762 Stream.
Write(Line.Get_Array(), nLineBytes);
771 int nLineBytes =
Get_NX() * nValueBytes;
773 if( m_Type == File_Type && !
is_Cached() && !bSwapBytes )
777 Stream.
Write((
char *)m_Values[bFlip ?
Get_NY() - y - 1 : y], nLineBytes);
782 CSG_Array Line(1, nLineBytes);
786 char *pValue = (
char *)Line.Get_Array();
788 for(
int x=0, yy=bFlip ?
Get_NY()-y-1 : y; x<
Get_NX(); x++, pValue+=nValueBytes)
805 _Swap_Bytes(pValue, nValueBytes);
809 Stream.
Write(Line.Get_Array(), nLineBytes);
826bool CSG_Grid::_Load_ASCII(
CSG_File &Stream,
bool bCached,
bool bFlip)
828 if( !Stream.
is_Reading() || !_Memory_Create(bCached) )
837 for(
int x=0, yy=bFlip ?
Get_NY()-y-1 : y; x<
Get_NX(); x++)
847bool CSG_Grid::_Save_ASCII(
CSG_File &Stream,
bool bFlip)
858 for(
int x=0, yy=bFlip ?
Get_NY()-y-1 : y; x<
Get_NX(); x++)
875bool CSG_Grid::_Load_Surfer(
const CSG_String &File,
bool bCached,
bool bLoadData)
877 const float NoData = 1.70141e38f;
892 char Identifier[4]; Stream.
Read(Identifier,
sizeof(
char), 4);
895 if( !strncmp(Identifier,
"DSBB", 4) )
897 short nx, ny;
double d; CSG_Rect r;
899 Stream.
Read(&nx ,
sizeof(
short ));
900 Stream.
Read(&ny ,
sizeof(
short ));
901 Stream.
Read(&r.
xMin,
sizeof(
double));
902 Stream.
Read(&r.
xMax,
sizeof(
double));
903 Stream.
Read(&r.
yMin,
sizeof(
double));
904 Stream.
Read(&r.
yMax,
sizeof(
double));
905 Stream.
Read(&d ,
sizeof(
double));
906 Stream.
Read(&d ,
sizeof(
double));
920 CSG_Array Line(
sizeof(
float),
Get_NX());
float *Values = (
float *)Line.Get_Array();
926 for(
int x=0; x<
Get_NX(); x++)
928 if( Values[x] == NoData )
944 else if( !strncmp(Identifier,
"DSAA", 4) )
955 dx = (xMax - xMin) / (nx - 1.);
969 for(
int x=0; x<
Get_NX(); x++)
973 if( Stream.
Scan(Value) && Value != NoData )
1015void CSG_Grid_File_Info::_On_Construction(
void)
1100 return(
Create(Stream) );
1113 if( !Stream.
Get_File(Archive_File +
"sgrd" )
1114 && !Stream.
Get_File(Archive_File +
"sg-grd") )
1127 return(
Create(Stream) );
1151 sLong NX = 0, NY = 0;
1152 double Cellsize = 0.0, xMin = 0.0, yMin = 0.0;
1158 switch( _Get_Key(Stream, Value) )
1205 while( !Stream.
is_EOF() );
1208 return(
m_System.Assign(Cellsize, xMin, yMin, (
int)NX, (
int)NY) );
1228 if( s.Find(sLine.
Left(s.Length())) >= 0 )
1257#define GRID_FILE_PRINT(Key, Val) { CSG_String s(gSG_Grid_File_Key_Names[Key]); s += "\t= " + Val + "\n"; Stream.Write(s); }
1264#ifdef WORDS_BIGENDIAN
1287 return( Info.
Save(File, bBinary) );
1295 return( Info.
Save(Stream, bBinary) );
1316 Stream.
Write(
"<PAMDataset>\n<SRS>");
1318 Stream.
Write(
"</SRS>\n</PAMDataset>\n");
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)
bool is_Cached(void) const
double Get_Offset(void) const
virtual int asInt(int x, int y, bool bScaled=true) const
virtual void Set_NoData(int x, int y)
virtual char asChar(int x, int y, bool bScaled=true) const
double Get_Scaling(void) const
virtual short asShort(int x, int y, bool bScaled=true) const
virtual void Set_Modified(bool bModified=true)
virtual bool is_Valid(void) const
const CSG_String & Get_Unit(void) const
virtual double asDouble(sLong i, bool bScaled=true) const
virtual BYTE asByte(int x, int y, bool bScaled=true) const
virtual bool Save(const char *File, int Format=0)
virtual float asFloat(int x, int y, bool bScaled=true) const
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 void Set_Value(sLong i, double Value, bool bScaled=true)
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
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
const SG_Char * c_str(void) const
bool is_Empty(void) const
CSG_String Left(size_t count) const
double asDouble(void) 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