//--------------------------------------------------------- void CPolygon_Clip::Clip_Lines(CSG_Shapes *pClips, CSG_Shapes *pInputs, CSG_Shapes *pOutputs) { for(int iClip=0; iClip<pClips->Get_Count() && Set_Progress(iClip, pClips->Get_Count()); iClip++) { CSG_Shape_Polygon *pClip = (CSG_Shape_Polygon *)pClips->Get_Shape(iClip); if( pInputs->Select(pClip->Get_Extent()) ) { for(int iInput=0; iInput<pInputs->Get_Selection_Count(); iInput++) { CSG_Shape *pNew_Line, *pLine = pInputs->Get_Selection(iInput); for(int iPart=0, jPart=-1; iPart<pLine->Get_Part_Count(); iPart++) { TSG_Point B, A = pLine->Get_Point(0, iPart); bool bIn = pClip->Contains(A); if( bIn ) { pNew_Line = pOutputs->Add_Shape(pLine, SHAPE_COPY_ATTR); pNew_Line->Add_Point(A, ++jPart); } for(int iPoint=1; iPoint<pLine->Get_Point_Count(iPart); iPoint++) { B = A; A = pLine->Get_Point(iPoint, iPart); if( bIn ) { if( pClip->Contains(A) ) { pNew_Line->Add_Point(A, jPart); } else { pNew_Line->Add_Point(Get_Crossing(pClip, A, B), jPart); bIn = false; } } else if( pClip->Contains(A) ) { if( jPart < 0 ) { pNew_Line = pOutputs->Add_Shape(pLine, SHAPE_COPY_ATTR); } pNew_Line->Add_Point(Get_Crossing(pClip, A, B), ++jPart); pNew_Line->Add_Point(A, jPart); bIn = true; } } } } } } }
//--------------------------------------------------------- void CPolygon_Clip::Clip_Points(CSG_Shapes *pClips, CSG_Shapes *pInputs, CSG_Shapes *pOutputs) { for(int iClip=0; iClip<pClips->Get_Count() && Set_Progress(iClip, pClips->Get_Count()); iClip++) { CSG_Shape_Polygon *pClip = (CSG_Shape_Polygon *)pClips->Get_Shape(iClip); for(int iInput=0; iInput<pInputs->Get_Count(); iInput++) { CSG_Shape *pInput = pInputs->Get_Shape(iInput); CSG_Shape *pOutput = NULL; for(int iPoint=0; iPoint<pInput->Get_Point_Count(0); iPoint++) { if( pClip->Contains(pInput->Get_Point(iPoint, 0)) ) { if( pOutput == NULL ) { pOutput = pOutputs->Add_Shape(pInput, SHAPE_COPY_ATTR); } pOutput->Add_Point(pInput->Get_Point(iPoint, 0)); } } } } }
//--------------------------------------------------------- bool CErosion_LS_Fields::Set_Fields(void) { CSG_Shapes *pFields = Parameters("FIELDS")->asShapes(); //----------------------------------------------------- if( !pFields || pFields->Get_Count() <= 0 ) { m_Fields.Create(*Get_System(), SG_DATATYPE_Char); // m_Fields.Set_NoData_Value(1.0); // m_Fields.Assign(0.0); #pragma omp parallel for for(int y=0; y<Get_NY(); y++) { for(int x=0; x<Get_NX(); x++) { if( !m_pDEM->is_InGrid(x, y) ) { m_Fields.Set_NoData(x, y); } } } return( true ); } //----------------------------------------------------- Process_Set_Text(_TL("Initializing Fields")); m_nFields = pFields->Get_Count(); m_Fields.Create(*Get_System(), m_nFields < pow(2.0, 16.0) - 1.0 ? SG_DATATYPE_Word : SG_DATATYPE_DWord); m_Fields.Set_NoData_Value(m_nFields); m_Fields.Assign_NoData(); //----------------------------------------------------- for(int iField=0; iField<pFields->Get_Count() && Set_Progress(iField, pFields->Get_Count()); iField++) { CSG_Shape_Polygon *pField = (CSG_Shape_Polygon *)pFields->Get_Shape(iField); int xMin = Get_System()->Get_xWorld_to_Grid(pField->Get_Extent().Get_XMin()) - 1; if( xMin < 0 ) xMin = 0; int xMax = Get_System()->Get_xWorld_to_Grid(pField->Get_Extent().Get_XMax()) + 1; if( xMax >= Get_NX() ) xMax = Get_NX() - 1; int yMin = Get_System()->Get_yWorld_to_Grid(pField->Get_Extent().Get_YMin()) - 1; if( yMin < 0 ) yMin = 0; int yMax = Get_System()->Get_yWorld_to_Grid(pField->Get_Extent().Get_YMax()) + 1; if( yMax >= Get_NY() ) yMax = Get_NY() - 1; for(int y=yMin; y<=yMax; y++) { for(int x=xMin; x<=xMax; x++) { if( m_pDEM->is_InGrid(x, y) && pField->Contains(Get_System()->Get_Grid_to_World(x, y)) ) { m_Fields.Set_Value(x, y, iField); } } } } //----------------------------------------------------- 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 CCRS_Transform_Grid::Set_Target_Area(const CSG_Grid_System &Source, const CSG_Grid_System &Target) { if( Parameters("TARGET_AREA")->asBool() == false ) { m_Target_Area.Destroy(); return( true ); } //----------------------------------------------------- CSG_Rect r(Source.Get_Extent()); if( m_Projector.Get_Source().Get_Type() == SG_PROJ_TYPE_CS_Geographic ) { if( r.Get_XMax() > 180.0 ) r.Move(-180.0, 0.0); if( r.Get_YMin() < -90.0 ) r.m_rect.yMin = -90.0; if( r.Get_YMax() < 90.0 ) r.m_rect.yMax = 90.0; } //----------------------------------------------------- CSG_Shapes Area(SHAPE_TYPE_Polygon); CSG_Shape_Polygon *pArea = (CSG_Shape_Polygon *)Area.Add_Shape(); TSG_Point p, q; double dx = Source.Get_XRange() / 100.0; double dy = Source.Get_YRange() / 100.0; m_Projector.Set_Inverse(false); for(p.x=r.Get_XMin(), p.y=r.Get_YMin(); p.y<r.Get_YMax(); p.y+=dy) { m_Projector.Get_Projection(q = p); pArea->Add_Point(q); } for(p.x=r.Get_XMin(), p.y=r.Get_YMax(); p.x<r.Get_XMax(); p.x+=dx) { m_Projector.Get_Projection(q = p); pArea->Add_Point(q); } for(p.x=r.Get_XMax(), p.y=r.Get_YMax(); p.y>r.Get_YMin(); p.y-=dy) { m_Projector.Get_Projection(q = p); pArea->Add_Point(q); } for(p.x=r.Get_XMax(), p.y=r.Get_YMin(); p.x>r.Get_XMin(); p.x-=dx) { m_Projector.Get_Projection(q = p); pArea->Add_Point(q); } m_Projector.Set_Inverse(true); //----------------------------------------------------- m_Target_Area.Create(Target, SG_DATATYPE_Char); m_Target_Area.Set_NoData_Value(0); for(int y=0; y<m_Target_Area.Get_NY() && Set_Progress(y, m_Target_Area.Get_NY()); y++) { double yWorld = Target.Get_yGrid_to_World(y); #pragma omp parallel for for(int x=0; x<m_Target_Area.Get_NX(); x++) { m_Target_Area.Set_Value(x, y, pArea->Contains(Target.Get_xGrid_to_World(x), yWorld) ? 1 : 0); } } //----------------------------------------------------- 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 CPoint_Zonal_Multi_Grid_Regression::On_Execute(void) { //----------------------------------------------------- CSG_Shapes *pPoints = Parameters("POINTS" )->asShapes(); CSG_Shapes *pZones = Parameters("ZONES" )->asShapes(); CSG_Grid *pRegression = Parameters("REGRESSION")->asGrid (); pRegression->Assign_NoData(); CSG_Grid Regression(*Get_System(), SG_DATATYPE_Float); SG_UI_Progress_Lock(true); // suppress dialogs from popping up for(int i=0; i<pZones->Get_Count() && Process_Get_Okay(); i++) { CSG_Shape_Polygon *pZone = (CSG_Shape_Polygon *)pZones->Get_Shape(i); //------------------------------------------------- // select all points located in current zone polygon bool bResult; CSG_Shapes Zone(SHAPE_TYPE_Polygon); Zone.Add_Shape(pZone); SG_RUN_TOOL(bResult, "shapes_tools", 5, // select points by location SG_TOOL_PARAMETER_SET("LOCATIONS", &Zone) && SG_TOOL_PARAMETER_SET("SHAPES" , pPoints) ); if( !bResult ) { SG_UI_Process_Set_Okay(); // don't stop overall work flow, if tool execution failed for current zone } else if( pPoints->Get_Selection_Count() > 0 ) { //--------------------------------------------- // copy selected points to a new (temporary) points layer CSG_Shapes Selection; SG_RUN_TOOL(bResult, "shapes_tools", 6, // copy selected points to a new layer SG_TOOL_PARAMETER_SET("INPUT" , pPoints) && SG_TOOL_PARAMETER_SET("OUTPUT", &Selection) ); pPoints->asShapes()->Select(); // unselect everything from original points layer //--------------------------------------------- // perform the regression analysis, regression grid for zone is temporary SG_RUN_TOOL(bResult, "statistics_regression", 1, // multiple linear regression for points and predictor grids SG_TOOL_PARAMETER_SET("PREDICTORS", Parameters("PREDICTORS")) && SG_TOOL_PARAMETER_SET("REGRESSION", &Regression ) && SG_TOOL_PARAMETER_SET("POINTS" , &Selection ) && SG_TOOL_PARAMETER_SET("ATTRIBUTE" , Parameters("ATTRIBUTE" )) && SG_TOOL_PARAMETER_SET("RESAMPLING", Parameters("RESAMPLING")) && SG_TOOL_PARAMETER_SET("COORD_X" , Parameters("COORD_X" )) && SG_TOOL_PARAMETER_SET("COORD_Y" , Parameters("COORD_Y" )) && SG_TOOL_PARAMETER_SET("INTERCEPT" , Parameters("INTERCEPT" )) && SG_TOOL_PARAMETER_SET("METHOD" , Parameters("METHOD" )) && SG_TOOL_PARAMETER_SET("P_VALUE" , Parameters("P_VALUE" )) ); //--------------------------------------------- // use zone polygon as mask for copying zonal regression result to final regression grid if( !bResult ) { SG_UI_Process_Set_Okay(); // don't stop overall work flow, if tool execution failed for current zone } else { #pragma omp parallel for // speed up using multiple processors for(int y=0; y<Get_NY(); y++) { for(int x=0; x<Get_NX(); x++) { if( !Regression.is_NoData(x, y) && pZone->Contains(Get_System()->Get_Grid_to_World(x, y)) ) { pRegression->Set_Value(x, y, Regression.asDouble(x, y)); } } } } } } //----------------------------------------------------- SG_UI_Progress_Lock(false); //----------------------------------------------------- Set_Residuals(pPoints, pRegression); //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CClip_Points::On_Execute(void) { int Method, iField; CSG_Shapes *pPoints, *pPolygons, *pClip; CSG_Parameter_Shapes_List *pClips; //----------------------------------------------------- pPoints = Parameters("POINTS") ->asShapes(); pPolygons = Parameters("POLYGONS") ->asShapes(); pClips = Parameters("CLIPS") ->asShapesList(); Method = Parameters("METHOD") ->asInt(); iField = Parameters("FIELD") ->asInt(); //----------------------------------------------------- if( !pPoints->is_Valid() ) { Message_Add(_TL("Invalid points layer.")); return( false ); } else if( !pPolygons->is_Valid() ) { Message_Add(_TL("Invalid polygon layer.")); return( false ); } //----------------------------------------------------- if( iField >= pPolygons->Get_Field_Count() ) { iField = -1; } pClips->Del_Items(); if( Method == 0 ) { pClip = SG_Create_Shapes(SHAPE_TYPE_Point, CSG_String::Format(SG_T("%s [%s]"), pPoints->Get_Name(), pPolygons->Get_Name()), pPoints); if( iField >= 0 ) { pClip->Add_Field(pPolygons->Get_Field_Name(iField), pPolygons->Get_Field_Type(iField)); } } //----------------------------------------------------- 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); if( Method == 1 ) { CSG_String Name(pPoints->Get_Name()); Name += iField >= 0 ? CSG_String::Format(SG_T(" [%s]"), pPolygon->asString(iField)) : CSG_String::Format(SG_T(" [%00d]"), 1 + pClips->Get_Count()); pClip = SG_Create_Shapes(SHAPE_TYPE_Point, Name, pPoints); if( iField >= 0 ) { pClip->Add_Field(pPolygons->Get_Field_Name(iField), pPolygons->Get_Field_Type(iField)); } } for(int iPoint=0; iPoint<pPoints->Get_Count() && Process_Get_Okay(false); iPoint++) { CSG_Shape *pPoint = pPoints->Get_Shape(iPoint); if( pPolygon->Contains(pPoint->Get_Point(0)) ) { pPoint = pClip->Add_Shape(pPoint, SHAPE_COPY); if( iField >= 0 ) { pPoint->Set_Value(pPoints->Get_Field_Count(), pPolygon->asString(iField)); } } } if( Method == 1 ) { if( pClip->Get_Count() > 0 ) { pClips->Add_Item(pClip); } else { delete(pClip); } } } //----------------------------------------------------- if( Method == 0 ) { if( pClip->Get_Count() > 0 ) { pClips->Add_Item(pClip); } else { delete(pClip); } } return( pClips->Get_Count() > 0 ); }