//--------------------------------------------------------- bool CSG_Grid::Assign(double Value) { if( is_Valid() ) { if( Value == 0.0 && m_Memory_Type == GRID_MEMORY_Normal ) { for(int n=0, m=_Get_nLineBytes(); n<Get_NY(); n++) { memset(m_Values[n], 0, m); } } else { for(int n=0; n<Get_NCells(); n++) { Set_Value(n, Value); } } //------------------------------------------------- Get_History().Destroy(); Get_History().Add_Child(SG_T("GRID_OPERATION"), Value)->Add_Property(SG_T("NAME"), LNG("Assign")); //------------------------------------------------- m_zStats.Invalidate(); Set_Update_Flag(false); return( true ); } return( false ); }
//--------------------------------------------------------- void CSG_Grid::Invert(void) { int x, y; double zMin, zMax; if( is_Valid() && Get_ZRange() > 0.0 ) { zMin = Get_ZMin(); zMax = Get_ZMax(); for(y=0; y<Get_NY() && SG_UI_Process_Set_Progress(y, Get_NY()); y++) { for(x=0; x<Get_NX(); x++) { if( !is_NoData(x, y) ) { Set_Value(x, y, zMax - (asDouble(x, y) - zMin)); } } } SG_UI_Process_Set_Ready(); Get_History().Add_Child(SG_T("GRID_OPERATION"), LNG("Inversion")); } }
bool CSG_Grid::Assign(CSG_Grid *pGrid, TSG_Grid_Interpolation Interpolation) { bool bResult = false; //----------------------------------------------------- if( is_Valid() && pGrid && pGrid->is_Valid() && is_Intersecting(pGrid->Get_Extent()) != INTERSECTION_None ) { if( Get_Cellsize() == pGrid->Get_Cellsize() // No-Scaling... && fmod(Get_XMin() - pGrid->Get_XMin(), Get_Cellsize()) == 0.0 && fmod(Get_YMin() - pGrid->Get_YMin(), Get_Cellsize()) == 0.0 ) { bResult = _Assign_Interpolated (pGrid, GRID_INTERPOLATION_NearestNeighbour); } else switch( Interpolation ) { case GRID_INTERPOLATION_NearestNeighbour: case GRID_INTERPOLATION_Bilinear: case GRID_INTERPOLATION_InverseDistance: case GRID_INTERPOLATION_BicubicSpline: case GRID_INTERPOLATION_BSpline: bResult = _Assign_Interpolated (pGrid, Interpolation); break; case GRID_INTERPOLATION_Mean_Nodes: case GRID_INTERPOLATION_Mean_Cells: bResult = _Assign_MeanValue (pGrid, Interpolation != GRID_INTERPOLATION_Mean_Nodes); break; case GRID_INTERPOLATION_Minimum: case GRID_INTERPOLATION_Maximum: bResult = _Assign_ExtremeValue (pGrid, Interpolation == GRID_INTERPOLATION_Maximum); break; default: if( Get_Cellsize() < pGrid->Get_Cellsize() ) // Down-Scaling... { bResult = _Assign_Interpolated (pGrid, GRID_INTERPOLATION_BSpline); } else // Up-Scaling... { bResult = _Assign_MeanValue (pGrid, Interpolation != GRID_INTERPOLATION_Mean_Nodes); } break; } //------------------------------------------------- if( bResult ) { // Set_Name (pGrid->Get_Name()); Set_Description (pGrid->Get_Description()); Set_Unit (pGrid->Get_Unit()); Set_ZFactor (pGrid->Get_ZFactor()); Set_NoData_Value_Range (pGrid->Get_NoData_Value(), pGrid->Get_NoData_hiValue()); } } //----------------------------------------------------- return( bResult ); }
//------------------------------Socket----------------------------------- int Socket::read(char* pBuf, int p_iMax_Len,int p_iMin_Len,int p_itime_out) { int iLeft = p_iMax_Len,i_timeleft=p_itime_out; time_t t_start = time(NULL); int i; while( iLeft > 0 ) { if(!is_Valid()) { //cout<<"socket is not Valid : "<<m_nSock<<endl; return -1; } for(;i_timeleft >0;) { fd_set events; struct timeval tm; FD_ZERO(&events); FD_SET(m_nSock, &events); tm.tv_sec = i_timeleft; tm.tv_usec = 0; while ((i = select(m_nSock+1, &events, NULL, NULL, &tm)) < 0 && errno == EINTR); if( i < 0 ) { //cout<<"select return "<<i<<endl; return -1; } i_timeleft = t_start+p_itime_out - time(NULL); if(FD_ISSET(m_nSock,&events)) break; //if(i==0&&is_Bad()) return -1; }; i = ::recv(m_nSock, pBuf, iLeft,0); if( i < 0 ) { cout<<"recv return "<<i<<endl; perror("recv:"); return -1; } iLeft -= i; pBuf += i; //printf("i_timeleft = %d \n",i_timeleft); if((p_iMax_Len - iLeft >= p_iMin_Len) || (i_timeleft <= 0)) break; } return ( p_iMax_Len - iLeft); }
//--------------------------------------------------------- void CSG_Grid::Normalise(void) { if( is_Valid() ) { Update(); if( m_zStats.Get_StdDev() > 0.0 ) { int x, y; if( (Get_NoData_hiValue() > -NORMALISED_NODATA && Get_NoData_hiValue() < NORMALISED_NODATA) || (Get_NoData_Value () > -NORMALISED_NODATA && Get_NoData_Value () < NORMALISED_NODATA) ) { for(y=0; y<Get_NY() && SG_UI_Process_Set_Progress(y, Get_NY()); y++) { for(x=0; x<Get_NX(); x++) { if( is_NoData(x, y) ) { Set_Value(x, y, -NORMALISED_NODATA); } } } Set_NoData_Value(-NORMALISED_NODATA); } for(y=0; y<Get_NY() && SG_UI_Process_Set_Progress(y, Get_NY()); y++) { for(x=0; x<Get_NX(); x++) { if( !is_NoData(x, y) ) { Set_Value(x, y, (asDouble(x, y) - m_zStats.Get_Mean()) / m_zStats.Get_StdDev() ); } } } SG_UI_Process_Set_Ready(); Get_History().Add_Child(SG_T("GRID_OPERATION"), LNG("Normalisation")); } } }
//--------------------------------------------------------- void CSG_Grid::Mirror(void) { int xA, xB, y; double d; if( is_Valid() ) { for(y=0; y<Get_NY() && SG_UI_Process_Set_Progress(y, Get_NY()); y++) { for(xA=0, xB=Get_NX()-1; xA<xB; xA++, xB--) { d = asDouble(xA, y); Set_Value(xA, y, asDouble(xB, y)); Set_Value(xB, y, d); } } SG_UI_Process_Set_Ready(); Get_History().Add_Child(SG_T("GRID_OPERATION"), LNG("Horizontally mirrored")); } }
//--------------------------------------------------------- bool CSG_Grid::_Save_ASCII(CSG_File &Stream, int xA, int yA, int xN, int yN, bool bFlip) { int x, y, ix, iy, dy; if( Stream.is_Open() && is_Valid() ) { Set_File_Type(GRID_FILE_FORMAT_ASCII); if( bFlip ) { y = yA + yN - 1; dy = -1; } else { y = yA; dy = 1; } //------------------------------------------------- for(iy=0; iy<yN && SG_UI_Process_Set_Progress(iy, yN); iy++, y+=dy) { for(ix=0, x=xA; ix<xN; ix++, x++) { Stream.Printf(SG_T("%lf "), asDouble(x, y)); } Stream.Printf(SG_T("\n")); } SG_UI_Process_Set_Ready(); return( true ); } return( false ); }
//--------------------------------------------------------- void CSG_Grid::DeNormalise(double ArithMean, double Variance) { int x, y; if( is_Valid() ) { Variance = sqrt(Variance); for(y=0; y<Get_NY() && SG_UI_Process_Set_Progress(y, Get_NY()); y++) { for(x=0; x<Get_NX(); x++) { if( !is_NoData(x, y) ) { Set_Value(x, y, Variance * asDouble(x, y) + ArithMean); } } } SG_UI_Process_Set_Ready(); Get_History().Add_Child(SG_T("GRID_OPERATION"), LNG("Denormalisation")); } }
//--------------------------------------------------------- void CSG_Grid::Flip(void) { int x, yA, yB; double *Line, d; if( is_Valid() ) { Line = (double *)SG_Malloc(Get_NX() * sizeof(double)); for(yA=0, yB=Get_NY()-1; yA<yB && SG_UI_Process_Set_Progress(2 * yA, Get_NY()); yA++, yB--) { for(x=0; x<Get_NX(); x++) { Line[x] = asDouble(x, yA); } for(x=0; x<Get_NX(); x++) { d = Line[x]; Line[x] = asDouble(x, yB); Set_Value(x, yB, d); } for(x=0; x<Get_NX(); x++) { Set_Value(x, yA, Line[x]); } } SG_UI_Process_Set_Ready(); SG_Free(Line); Get_History().Add_Child(SG_T("GRID_OPERATION"), LNG("Vertically mirrored")); } }
//--------------------------------------------------------- bool CSG_Grid::_Load_Binary(CSG_File &Stream, TSG_Data_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 == SG_DATATYPE_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 = SG_Data_Type_Get_Size(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 SG_DATATYPE_Byte: Set_Value(x, y, *(BYTE *)pValue); break; case SG_DATATYPE_Char: Set_Value(x, y, *(char *)pValue); break; case SG_DATATYPE_Word: Set_Value(x, y, *(WORD *)pValue); break; case SG_DATATYPE_Short: Set_Value(x, y, *(short *)pValue); break; case SG_DATATYPE_DWord: Set_Value(x, y, *(DWORD *)pValue); break; case SG_DATATYPE_Int: Set_Value(x, y, *(int *)pValue); break; case SG_DATATYPE_Float: Set_Value(x, y, *(float *)pValue); break; case SG_DATATYPE_Double: Set_Value(x, y, *(double *)pValue); break; } } } SG_Free(Line); } } //------------------------------------------------- SG_UI_Process_Set_Ready(); return( true ); } return( false ); }