//--------------------------------------------------------- bool CGW_Regression::Get_Model(int x, int y, CSG_Regression_Weighted &Model) { //----------------------------------------------------- TSG_Point Point = m_pIntercept->Get_System().Get_Grid_to_World(x, y); int nPoints = m_Search.Set_Location(Point); Model.Destroy(); for(int iPoint=0; iPoint<nPoints; iPoint++) { double ix, iy, iz; CSG_Shape *pPoint = m_Search.Do_Use_All() && m_Search.Get_Point(iPoint, ix, iy, iz) ? m_pPoints->Get_Shape((int)iz) : m_pPoints->Get_Shape(iPoint); if( !pPoint->is_NoData(m_iDependent) && !pPoint->is_NoData(m_iPredictor) ) { Model.Add_Sample( m_Weighting.Get_Weight(SG_Get_Distance(Point, pPoint->Get_Point(0))), pPoint->asDouble(m_iDependent), CSG_Vector(1, &(iz = pPoint->asDouble(m_iPredictor))) ); } } //----------------------------------------------------- return( Model.Calculate() ); }
//--------------------------------------------------------- bool CKernel_Density::On_Execute(void) { int Population; double Radius; CSG_Shapes *pPoints; //----------------------------------------------------- pPoints = Parameters("POINTS" )->asShapes(); Population = Parameters("POPULATION")->asInt(); Radius = Parameters("RADIUS" )->asDouble(); m_Kernel = Parameters("KERNEL" )->asInt(); if( Population < 0 || Population >= pPoints->Get_Field_Count() || pPoints->Get_Field_Type(Population) == SG_DATATYPE_String ) { Population = -1; } //----------------------------------------------------- if( (m_pGrid = m_Grid_Target.Get_Grid()) == NULL ) { return( false ); } m_pGrid->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pPoints->Get_Name(), _TL("Kernel Density"))); m_pGrid->Set_NoData_Value(0.0); m_pGrid->Assign(0.0); DataObject_Set_Colors(m_pGrid, 100, SG_COLORS_BLACK_WHITE, true); m_dRadius = Radius / m_pGrid->Get_Cellsize(); m_iRadius = 1 + (int)m_dRadius; //----------------------------------------------------- if( pPoints->Get_Selection_Count() > 0 ) { for(int iPoint=0; iPoint<pPoints->Get_Selection_Count() && Set_Progress(iPoint, pPoints->Get_Selection_Count()); iPoint++) { CSG_Shape *pPoint = pPoints->Get_Selection(iPoint); Set_Kernel(pPoint->Get_Point(0), Population < 0 ? 1.0 : pPoint->asDouble(Population)); } } else { for(int iPoint=0; iPoint<pPoints->Get_Count() && Set_Progress(iPoint, pPoints->Get_Count()); iPoint++) { CSG_Shape *pPoint = pPoints->Get_Shape(iPoint); Set_Kernel(pPoint->Get_Point(0), Population < 0 ? 1.0 : pPoint->asDouble(Population)); } } return( true ); }
//--------------------------------------------------------- bool CGrid_Flow_Profile::Add_Point(int x, int y) { if( !m_pDEM->is_InGrid(x, y) ) { return( false ); } TSG_Point Point = Get_System()->Get_Grid_to_World(x, y); double d, dSurface; if( m_pPoints->Get_Count() == 0 ) { d = dSurface = 0.0; } else { CSG_Shape *pLast = m_pPoints->Get_Shape(m_pPoints->Get_Count() - 1); d = SG_Get_Distance(Point, pLast->Get_Point(0)); dSurface = pLast->asDouble(5) - m_pDEM->asDouble(x, y); dSurface = sqrt(d*d + dSurface*dSurface); d += pLast->asDouble(1); dSurface += pLast->asDouble(2); } CSG_Shape *pPoint = m_pPoints->Add_Shape(); pPoint->Add_Point(Point); pPoint->Set_Value(0, m_pPoints->Get_Count()); pPoint->Set_Value(1, d); pPoint->Set_Value(2, dSurface); pPoint->Set_Value(3, Point.x); pPoint->Set_Value(4, Point.y); pPoint->Set_Value(5, m_pDEM->asDouble(x, y)); for(int i=0; i<m_pValues->Get_Count(); i++) { pPoint->Set_Value(VALUE_OFFSET + i, m_pValues->asGrid(i)->asDouble(x, y)); } m_pLine->Add_Point(Point); return( true ); }
//--------------------------------------------------------- bool CSG_Parameters_Search_Points::Get_Point(int Index, double &x, double &y, double &z) { if( m_pPoints ) // without search engine { CSG_Shape *pPoint = m_pPoints->Get_Shape(Index); if( !pPoint || pPoint->is_NoData(m_zField) ) { return( false ); } x = pPoint->Get_Point(0).x; y = pPoint->Get_Point(0).y; z = m_zField < 0 ? Index : pPoint->asDouble(m_zField); } else // using search engine { if( !m_Search.Get_Selected_Point(Index, x, y, z) ) { return( false ); } } return( true ); }
//--------------------------------------------------------- bool CCollect_Points::On_Execute_Finish(void) { CSG_Shapes *pTarget = Parameters("REF_TARGET")->asShapes(); if( pTarget != NULL ) { pTarget->Create(SHAPE_TYPE_Point, _TL("Reference Points (Projection)")); pTarget->Add_Field("X_SRC", SG_DATATYPE_Double); pTarget->Add_Field("Y_SRC", SG_DATATYPE_Double); pTarget->Add_Field("X_MAP", SG_DATATYPE_Double); pTarget->Add_Field("Y_MAP", SG_DATATYPE_Double); pTarget->Add_Field("RESID", SG_DATATYPE_Double); for(int iPoint=0; iPoint<m_pPoints->Get_Count(); iPoint++) { CSG_Shape *pPoint = pTarget->Add_Shape(m_pPoints->Get_Shape(iPoint), SHAPE_COPY_ATTR); pPoint->Add_Point( pPoint->asDouble(2), pPoint->asDouble(3) ); } } m_Engine.Destroy(); return( true ); }
//--------------------------------------------------------- bool CSG_PRQuadTree::Create(CSG_Shapes *pShapes, int Attribute, bool bStatistics) { Destroy(); if( pShapes && pShapes->is_Valid() && Create(pShapes->Get_Extent(), bStatistics) ) { for(int iShape=0; iShape<pShapes->Get_Count() && SG_UI_Process_Set_Progress(iShape, pShapes->Get_Count()); iShape++) { CSG_Shape *pShape = pShapes->Get_Shape(iShape); if( Attribute < 0 || !pShape->is_NoData(Attribute) ) { double z = Attribute < 0 ? iShape : pShape->asDouble(Attribute); for(int iPart=0; iPart<pShape->Get_Part_Count(); iPart++) { for(int iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++) { Add_Point(pShape->Get_Point(iPoint, iPart), z); } } } } return( Get_Point_Count() > 0 ); } return( false ); }
//--------------------------------------------------------- int CGW_Multi_Regression::Set_Variables(int x, int y) { int iPoint, jPoint, nPoints, iPredictor; TSG_Point Point; CSG_Shape *pPoint; Point = m_pIntercept->Get_System().Get_Grid_to_World(x, y); nPoints = m_Search.is_Okay() ? m_Search.Select_Nearest_Points(Point.x, Point.y, m_nPoints_Max, m_Radius, m_Mode == 0 ? -1 : 4) : m_pPoints->Get_Count(); for(iPoint=0, jPoint=0; iPoint<nPoints; iPoint++) { if( m_Search.is_Okay() ) { double ix, iy, iz; m_Search.Get_Selected_Point(iPoint, ix, iy, iz); pPoint = m_pPoints->Get_Shape((int)iz); } else { pPoint = m_pPoints->Get_Shape(iPoint); } m_z[iPoint] = pPoint->asDouble(m_iDependent); m_w[iPoint] = m_Weighting.Get_Weight(SG_Get_Distance(Point, pPoint->Get_Point(0))); for(iPredictor=0; iPredictor<m_nPredictors; iPredictor++) { if( !pPoint->is_NoData(m_iPredictor[iPredictor]) ) { m_y[jPoint][iPredictor] = pPoint->asDouble(m_iPredictor[iPredictor]); } else { iPredictor = m_nPredictors + 1; } } if( iPredictor == m_nPredictors ) { jPoint++; } } return( jPoint ); }
//--------------------------------------------------------- bool CPoint_Zonal_Multi_Grid_Regression::Set_Residuals(CSG_Shapes *pPoints, CSG_Grid *pRegression) { CSG_Shapes *pResiduals = Parameters("RESIDUALS")->asShapes(); int iAttribute = Parameters("ATTRIBUTE")->asInt (); if( !pRegression || !pResiduals ) { return( false ); } //----------------------------------------------------- pResiduals->Create(SHAPE_TYPE_Point, CSG_String::Format(SG_T("%s [%s]"), Parameters("ATTRIBUTE")->asString(), _TL("Residuals"))); pResiduals->Add_Field(pPoints->Get_Field_Name(iAttribute), SG_DATATYPE_Double); pResiduals->Add_Field("TREND" , SG_DATATYPE_Double); pResiduals->Add_Field("RESIDUAL", SG_DATATYPE_Double); TSG_Grid_Resampling Resampling; switch( Parameters("RESAMPLING")->asInt() ) { default: Resampling = GRID_RESAMPLING_NearestNeighbour; break; case 1: Resampling = GRID_RESAMPLING_Bilinear; break; case 2: Resampling = GRID_RESAMPLING_BicubicSpline; break; case 3: Resampling = GRID_RESAMPLING_BSpline; break; } //----------------------------------------------------- for(int iShape=0; iShape<pPoints->Get_Count() && Set_Progress(iShape, pPoints->Get_Count()); iShape++) { CSG_Shape *pShape = pPoints->Get_Shape(iShape); if( !pShape->is_NoData(iAttribute) ) { double zShape = pShape->asDouble(iAttribute); for(int iPart=0; iPart<pShape->Get_Part_Count(); iPart++) { for(int iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++) { double zGrid; TSG_Point Point = pShape->Get_Point(iPoint, iPart); if( pRegression->Get_Value(Point, zGrid, Resampling) ) { CSG_Shape *pResidual = pResiduals->Add_Shape(); pResidual->Add_Point(Point); pResidual->Set_Value(0, zShape); pResidual->Set_Value(1, zGrid); pResidual->Set_Value(2, zShape - zGrid); } } } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CSurfer_BLN_Export::On_Execute(void) { //----------------------------------------------------- CSG_File Stream; if( !Stream.Open(Parameters("FILE")->asString(), SG_FILE_W) ) { return( false ); } CSG_Shapes *pShapes = Parameters("SHAPES")->asShapes(); if( !pShapes->is_Valid() || pShapes->Get_Count() <= 0 ) { return( false ); } //----------------------------------------------------- int iName = Parameters("BNAME" )->asBool() ? Parameters("NAME")->asInt() : -1; int iDesc = Parameters("BDESC" )->asBool() ? Parameters("DESC")->asInt() : -1; int iZVal = Parameters("BZVAL" )->asBool() ? Parameters("ZVAL")->asInt() : -1; int Flag = 1; //----------------------------------------------------- for(int iShape=0; iShape<pShapes->Get_Count() && Set_Progress(iShape, pShapes->Get_Count()); iShape++) { CSG_Shape *pShape = pShapes->Get_Shape(iShape); for(int iPart=0; iPart<pShape->Get_Part_Count(); iPart++) { Stream.Printf(SG_T("%d,%d"), pShape->Get_Point_Count(iPart), Flag); if( iName >= 0 ) { Stream.Printf(SG_T(",\"%s\""), pShape->asString(iName)); } if( iDesc >= 0 ) { Stream.Printf(SG_T(",\"%s\""), pShape->asString(iDesc)); } Stream.Printf(SG_T("\n")); for(int iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++) { TSG_Point p = pShape->Get_Point(iPoint, iPart); if( iZVal >= 0 ) { Stream.Printf(SG_T("%f,%f,%f\n"), p.x, p.y, pShape->asDouble(iZVal)); } else { Stream.Printf(SG_T("%f,%f\n" ), p.x, p.y); } } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CKriging_Universal::On_Initialize(void) { m_pGrids = Parameters("GRIDS" )->asGridList(); m_Interpolation = Parameters("INTERPOL" )->asInt(); m_nPoints_Min = Parameters("SEARCH_POINTS_MIN")->asInt (); m_nPoints_Max = Parameters("SEARCH_POINTS_ALL")->asInt () == 0 ? Parameters("SEARCH_POINTS_MAX")->asInt () : 0; m_Radius = Parameters("SEARCH_RANGE" )->asInt () == 0 ? Parameters("SEARCH_RADIUS" )->asDouble() : 0.0; m_Direction = Parameters("SEARCH_DIRECTION" )->asInt () == 0 ? -1 : 4; //----------------------------------------------------- if( m_nPoints_Max <= 0 && m_Radius <= 0 ) // global { return( CKriging_Universal_Global::On_Initialize() ); } //----------------------------------------------------- m_Search.Create(m_pPoints->Get_Extent()); for(int iPoint=0; iPoint<m_pPoints->Get_Count() && Set_Progress(iPoint, m_pPoints->Get_Count()); iPoint++) { CSG_Shape *pPoint = m_pPoints->Get_Shape(iPoint); if( !pPoint->is_NoData(m_zField) ) { bool bAdd = true; for(int iGrid=0; iGrid<m_pGrids->Get_Count(); iGrid++) { if( !m_pGrids->asGrid(iGrid)->is_InGrid_byPos(pPoint->Get_Point(0)) ) { bAdd = false; } } if( bAdd ) { m_Search.Add_Point(pPoint->Get_Point(0).x, pPoint->Get_Point(0).y, m_bLog ? log(pPoint->asDouble(m_zField)) : pPoint->asDouble(m_zField)); } } } if( !m_Search.is_Okay() ) { SG_UI_Msg_Add(_TL("could not initialize point search engine"), true); return( false ); } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool C_Kriging_Ordinary_Global::Get_Weights(void) { int i, j, n; //----------------------------------------------------- for(int iShape=0; iShape<m_pShapes->Get_Count(); iShape++) { CSG_Shape *pShape = m_pShapes->Get_Shape(iShape); if( !pShape->is_NoData(m_zField) ) { for(int iPart=0; iPart<pShape->Get_Part_Count(); iPart++) { for(int iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++) { m_Points.Add( pShape->Get_Point(iPoint, iPart).x, pShape->Get_Point(iPoint, iPart).y, pShape->asDouble(m_zField) ); } } } } //----------------------------------------------------- if( (n = m_Points.Get_Count()) > 4 ) { m_G .Create(n + 1); m_W .Create(n + 1, n + 1); for(i=0; i<n; i++) { m_W[i][i] = 0.0; // diagonal... m_W[i][n] = m_W[n][i] = 1.0; // edge... for(j=i+1; j<n; j++) { m_W[i][j] = m_W[j][i] = Get_Weight( m_Points[i].x - m_Points[j].x, m_Points[i].y - m_Points[j].y ); } } m_W[n][n] = 0.0; return( m_W.Set_Inverse(false) ); } return( false ); }
//--------------------------------------------------------- bool CGridding_Spline_Base::_Get_Points(CSG_Points_Z &Points, bool bInGridOnly) { Points.Clear(); if( m_bGridPoints ) { int x, y; TSG_Point p; CSG_Grid *pGrid = Parameters("GRIDPOINTS") ->asGrid(); for(y=0, p.y=pGrid->Get_YMin(); y<pGrid->Get_NY() && Set_Progress(y, pGrid->Get_NY()); y++, p.y+=pGrid->Get_Cellsize()) { for(x=0, p.x=pGrid->Get_XMin(); x<pGrid->Get_NX(); x++, p.x+=pGrid->Get_Cellsize()) { if( !pGrid->is_NoData(x, y) && (!bInGridOnly || m_pGrid->is_InGrid_byPos(p)) ) { Points.Add(p.x, p.y, pGrid->asDouble(x, y)); } } } } else { CSG_Shapes *pShapes = Parameters("SHAPES") ->asShapes(); int zField = Parameters("FIELD") ->asInt(); for(int iShape=0; iShape<pShapes->Get_Count() && Set_Progress(iShape, pShapes->Get_Count()); iShape++) { CSG_Shape *pShape = pShapes->Get_Shape(iShape); if( !pShape->is_NoData(zField) ) { double zValue = pShape->asDouble(zField); for(int iPart=0; iPart<pShape->Get_Part_Count(); iPart++) { for(int iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++) { TSG_Point p = pShape->Get_Point(iPoint, iPart); if( !bInGridOnly || m_pGrid->is_InGrid_byPos(p) ) { Points.Add(p.x, p.y, zValue); } } } } } } return( Points.Get_Count() >= 3 ); }
//--------------------------------------------------------- bool CShapes_Buffer::Get_Buffers(CSG_Shapes *pShapes, int Field, CSG_Shapes *pBuffers, double Scale, bool bDissolve) { //----------------------------------------------------- double Distance; CSG_Shapes Part(SHAPE_TYPE_Polygon); CSG_Shape *pPart = Part.Add_Shape(), *pBuffer; Distance = Parameters("DIST_FIELD")->asDouble() * Scale; Scale = Parameters("DIST_SCALE")->asDouble() * Scale; if( !bDissolve ) { pBuffers->Create(SHAPE_TYPE_Polygon, CSG_String::Format(SG_T("%s [%s]"), pShapes->Get_Name(), _TL("Buffer")), pShapes); } else { pBuffers->Create(SHAPE_TYPE_Polygon, CSG_String::Format(SG_T("%s [%s]"), pShapes->Get_Name(), _TL("Buffer"))); pBuffers->Add_Field(_TL("ID"), SG_DATATYPE_Int); pBuffer = pBuffers->Add_Shape(); } //----------------------------------------------------- for(int iShape=0; iShape<pShapes->Get_Count() && Set_Progress(iShape, pShapes->Get_Count()); iShape++) { CSG_Shape *pShape = pShapes->Get_Shape(iShape); if( Field < 0 || (Distance = Scale * pShape->asDouble(Field)) > 0.0 ) { if( !bDissolve ) { pBuffer = pBuffers->Add_Shape(pShape, SHAPE_COPY_ATTR); } if( pBuffer->Get_Part_Count() == 0 ) { Get_Buffer(pShape, pBuffer, Distance); } else { Get_Buffer(pShape, pPart , Distance); SG_Polygon_Union(pBuffer, pPart); pPart->Del_Parts(); } } } //----------------------------------------------------- return( pBuffers->is_Valid() ); }
//--------------------------------------------------------- bool CGW_Regression_Grid::Set_Residuals(void) { CSG_Shapes *pResiduals = Parameters("RESIDUALS")->asShapes(); if( !pResiduals || !m_pPoints || !m_pRegression ) { return( false ); } //----------------------------------------------------- pResiduals->Create(SHAPE_TYPE_Point, CSG_String::Format(SG_T("%s [%s]"), m_pPoints->Get_Name(), _TL("Residuals"))); pResiduals->Add_Field(m_pPoints->Get_Field_Name(m_iDependent), SG_DATATYPE_Double); pResiduals->Add_Field("TREND" , SG_DATATYPE_Double); pResiduals->Add_Field("RESIDUAL", SG_DATATYPE_Double); //------------------------------------------------- for(int iShape=0; iShape<m_pPoints->Get_Count() && Set_Progress(iShape, m_pPoints->Get_Count()); iShape++) { CSG_Shape *pShape = m_pPoints->Get_Shape(iShape); double zShape = pShape->asDouble(m_iDependent); for(int iPart=0; iPart<pShape->Get_Part_Count(); iPart++) { for(int iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++) { double zRegression; TSG_Point Point = pShape->Get_Point(iPoint, iPart); if( m_pRegression->Get_Value(Point, zRegression) ) { CSG_Shape *pResidual = pResiduals->Add_Shape(); pResidual->Add_Point(Point); pResidual->Set_Value(0, zShape); pResidual->Set_Value(1, zRegression); pResidual->Set_Value(2, zShape - zRegression); } } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CVIEW_ScatterPlot::_Initialize_Shapes(void) { CSG_Shapes *pPoints = m_Parameters("POINTS")->asShapes(); int Field = m_Parameters("FIELD" )->asInt(); TSG_Grid_Resampling Resampling; switch( m_Parameters("RESAMPLING")->asInt() ) { default: Resampling = GRID_RESAMPLING_NearestNeighbour; break; case 1: Resampling = GRID_RESAMPLING_Bilinear ; break; case 2: Resampling = GRID_RESAMPLING_BicubicSpline ; break; case 3: Resampling = GRID_RESAMPLING_BSpline ; break; } CHECK_DATA(m_pGrid); CHECK_DATA(pPoints); if( !m_pGrid || !pPoints || Field < 0 || Field >= pPoints->Get_Field_Count() ) { return( false ); } m_sTitle.Printf("%s: [%s/%s]", _TL("Scatterplot"), m_pGrid->Get_Name(), pPoints->Get_Name()); m_sX.Printf("%s", m_pGrid->Get_Name()); m_sY.Printf("%s", pPoints->Get_Field_Name(Field)); int maxSamples = m_Options("SAMPLES_MAX")->asInt(); double x, Step = maxSamples > 0 && pPoints->Get_Count() > maxSamples ? pPoints->Get_Count() / maxSamples : 1.0; for(double i=0; i<pPoints->Get_Count() && PROGRESSBAR_Set_Position(i, pPoints->Get_Count()); i+=Step) { CSG_Shape *pShape = pPoints->Get_Shape((int)i); if( !pShape->is_NoData(Field) && m_pGrid->Get_Value(pShape->Get_Point(0), x, Resampling) ) { m_Trend.Add_Data(x, pShape->asDouble(Field)); } } return( true ); }
//--------------------------------------------------------- bool CCollect_Points::On_Execute(void) { m_Engine.Destroy(); m_pPoints = Parameters("REF_SOURCE")->asShapes(); Get_Parameters("REFERENCE")->Restore_Defaults(); if( !is_Compatible(m_pPoints) || Parameters("REFRESH")->asBool() ) { m_pPoints->Create(SHAPE_TYPE_Point, _TL("Reference Points (Origin)")); m_pPoints->Add_Field("X_SRC", SG_DATATYPE_Double); m_pPoints->Add_Field("Y_SRC", SG_DATATYPE_Double); m_pPoints->Add_Field("X_MAP", SG_DATATYPE_Double); m_pPoints->Add_Field("Y_MAP", SG_DATATYPE_Double); m_pPoints->Add_Field("RESID", SG_DATATYPE_Double); } else { for(int i=0; i<m_pPoints->Get_Count(); i++) { CSG_Shape *pPoint = m_pPoints->Get_Shape(i); m_Engine.Add_Reference(pPoint->Get_Point(0), CSG_Point( pPoint->asDouble(2), pPoint->asDouble(3) )); } int Method = Parameters("METHOD")->asInt(); int Order = Parameters("ORDER" )->asInt(); m_Engine.Evaluate(Method, Order); } return( true ); }
//--------------------------------------------------------- CSG_Shapes * CInterpolation::Get_Points(bool bOnlyNonPoints) { m_pShapes = Parameters("SHAPES") ->asShapes(); if( !bOnlyNonPoints || m_pShapes->Get_Type() != SHAPE_TYPE_Point ) { CSG_Shapes *pPoints = SG_Create_Shapes(SHAPE_TYPE_Point); pPoints->Set_NoData_Value_Range(m_pShapes->Get_NoData_Value(), m_pShapes->Get_NoData_hiValue()); pPoints->Add_Field(SG_T("Z"), SG_DATATYPE_Double); for(int iShape=0; iShape<m_pShapes->Get_Count() && Set_Progress(iShape, m_pShapes->Get_Count()); iShape++) { CSG_Shape *pShape = m_pShapes->Get_Shape(iShape); if( !pShape->is_NoData(m_zField) ) { for(int iPart=0; iPart<pShape->Get_Part_Count(); iPart++) { for(int iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++) { CSG_Shape *pPoint = pPoints->Add_Shape(); pPoint->Add_Point(pShape->Get_Point(iPoint, iPart)); pPoint->Set_Value(0, pShape->asDouble(m_zField)); } } } } m_zField = 0; m_pShapes = pPoints; } return( m_pShapes ); }
//--------------------------------------------------------- int CGW_Regression_Grid::Set_Variables(int x, int y) { int iPoint, jPoint, nPoints; double z; TSG_Point Point; CSG_Shape *pPoint; Point = Get_System()->Get_Grid_to_World(x, y); nPoints = m_Search.is_Okay() ? (int)m_Search.Select_Nearest_Points(Point.x, Point.y, m_nPoints_Max, m_Radius, m_Direction) : m_pPoints->Get_Count(); for(iPoint=0, jPoint=0; iPoint<nPoints; iPoint++) { if( m_Search.is_Okay() ) { double ix, iy, iz; m_Search.Get_Selected_Point(iPoint, ix, iy, iz); pPoint = m_pPoints->Get_Shape((int)iz); } else { pPoint = m_pPoints->Get_Shape(iPoint); } if( !pPoint->is_NoData(m_iDependent) && m_pPredictor->Get_Value(pPoint->Get_Point(0), z) ) { m_w[jPoint] = m_Weighting.Get_Weight(SG_Get_Distance(Point, pPoint->Get_Point(0))); m_z[jPoint] = pPoint->asDouble(m_iDependent); m_y[jPoint] = z; jPoint++; } } return( jPoint ); }
//--------------------------------------------------------- bool CKriging_Universal::On_Initialise(void) { m_pGrids = Parameters("GRIDS") ->asGridList(); m_Interpolation = Parameters("INTERPOL") ->asInt(); m_Radius = Parameters("MAXRADIUS") ->asDouble(); m_nPoints_Min = (int)Parameters("NPOINTS")->asRange()->Get_LoVal(); m_nPoints_Max = (int)Parameters("NPOINTS")->asRange()->Get_HiVal(); m_Mode = Parameters("MODE") ->asInt(); //----------------------------------------------------- m_Search.Create(m_pPoints->Get_Extent()); for(int iPoint=0; iPoint<m_pPoints->Get_Count() && Set_Progress(iPoint, m_pPoints->Get_Count()); iPoint++) { CSG_Shape *pPoint = m_pPoints->Get_Shape(iPoint); if( !pPoint->is_NoData(m_zField) ) { bool bAdd = true; for(int iGrid=0; iGrid<m_pGrids->Get_Count(); iGrid++) { if( !m_pGrids->asGrid(iGrid)->is_InGrid_byPos(pPoint->Get_Point(0)) ) { bAdd = false; } } if( bAdd ) { m_Search.Add_Point(pPoint->Get_Point(0).x, pPoint->Get_Point(0).y, pPoint->asDouble(m_zField)); } } } if( !m_Search.is_Okay() ) { SG_UI_Msg_Add(_TL("could not initialize point search engine"), true); return( false ); } //----------------------------------------------------- int nPoints_Max; switch( m_Mode ) { default: nPoints_Max = m_nPoints_Max; break; case 1: nPoints_Max = m_nPoints_Max * 4; break; } m_Points.Set_Count (nPoints_Max); m_G .Create (nPoints_Max + 1 + m_pGrids->Get_Count() + (m_bCoords ? 2 : 0)); m_W .Create (nPoints_Max + 1 + m_pGrids->Get_Count() + (m_bCoords ? 2 : 0), nPoints_Max + 1 + m_pGrids->Get_Count() + (m_bCoords ? 2 : 0)); return( true ); }
//--------------------------------------------------------- bool CAdd_Polygon_Attributes::On_Execute(void) { //----------------------------------------------------- CSG_Shapes *pInput = Parameters("INPUT")->asShapes(); if( !pInput->is_Valid() ) { Error_Set(_TL("Invalid points layer.")); return( false ); } //----------------------------------------------------- CSG_Shapes *pPolygons = Parameters("POLYGONS")->asShapes(); if( !pPolygons->is_Valid() ) { Error_Set(_TL("Invalid polygon layer.")); return( false ); } //----------------------------------------------------- CSG_Parameter_Table_Fields *pFields = Parameters("FIELDS")->asTableFields(); if( pFields->Get_Count() == 0 ) { CSG_String sFields; for(int iField=0; iField<pPolygons->Get_Field_Count(); iField++) { sFields += CSG_String::Format(SG_T("%d,"), iField); } pFields->Set_Value(sFields); } //----------------------------------------------------- CSG_Shapes *pOutput = Parameters("OUTPUT")->asShapes(); if( pOutput && pOutput != pInput ) { pOutput->Create(*pInput); } else { Parameters("OUTPUT")->Set_Value(pOutput = pInput); } pOutput->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pInput->Get_Name(), pPolygons->Get_Name())); //----------------------------------------------------- int outField = pOutput->Get_Field_Count(); { for(int iField=0; iField<pFields->Get_Count(); iField++) { int jField = pFields->Get_Index(iField); pOutput->Add_Field(pPolygons->Get_Field_Name(jField), pPolygons->Get_Field_Type(jField)); } } //----------------------------------------------------- for(int iPoint=0; iPoint<pOutput->Get_Count() && Set_Progress(iPoint, pOutput->Get_Count()); iPoint++) { CSG_Shape *pPoint = pOutput ->Get_Shape(iPoint); CSG_Shape *pPolygon = pPolygons ->Get_Shape(pPoint->Get_Point(0)); if( pPolygon ) { for(int iField=0; iField<pFields->Get_Count(); iField++) { int jField = pFields->Get_Index(iField); switch( pPolygons->Get_Field_Type(jField) ) { case SG_DATATYPE_String: case SG_DATATYPE_Date: pPoint->Set_Value(outField + iField, pPolygon->asString(jField)); break; default: pPoint->Set_Value(outField + iField, pPolygon->asDouble(jField)); break; } } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CWASP_MAP_Export::On_Execute(void) { int zField; FILE *Stream; CSG_String fName; CSG_Shape *pLine; CSG_Shapes *pLines; //----------------------------------------------------- pLines = Parameters("SHAPES") ->asShapes(); zField = Parameters("ELEVATION") ->asInt(); fName = Parameters("FILE") ->asString(); //----------------------------------------------------- if( pLines && pLines->is_Valid() && (Stream = fopen(fName.b_str(), "w")) != NULL ) { // 1) Text string identifying the terrain map: + ... fprintf(Stream, "+ %s\n", pLines->Get_Name()); // 2) Fixed point #1 in user and metric [m] coordinates: // X1(user) Y1(user) X1(metric) Y1(metric) fprintf(Stream, "%f %f %f %f\n", 0.0, 0.0, 0.0, 0.0); // 3) Fixed point #2 in user and metric [m] coordinates: // X2(user) Y2(user) X2(metric) Y2(metric) fprintf(Stream, "%f %f %f %f\n", 1.0, 1.0, 1.0, 1.0); // 4) Scaling factor and offset for height scale (Z): // Zmetric = {scaling factor}(Zuser + {offset}) fprintf(Stream, "%f %f\n", 1.0, 0.0); for(int iLine=0; iLine<pLines->Get_Count() && Set_Progress(iLine, pLines->Get_Count()); iLine++) { pLine = pLines->Get_Shape(iLine); for(int iPart=0; iPart<pLine->Get_Part_Count(); iPart++) { if( pLine->Get_Point_Count(iPart) > 1 ) { // 5a) Height contour: elevation (Z) and number of points (n) in line: // Z n fprintf(Stream, "%f %d\n", pLine->asDouble(zField), pLine->Get_Point_Count(iPart)); // 5b) Roughness change line: // roughness lengths to the left (z0l) and right (z0r) side of the line, // respectively, and number of points: // z0l z0r n // 5c) Roughness and contour line: // roughness lengths to the left and right of the line, // respectively, elevation and number of points: // z0l z0r Z n // 6–) Cartesian coordinates (X, Y) of line described in 5a, 5b or 5c: // X1 Y1 [... Xn Yn] // Xn+1 Yn+1 // ... where [] embrace optional numbers and n is > 0 for(int iPoint=0; iPoint<pLine->Get_Point_Count(iPart); iPoint++) { TSG_Point p = pLine->Get_Point(iPoint, iPart); fprintf(Stream, "%f\t%f\n", p.x, p.y); } } } } fclose(Stream); return( true ); } return( false ); }
//--------------------------------------------------------- bool CAdd_Polygon_Attributes::On_Execute(void) { int inField, outField; CSG_Shapes *pInput, *pOutput, *pPolygons; //----------------------------------------------------- pInput = Parameters("INPUT") ->asShapes(); pOutput = Parameters("OUTPUT") ->asShapes(); pPolygons = Parameters("POLYGONS") ->asShapes(); inField = Parameters("FIELD") ->asInt(); //----------------------------------------------------- if( !pInput->is_Valid() ) { Message_Add(_TL("Invalid points layer.")); return( false ); } else if( !pPolygons->is_Valid() ) { Message_Add(_TL("Invalid polygon layer.")); return( false ); } //----------------------------------------------------- if( pOutput && pOutput != pInput ) { pOutput->Create(*pInput); } else { Parameters("RESULT")->Set_Value(pOutput = pInput); } pOutput->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pInput->Get_Name(), pPolygons->Get_Name())); //----------------------------------------------------- outField = pOutput->Get_Field_Count(); if( inField >= 0 && inField < pPolygons->Get_Field_Count() ) { // add single attribute pOutput->Add_Field(pPolygons->Get_Field_Name(inField), pPolygons->Get_Field_Type(inField)); } else { // add all attributes inField = -1; for(int iField=0; iField<pPolygons->Get_Field_Count(); iField++) { pOutput->Add_Field(pPolygons->Get_Field_Name(iField), pPolygons->Get_Field_Type(iField)); } } //----------------------------------------------------- for(int iPoint=0; iPoint<pOutput->Get_Count() && Set_Progress(iPoint, pOutput->Get_Count()); iPoint++) { CSG_Shape *pPoint = pOutput ->Get_Shape(iPoint); CSG_Shape *pPolygon = pPolygons ->Get_Shape(pPoint->Get_Point(0)); if( pPolygon ) { if( inField >= 0 ) { // add single attribute pPoint->Set_Value(outField, pPolygon->asString(inField)); } else { // add all attributes for(int iField=0; iField<pPolygons->Get_Field_Count(); iField++) { switch( pPolygons->Get_Field_Type(iField) ) { case SG_DATATYPE_String: case SG_DATATYPE_Date: pPoint->Set_Value(outField + iField, pPolygon->asString(iField)); break; default: pPoint->Set_Value(outField + iField, pPolygon->asDouble(iField)); break; } } } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool COGR_DataSource::Write_Shapes(CSG_Shapes *pShapes) { OGRLayer *pLayer; //----------------------------------------------------- if( m_pDataSource && pShapes && pShapes->is_Valid() && (pLayer = m_pDataSource->CreateLayer(SG_STR_SGTOMB(pShapes->Get_Name()), NULL, g_OGR_Driver.Get_Type(pShapes->Get_Type()))) != NULL ) { bool bResult = true; int iField; //------------------------------------------------- for(iField=0; iField<pShapes->Get_Field_Count() && bResult; iField++) { OGRFieldDefn DefField(SG_STR_SGTOMB(pShapes->Get_Field_Name(iField)), g_OGR_Driver.Get_Type(pShapes->Get_Field_Type(iField))); // DefField.SetWidth(32); if( pLayer->CreateField(&DefField) != OGRERR_NONE ) { bResult = false; } } //------------------------------------------------- for(int iShape=0; iShape<pShapes->Get_Count() && bResult && SG_UI_Process_Set_Progress(iShape, pShapes->Get_Count()); iShape++) { CSG_Shape *pShape = pShapes->Get_Shape(iShape); OGRFeature *pFeature = OGRFeature::CreateFeature(pLayer->GetLayerDefn()); for(iField=0; iField<pShapes->Get_Field_Count(); iField++) { switch( pShapes->Get_Field_Type(iField) ) { default: case SG_DATATYPE_Char: case SG_DATATYPE_String: case SG_DATATYPE_Date: pFeature->SetField(iField, SG_STR_SGTOMB(pShape->asString(iField))); break; case SG_DATATYPE_Short: case SG_DATATYPE_Int: case SG_DATATYPE_Long: case SG_DATATYPE_Color: pFeature->SetField(iField, pShape->asInt(iField)); break; case SG_DATATYPE_Float: case SG_DATATYPE_Double: pFeature->SetField(iField, pShape->asDouble(iField)); break; } } if( !_Write_Geometry(pShape, pFeature) || pLayer->CreateFeature(pFeature) != OGRERR_NONE ) { bResult = false; } OGRFeature::DestroyFeature(pFeature); } //------------------------------------------------- return( bResult ); } return( false ); }
//--------------------------------------------------------- bool CPoints_Filter::On_Execute(void) { bool bQuadrants; int zField; CSG_Shapes *pPoints, *pFilter; //----------------------------------------------------- pPoints = Parameters("POINTS") ->asShapes(); pFilter = Parameters("FILTER") ->asShapes(); zField = Parameters("FIELD") ->asInt(); bQuadrants = Parameters("QUADRANTS") ->asBool(); m_Method = Parameters("METHOD") ->asInt(); m_nMinPoints = Parameters("MINNUM") ->asInt(); m_nMaxPoints = Parameters("MAXNUM") ->asInt(); m_Radius = Parameters("RADIUS") ->asDouble(); m_Tolerance = Parameters("TOLERANCE") ->asDouble(); m_Percentile = Parameters("PERCENT") ->asDouble(); //----------------------------------------------------- if( !pPoints->is_Valid() ) { Error_Set(_TL("invalid points layer")); return( false ); } if( pPoints->Get_Count() <= 0 ) { Error_Set(_TL("no points in layer")); return( false ); } if( !m_Search.Create(pPoints, zField) ) { Error_Set(_TL("failed to initialise search engine")); return( false ); } //----------------------------------------------------- if( pFilter ) { pFilter->Create(SHAPE_TYPE_Point, CSG_String::Format(SG_T("%s [%s]"), pPoints->Get_Name(), _TL("Filtered")), pPoints); } else { pPoints->Select(); } //----------------------------------------------------- int nFiltered = 0; for(int i=0; i<pPoints->Get_Count() && Set_Progress(i, pPoints->Get_Count()); i++) { CSG_Shape *pPoint = pPoints->Get_Shape(i); if( pPoint ) { bool bFilter = bQuadrants ? Do_Filter(pPoint->Get_Point(0), pPoint->asDouble(zField), 0) || Do_Filter(pPoint->Get_Point(0), pPoint->asDouble(zField), 1) || Do_Filter(pPoint->Get_Point(0), pPoint->asDouble(zField), 2) || Do_Filter(pPoint->Get_Point(0), pPoint->asDouble(zField), 3) : Do_Filter(pPoint->Get_Point(0), pPoint->asDouble(zField)); if( bFilter ) { nFiltered++; if( !pFilter ) { pPoints->Select(i, true); } } else if( pFilter ) { pFilter->Add_Shape(pPoint); } } } //----------------------------------------------------- if( !pFilter ) { pPoints->Del_Selection(); DataObject_Update(pPoints); } Message_Fmt("\n%d %s", nFiltered, _TL("points have been filtered")); return( true ); }
//--------------------------------------------------------- bool CGSPoints_Semi_Variances::On_Execute(void) { int i, j, k, n, nDistances, nSkip, Attribute; double zi, zj, zMean, v, c, maxDistance, lagDistance; TSG_Point Pt_i, Pt_j; CSG_Vector Count, Variance, Covariance; CSG_Table_Record *pRecord; CSG_Table *pTable; CSG_Shape *pPoint; CSG_Shapes *pPoints; //----------------------------------------------------- pPoints = Parameters("POINTS") ->asShapes(); pTable = Parameters("RESULT") ->asTable(); Attribute = Parameters("FIELD") ->asInt(); nSkip = Parameters("NSKIP") ->asInt(); maxDistance = Parameters("DISTMAX") ->asDouble(); nDistances = Parameters("DISTCOUNT") ->asInt(); if( maxDistance <= 0.0 ) { maxDistance = SG_Get_Length(pPoints->Get_Extent().Get_XRange(), pPoints->Get_Extent().Get_YRange()); } lagDistance = maxDistance / nDistances; zMean = pPoints->Get_Mean(Attribute); Count .Create(nDistances); Variance .Create(nDistances); Covariance .Create(nDistances); //----------------------------------------------------- for(i=0, n=0; i<pPoints->Get_Count() && Set_Progress(n, SG_Get_Square(pPoints->Get_Count()/nSkip)/2); i+=nSkip) { pPoint = pPoints->Get_Shape(i); if( !pPoint->is_NoData(Attribute) ) { Pt_i = pPoint->Get_Point(0); zi = pPoint->asDouble(Attribute); for(j=i+nSkip; j<pPoints->Get_Count(); j+=nSkip, n++) { pPoint = pPoints->Get_Shape(j); if( !pPoint->is_NoData(Attribute) ) { Pt_j = pPoint->Get_Point(0); k = (int)(SG_Get_Distance(Pt_i, Pt_j) / lagDistance); if( k < nDistances ) { zj = pPoint->asDouble(Attribute); v = SG_Get_Square(zi - zj); c = (zi - zMean) * (zj - zMean); Count [k] ++; Variance [k] += v; Covariance[k] += c; } } } } } //----------------------------------------------------- pTable->Destroy(); pTable->Set_Name(CSG_String::Format(SG_T("%s [%s: %s]"), pPoints->Get_Name(), _TL("Variogram"), pPoints->Get_Field_Name(Attribute))); pTable->Add_Field(_TL("Class") , SG_DATATYPE_Int); // FIELD_CLASSNR pTable->Add_Field(_TL("Distance") , SG_DATATYPE_Double); // FIELD_DISTANCE pTable->Add_Field(_TL("Count") , SG_DATATYPE_Int); // FIELD_COUNT pTable->Add_Field(_TL("Variance") , SG_DATATYPE_Double); // FIELD_VARIANCE pTable->Add_Field(_TL("Cum.Var.") , SG_DATATYPE_Double); // FIELD_VARCUMUL pTable->Add_Field(_TL("Covariance") , SG_DATATYPE_Double); // FIELD_COVARIANCE pTable->Add_Field(_TL("Cum.Covar.") , SG_DATATYPE_Double); // FIELD_COVARCUMUL for(i=0, v=0.0, c=0.0, n=0; i<nDistances; i++) { if( Count[i] > 0 ) { n += (int)Count[i]; v += Variance [i]; c += Covariance[i]; pRecord = pTable->Add_Record(); pRecord->Set_Value(FIELD_CLASSNR , (i + 1)); pRecord->Set_Value(FIELD_DISTANCE , (i + 1) * lagDistance); pRecord->Set_Value(FIELD_COUNT , Count[i]); pRecord->Set_Value(FIELD_VARIANCE , 0.5 * Variance [i] / Count[i]); pRecord->Set_Value(FIELD_VARCUMUL , 0.5 * v / n); pRecord->Set_Value(FIELD_COVARIANCE , 1.0 * Covariance[i] / Count[i]); pRecord->Set_Value(FIELD_COVARCUMUL , 1.0 * c / n); } } return( true ); }
//--------------------------------------------------------- bool CShapes2Grid::On_Execute(void) { int iField, iShape, iType; //----------------------------------------------------- m_pShapes = Parameters("INPUT") ->asShapes(); m_Method_Lines = Parameters("LINE_TYPE") ->asInt(); iField = Parameters("FIELD") ->asInt(); iType = Parameters("GRID_TYPE") ->asInt(); if( iField < 0 || iField >= m_pShapes->Get_Field_Count() || m_pShapes->Get_Field_Type(iField) == SG_DATATYPE_String ) { iField = -1; Message_Add(_TL("WARNING: selected attribute is not numeric; generating unique identifiers instead.")); } //----------------------------------------------------- m_pGrid = NULL; switch( Parameters("TARGET")->asInt() ) { case 0: // user defined... if( m_Grid_Target.Init_User(m_pShapes->Get_Extent()) && Dlg_Parameters("USER") ) { m_pGrid = m_Grid_Target.Get_User(Get_Grid_Type(iType)); } break; case 1: // grid... if( Dlg_Parameters("GRID") ) { m_pGrid = m_Grid_Target.Get_Grid(Get_Grid_Type(iType)); } break; } if( m_pGrid == NULL ) { return( false ); } //------------------------------------------------- m_pGrid->Set_Name(CSG_String::Format(SG_T("%s [%s]"), m_pShapes->Get_Name(), iField < 0 ? _TL("ID") : m_pShapes->Get_Field_Name(iField))); m_pGrid->Assign_NoData(); m_pLock = m_pShapes->Get_Type() == SHAPE_TYPE_Point ? NULL : SG_Create_Grid(m_pGrid, SG_DATATYPE_Byte); //----------------------------------------------------- for(iShape=0, m_Lock_ID=1; iShape<m_pShapes->Get_Count() && Set_Progress(iShape, m_pShapes->Get_Count()); iShape++, m_Lock_ID++) { CSG_Shape *pShape = m_pShapes->Get_Shape(iShape); if( m_pShapes->Get_Selection_Count() <= 0 || pShape->is_Selected() ) { if( iField < 0 || !pShape->is_NoData(iField) ) { m_Value = iField < 0 ? iShape + 1 : pShape->asDouble(iField); if( pShape->Intersects(m_pGrid->Get_Extent().m_rect) ) { switch( m_pShapes->Get_Type() ) { case SHAPE_TYPE_Point: case SHAPE_TYPE_Points: Set_Points (pShape); break; case SHAPE_TYPE_Line: Set_Line (pShape); break; case SHAPE_TYPE_Polygon: Set_Polygon (pShape); break; } } } } } //----------------------------------------------------- if( m_pLock ) { delete(m_pLock); } return( true ); }
//--------------------------------------------------------- bool CPolygonStatisticsFromPoints::On_Execute(void) { //----------------------------------------------------- bool bSum = Parameters("SUM")->asBool(); bool bAvg = Parameters("AVG")->asBool(); bool bVar = Parameters("VAR")->asBool(); bool bDev = Parameters("DEV")->asBool(); bool bMin = Parameters("MIN")->asBool(); bool bMax = Parameters("MAX")->asBool(); bool bNum = Parameters("NUM")->asBool(); if( !bSum && !bAvg && !bVar && !bDev && !bMin && !bMax && !bNum ) { Error_Set(_TL("no target variable in selection")); return( false ); } //----------------------------------------------------- CSG_Parameter_Table_Fields *pFields = Parameters("FIELDS")->asTableFields(); if( pFields->Get_Count() <= 0 ) { Error_Set(_TL("no attributes in selection")); return( false ); } //----------------------------------------------------- CSG_Shapes *pPoints = Parameters("POINTS" )->asShapes(); CSG_Shapes *pPolygons = Parameters("POLYGONS")->asShapes(); if( pPolygons->Get_Count() <= 0 || pPoints->Get_Count() <= 0 ) { Error_Set(_TL("no records in input data")); return( false ); } //----------------------------------------------------- if( Parameters("STATISTICS")->asShapes() == NULL ) { Parameters("STATISTICS")->Set_Value(pPolygons); } else if( pPolygons != Parameters("STATISTICS")->asShapes() ) { Parameters("STATISTICS")->asShapes()->Assign(pPolygons); pPolygons = Parameters("STATISTICS")->asShapes(); } //----------------------------------------------------- int i, n, Offset = pPolygons->Get_Field_Count(); for(i=0; i<pFields->Get_Count(); i++) { CSG_String sName = pPoints->Get_Field_Name(pFields->Get_Index(i)); if( bSum ) { pPolygons->Add_Field(Get_Field_Name("SUM", sName), SG_DATATYPE_Double); } if( bAvg ) { pPolygons->Add_Field(Get_Field_Name("AVG", sName), SG_DATATYPE_Double); } if( bVar ) { pPolygons->Add_Field(Get_Field_Name("VAR", sName), SG_DATATYPE_Double); } if( bDev ) { pPolygons->Add_Field(Get_Field_Name("DEV", sName), SG_DATATYPE_Double); } if( bMin ) { pPolygons->Add_Field(Get_Field_Name("MIN", sName), SG_DATATYPE_Double); } if( bMax ) { pPolygons->Add_Field(Get_Field_Name("MAX", sName), SG_DATATYPE_Double); } if( bNum ) { pPolygons->Add_Field(Get_Field_Name("NUM", sName), SG_DATATYPE_Long ); } } //----------------------------------------------------- CSG_Simple_Statistics *Statistics = new CSG_Simple_Statistics[pFields->Get_Count()]; for(int iPolygon=0; iPolygon<pPolygons->Get_Count() && Set_Progress(iPolygon, pPolygons->Get_Count()); iPolygon++) { CSG_Shape_Polygon *pPolygon = (CSG_Shape_Polygon *)pPolygons->Get_Shape(iPolygon); //------------------------------------------------- for(i=0; i<pFields->Get_Count(); i++) { Statistics[i].Invalidate(); } //------------------------------------------------- for(int iPoint=0; iPoint<pPoints->Get_Count() && Process_Get_Okay(); iPoint++) { CSG_Shape *pPoint = pPoints->Get_Shape(iPoint); if( pPolygon->Contains(pPoint->Get_Point(0)) ) { for(i=0; i<pFields->Get_Count(); i++) { if( !pPoint->is_NoData(pFields->Get_Index(i))) { Statistics[i].Add_Value(pPoint->asDouble(pFields->Get_Index(i))); } } } } //------------------------------------------------- for(i=0, n=Offset; i<pFields->Get_Count(); i++) { if( Statistics[i].Get_Count() > 0 ) { if( bSum ) { pPolygon->Set_Value (n++, Statistics[i].Get_Sum()); } if( bAvg ) { pPolygon->Set_Value (n++, Statistics[i].Get_Mean()); } if( bVar ) { pPolygon->Set_Value (n++, Statistics[i].Get_Variance()); } if( bDev ) { pPolygon->Set_Value (n++, Statistics[i].Get_StdDev()); } if( bMin ) { pPolygon->Set_Value (n++, Statistics[i].Get_Minimum()); } if( bMax ) { pPolygon->Set_Value (n++, Statistics[i].Get_Maximum()); } if( bNum ) { pPolygon->Set_Value (n++, (double)Statistics[i].Get_Count()); } } else { if( bSum ) { pPolygon->Set_NoData(n++); } if( bAvg ) { pPolygon->Set_NoData(n++); } if( bVar ) { pPolygon->Set_NoData(n++); } if( bDev ) { pPolygon->Set_NoData(n++); } if( bMin ) { pPolygon->Set_NoData(n++); } if( bMax ) { pPolygon->Set_NoData(n++); } if( bNum ) { pPolygon->Set_Value (n++, 0.0); } } } } //----------------------------------------------------- delete[](Statistics); DataObject_Update(pPolygons); return( true ); }
//--------------------------------------------------------- bool CPolygonStatisticsFromPoints::On_Execute(void) { int i, j, n, Offset, *bAttribute; CSG_Simple_Statistics *Statistics; CSG_Parameters *pParameters; CSG_Shapes *pPolygons, *pPoints; //----------------------------------------------------- pPoints = Parameters("POINTS") ->asShapes(); pPolygons = Parameters("POLYGONS") ->asShapes(); if( pPolygons->Get_Count() <= 0 || pPoints->Get_Count() <= 0 ) { return( false ); } //----------------------------------------------------- pParameters = Get_Parameters("ATTRIBUTES"); pParameters->Del_Parameters(); for(i=0; i<pPoints->Get_Field_Count(); i++) { if( SG_Data_Type_is_Numeric(pPoints->Get_Field_Type(i)) ) { CSG_Parameter *pNode = pParameters->Add_Node(NULL, CSG_String::Format(SG_T("%d"), i), pPoints->Get_Field_Name(i), _TL("")); for(j=0; j<STAT_Count; j++) { pParameters->Add_Value(pNode, CSG_String::Format(SG_T("%d|%d"), i, j), CSG_String::Format(SG_T("[%s]"), STAT_Name[j].c_str()), _TL(""), PARAMETER_TYPE_Bool, false ); } } } if( !Dlg_Parameters("ATTRIBUTES") ) { return( false ); } //----------------------------------------------------- if( Parameters("STATISTICS")->asShapes() == NULL ) { Parameters("STATISTICS")->Set_Value(pPolygons); } else if( pPolygons != Parameters("STATISTICS")->asShapes() ) { Parameters("STATISTICS")->asShapes()->Assign(pPolygons); pPolygons = Parameters("STATISTICS")->asShapes(); } //----------------------------------------------------- bAttribute = new int[pPoints->Get_Field_Count()]; Offset = pPolygons->Get_Field_Count(); for(i=0, n=0; i<pPoints->Get_Field_Count(); i++) { bAttribute[i] = 0; if( SG_Data_Type_is_Numeric(pPoints->Get_Field_Type(i)) ) { for(j=0; j<STAT_Count; j++) { CSG_Parameter *pParameter = pParameters->Get_Parameter(CSG_String::Format(SG_T("%d|%d"), i, j)); if( pParameter && pParameter->asBool() ) { bAttribute[i] |= STAT_Flag[j]; pPolygons->Add_Field(CSG_String::Format(SG_T("%s_%s"), pPoints->Get_Field_Name(i), STAT_Name[j].c_str()), SG_DATATYPE_Double); n++; } } } } if( n == 0 ) { delete[](bAttribute); return( false ); } //----------------------------------------------------- Statistics = new CSG_Simple_Statistics[pPoints->Get_Field_Count()]; for(int iPolygon=0; iPolygon<pPolygons->Get_Count() && Set_Progress(iPolygon, pPolygons->Get_Count()); iPolygon++) { CSG_Shape_Polygon *pPolygon = (CSG_Shape_Polygon *)pPolygons->Get_Shape(iPolygon); //------------------------------------------------- for(i=0; i<pPoints->Get_Field_Count(); i++) { Statistics[i].Invalidate(); } //------------------------------------------------- for(int iPoint=0; iPoint<pPoints->Get_Count() && Process_Get_Okay(); iPoint++) { CSG_Shape *pPoint = pPoints->Get_Shape(iPoint); if( pPolygon->is_Containing(pPoint->Get_Point(0)) ) { for(i=0; i<pPoints->Get_Field_Count(); i++) { if( bAttribute[i] ) { Statistics[i].Add_Value(pPoint->asDouble(i)); } } } } //------------------------------------------------- for(i=0, n=Offset; i<pPoints->Get_Field_Count(); i++) { if( bAttribute[i] ) { if( Statistics[i].Get_Count() > 0 ) { if( bAttribute[i] & STAT_Flag[STAT_Sum] ) { pPolygon->Set_Value(n++, Statistics[i].Get_Sum()); } if( bAttribute[i] & STAT_Flag[STAT_Avg] ) { pPolygon->Set_Value(n++, Statistics[i].Get_Mean()); } if( bAttribute[i] & STAT_Flag[STAT_Var] ) { pPolygon->Set_Value(n++, Statistics[i].Get_Variance()); } if( bAttribute[i] & STAT_Flag[STAT_Dev] ) { pPolygon->Set_Value(n++, Statistics[i].Get_StdDev()); } if( bAttribute[i] & STAT_Flag[STAT_Min] ) { pPolygon->Set_Value(n++, Statistics[i].Get_Minimum()); } if( bAttribute[i] & STAT_Flag[STAT_Max] ) { pPolygon->Set_Value(n++, Statistics[i].Get_Maximum()); } if( bAttribute[i] & STAT_Flag[STAT_Num] ) { pPolygon->Set_Value(n++, Statistics[i].Get_Count()); } } else { if( bAttribute[i] & STAT_Flag[STAT_Sum] ) { pPolygon->Set_NoData(n++); } if( bAttribute[i] & STAT_Flag[STAT_Avg] ) { pPolygon->Set_NoData(n++); } if( bAttribute[i] & STAT_Flag[STAT_Var] ) { pPolygon->Set_NoData(n++); } if( bAttribute[i] & STAT_Flag[STAT_Dev] ) { pPolygon->Set_NoData(n++); } if( bAttribute[i] & STAT_Flag[STAT_Min] ) { pPolygon->Set_NoData(n++); } if( bAttribute[i] & STAT_Flag[STAT_Max] ) { pPolygon->Set_NoData(n++); } if( bAttribute[i] & STAT_Flag[STAT_Num] ) { pPolygon->Set_NoData(n++); } } } } } //----------------------------------------------------- delete[](Statistics); delete[](bAttribute); DataObject_Update(pPolygons); return( true ); }
//--------------------------------------------------------- bool CWaterRetentionCapacity::On_Execute(void) { CSG_Shapes *pInput = Parameters("SHAPES")->asShapes(); CSG_Shapes *pOutput = Parameters("OUTPUT")->asShapes(); if( pInput->Get_Field_Count() < 5 ) { Error_Set(_TL("Plot hole data has to provide at the very least five attributes (horizon depth, TF, L, Ar, Mo).")); return( false ); } pOutput->Create(SHAPE_TYPE_Point, _TL("Water Retention Capacity")); pOutput->Add_Field("CCC" , SG_DATATYPE_Double); pOutput->Add_Field("CIL" , SG_DATATYPE_Double); pOutput->Add_Field("Permeability" , SG_DATATYPE_Double); pOutput->Add_Field("Equivalent Moisture" , SG_DATATYPE_Double); pOutput->Add_Field("Water Retention Capacity", SG_DATATYPE_Double); //----------------------------------------------------- CSG_Grid *pDEM = Parameters("DEM")->asGrid(); CSG_Matrix Data(5, pInput->Get_Field_Count() / 5); for(int iPoint=0; iPoint<pInput->Get_Count(); iPoint++) { CSG_Shape *pPoint = pInput->Get_Shape(iPoint); for(int iHorizon=0, n=0; iHorizon<Data.Get_NRows(); iHorizon++, n+=5) { for(int i=0; i<5; i++) { Data[iHorizon][i] = pPoint->asDouble(n + i); } } double Slope, Aspect; if( !pDEM->Get_Gradient(pPoint->Get_Point(0), Slope, Aspect, GRID_RESAMPLING_BSpline) ) { Slope = 0.0; } Get_WaterRetention(Data, 1. - tan(Slope), pOutput->Add_Shape(pPoint, SHAPE_COPY_GEOM)); } //----------------------------------------------------- CSG_Grid *pRetention = Parameters("RETENTION")->asGrid(); if( pRetention ) { switch( Parameters("INTERPOL")->asInt() ) { default: // Multlevel B-Spline Interpolation SG_RUN_TOOL_ExitOnError("grid_spline", 4, SG_TOOL_PARAMETER_SET("SHAPES" , pOutput) && SG_TOOL_PARAMETER_SET("FIELD" , pOutput->Get_Field_Count() - 1) && SG_TOOL_PARAMETER_SET("TARGET_DEFINITION", 1) // grid or grid system && SG_TOOL_PARAMETER_SET("TARGET_OUT_GRID" , pRetention) ); break; case 1: // Inverse Distance Weighted SG_RUN_TOOL_ExitOnError("grid_gridding", 1, SG_TOOL_PARAMETER_SET("SHAPES" , pOutput) && SG_TOOL_PARAMETER_SET("FIELD" , pOutput->Get_Field_Count() - 1) && SG_TOOL_PARAMETER_SET("TARGET_DEFINITION", 1) // grid or grid system && SG_TOOL_PARAMETER_SET("TARGET_OUT_GRID" , pRetention) && SG_TOOL_PARAMETER_SET("SEARCH_RANGE" , 1) // global && SG_TOOL_PARAMETER_SET("SEARCH_POINTS_ALL", 1) // all points within search distance ); break; } if( Parameters("SLOPECORR")->asBool() ) { #pragma omp parallel for for(int y=0; y<Get_NY(); y++) { for(int x=0; x<Get_NX(); x++) { if( !pRetention->is_NoData(x, y) ) { double Slope, Aspect; if( !pDEM->Get_Gradient(x, y, Slope, Aspect) ) { Slope = 0.0; } pRetention->Mul_Value(x, y, 1. - tan(Slope)); } } } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CShapes_Convert_Vertex_Type::On_Execute(void) { CSG_Shapes *pInput, *pOutput; int iFieldZ, iFieldM; pInput = Parameters("INPUT")->asShapes(); iFieldZ = Parameters("FIELD_Z")->asInt(); iFieldM = Parameters("FIELD_M")->asInt(); pOutput = Parameters("OUTPUT")->asShapes(); if( pInput->Get_Count() < 1 ) { SG_UI_Msg_Add_Error(_TL("Input shape is empty!")); return (false); } //----------------------------------------------------- if( pInput->Get_Vertex_Type() == SG_VERTEX_TYPE_XY ) { if( iFieldZ < 0 ) { SG_UI_Msg_Add_Error(_TL("Please provide an attribute field with z-information!")); return( false ); } if( iFieldM < 0 ) { pOutput->Create(pInput->Get_Type(), CSG_String::Format(SG_T("%s_Z"), pInput->Get_Name()), pInput, SG_VERTEX_TYPE_XYZ); } else { pOutput->Create(pInput->Get_Type(), CSG_String::Format(SG_T("%s_ZM"), pInput->Get_Name()), pInput, SG_VERTEX_TYPE_XYZM); } } else { pOutput->Create(pInput->Get_Type(), CSG_String::Format(SG_T("%s_XY"), pInput->Get_Name()), pInput, SG_VERTEX_TYPE_XY); pOutput->Add_Field(SG_T("Z"), SG_DATATYPE_Double); if( pInput->Get_Vertex_Type() == SG_VERTEX_TYPE_XYZM ) { pOutput->Add_Field(SG_T("M"), SG_DATATYPE_Double); } } //----------------------------------------------------- for(int iShape=0; iShape<pInput->Get_Count(); iShape++) { CSG_Shape *pShapeIn = pInput ->Get_Shape(iShape); CSG_Shape *pShapeOut = pOutput ->Add_Shape(pShapeIn, SHAPE_COPY_ATTR); for(int iPart=0; iPart<pShapeIn->Get_Part_Count(); iPart++) { for(int iPoint=0; iPoint<pShapeIn->Get_Point_Count(iPart); iPoint++) { pShapeOut->Add_Point(pShapeIn->Get_Point(iPoint, iPart), iPart); if( pInput->Get_Vertex_Type() == SG_VERTEX_TYPE_XY ) { pShapeOut->Set_Z(pShapeIn->asDouble(iFieldZ), iPoint, iPart); if( pOutput->Get_Vertex_Type() == SG_VERTEX_TYPE_XYZM ) { pShapeOut->Set_M(pShapeIn->asDouble(iFieldM), iPoint, iPart); } } else { if( pInput->Get_Vertex_Type() == SG_VERTEX_TYPE_XYZM ) { pShapeOut->Set_Value(pOutput->Get_Field_Count() - 1, pShapeIn->Get_M(iPoint, iPart)); pShapeOut->Set_Value(pOutput->Get_Field_Count() - 2, pShapeIn->Get_Z(iPoint, iPart)); } else { pShapeOut->Set_Value(pOutput->Get_Field_Count() - 1, pShapeIn->Get_Z(iPoint, iPart)); } } } } } //----------------------------------------------------- return( true ); }