//--------------------------------------------------------- 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 CPolygon_Vertex_Check::On_Execute(void) { //----------------------------------------------------- CSG_Shapes *pPolygons = Parameters("POLYGONS")->asShapes(); if( Parameters("CHECKED")->asShapes() && Parameters("CHECKED")->asShapes() != pPolygons ) { CSG_Shapes *pCopy = Parameters("CHECKED")->asShapes(); pCopy->Create(*pPolygons); pCopy->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pPolygons->Get_Name(), _TL("checked"))); pPolygons = pCopy; } if( (m_pAdded = Parameters("ADDED")->asShapes()) != NULL ) { m_pAdded->Create(SHAPE_TYPE_Point, _TL("Added")); } double Epsilon = Parameters("EPSILON")->asDouble(); //----------------------------------------------------- for(int iPolygon=0; iPolygon<pPolygons->Get_Count()-1 && Set_Progress(iPolygon, pPolygons->Get_Count()-1); iPolygon++) { CSG_Shape_Polygon *pA = (CSG_Shape_Polygon *)pPolygons->Get_Shape(iPolygon); for(int jPolygon=iPolygon+1; jPolygon<pPolygons->Get_Count() && Process_Get_Okay(); jPolygon++) { CSG_Shape_Polygon *pB = (CSG_Shape_Polygon *)pPolygons->Get_Shape(jPolygon); for(int iPart=0; iPart<pA->Get_Part_Count() && Process_Get_Okay(); iPart++) { for(int jPart=0; jPart<pB->Get_Part_Count() && Process_Get_Okay(); jPart++) { if( pA->Get_Part(iPart)->Get_Extent().Intersects(pB->Get_Part(jPart)->Get_Extent()) ) { Check_Vertices(pA->Get_Part(iPart), pB->Get_Part(jPart), Epsilon); Check_Vertices(pB->Get_Part(jPart), pA->Get_Part(iPart), Epsilon); } } } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CPolygon_Centroids::On_Execute(void) { bool bPart; int iShape, iPart; CSG_Shape *pCentroid; CSG_Shape_Polygon *pPolygon; CSG_Shapes *pPolygons, *pCentroids; pPolygons = Parameters("POLYGONS") ->asShapes(); pCentroids = Parameters("CENTROIDS") ->asShapes(); bPart = Parameters("METHOD") ->asBool(); if( pPolygons->Get_Type() == SHAPE_TYPE_Polygon && pPolygons->Get_Count() > 0 ) { pCentroids->Create(SHAPE_TYPE_Point, pPolygons->Get_Name(), pPolygons); //------------------------------------------------- for(iShape=0; iShape<pPolygons->Get_Count(); iShape++) { pPolygon = (CSG_Shape_Polygon *)pPolygons->Get_Shape(iShape); if( bPart ) { for(iPart=0; iPart<pPolygon->Get_Part_Count(); iPart++) { pCentroid = pCentroids->Add_Shape(pPolygon, SHAPE_COPY_ATTR); pCentroid->Add_Point(pPolygon->Get_Centroid(iPart)); } } else { pCentroid = pCentroids->Add_Shape(pPolygon, SHAPE_COPY_ATTR); pCentroid->Add_Point(pPolygon->Get_Centroid()); } } return( true ); } return( false ); }
//--------------------------------------------------------- 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 CSelectByTheme::Select(CSG_Shapes *pShapes, CSG_Shapes *pShapes2, int iCondition, bool bFromSelection) { CSG_Shapes Intersect(SHAPE_TYPE_Polygon); CSG_Shape_Polygon *pIntersect = (CSG_Shape_Polygon *)Intersect.Add_Shape(); m_Selection.clear(); for(int i=0; i<pShapes->Get_Count() && Set_Progress(i, pShapes->Get_Count()); i++) { CSG_Shape_Polygon *pShape = (CSG_Shape_Polygon *)pShapes->Get_Shape(i); bool bSelect = false; for(int j=0; !bSelect && j<pShapes2->Get_Count(); j++) { if( !bFromSelection || pShapes2->Get_Record(j)->is_Selected() ) { CSG_Shape_Polygon *pShape2 = (CSG_Shape_Polygon *)pShapes2->Get_Shape(j); switch( iCondition ) { case 0: //intersect if( GPC_Intersection(pShape, pShape2, pIntersect) ) { bSelect = true; } break; case 1: //are completely within if( GPC_Intersection(pShape, pShape2, pIntersect) && pIntersect->Get_Area() == pShape->Get_Area() ) { bSelect = true; } break; case 2: //Completely contain if( GPC_Intersection(pShape, pShape2, pIntersect) && pIntersect->Get_Area() == pShape2->Get_Area() ) { bSelect = true; } break; case 3: //have their center in if( pShape2->is_Containing(pShape->Get_Centroid()) ) { bSelect = true; } break; case 4: //contain center of if( pShape->is_Containing(pShape2->Get_Centroid()) ) { bSelect = true; } break; } if( bSelect ) { m_Selection.push_back(i); } } } } return( m_Selection.size() > 0 ); }
//--------------------------------------------------------- bool CShape_Index::On_Execute(void) { CSG_Shapes *pShapes, *pIndex; //----------------------------------------------------- pShapes = Parameters("SHAPES") ->asShapes(); pIndex = Parameters("INDEX") ->asShapes(); //----------------------------------------------------- if( pShapes->is_Valid() ) { int iField = pShapes->Get_Field_Count(); if( pIndex == NULL ) { pIndex = pShapes; } if( pIndex != pShapes ) { pIndex->Create(SHAPE_TYPE_Polygon, _TL("Shape Index"), pShapes); } pIndex->Add_Field(_TL("Area") , SG_DATATYPE_Double); pIndex->Add_Field(_TL("Perimenter") , SG_DATATYPE_Double); pIndex->Add_Field(_TL("P/A") , SG_DATATYPE_Double); pIndex->Add_Field(_TL("P/sqrt(A)") , SG_DATATYPE_Double); pIndex->Add_Field(_TL("Max.Distance") , SG_DATATYPE_Double); pIndex->Add_Field(_TL("D/A") , SG_DATATYPE_Double); pIndex->Add_Field(_TL("D/sqrt(A)") , SG_DATATYPE_Double); pIndex->Add_Field(_TL("Shape Index") , SG_DATATYPE_Double); for(int iShape=0; iShape<pShapes->Get_Count() && Set_Progress(iShape, pShapes->Get_Count()); iShape++) { CSG_Shape_Polygon *pShape = (CSG_Shape_Polygon *)pShapes->Get_Shape(iShape); double Area = pShape->Get_Area(); double Perimeter = pShape->Get_Perimeter(); double Distance = Get_Distance(pShape); if( Perimeter > 0.0 && Distance > 0.0 ) { if( pIndex != pShapes ) { pShape = (CSG_Shape_Polygon *)pIndex->Add_Shape(pShape, SHAPE_COPY_ATTR); } pShape->Set_Value(iField + 0, Area); pShape->Set_Value(iField + 1, Perimeter); pShape->Set_Value(iField + 2, Perimeter / Area); pShape->Set_Value(iField + 3, Perimeter / sqrt(Area)); pShape->Set_Value(iField + 4, Distance); pShape->Set_Value(iField + 5, Distance / Area); pShape->Set_Value(iField + 6, Distance / sqrt(Area)); pShape->Set_Value(iField + 7, Perimeter / (2.0 * sqrt(M_PI * Area))); } } if( pIndex == pShapes ) { DataObject_Update(pShapes); } return( pIndex->is_Valid() ); } //----------------------------------------------------- return( false ); }
//--------------------------------------------------------- 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 CShape_Index::On_Execute(void) { //----------------------------------------------------- CSG_Shapes *pPolygons = Parameters("SHAPES")->asShapes(); if( !pPolygons->is_Valid() ) { Error_Set(_TL("invalid polygons layer")); return( false ); } //----------------------------------------------------- if( Parameters("INDEX")->asShapes() && Parameters("INDEX")->asShapes() != pPolygons ) { CSG_Shapes *pTarget = Parameters("INDEX")->asShapes(); pTarget->Create(SHAPE_TYPE_Polygon, CSG_String::Format("%s [%s]", pPolygons->Get_Name(), _TL("Shape Indices"))); pTarget->Add_Field("ID", SG_DATATYPE_Int); for(int iPolygon=0; iPolygon<pPolygons->Get_Count() && Set_Progress(iPolygon, pPolygons->Get_Count()); iPolygon++) { pTarget->Add_Shape(pPolygons->Get_Shape(iPolygon), SHAPE_COPY)->Set_Value(0, iPolygon); } pPolygons = pTarget; } //----------------------------------------------------- int offIndices = pPolygons->Get_Field_Count(); pPolygons->Add_Field(_TL("A" ), SG_DATATYPE_Double); // 0 pPolygons->Add_Field(_TL("P" ), SG_DATATYPE_Double); // 1 pPolygons->Add_Field(_TL("P/A" ), SG_DATATYPE_Double); // 2 pPolygons->Add_Field(_TL("P/sqrt(A)" ), SG_DATATYPE_Double); // 3 pPolygons->Add_Field(_TL("Depqc" ), SG_DATATYPE_Double); // 4 pPolygons->Add_Field(_TL("Sphericity" ), SG_DATATYPE_Double); // 5 pPolygons->Add_Field(_TL("Shape Index" ), SG_DATATYPE_Double); // 6 pPolygons->Add_Field(_TL("Dmax" ), SG_DATATYPE_Double); // 7 pPolygons->Add_Field(_TL("DmaxDir" ), SG_DATATYPE_Double); // 8 pPolygons->Add_Field(_TL("Dmax/A" ), SG_DATATYPE_Double); // 9 pPolygons->Add_Field(_TL("Dmax/sqrt(A)"), SG_DATATYPE_Double); // 10 bool bGyros = Parameters("GYROS")->asBool(); if( bGyros ) { pPolygons->Add_Field(_TL("Dgyros" ), SG_DATATYPE_Double); // 11 } int offFeret = pPolygons->Get_Field_Count(); double dFeret = 0.0; if( Parameters("FERET")->asBool() ) { dFeret = M_DEG_TO_RAD * (180. / (1. + Parameters("FERET_DIRS")->asInt())); pPolygons->Add_Field(_TL("Fmax" ), SG_DATATYPE_Double); // 0 pPolygons->Add_Field(_TL("FmaxDir" ), SG_DATATYPE_Double); // 1 pPolygons->Add_Field(_TL("Fmin" ), SG_DATATYPE_Double); // 2 pPolygons->Add_Field(_TL("FminDir" ), SG_DATATYPE_Double); // 3 pPolygons->Add_Field(_TL("Fmean" ), SG_DATATYPE_Double); // 4 pPolygons->Add_Field(_TL("Fmax90" ), SG_DATATYPE_Double); // 5 pPolygons->Add_Field(_TL("Fmin90" ), SG_DATATYPE_Double); // 6 pPolygons->Add_Field(_TL("Fvol" ), SG_DATATYPE_Double); // 7 } //----------------------------------------------------- CSG_Shapes *pDmax = Parameters("DMAX")->asShapes(); if( pDmax ) { pDmax->Create(SHAPE_TYPE_Line, CSG_String::Format("%s [%s]", pPolygons->Get_Name(), _TL("Maximum Diameter"))); pDmax->Add_Field("ID", SG_DATATYPE_Int ); pDmax->Add_Field("D" , SG_DATATYPE_Double); } //----------------------------------------------------- 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); double A = pPolygon->Get_Area (); double P = pPolygon->Get_Perimeter(); if( A > 0.0 && P > 0.0 ) { pPolygon->Set_Value(offIndices + 0, A); pPolygon->Set_Value(offIndices + 1, P); pPolygon->Set_Value(offIndices + 2, P / A); pPolygon->Set_Value(offIndices + 3, P / sqrt(A)); pPolygon->Set_Value(offIndices + 4, 2 * sqrt(A / M_PI)); pPolygon->Set_Value(offIndices + 5, (2 * sqrt(A * M_PI)) / P); pPolygon->Set_Value(offIndices + 6, P / (2 * sqrt(A * M_PI))); double Dmax; TSG_Point Pmax[2]; if( Get_Diameter_Max(pPolygon, Dmax, Pmax) ) { double DmaxDir = SG_Get_Angle_Of_Direction(Pmax[0], Pmax[1]); if( DmaxDir > M_PI_180 ) DmaxDir -= M_PI_180; pPolygon->Set_Value(offIndices + 7, Dmax); pPolygon->Set_Value(offIndices + 8, DmaxDir * M_RAD_TO_DEG); pPolygon->Set_Value(offIndices + 9, Dmax / A); pPolygon->Set_Value(offIndices + 10, Dmax / sqrt(A)); if( pDmax ) { CSG_Shape *pLine = pDmax->Add_Shape(); pLine->Add_Point(Pmax[0]); pLine->Add_Point(Pmax[1]); pLine->Set_Value(0, iPolygon); pLine->Set_Value(1, Dmax); } if( bGyros ) { Get_Diameter_Gyros(pPolygon, offIndices + 11); } if( dFeret > 0.0 ) { Get_Diameters_Feret(pPolygon, offFeret, dFeret); } } else { for(int iField=offIndices+7; iField<pPolygons->Get_Field_Count(); iField++) { pPolygon->Set_NoData(iField); } } } else { for(int iField=offIndices; iField<pPolygons->Get_Field_Count(); iField++) { pPolygon->Set_NoData(iField); } } } //----------------------------------------------------- if( pPolygons == Parameters("SHAPES")->asShapes() ) { // output is always updated automatically - but if input has been modified, this needs a manual update! DataObject_Update(pPolygons); } return( pPolygons->is_Valid() ); }
//--------------------------------------------------------- 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 ); }
//--------------------------------------------------------- bool CPolygon_Shared_Edges::On_Execute(void) { //----------------------------------------------------- CSG_Shapes *pPolygons = Parameters("POLYGONS")->asShapes(); m_Field = Parameters("ATTRIBUTE")->asInt(); m_pEdges = Parameters("EDGES")->asShapes(); m_pEdges->Create(SHAPE_TYPE_Line, CSG_String::Format(SG_T("%s [%s]"), pPolygons->Get_Name(), _TL("Edges"))); m_pEdges->Add_Field("ID_A", m_Field < 0 ? SG_DATATYPE_Int : pPolygons->Get_Field_Type(m_Field)); m_pEdges->Add_Field("ID_B", m_Field < 0 ? SG_DATATYPE_Int : pPolygons->Get_Field_Type(m_Field)); // m_pNodes = Parameters("NODES")->asShapes(); // m_pNodes->Create(SHAPE_TYPE_Point, CSG_String::Format(SG_T("%s [%s]"), pPolygons->Get_Name(), _TL("Nodes"))); // m_pNodes->Add_Field("ID", SG_DATATYPE_Int); bool bVertices = Parameters("VERTICES")->asBool (); double Epsilon = Parameters("EPSILON" )->asDouble(); int iPolygon, nAdded = 0, nRemoved = 0; //----------------------------------------------------- if( bVertices ) { for(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(int iPart=0; iPart<pPolygon->Get_Part_Count() && Process_Get_Okay(); iPart++) { CSG_Shape_Part *pPart = pPolygon->Get_Part(iPart); CSG_Point A = pPart->Get_Point(pPart->Get_Count() - 1); if( A != pPart->Get_Point(0) ) { pPart->Add_Point(A); } for(int iPoint=pPart->Get_Count()-2; iPoint>=0; iPoint--) { CSG_Point B = A; A = pPart->Get_Point(iPoint); if( A == B ) { pPart->Del_Point(iPoint + 1); nRemoved--; } } } } } //----------------------------------------------------- for(iPolygon=0; iPolygon<pPolygons->Get_Count()-1 && Set_Progress(iPolygon, pPolygons->Get_Count()-1); iPolygon++) { CSG_Shape_Polygon *pA = (CSG_Shape_Polygon *)pPolygons->Get_Shape(iPolygon); for(int jPolygon=iPolygon+1; jPolygon<pPolygons->Get_Count() && Process_Get_Okay(); jPolygon++) { CSG_Shape_Polygon *pB = (CSG_Shape_Polygon *)pPolygons->Get_Shape(jPolygon); for(int iPart=0; iPart<pA->Get_Part_Count() && Process_Get_Okay(); iPart++) { for(int jPart=0; jPart<pB->Get_Part_Count() && Process_Get_Okay(); jPart++) { if( pA->Get_Part(iPart)->Get_Extent().Intersects(pB->Get_Part(jPart)->Get_Extent()) ) { if( bVertices ) { nAdded += Check_Vertices(pA->Get_Part(iPart), pB->Get_Part(jPart), Epsilon); nAdded += Check_Vertices(pB->Get_Part(jPart), pA->Get_Part(iPart), Epsilon); } Get_Shared_Edges(pA->Get_Part(iPart), pB->Get_Part(jPart), Epsilon); } } } } } //----------------------------------------------------- if( Parameters("DOUBLE")->asBool() ) { for(int iEdge=0, nEdges=m_pEdges->Get_Count(); iEdge<nEdges && Set_Progress(iEdge, nEdges); iEdge++) { CSG_Shape *pA = m_pEdges->Get_Shape(iEdge); CSG_Shape *pB = m_pEdges->Add_Shape(pA); *(pB->Get_Value(0)) = *(pA->Get_Value(1)); *(pB->Get_Value(1)) = *(pA->Get_Value(0)); } } //----------------------------------------------------- if( nAdded > 0 || nRemoved > 0 ) { Message_Add(CSG_String::Format(SG_T("\n%s: %d %s, %d %s\n"), _TL("Vertices"), nAdded , _TL("added" ), nRemoved, _TL("removed") ), false); DataObject_Update(pPolygons); } return( true ); }
//--------------------------------------------------------- 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 CSG_Shapes::Make_Clean(void) { if( m_Type != SHAPE_TYPE_Polygon ) { return( true ); } for(int iShape=0; iShape<Get_Count() && SG_UI_Process_Set_Progress(iShape, Get_Count()); iShape++) { CSG_Shape_Polygon *pPolygon = (CSG_Shape_Polygon *)Get_Shape(iShape); for(int iPart=0; iPart<pPolygon->Get_Part_Count(); iPart++) { if( m_Vertex_Type == SG_VERTEX_TYPE_XY ) // currently we have to disable this check for 3D shapefiles since the // _Update_Area() method can not handle polygons with no horizontal extent { //-------------------------------------------- // ring direction: outer rings > clockwise, inner rings (lakes) > counterclockwise ! if( (pPolygon->is_Lake(iPart) == pPolygon->is_Clockwise(iPart)) ) { pPolygon->Revert_Points(iPart); } } //-------------------------------------------- // last point == first point ! if( !CSG_Point(pPolygon->Get_Point(0, iPart)).is_Equal(pPolygon->Get_Point(pPolygon->Get_Point_Count(iPart) - 1, iPart)) ) { ((CSG_Shape *)pPolygon)->Add_Point(pPolygon->Get_Point(0, iPart), iPart); if( m_Vertex_Type != SG_VERTEX_TYPE_XY ) { pPolygon->Set_Z(pPolygon->Get_Z(0, iPart), pPolygon->Get_Point_Count(iPart) - 1, iPart); if( m_Vertex_Type == SG_VERTEX_TYPE_XYZM ) { pPolygon->Set_M(pPolygon->Get_M(0, iPart), pPolygon->Get_Point_Count(iPart) - 1, iPart); } } } //-------------------------------------------- // no self intersection ! } } return( true ); }
//--------------------------------------------------------- bool CGrid_Class_Statistics_For_Polygons::On_Execute(void) { //----------------------------------------------------- CSG_Grid *pGrid = Parameters("GRID")->asGrid(); CSG_Shapes *pPolygons = Parameters("POLYGONS")->asShapes(); if( pPolygons->Get_Count() <= 0 || !pPolygons->Get_Extent().Intersects(pGrid->Get_Extent()) ) { Error_Set(_TL("no spatial intersection between grid and polygon layer")); return( false ); } //----------------------------------------------------- if( Parameters("RESULT")->asShapes() != NULL && Parameters("RESULT")->asShapes() != pPolygons ) { Process_Set_Text(_TL("copying polygons")); CSG_Shapes *pResult = Parameters("RESULT")->asShapes(); pResult->Create(SHAPE_TYPE_Polygon, CSG_String::Format("%s [%s]", pPolygons->Get_Name(), _TL("Grid Classes"))); for(int i=0; i<pPolygons->Get_Count() && Set_Progress(i, pPolygons->Get_Count()); i++) { pResult->Add_Shape(pPolygons->Get_Shape(i), SHAPE_COPY_GEOM); } pPolygons = pResult; } //----------------------------------------------------- int fStart = pPolygons->Get_Field_Count(); if( !Get_Classes(pGrid, pPolygons) ) { Error_Set(_TL("undefined grid classes")); return( false ); } //----------------------------------------------------- bool bCenter = Parameters("METHOD")->asInt() == 0; Process_Set_Text(_TL("calculating class areas")); //----------------------------------------------------- int x, y; TSG_Point p; 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( m_Classes.asInt(x, y) >= 0 ) { int fClass = fStart + m_Classes.asInt(x, y); #pragma omp parallel for for(int i=0; i<pPolygons->Get_Count(); i++) { CSG_Shape_Polygon *pPolygon = (CSG_Shape_Polygon *)pPolygons->Get_Shape(i); double Area = Get_Intersection(pPolygon, p, bCenter); if( Area > 0.0 ) { pPolygon->Add_Value(fClass, Area); } } } } } //----------------------------------------------------- m_Classes.Destroy(); DataObject_Update(pPolygons); return( true ); }