//--------------------------------------------------------- bool CFilter_Resample::On_Execute(void) { double Cellsize; CSG_Grid *pGrid, *pLoPass, *pHiPass; //----------------------------------------------------- pGrid = Parameters("GRID" )->asGrid(); pLoPass = Parameters("LOPASS")->asGrid(); pHiPass = Parameters("HIPASS")->asGrid(); Cellsize = Parameters("SCALE" )->asDouble() * Get_Cellsize(); //----------------------------------------------------- if( Cellsize > 0.5 * SG_Get_Length(Get_System()->Get_XRange(), Get_System()->Get_YRange()) ) { Error_Set(_TL("resampling cell size is too large")); return( false ); } //----------------------------------------------------- CSG_Grid Grid(CSG_Grid_System(Cellsize, Get_XMin(), Get_YMin(), Get_XMax(), Get_YMax()), SG_DATATYPE_Float); Grid.Assign(pGrid, GRID_RESAMPLING_Mean_Cells); //----------------------------------------------------- pLoPass->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pGrid->Get_Name(), _TL("Low Pass"))); pHiPass->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pGrid->Get_Name(), _TL("High Pass"))); CSG_Colors Colors; DataObject_Get_Colors(pGrid , Colors); DataObject_Set_Colors(pLoPass, Colors); DataObject_Set_Colors(pHiPass, 11, SG_COLORS_RED_GREY_BLUE); //----------------------------------------------------- for(int y=0; y<Get_NY() && Set_Progress(y); y++) { double py = Get_YMin() + y * Get_Cellsize(); #pragma omp parallel for for(int x=0; x<Get_NX(); x++) { double z, px = Get_XMin() + x * Get_Cellsize(); if( !pGrid->is_NoData(x, y) && Grid.Get_Value(px, py, z) ) { pLoPass->Set_Value(x, y, z); pHiPass->Set_Value(x, y, pGrid->asDouble(x, y) - z); } else { pLoPass->Set_NoData(x, y); pHiPass->Set_NoData(x, y); } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- double CGSGrid_Variance::Get_Steigung(void) { int i; double summe_mg, summe_g; //----------------------------------------------------- // Steigungen berechnen... m[0] = V[0] / Get_Cellsize(); for(i=1; i<maxRadius; i++) m[i] = (V[i] - V[i-1]) / Get_Cellsize(); //----------------------------------------------------- // Gewichte berechnen (inverse distance)... for(i=0; i<maxRadius; i++) g[i] = pow(Get_Cellsize() * (i + 1), -Exponent); //----------------------------------------------------- // Berechne Summe der gewichteten Steigungen und Summe der Gewichte... summe_mg = 0; summe_g = 0; for(i=0; i<maxRadius; i++) { summe_mg += m[i] * g[i]; summe_g += g[i]; } return( summe_mg / summe_g ); }
//--------------------------------------------------------- double CExercise_14::Vectorise(int x, int y, CSG_Shape *pSegment) { int Dir, ix, iy; double Length; Length = 0.0; pSegment->Add_Point(Get_XMin() + x * Get_Cellsize(), Get_YMin() + y * Get_Cellsize()); if( (Dir = m_pDir->asInt(x, y)) >= 0 ) { Length = Get_Length(Dir); ix = Get_xTo(Dir, x); iy = Get_yTo(Dir, y); switch( m_pChnl->asInt(ix, iy) ) { case CHANNEL: Length += Vectorise(ix, iy, pSegment); // recursive function call... break; case MOUTH: Length += Get_Length(Dir); pSegment->Add_Point(Get_XMin() + ix * Get_Cellsize(), Get_YMin() + iy * Get_Cellsize()); break; } } return( Length ); }
//--------------------------------------------------------- double CExercise_09::Get_Area(int x, int y) { int i, ix, iy; double area; //----------------------------------------------------- area = m_pArea->asDouble(x, y); if( area <= 0.0 ) // cell has not been processed yet... { m_pArea->Set_Value(x, y, 1.0); // Very important: mark this cell as processed to prevent endless loops... area = Get_Cellsize() * Get_Cellsize(); // initialize the cell's area with its own cell size... for(i=0; i<8; i++) { ix = Get_xFrom(i, x); iy = Get_yFrom(i, y); if( is_InGrid(ix, iy) && i == m_pDir->asInt(ix, iy) ) // drains ith neigbour into this cell ???... { area += Get_Area(ix, iy); // ...then add its area (recursive call of this function!)... } } m_pArea->Set_Value(x, y, area); } //----------------------------------------------------- return( area ); }
//--------------------------------------------------------- void CFlow_Parallel::BRM_Init(void) { int i; double DXT = Get_Cellsize()/2, DYT = Get_Cellsize()/2; //----------------------------------------------------- BRM_kgexp[0] = (int)(atan2(DXT , Get_Cellsize()) * M_RAD_TO_DEG); BRM_kgexp[1] = (int)(atan2(Get_Cellsize(), DYT ) * M_RAD_TO_DEG) + 1; BRM_kgexp[2] = (int)(atan2(Get_Cellsize(),-DYT ) * M_RAD_TO_DEG); BRM_kgexp[3] = (int)(atan2(DXT ,-Get_Cellsize()) * M_RAD_TO_DEG) + 1; for(i=0; i<4; i++) BRM_kgexp[i+4] = BRM_kgexp[i] + 180; //---BRM_idreh--------------------------------------------- BRM_idreh[0] = 180; BRM_idreh[1] = 180 - BRM_nint(atan2(Get_Cellsize(), Get_Cellsize()) * M_RAD_TO_DEG); BRM_idreh[2] = 90; BRM_idreh[3] = BRM_nint(atan2(Get_Cellsize(), Get_Cellsize()) * M_RAD_TO_DEG); BRM_idreh[4] = 0; for(i=1; i<4; i++) BRM_idreh[i+4] = BRM_idreh[i] + 180; }
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 ); }
//--------------------------------------------------------- bool CSG_Grid::Get_Gradient(int x, int y, double &Decline, double &Azimuth) const { int i, ix, iy, iDir; double z, zm[4], G, H; if( is_InGrid(x, y) ) { z = asDouble(x, y); for(i=0, iDir=0; i<4; i++, iDir+=2) { ix = m_System.Get_xTo(iDir, x); iy = m_System.Get_yTo(iDir, y); if( is_InGrid(ix, iy) ) { zm[i] = asDouble(ix, iy) - z; } else { ix = m_System.Get_xFrom(iDir, x); iy = m_System.Get_yFrom(iDir, y); if( is_InGrid(ix, iy) ) { zm[i] = z - asDouble(ix, iy); } else { zm[i] = 0.0; } } } G = (zm[0] - zm[2]) / (2.0 * Get_Cellsize()); H = (zm[1] - zm[3]) / (2.0 * Get_Cellsize()); Decline = atan(sqrt(G*G + H*H)); if( G != 0.0 ) Azimuth = M_PI_180 + atan2(H, G); else Azimuth = H > 0.0 ? M_PI_270 : (H < 0.0 ? M_PI_090 : -1.0); return( true ); } Decline = 0.0; Azimuth = -1.0; return( false ); }
//--------------------------------------------------------- bool CSG_Grid::_Assign_ExtremeValue(CSG_Grid *pGrid, bool bMaximum) { if( Get_Cellsize() < pGrid->Get_Cellsize() || is_Intersecting(pGrid->Get_Extent()) == INTERSECTION_None ) { return( false ); } //----------------------------------------------------- int x, y, ix, iy; double px, py, ax, ay, d, z; CSG_Matrix S(Get_NY(), Get_NX()), N(Get_NY(), Get_NX()); d = pGrid->Get_Cellsize() / Get_Cellsize(); Set_NoData_Value(pGrid->Get_NoData_Value()); Assign_NoData(); //----------------------------------------------------- ax = 0.5 + (pGrid->Get_XMin() - Get_XMin()) / Get_Cellsize(); ay = 0.5 + (pGrid->Get_YMin() - Get_YMin()) / Get_Cellsize(); for(y=0, py=ay; y<pGrid->Get_NY() && SG_UI_Process_Set_Progress(y, pGrid->Get_NY()); y++, py+=d) { if( (iy = (int)floor(py)) >= 0 && iy < Get_NY() ) { for(x=0, px=ax; x<pGrid->Get_NX(); x++, px+=d) { if( !pGrid->is_NoData(x, y) && (ix = (int)floor(px)) >= 0 && ix < Get_NX() ) { z = pGrid->asDouble(x, y); if( is_NoData(ix, iy) || (bMaximum == true && z > asDouble(ix, iy)) || (bMaximum == false && z < asDouble(ix, iy)) ) { Set_Value(ix, iy, z); } } } } } //----------------------------------------------------- Get_History() = pGrid->Get_History(); Get_History().Add_Child(SG_T("GRID_OPERATION"), CSG_String::Format(SG_T("%f -> %f"), pGrid->Get_Cellsize(), Get_Cellsize()))->Add_Property(SG_T("NAME"), LNG("Resampling")); SG_UI_Process_Set_Ready(); return( true ); }
//--------------------------------------------------------- bool CXYZ_Export::On_Execute(void) { bool bExNoData; int x, y, i; TSG_Point p; CSG_File Stream; CSG_String FileName; CSG_Parameter_Grid_List *pGrids; pGrids = Parameters("GRIDS") ->asGridList(); FileName = Parameters("FILENAME")->asString(); bExNoData = Parameters("EX_NODATA")->asBool(); if( pGrids->Get_Count() > 0 && Stream.Open(FileName, SG_FILE_W, false) ) { if( Parameters("CAPTION")->asBool() ) { Stream.Printf(SG_T("\"X\"\t\"Y\"")); for(i=0; i<pGrids->Get_Count(); i++) { Stream.Printf(SG_T("\t\"%s\""), pGrids->asGrid(i)->Get_Name()); } Stream.Printf(SG_T("\n")); } for(y=0, p.y=Get_YMin(); y<Get_NY() && Set_Progress(y); y++, p.y+=Get_Cellsize()) { for(x=0, p.x=Get_XMin(); x<Get_NX(); x++, p.x+=Get_Cellsize()) { if( !bExNoData || (bExNoData && !pGrids->asGrid(0)->is_NoData(x, y)) ) { Stream.Printf(SG_T("%f\t%f"), p.x, p.y); for(i=0; i<pGrids->Get_Count(); i++) { Stream.Printf(SG_T("\t%f"), pGrids->asGrid(i)->asDouble(x, y)); } Stream.Printf(SG_T("\n")); } } } return( true ); } return( false ); }
//--------------------------------------------------------- CvMat* COpenCV_NNet::GetEvalMatrix(CSG_Parameter_Grid_List *gl_grids, int type) { bool b_NoData; int x,y,i_Grid; CSG_Table *t_data; CSG_Table_Record *tr_rec; TSG_Point p; CvMat *mat; // We will use this table as a temporary data store, // since we cannot dynamically resize the CvMat t_data = new CSG_Table(); // We need a column for each grid and the output lable for (int i = 0; i < gl_grids->Get_Count(); i++) { t_data->Add_Field(CSG_String::Format(SG_T("GRID_%d"), i), SG_DATATYPE_Float, i); } // Traverse all grids, every point for(y=0, p.y=Get_YMin(); y<Get_NY() && Set_Progress(y); y++, p.y+=Get_Cellsize()) { for(x=0, p.x=Get_XMin(); x<Get_NX(); x++, p.x+=Get_Cellsize()) { for(i_Grid=0, b_NoData=false; i_Grid<gl_grids->Get_Count() && !b_NoData; i_Grid++) { // If there is one grid that has no data in this point p, then set the no data flag if( gl_grids->asGrid(i_Grid)->is_NoData(x, y) ) { b_NoData = true; } } if (!b_NoData) { // We have data in all grids, so lets add them to the eval data table tr_rec = t_data->Add_Record(); for(i_Grid=0; i_Grid<gl_grids->Get_Count(); i_Grid++) { tr_rec->Set_Value(i_Grid, (float) gl_grids->asGrid(i_Grid)->asFloat(x, y)); } } } } // Now create the matrix and add all data from the table to the matrix mat = GetEvalMatrix(t_data, type); return mat; }
//--------------------------------------------------------- double CGrid_Class_Statistics_For_Polygons::Get_Intersection(CSG_Shape_Polygon *pPolygon, TSG_Point p, bool bCenter) { //----------------------------------------------------- if( bCenter ) { return( pPolygon->Contains(p) ? Get_Cellarea() : 0.0 ); } //----------------------------------------------------- CSG_Shapes Cells(SHAPE_TYPE_Polygon); CSG_Shape *pCell = Cells.Add_Shape(); CSG_Shape *pArea = Cells.Add_Shape(); pCell->Add_Point(p.x - 0.5 * Get_Cellsize(), p.y - 0.5 * Get_Cellsize()); pCell->Add_Point(p.x - 0.5 * Get_Cellsize(), p.y + 0.5 * Get_Cellsize()); pCell->Add_Point(p.x + 0.5 * Get_Cellsize(), p.y + 0.5 * Get_Cellsize()); pCell->Add_Point(p.x + 0.5 * Get_Cellsize(), p.y - 0.5 * Get_Cellsize()); if( SG_Polygon_Intersection(pPolygon, pCell, pArea) ) { return( ((CSG_Shape_Polygon *)pArea)->Get_Area() ); } return( 0.0 ); }
//--------------------------------------------------------- bool CGrid_Mask::On_Execute(void) { CSG_Grid *pGrid = Parameters("GRID")->asGrid(); CSG_Grid *pMask = Parameters("MASK")->asGrid(); if( !pGrid->is_Intersecting(pMask->Get_Extent()) ) { Message_Add(_TL("no intersection with mask grid.")); return( false ); } //----------------------------------------------------- CSG_Grid *pMasked = Parameters("MASKED")->asGrid(); if( pMasked && pMasked != pGrid ) { pMasked->Create(*pGrid); pMasked->Fmt_Name("%s [%s]", pGrid->Get_Name(), _TL("masked")); pGrid = pMasked; } //----------------------------------------------------- Process_Set_Text(_TL("masking...")); for(int y=0; y<Get_NY() && Set_Progress(y); y++) { double py = Get_YMin() + y * Get_Cellsize(); #pragma omp parallel for for(int x=0; x<Get_NX(); x++) { if( !pGrid->is_NoData(x, y) ) { double px = Get_XMin() + x * Get_Cellsize(); if( !pMask->is_InGrid_byPos(px, py) ) { pGrid->Set_NoData(x, y); } } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CExercise_03::Method_04(void) { int x, y, ix; double a, b, c; //----------------------------------------------------- for(y=0; y<Get_NY() && Set_Progress(y); y++) { for(x=0, ix=1; x<Get_NX()-1; x++, ix++) { if( m_pInput->is_NoData(x, y) || m_pInput->is_NoData(ix, y) ) // don't work with 'no data'... { m_pOutput->Set_NoData(x, y); } else { a = m_pInput->asDouble( x, y); b = m_pInput->asDouble(ix, y); c = atan((a - b) / Get_Cellsize()) * 180.0 / M_PI; m_pOutput->Set_Value(x, y, c); } } m_pOutput->Set_NoData(x, y); // what shall we do with the last cell in a row ??!! } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CSG_Grid::_Assign_Interpolated(CSG_Grid *pGrid, TSG_Grid_Interpolation Interpolation) { int x, y; double xPosition, yPosition, z; Set_NoData_Value_Range(pGrid->Get_NoData_Value(), pGrid->Get_NoData_hiValue()); for(y=0, yPosition=Get_YMin(); y<Get_NY() && SG_UI_Process_Set_Progress(y, Get_NY()); y++, yPosition+=Get_Cellsize()) { for(x=0, xPosition=Get_XMin(); x<Get_NX(); x++, xPosition+=Get_Cellsize()) { if( pGrid->Get_Value(xPosition, yPosition, z, Interpolation) ) { Set_Value (x, y, z); } else { Set_NoData(x, y); } } } Get_History() = pGrid->Get_History(); Get_History().Add_Child(SG_T("GRID_OPERATION"), CSG_String::Format(SG_T("%f -> %f"), pGrid->Get_Cellsize(), Get_Cellsize()))->Add_Property(SG_T("NAME"), LNG("Resampling")); SG_UI_Process_Set_Ready(); return( true ); }
//--------------------------------------------------------- double CGSGrid_Variance::Get_Laenge(int x, int y) { int iRadius, Count; double d; //----------------------------------------------------- V[0] = Get_GSGrid_Variance(x,y,1,Count); Z[0] = Count; for(iRadius=1; iRadius<maxRadius; iRadius++) { V[iRadius] = V[iRadius-1] + Get_GSGrid_Variance(x, y, iRadius + 1, Count); Z[iRadius] = Z[iRadius-1] + Count; } for(iRadius=0; iRadius<maxRadius; iRadius++) { V[iRadius] /= (double)Z[iRadius]; } //----------------------------------------------------- d = Get_Steigung(); if( d == 0.0 ) return( Get_Cellsize() * maxRadius ); else return( V[maxRadius-1] / d / 2.0 ); }
//--------------------------------------------------------- bool CFragmentation_Resampling::Initialise(CSG_Grid *pClasses, int Class) { int x, y, Level_Count; double Level_Grow, Level_Start, Density, Connectivity; CSG_Grid *pDensity, *pConnectivity; pDensity = Parameters("DENSITY") ->asGrid(); pConnectivity = Parameters("CONNECTIVITY") ->asGrid(); Level_Grow = Parameters("LEVEL_GROW") ->asDouble(); m_bDensityMean = Parameters("DENSITY_MEAN") ->asBool(); //----------------------------------------------------- if( Level_Grow > 0.0 ) { for(y=0; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { if( Get_Connectivity(x, y, pClasses, Class, Density, Connectivity) ) { pDensity ->Set_Value (x, y, Density); pConnectivity ->Set_Value (x, y, Connectivity); } else { pDensity ->Set_NoData(x, y); pConnectivity ->Set_NoData(x, y); } } } //------------------------------------------------- // Level_Grow *= Get_Cellsize(); // Level_Start = 0.0; // Level_Count = m_Radius_iMax; // m_Radius_iMin--; // m_Radius_iMax--; Level_Count = 1 + (int)((m_Radius_Max - m_Radius_Min) / Level_Grow); Level_Grow *= Get_Cellsize(); Level_Start = Level_Grow * (1.0 + 2.0 * m_Radius_Min); if( m_Density .Create(pDensity , Level_Grow, Level_Start, Level_Count, GRID_PYRAMID_Mean, GRID_PYRAMID_Arithmetic) && m_Connectivity .Create(pConnectivity , Level_Grow, Level_Start, Level_Count, GRID_PYRAMID_Mean, GRID_PYRAMID_Arithmetic) ) { for(int iGrid=0; iGrid<m_Density.Get_Count(); iGrid++) { Message_Add(CSG_String::Format(SG_T("%s %d: %f (%f)"), _TL("Scale"), 1 + iGrid, m_Density.Get_Grid(iGrid)->Get_Cellsize(), m_Density.Get_Grid(iGrid)->Get_Cellsize() / Get_Cellsize())); // DataObject_Add(SG_Create_Grid(*m_Density .Get_Grid(iGrid))); // DataObject_Add(SG_Create_Grid(*m_Connectivity .Get_Grid(iGrid))); } return( true ); } } return( false ); }
//--------------------------------------------------------- void CAir_Flow_Height::Get_Lee(int x, int y, double &Sum_A, double &Sum_B) { double Weight_A = Sum_A = 0.0; double Weight_B = Sum_B = 0.0; if( m_pDEM->is_InGrid(x, y) ) { double z, d, id, w; TSG_Point p; d = id = Get_Cellsize(); p = Get_System()->Get_Grid_to_World(x, y); while( id <= m_maxDistance && Get_Next(p, d, true) ) { if( Get_Z(p, d, z) ) { Weight_A += w = d * pow(id, -m_dLuv); Sum_A += w * z; Weight_B += w = d * pow(id, -m_dLee); Sum_B += w * z; } d *= m_Acceleration; id += d; } if( Weight_A > 0.0 ) { Sum_A /= Weight_A; } if( Weight_B > 0.0 ) { Sum_B /= Weight_B; } } }
//--------------------------------------------------------- bool CGrid_Profile::Set_Profile(TSG_Point A, TSG_Point B) { double dx, dy, d, n; TSG_Point p; //----------------------------------------------------- dx = fabs(B.x - A.x); dy = fabs(B.y - A.y); if( dx > 0.0 || dy > 0.0 ) { if( dx > dy ) { dx /= Get_Cellsize(); n = dx; dy /= dx; dx = Get_Cellsize(); } else { dy /= Get_Cellsize(); n = dy; dx /= dy; dy = Get_Cellsize(); } if( B.x < A.x ) { dx = -dx; } if( B.y < A.y ) { dy = -dy; } //------------------------------------------------- for(d=0.0, p.x=A.x, p.y=A.y; d<=n; d++, p.x+=dx, p.y+=dy) { Add_Point(p); } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CGrid_Mask::On_Execute(void) { int x, y; double z; TSG_Point p; CSG_Grid *pGrid, *pMask, *pMasked; pGrid = Parameters("GRID") ->asGrid(); pMask = Parameters("MASK") ->asGrid(); pMasked = Parameters("MASKED") ->asGrid(); if( !pGrid->is_Intersecting(pMask->Get_Extent()) ) { Message_Add(_TL("no intersection with mask grid.")); return( false ); } if( pMasked == NULL ) { pMasked = pGrid; Parameters("MASKED")->Set_Value(pMasked); } else if( pMasked != pGrid ) { pMasked->Assign(pGrid); } Process_Set_Text(_TL("masking...")); for(y=0, p.y=Get_YMin(); y<Get_NY() && Set_Progress(y); y++, p.y+=Get_Cellsize()) { for(x=0, p.x=Get_XMin(); x<Get_NX(); x++, p.x+=Get_Cellsize()) { if( !pMasked->is_NoData(x, y) && !pMask->Get_Value(p, z, GRID_INTERPOLATION_NearestNeighbour) ) { pMasked->Set_NoData(x, y); } } } return( true ); }
//--------------------------------------------------------- bool CGWR_Grid_Downscaling::Set_Model(void) { CSG_Grid *pRegression = Parameters("REGRESSION" )->asGrid(); CSG_Grid *pReg_ResCorr = Parameters("REG_RESCORR")->asGrid(); pRegression->Set_Name(CSG_String::Format(SG_T("%s [%s]"), m_pDependent->Get_Name(), _TL("GWR"))); if( pReg_ResCorr ) { pReg_ResCorr->Set_Name(CSG_String::Format(SG_T("%s [%s, %s]"), m_pDependent->Get_Name(), _TL("GWR"), _TL("Residual Correction"))); } for(int y=0; y<Get_NY() && Set_Progress(y); y++) { double p_y = Get_YMin() + y * Get_Cellsize(); #pragma omp parallel for for(int x=0; x<Get_NX(); x++) { double Value, Residual, p_x = Get_XMin() + x * Get_Cellsize(); if( Set_Model(p_x, p_y, Value, Residual) ) { pRegression->Set_Value(x, y, Value); if( pReg_ResCorr ) { pReg_ResCorr->Set_Value(x, y, Value + Residual); } } else { pRegression->Set_NoData(x, y); if( pReg_ResCorr ) { pReg_ResCorr->Set_NoData(x, y); } } } } return( true ); }
//--------------------------------------------------------- // find_obs() - Function to find the observed vector as part of // the set of normal equations for least squares. // V.1.0, Jo Wood, 11th December, 1994. //--------------------------------------------------------- bool CParam_Scale::Get_Observed(int x, int y, CSG_Vector &Observed, bool bConstrain) { if( m_pDEM->is_NoData(x, y) || x < m_Radius || x > Get_NX() - m_Radius || y < m_Radius || y > Get_NY() - m_Radius ) { return( false ); } //----------------------------------------------------- int ix, iy, jx, jy; double dx, dy, dz, z; Observed.Create(6); z = m_pDEM->asDouble(x, y); for(iy=0, jy=y-m_Radius, dy=-m_Radius*Get_Cellsize(); iy<m_Weights.Get_NY(); iy++, jy++, dy+=Get_Cellsize()) { for(ix=0, jx=x-m_Radius, dx=-m_Radius*Get_Cellsize(); ix<m_Weights.Get_NX(); ix++, jx++, dx+=Get_Cellsize()) { dz = m_pDEM->is_InGrid(jx, jy) ? m_pDEM->asDouble(jx, jy) - z : 0.0; if( dz ) { dz *= m_Weights[iy][ix]; Observed[0] += dz * dx * dx; Observed[1] += dz * dy * dy; Observed[2] += dz * dx * dy; Observed[3] += dz * dx; Observed[4] += dz * dy; if( !bConstrain ) // if constrained, should remain 0.0 { Observed[5] += dz; } } } } return( true ); }
//--------------------------------------------------------- bool CGW_Multi_Regression_Grid::Set_Model(void) { CSG_Grid *pRegression = Parameters("REGRESSION")->asGrid(); CSG_Grid *pQuality = Parameters("QUALITY" )->asGrid(); pRegression->Set_Name(CSG_String::Format(SG_T("%s [%s]" ), m_Points.Get_Name(), _TL("GWR"))); pQuality ->Set_Name(CSG_String::Format(SG_T("%s [%s, %s]"), m_Points.Get_Name(), _TL("GWR"), _TL("Quality"))); if( m_pQuality == Parameters("QUALITY")->asGrid() ) { pQuality = NULL; } //----------------------------------------------------- for(int y=0; y<Get_NY() && Set_Progress(y); y++) { double p_y = Get_YMin() + y * Get_Cellsize(); #pragma omp parallel for for(int x=0; x<Get_NX(); x++) { double Value, p_x = Get_XMin() + x * Get_Cellsize(); if( Set_Model(p_x, p_y, Value) ) { GRID_SET_VALUE(pRegression, x, y, Value); GRID_SET_VALUE(pQuality , x, y, m_pQuality->Get_Value(p_x, p_y)); } else { GRID_SET_NODATA(pRegression, x, y); GRID_SET_NODATA(pQuality , x, y); } } } //----------------------------------------------------- Set_Residuals(); return( true ); }
//--------------------------------------------------------- bool CMelton_Ruggedness::On_Execute(void) { CSG_Grid *pDEM, *pArea, *pMRN, *pZMax; //----------------------------------------------------- pDEM = Parameters("DEM" )->asGrid(); pArea = Parameters("AREA")->asGrid(); pZMax = Parameters("ZMAX")->asGrid(); pMRN = Parameters("MRN" )->asGrid(); if( !pDEM->Set_Index() ) { Error_Set(_TL("index creation failed")); return( false ); } pArea->Set_NoData_Value(0.0); pArea->Assign_NoData(); pZMax->Assign_NoData(); pMRN ->Assign_NoData(); //------------------------------------------------- for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { int x, y, i, ix, iy; if( pDEM->Get_Sorted(n, x, y, true, true) ) { pArea->Add_Value(x, y, Get_Cellsize()); if( pZMax->is_NoData(x, y) ) { pZMax->Set_Value(x, y, pDEM->asDouble(x, y)); } if( (i = pDEM->Get_Gradient_NeighborDir(x, y, true)) >= 0 && Get_System().Get_Neighbor_Pos(i, x, y, ix, iy) ) { pArea->Add_Value(ix, iy, pArea->asDouble(x, y)); if( pZMax->is_NoData(ix, iy) || pZMax->asDouble(ix, iy) < pZMax->asDouble(x, y) ) { pZMax->Set_Value(ix, iy, pZMax->asDouble(x, y)); } } pMRN->Set_Value(x, y, (pZMax->asDouble(x, y) - pDEM->asDouble(x, y)) / sqrt(pArea->asDouble(x, y))); } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CGrid_Classify_Supervised::Set_Classifier(CSG_Classifier_Supervised &Classifier, CSG_Shapes *pPolygons, int Field) { Process_Set_Text(_TL("training")); //----------------------------------------------------- TSG_Point p; p.y = Get_YMin(); for(int y=0; y<Get_NY() && Set_Progress(y); y++, p.y+=Get_Cellsize()) { p.x = Get_XMin(); for(int x=0; x<Get_NX(); x++, p.x+=Get_Cellsize()) { CSG_Vector Features(m_pFeatures->Get_Count()); if( Get_Features(x, y, Features) ) { for(int iPolygon=0; iPolygon<pPolygons->Get_Count(); iPolygon++) { CSG_Shape_Polygon *pPolygon = (CSG_Shape_Polygon *)pPolygons->Get_Shape(iPolygon); if( pPolygon->Contains(p) ) { Classifier.Train_Add_Sample(pPolygon->asString(Field), Features); } } } } } //----------------------------------------------------- if( Classifier.Train(true) ) { Classifier.Save(Parameters("FILE_SAVE")->asString()); return( true ); } return( false ); }
//--------------------------------------------------------- bool CGrid_To_Points_Random::On_Execute(void) { int x, y, n; double frequency; CSG_Grid *pGrid; CSG_Shape *pShape; CSG_Shapes *pShapes; pGrid = Parameters("GRID")->asGrid(); frequency = 1.0 / Parameters("FREQ")->asDouble(); pShapes = Parameters("POINTS")->asShapes(); pShapes->Create(SHAPE_TYPE_Point, pGrid->Get_Name()); pShapes->Add_Field("ID" , SG_DATATYPE_Int); pShapes->Add_Field("VALUE" , SG_DATATYPE_Double); srand((unsigned)time(NULL)); for(n=0, y=0; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { if( (double)rand() / (double)RAND_MAX <= frequency ) { pShape = pShapes->Add_Shape(); pShape->Add_Point( pGrid->Get_XMin() + x * Get_Cellsize(), pGrid->Get_YMin() + y * Get_Cellsize() ); pShape->Set_Value(0, ++n); pShape->Set_Value(1, pGrid->asDouble(x, y)); } } } return( true ); }
//--------------------------------------------------------- bool CDiffuse_Pollution_Risk::Set_Flow(void) { Process_Set_Text(_TL("initialization")); CSG_Grid *pWeight = Parameters("WEIGHT")->asGrid (); double Weight = Parameters("WEIGHT")->asDouble(); CSG_Grid *pRain = Parameters("RAIN" )->asGrid (); double Rain = Parameters("RAIN" )->asDouble(); m_FlowDir.Create(*Get_System(), SG_DATATYPE_Char); m_RainAcc.Create(*Get_System()); m_TWI .Create(*Get_System()); for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { int x, y; if( !m_pDEM->Get_Sorted(n, x, y, true) || (pRain && pRain->is_NoData(x, y)) || !Set_Flow(x, y, pRain ? pRain->asDouble(x, y) : Rain) ) { m_FlowDir .Set_NoData(x, y); m_RainAcc .Set_NoData(x, y); m_TWI .Set_NoData(x, y); m_pRisk_Point->Set_NoData(x, y); } else { double s, a; m_pDEM->Get_Gradient(x, y, s, a); s = tan(s); // tangens of slope a = (fabs(sin(a)) + fabs(cos(a))) * Get_Cellsize(); // flow width double SCA = m_RainAcc.asDouble(x, y) / a; // rain * specific catchment area m_TWI.Set_Value(x, y, log(SCA / (s < M_ALMOST_ZERO ? M_ALMOST_ZERO : s))); if( pWeight && pWeight->is_NoData(x, y) ) { m_pRisk_Point->Set_NoData(x, y); } else { m_pRisk_Point->Set_Value(x, y, SCA * s * (pWeight ? pWeight->asDouble(x, y) : Weight)); // Point Scale Risk Calculation according to Milledge et al. 2012 } } } return( true ); }
//--------------------------------------------------------- bool CMRVBF::Get_Flatness(CSG_Grid *pSlopes, CSG_Grid *pPercentiles, CSG_Grid *pCF, CSG_Grid *pVF, CSG_Grid *pRF, double T_Slope) { // const int Interpolation = GRID_INTERPOLATION_Bilinear; const int Interpolation = GRID_INTERPOLATION_BSpline; if( pSlopes && pSlopes->is_Valid() && pPercentiles && pPercentiles->is_Valid() ) { int x, y; double xp, yp, Slope, Percentile, cf, vf, rf; for(y=0, yp=Get_YMin(); y<Get_NY() && Set_Progress(y); y++, yp+=Get_Cellsize()) { for(x=0, xp=Get_XMin(); x<Get_NX(); x++, xp+=Get_Cellsize()) { if( pSlopes ->Get_Value(xp, yp, Slope , Interpolation) && pPercentiles->Get_Value(xp, yp, Percentile, Interpolation) ) { cf = pCF->asDouble(x, y) * Get_Transformation(Slope, T_Slope, m_P_Slope); vf = cf * Get_Transformation( Percentile, m_T_Pctl_V, m_P_Pctl); rf = cf * Get_Transformation(1.0 - Percentile, m_T_Pctl_R, m_P_Pctl); pCF->Set_Value (x, y, cf); pVF->Set_Value (x, y, 1.0 - Get_Transformation(vf, 0.3, 4.0)); pRF->Set_Value (x, y, 1.0 - Get_Transformation(rf, 0.3, 4.0)); } else { pVF->Set_NoData (x, y); pRF->Set_NoData (x, y); } } } return( true ); } return( false ); }
//--------------------------------------------------------- bool CTopographic_Correction::Get_Illumination(void) { Process_Set_Text(_TL("Illumination calculation")); //----------------------------------------------------- CSG_Grid DEM, *pDEM = Parameters("DEM")->asGrid(); if( !pDEM->Get_System().is_Equal(*Get_System()) ) { DEM.Create(*Get_System()); DEM.Assign(pDEM, pDEM->Get_Cellsize() > Get_Cellsize() ? GRID_INTERPOLATION_BSpline : GRID_INTERPOLATION_Mean_Cells); pDEM = &DEM; } //----------------------------------------------------- double Azi = Parameters("AZI")->asDouble() * M_DEG_TO_RAD; double Hgt = Parameters("HGT")->asDouble() * M_DEG_TO_RAD; m_cosTz = cos(M_PI_090 - Hgt); m_sinTz = sin(M_PI_090 - Hgt); m_Slope .Create(*Get_System()); m_Illumination .Create(*Get_System()); //----------------------------------------------------- for(int y=0; y<Get_NY() && Set_Progress(y); y++) { for(int x=0; x<Get_NX(); x++) { double Slope, Aspect; if( pDEM->Get_Gradient(x, y, Slope, Aspect) ) { m_Slope .Set_Value(x, y, Slope); m_Illumination .Set_Value(x, y, cos(Slope) * m_cosTz + sin(Slope) * m_sinTz * cos(Azi - Aspect)); } else { m_Slope .Set_Value(x, y, 0.0); m_Illumination .Set_Value(x, y, m_cosTz); } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CGrid_Completion::On_Execute(void) { int x, y; double xPos, yPos, Value; TSG_Grid_Interpolation Interpolation; CSG_Grid *pGrid, *pAdditional; pAdditional = Parameters("ADDITIONAL") ->asGrid(); pGrid = Parameters("COMPLETED") ->asGrid(); if( pGrid->is_Intersecting(pAdditional->Get_Extent()) ) { if( pGrid != Parameters("ORIGINAL")->asGrid() ) { Process_Set_Text(_TL("Copying original data...")); pGrid->Assign(Parameters("ORIGINAL")->asGrid()); } Interpolation = (TSG_Grid_Interpolation)Parameters("INTERPOLATION")->asInt(); Process_Set_Text(_TL("Data completion...")); for(y=0, yPos=Get_YMin(); y<Get_NY() && Set_Progress(y, Get_NY()); y++, yPos+=Get_Cellsize()) { if( yPos >= pAdditional->Get_YMin() ) { for(x=0, xPos=Get_XMin(); x<Get_NX() && xPos<=pAdditional->Get_XMax(); x++, xPos+=Get_Cellsize()) { if( pGrid->is_NoData(x, y) && xPos >= pAdditional->Get_XMin() ) { if( !pAdditional->is_NoData_Value(Value = pAdditional->Get_Value(xPos, yPos, Interpolation)) ) { pGrid->Set_Value(x, y, Value); } } } } } return( true ); } Error_Set(_TL("Nothing to do: there is no intersection with additonal grid.")); return( false ); }
//--------------------------------------------------------- inline void CGrid_3D_Image::_Get_Position(double x, double y, double z, T3DPoint &p) { bool bResult; //----------------------------------------------------- if( m_ZRotate != 0.0 ) { _Get_Rotated(0.5 * Get_NX(), x, 0.5 * Get_NY(), y, m_ZRotate); } x = m_XScale * x; y = m_YScale * y; z = m_ZExagg * ((z - m_ZMean) / Get_Cellsize()); //----------------------------------------------------- switch( m_Projection ) { case 0: default: bResult = _Get_Panorama (y, z); break; case 1: bResult = _Get_Circle (y, z); break; case 2: bResult = _Get_Sinus (y, z); break; case 3: bResult = _Get_Hyperbel (y, z); break; } //----------------------------------------------------- if( bResult ) { p.bOk = true; p.x = (int)x; p.y = (int)y; p.z = z; } else { p.bOk = false; } }