62BYTE CSG_Grid::m_Bitmask[8] = { 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80 };
82 if( !pGrid->
is_Valid() ) {
delete(pGrid);
return( NULL ); }
return( pGrid );
92 if( pGrid->
Create(File, Type, bCached, bLoadData) )
97 delete(pGrid);
return( NULL );
103 pGrid =
new CSG_Grid(pGrid, Type, bCached);
105 if( !pGrid->
is_Valid() ) {
delete(pGrid);
return( NULL ); }
return( pGrid );
113 if( !pGrid->
is_Valid() ) {
delete(pGrid);
return( NULL ); }
return( pGrid );
119 CSG_Grid *pGrid =
new CSG_Grid(Type, NX, NY, Cellsize > 0. ? Cellsize : 1., xMin, yMin, bCached);
121 if( !pGrid->
is_Valid() ) {
delete(pGrid);
return( NULL ); }
return( pGrid );
150 _On_Construction();
Create(Grid);
163 _On_Construction();
Create(File, Type, bCached, bLoadData);
174 _On_Construction();
Create(pGrid, Type, bCached);
185 _On_Construction();
Create(System, Type, bCached);
199 _On_Construction();
Create(Type, NX, NY, Cellsize, xMin, yMin, bCached);
208void CSG_Grid::_On_Construction(
void)
214 m_Cache_Stream = NULL;
216 m_Cache_bSwap =
false;
217 m_Cache_bFlip =
false;
247 #pragma omp parallel for
248 for(
int y=0; y<
Get_NY(); y++)
for(
int x=0; x<
Get_NX(); x++)
281 switch( (m_Type = Type) )
301 if( m_System.Create(System) ==
false )
306 m_Statistics.Invalidate();
313 return( _Memory_Create(bCached) );
327 if( _Load_PGSQL (File, bCached, bLoadData)
328 || _Load_Native (File, bCached, bLoadData)
329 || _Load_Compressed(File, bCached, bLoadData)
330 || _Load_Surfer (File, bCached, bLoadData)
331 || _Load_External (File, bCached, bLoadData) )
408 if( (Scale != m_zScale && Scale != 0.) || Offset != m_zOffset )
445 return( m_Values != NULL ||
is_Cached() );
475 return( m_System == System );
495 if( y >= 0 && y <
Get_NY() )
499 for(
int x=0; x<
Get_NX(); x++)
513 for(
int x=0; x<
Get_NX(); x++)
548 return(
Get_Value(p.
x, p.
y, Value, Resampling, bNoData, bByteWise) );
554 if( m_System.Get_Extent(
true).Contains(x, y) )
559 if( bNoData ||
is_InGrid(ix + (
int)(0.5 + dx), iy + (
int)(0.5 + dy)) )
582inline bool CSG_Grid::_Get_ValAtPos_NearestNeighbour(
double &Value,
int x,
int y,
double dx,
double dy)
const
584 if(
is_InGrid(x = x + (
int)(0.5 + dx), y = y + (
int)(0.5 + dy)) )
600inline bool CSG_Grid::_Get_ValAtPos_BiLinear(
double &Value,
int x,
int y,
double dx,
double dy,
bool bByteWise)
const
604 double z = 0., n = 0.;
606 #define BILINEAR_ADD(ix, iy, d) if( is_InGrid(ix, iy) ) { n += d; z += d * asDouble(ix, iy); }
626 double n = 0.; CSG_Vector z(4);
628 #define BILINEAR_ADD(ix, iy, d) if( is_InGrid(ix, iy) ) { n += d; int v = asInt(ix, iy);\
629 z[0] += d * SG_GET_BYTE_0(v);\
630 z[1] += d * SG_GET_BYTE_1(v);\
631 z[2] += d * SG_GET_BYTE_2(v);\
632 z[3] += d * SG_GET_BYTE_3(v);\
646 BYTE Bytes[4] = { (BYTE)z[0], (BYTE)z[1], (BYTE)z[2], (BYTE)z[3] };
648 Value = *(
int *)Bytes;
663inline double CSG_Grid::_Get_ValAtPos_BiCubic_1(
double dx,
double dy,
double v_xy[4][4])
const
665 #define BiCubicSpline(d, v) (v[1] + 0.5 * d * (v[2] - v[0] + d * (2 * v[0] - 5 * v[1] + 4 * v[2] - v[3] + d * (3 * (v[1] - v[2]) + v[3] - v[0]))))
669 for(
int ix=0; ix<4; ix++)
678inline bool CSG_Grid::_Get_ValAtPos_BiCubic_1(
double &Value,
int x,
int y,
double dx,
double dy,
bool bByteWise)
const
684 if( _Get_ValAtPos_Fill4x4Submatrix(x, y, v_xy) )
686 Value = _Get_ValAtPos_BiCubic_1(dx, dy, v_xy);
693 double v_xy[4][4][4];
695 if( _Get_ValAtPos_Fill4x4Submatrix(x, y, v_xy) )
699 #define SET_BYTE(b, z) { double d = z; b = (BYTE)(d < 0. ? 0. : d < 255. ? d : 255.); }
701 SET_BYTE(Bytes[0], _Get_ValAtPos_BiCubic_1(dx, dy, v_xy[0]));
702 SET_BYTE(Bytes[1], _Get_ValAtPos_BiCubic_1(dx, dy, v_xy[1]));
703 SET_BYTE(Bytes[2], _Get_ValAtPos_BiCubic_1(dx, dy, v_xy[2]));
704 SET_BYTE(Bytes[3], _Get_ValAtPos_BiCubic_1(dx, dy, v_xy[3]));
706 Value = *(
int *)Bytes;
721inline double CSG_Grid::_Get_ValAtPos_BiCubic_2(
double dx,
double dy,
double v_xy[4][4])
const
725 for(
int i=0; i<4; i++)
730 if( (d = i - dx + 1.) > 0. ) s += d*d*d;
731 if( (d = i - dx + 0.) > 0. ) s += -4. * d*d*d;
732 if( (d = i - dx - 1.) > 0. ) s += 6. * d*d*d;
733 if( (d = i - dx - 2.) > 0. ) s += -4. * d*d*d;
738 if( (d = i - dy + 1.) > 0. ) s += d*d*d;
739 if( (d = i - dy + 0.) > 0. ) s += -4. * d*d*d;
740 if( (d = i - dy - 1.) > 0. ) s += 6. * d*d*d;
741 if( (d = i - dy - 2.) > 0. ) s += -4. * d*d*d;
748 for(
int iy=0; iy<4; iy++)
750 for(
int ix=0; ix<4; ix++)
752 z += v_xy[ix][iy] * Rx[ix] * Ry[iy];
760inline bool CSG_Grid::_Get_ValAtPos_BiCubic_2(
double &Value,
int x,
int y,
double dx,
double dy,
bool bByteWise)
const
766 if( _Get_ValAtPos_Fill4x4Submatrix(x, y, v_xy) )
768 Value = _Get_ValAtPos_BiCubic_2(dx, dy, v_xy);
775 double v_xy[4][4][4];
777 if( _Get_ValAtPos_Fill4x4Submatrix(x, y, v_xy) )
780 (BYTE)_Get_ValAtPos_BiCubic_2(dx, dy, v_xy[0]),
781 (BYTE)_Get_ValAtPos_BiCubic_2(dx, dy, v_xy[1]),
782 (BYTE)_Get_ValAtPos_BiCubic_2(dx, dy, v_xy[2]),
783 (BYTE)_Get_ValAtPos_BiCubic_2(dx, dy, v_xy[3])
786 Value = *(
int *)Bytes;
801inline bool CSG_Grid::_Get_ValAtPos_Fill4x4Submatrix(
int x,
int y,
double v_xy[4][4])
const
803 int ix, iy, jx, jy, nNoData = 0;
806 for(iy=0, jy=y-1; iy<4; iy++, jy++)
808 for(ix=0, jx=x-1; ix<4; ix++, jx++)
824 for(
int i=0; nNoData>0 && nNoData<16 && i<16; i++)
828 for(iy=0; iy<4; iy++)
for(ix=0; ix<4; ix++)
830 t_xy[ix][iy] = v_xy[ix][iy];
833 for(iy=0; iy<4; iy++)
for(ix=0; ix<4; ix++)
840 for(jy=iy-1; jy<=iy+1; jy++)
for(jx=ix-1; jx<=ix+1; jx++)
844 s +=
asDouble(jx + x - 1, jy + y - 1);
847 else if( jy >= 0 && jy < 4 && jx >= 0 && jx < 4 && !
is_NoData_Value(t_xy[jx][jy]) )
856 v_xy[ix][iy] = s / n;
865 return( nNoData == 0 );
869inline bool CSG_Grid::_Get_ValAtPos_Fill4x4Submatrix(
int x,
int y,
double v_xy[4][4][4])
const
871 int ix, iy, jx, jy, nNoData = 0;
874 for(iy=0, jy=y-1; iy<4; iy++, jy++)
876 for(ix=0, jx=x-1; ix<4; ix++, jx++)
880 int z =
asInt(jx, jy);
889 v_xy[0][ix][iy] = -1;
897 for(
int i=0; nNoData>0 && nNoData<16 && i<16; i++)
899 double t_xy[4][4][4];
901 for(iy=0; iy<4; iy++)
for(ix=0; ix<4; ix++)
903 t_xy[0][ix][iy] = v_xy[0][ix][iy];
904 t_xy[1][ix][iy] = v_xy[1][ix][iy];
905 t_xy[2][ix][iy] = v_xy[2][ix][iy];
906 t_xy[3][ix][iy] = v_xy[3][ix][iy];
909 for(iy=0; iy<4; iy++)
for(ix=0; ix<4; ix++)
911 if( t_xy[0][ix][iy] < 0 )
914 double s[4]; s[0] = s[1] = s[2] = s[3] = 0.;
916 for(jy=iy-1; jy<=iy+1; jy++)
for(jx=ix-1; jx<=ix+1; jx++)
920 int z =
asInt(jx + x - 1, jy + y - 1);
928 else if( jy >= 0 && jy < 4 && jx >= 0 && jx < 4 && !
is_NoData_Value(t_xy[0][jx][jy]) )
930 s[0] += t_xy[0][jx][jy];
931 s[1] += t_xy[1][jx][jy];
932 s[2] += t_xy[2][jx][jy];
933 s[3] += t_xy[3][jx][jy];
940 v_xy[0][ix][iy] = s[0] / n;
941 v_xy[1][ix][iy] = s[1] / n;
942 v_xy[2][ix][iy] = s[2] / n;
943 v_xy[3][ix][iy] = s[3] / n;
952 return( nNoData == 0 );
972 m_Statistics.Invalidate();
973 m_Histogram.Destroy();
987 m_Statistics += Scaling ? Offset + Scaling * Value : Value;
999 for(
int x=0; x<
Get_NX(); x++)
1001 double Value =
asDouble(x, y,
false);
1005 m_Statistics += Scaling ? Offset + Scaling * Value : Value;
1019 Update();
return( m_Statistics.Get_Mean() );
1024 Update();
return( m_Statistics.Get_Minimum() );
1029 Update();
return( m_Statistics.Get_Maximum() );
1034 Update();
return( m_Statistics.Get_Range() );
1039 Update();
return( m_Statistics.Get_StdDev() );
1044 Update();
return( m_Statistics.Get_Variance() );
1050 Update();
return( m_Statistics.Get_Count() );
1061 if( Quantile <= 0. ) {
return(
Get_Min() ); }
1062 if( Quantile >= 1. ) {
return(
Get_Max() ); }
1064 if( bFromHistogram )
1084 return(
Get_Quantile(0.01 * Percentile, bFromHistogram) );
1101 Update();
return( m_Statistics );
1118 if( xMin > xMax || yMin > yMax )
1123 Statistics.
Create(bHoldValues);
1125 int nx = 1 + (xMax - xMin);
1126 int ny = 1 + (yMax - yMin);
1127 sLong nCells = nx * ny;
1135 for(
double i=0; i<(double)nCells; i+=d)
1137 int y = yMin + (int)i / nx;
1138 int x = xMin + (int)i % nx;
1140 double Value =
asDouble(x, y,
false);
1144 Statistics += Scaling ? Offset + Scaling * Value : Value;
1150 for(
int y=yMin; y<=yMax; y++)
1152 for(
int x=xMin; x<=xMax; x++)
1154 double Value =
asDouble(x, y,
false);
1158 Statistics += Scaling ? Offset + Scaling * Value : Value;
1168#define SG_GRID_HISTOGRAM_CLASSES_DEFAULT 256
1179 if( nClasses > 1 && nClasses != m_Histogram.Get_Class_Count() )
1181 m_Histogram.Destroy();
1184 if( m_Histogram.Get_Statistics().Get_Count() < 1 )
1189 return( m_Histogram );
1207 if( xMin > xMax || yMin > yMax )
1214 int nx = 1 + (xMax - xMin);
1215 int ny = 1 + (yMax - yMin);
1224 for(
double i=0; i<(double)nCells; i+=d)
1226 int y = yMin + (int)i / nx;
1227 int x = xMin + (int)i % nx;
1229 double Value =
asDouble(x, y,
false);
1233 Histogram += Scaling ? Offset + Scaling * Value : Value;
1239 for(
int y=yMin; y<=yMax; y++)
for(
int x=xMin; x<=xMax; x++)
1241 double Value =
asDouble(x, y,
false);
1245 Histogram += Scaling ? Offset + Scaling * Value : Value;
1250 return( Histogram.
Update() );
1261bool CSG_Grid::_Set_Index(
void)
1273 sLong i, j, k, l, ir, n, *istack, jstack, nstack, indxt, itemp, nData;
1283 m_Index[--nData] = i;
1322 for(j=l+1; j<=ir; j++)
1327 for(i=j-1; i>=0; i--)
1334 m_Index[i + 1] = m_Index[i];
1337 m_Index[i + 1] = indxt;
1345 ir = istack[jstack--];
1346 l = istack[jstack--];
1352 #define SORT_SWAP(a,b) {itemp=(a);(a)=(b);(b)=itemp;}
1374 do i++;
while(
asDouble(m_Index[i]) < a);
1375 do j--;
while(
asDouble(m_Index[j]) > a);
1387 m_Index[l] = m_Index[j];
1391 if( jstack >= nstack )
1397 if( ir - i + 1 >= j - l )
1399 istack[jstack] = ir;
1400 istack[jstack - 1] = i;
1405 istack[jstack] = j - 1;
1406 istack[jstack - 1] = l;
void SG_UI_Msg_Add_Error(const char *Message)
void SG_UI_Msg_Add(const char *Message, bool bNewLine, TSG_UI_MSG_STYLE Style)
void SG_UI_Process_Set_Text(const CSG_String &Text)
bool SG_UI_Process_Set_Ready(void)
bool SG_UI_Process_Set_Progress(int Position, int Range)
@ SG_UI_MSG_STYLE_FAILURE
@ SG_UI_MSG_STYLE_SUCCESS
#define SG_GET_BYTE_1(vLong)
SAGA_API_DLL_EXPORT void * SG_Malloc(size_t size)
SAGA_API_DLL_EXPORT void SG_Free(void *memblock)
SAGA_API_DLL_EXPORT void * SG_Realloc(void *memblock, size_t size)
#define SG_GET_BYTE_3(vLong)
#define SG_FREE_SAFE(PTR)
#define SG_GET_BYTE_2(vLong)
size_t SG_Data_Type_Get_Size(TSG_Data_Type Type)
#define SG_GET_BYTE_0(vLong)
const SG_Char * Get_Name(void) const
CSG_Data_Object * m_pOwner
void Set_Name(const CSG_String &Name)
bool is_NoData_Value(double Value) const
void Set_Update_Flag(bool bOn=true)
virtual bool Set_Max_Samples(sLong Max_Samples)
double Get_NoData_Value(bool bUpper=false) const
CSG_MetaData & Get_MetaData(void) const
virtual bool Set_NoData_Value_Range(double Lower, double Upper)
bool Update(bool bForce=false)
virtual bool Destroy(void)
void Set_Description(const CSG_String &Description)
virtual bool Set_NoData_Value(double Value)
const SG_Char * Get_Description(void) const
sLong Get_Max_Samples(void) const
CSG_Projection & Get_Projection(void)
double Get_XMin(bool bCells=false) const
double Get_YMin(bool bCells=false) const
int Get_xWorld_to_Grid(double xWorld) const
int Get_yWorld_to_Grid(double yWorld) const
const CSG_Simple_Statistics & Get_Statistics(void)
double Get_YMin(bool bCells=false) const
bool is_Cached(void) const
virtual bool Destroy(void)
double Get_Offset(void) const
TSG_Intersection is_Intersecting(const CSG_Rect &Extent) const
virtual int asInt(int x, int y, bool bScaled=true) const
double Get_Quantile(double Quantile, bool bFromHistogram=true)
double Get_XMin(bool bCells=false) const
const CSG_Histogram & Get_Histogram(size_t nClasses=0)
CSG_Vector Get_Row(int y) const
double Get_Cellsize(void) const
double Get_Scaling(void) const
virtual bool On_Update(void)
double Get_Percentile(double Percentile, bool bFromHistogram=true)
virtual bool is_NoData(int x, int y) const
virtual void Set_Modified(bool bModified=true)
bool Set_Row(int y, const CSG_Vector &Values)
sLong Get_Sorted(sLong Position, bool bDown=true, bool bCheckNoData=true)
virtual bool is_Valid(void) const
const CSG_String & Get_Unit(void) const
virtual double asDouble(sLong i, bool bScaled=true) const
double Get_Variance(void)
sLong Get_Data_Count(void)
sLong Get_NCells(void) const
void Set_Scaling(double Scale=1., double Offset=0.)
bool Create(const CSG_Grid &Grid)
const CSG_Grid_System & Get_System(void) const
double Get_Value(double x, double y, CSG_Grid_Resampling Resampling=CSG_Grid_Resampling::Bicubic_2, bool bByteWise=false) const
void Set_Unit(const CSG_String &Unit)
virtual const CSG_Rect & Get_Extent(void)
sLong Get_NoData_Count(void)
bool is_InGrid(int x, int y, bool bCheckNoData=true) const
bool is_Scaled(void) const
virtual void Set_Value(sLong i, double Value, bool bScaled=true)
bool is_Compatible(CSG_Grid *pGrid) const
TSG_Data_Type Get_Type(void) const
bool Create(const CSG_Histogram &Histogram)
bool Create(const CSG_Projection &Projection)
double Get_XMax(void) const
double Get_XMin(void) const
double Get_YMin(void) const
double Get_YMax(void) const
bool Create(bool bHoldValues=false)
sLong Get_Count(void) const
static CSG_String Format(const char *Format,...)
const SG_Char * c_str(void) const
bool Create(const CSG_Vector &Vector)
sLong SG_DataObject_Get_Max_Samples(void)
#define SG_GRID_HISTOGRAM_CLASSES_DEFAULT
#define BiCubicSpline(d, v)
#define BILINEAR_ADD(ix, iy, d)
CSG_Grid * SG_Create_Grid(void)