//--------------------------------------------------------- bool CSG_Shapes::On_Update(void) { if( Get_Count() > 0 ) { CSG_Shape *pShape = Get_Shape(0); m_Extent = pShape->Get_Extent(); m_ZMin = pShape->Get_ZMin(); m_ZMax = pShape->Get_ZMax(); m_MMin = pShape->Get_MMin(); m_MMax = pShape->Get_MMax(); for(int i=1; i<Get_Count(); i++) { pShape = Get_Shape(i); m_Extent.Union(pShape->Get_Extent()); switch( m_Vertex_Type ) { case SG_VERTEX_TYPE_XYZM: if( m_MMin > pShape->Get_MMin() ) m_MMin = pShape->Get_MMin(); if( m_MMax < pShape->Get_MMax() ) m_MMax = pShape->Get_MMax(); case SG_VERTEX_TYPE_XYZ: if( m_ZMin > pShape->Get_ZMin() ) m_ZMin = pShape->Get_ZMin(); if( m_ZMax < pShape->Get_ZMax() ) m_ZMax = pShape->Get_ZMax(); break; default: break; } } } else { m_Extent.Assign(0.0, 0.0, 0.0, 0.0); } return( CSG_Table::On_Update() ); }
//--------------------------------------------------------- bool Cut_Shapes(CSG_Rect Extent, int Method, CSG_Shapes *pShapes, CSG_Shapes *pCut) { if( pCut && pShapes && pShapes->is_Valid() && Extent.Intersects(pShapes->Get_Extent()) ) { pCut->Create( pShapes->Get_Type(), CSG_String::Format(SG_T("%s [%s]"), pShapes->Get_Name(), _TL("Cut")), pShapes ); for(int iShape=0; iShape<pShapes->Get_Count() && SG_UI_Process_Set_Progress(iShape, pShapes->Get_Count()); iShape++) { bool bAdd; CSG_Shape *pShape = pShapes->Get_Shape(iShape); if( Method == 2 ) // center { bAdd = pShapes->Get_Type() == SHAPE_TYPE_Polygon ? Extent.Contains(((CSG_Shape_Polygon *)pShape)->Get_Centroid()) : Extent.Contains(pShape->Get_Extent().Get_Center()); } else // completely contained, intersects { switch( pShape->Intersects(Extent) ) { case INTERSECTION_Identical: case INTERSECTION_Contained: bAdd = true; break; case INTERSECTION_Overlaps: case INTERSECTION_Contains: bAdd = Method == 1; break; default: bAdd = false; break; } } if( bAdd ) { pCut->Add_Shape(pShape); } } return( pCut->Get_Count() > 0 ); } return( false ); }
//--------------------------------------------------------- bool CSelect_Location::Do_Select(CSG_Shape *pShape, int Condition) { for(int i=0; i<m_pLocations->Get_Count() && Process_Get_Okay(); i++) { CSG_Shape *pLocation = m_pLocations->Get_Shape(i); if( pShape->Intersects(pLocation->Get_Extent()) ) { switch( Condition ) { case 0: // intersect if( pLocation->Intersects(pShape) ) { return( true ); } break; case 1: // are completely within if( pLocation->Intersects(pShape) == INTERSECTION_Contains ) { return( true ); } break; case 2: // completely contain if( pShape->Intersects(pLocation) == INTERSECTION_Contains ) { return( true ); } break; case 3: // have their centroid in if( ((CSG_Shape_Polygon *)pLocation)->Contains(pShape->Get_Centroid()) ) { return( true ); } break; case 4: // contain the centroid of if( ((CSG_Shape_Polygon *)pShape)->Contains(pLocation->Get_Centroid()) ) { return( true ); } break; } } } return( false ); }
//--------------------------------------------------------- bool Cut_Shapes(CSG_Shapes *pPolygons, int Method, CSG_Shapes *pShapes, CSG_Shapes *pCut) { if( pCut && pShapes && pShapes->is_Valid() && pPolygons && pPolygons->is_Valid() && pPolygons->Get_Extent().Intersects(pShapes->Get_Extent()) ) { pCut->Create( pShapes->Get_Type(), CSG_String::Format(SG_T("%s [%s]"), pShapes->Get_Name(), _TL("Cut")), pShapes ); for(int iShape=0; iShape<pShapes->Get_Count() && SG_UI_Process_Set_Progress(iShape, pShapes->Get_Count()); iShape++) { bool bAdd; CSG_Shape *pShape = pShapes->Get_Shape(iShape); if( Method == 2 ) // center { bAdd = false; TSG_Point Center; if( pShapes->Get_Type() == SHAPE_TYPE_Polygon ) Center = ((CSG_Shape_Polygon *)pShape)->Get_Centroid(); else Center = pShape->Get_Extent().Get_Center(); if( pPolygons->Select(Center) ) { bAdd = true; } } else if( Method == 1 ) // intersects { bAdd = false; for(int iPart=0; iPart<pShape->Get_Part_Count() && !bAdd; iPart++) { for(int iPoint=0; iPoint<pShape->Get_Point_Count(iPart) && !bAdd; iPoint++) { if( pPolygons->Select(pShape->Get_Point(iPoint, iPart)) ) { bAdd = true; } } } } else // completely contained { bAdd = true; for(int iPart=0; iPart<pShape->Get_Part_Count() && bAdd; iPart++) { for(int iPoint=0; iPoint<pShape->Get_Point_Count(iPart) && bAdd; iPoint++) { if( pPolygons->Select(pShape->Get_Point(iPoint, iPart)) == false ) { bAdd = false; } } } } if( bAdd ) { pCut->Add_Shape(pShape); } } return( pCut->Get_Count() > 0 ); } return( false ); }
/////////////////////////////////////////////////////////// //--------------------------------------------------------- // This function has been copied from Module: 'Grid_Statistics_AddTo_Polygon' // Function: Get_ShapeIDs(...) // copyright by Olaf Conrad // // added support to clip only with selected polygons (Volker Wichmann) //--------------------------------------------------------- bool CGrid_Polygon_Clip::Get_Mask(CSG_Shapes *pShapes, CSG_Grid *pMask) { bool bFill, *bCrossing; bool bOnlySelected = false; int x, y, ix, xStart, xStop, iShape, iPart, iPoint; double yPos; TSG_Point pLeft, pRight, pa, pb, p; TSG_Rect Extent; CSG_Shape *pShape; //----------------------------------------------------- pMask->Assign(MASK_OFF); bCrossing = (bool *)SG_Malloc(pMask->Get_NX() * sizeof(bool)); if (pShapes->Get_Selection_Count() > 0) bOnlySelected = true; //----------------------------------------------------- for(iShape=0; iShape<pShapes->Get_Count() && Set_Progress(iShape, pShapes->Get_Count()); iShape++) { if (bOnlySelected && !pShapes->Get_Shape(iShape)->is_Selected()) continue; pShape = pShapes->Get_Shape(iShape); Extent = pShape->Get_Extent().m_rect; xStart = Get_System()->Get_xWorld_to_Grid(Extent.xMin) - 1; if( xStart < 0 ) xStart = 0; xStop = Get_System()->Get_xWorld_to_Grid(Extent.xMax) + 1; if( xStop >= Get_NX() ) xStop = Get_NX() - 1; pLeft.x = pMask->Get_XMin() - 1.0; pRight.x = pMask->Get_XMax() + 1.0; //------------------------------------------------- for(y=0, yPos=pMask->Get_YMin(); y<pMask->Get_NY(); y++, yPos+=pMask->Get_Cellsize()) { if( yPos >= Extent.yMin && yPos <= Extent.yMax ) { memset(bCrossing, 0, pMask->Get_NX() * sizeof(bool)); pLeft.y = pRight.y = yPos; //----------------------------------------- for(iPart=0; iPart<pShape->Get_Part_Count(); iPart++) { pb = pShape->Get_Point(pShape->Get_Point_Count(iPart) - 1, iPart); for(iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++) { pa = pb; pb = pShape->Get_Point(iPoint, iPart); if( ( (pa.y <= yPos && yPos < pb.y) || (pa.y > yPos && yPos >= pb.y) ) ) { SG_Get_Crossing(p, pa, pb, pLeft, pRight, false); ix = (int)((p.x - pMask->Get_XMin()) / pMask->Get_Cellsize() + 1.0); if( ix < 0) { ix = 0; } else if( ix >= pMask->Get_NX() ) { continue; } bCrossing[ix] = !bCrossing[ix]; } } } //----------------------------------------- for(x=xStart, bFill=false; x<=xStop; x++) { if( bCrossing[x] ) { bFill = !bFill; } if( bFill ) { pMask->Set_Value(x, y, MASK_ON); } } } } } //----------------------------------------------------- SG_Free(bCrossing); return( true ); }
//--------------------------------------------------------- bool CGrid_Values_AddTo_Shapes::On_Execute(void) { CSG_Parameter_Grid_List *pGrids; CSG_Shapes *pShapes; //----------------------------------------------------- pShapes = Parameters("RESULT")->asShapes(); pGrids = Parameters("GRIDS" )->asGridList(); //----------------------------------------------------- if( pGrids->Get_Count() <= 0 ) { return( false ); } //----------------------------------------------------- if( pShapes == NULL ) { pShapes = Parameters("SHAPES")->asShapes(); } else if( pShapes != Parameters("SHAPES")->asShapes() ) { pShapes->Create(*Parameters("SHAPES")->asShapes()); } //----------------------------------------------------- switch( Parameters("RESAMPLING")->asInt() ) { default: m_Resampling = GRID_RESAMPLING_NearestNeighbour; break; case 1: m_Resampling = GRID_RESAMPLING_Bilinear; break; case 2: m_Resampling = GRID_RESAMPLING_BicubicSpline; break; case 3: m_Resampling = GRID_RESAMPLING_BSpline; break; } //----------------------------------------------------- for(int iGrid=0; iGrid<pGrids->Get_Count(); iGrid++) { CSG_Grid *pGrid = pGrids->asGrid(iGrid); int Field = pShapes->Get_Field_Count(); pShapes->Add_Field(pGrid->Get_Name(), SG_DATATYPE_Double); for(int iShape=0; iShape<pShapes->Get_Count() && Set_Progress(iShape, pShapes->Get_Count()); iShape++) { CSG_Simple_Statistics Statistics; CSG_Shape *pShape = pShapes->Get_Shape(iShape); if( pShape->Get_Extent().Intersects(pGrid->Get_Extent()) ) { switch( pShapes->Get_Type() ) { case SHAPE_TYPE_Point: default: case SHAPE_TYPE_Points: Get_Data_Point (Statistics, pShape, pGrid); break; case SHAPE_TYPE_Line: Get_Data_Line (Statistics, pShape, pGrid); break; case SHAPE_TYPE_Polygon: Get_Data_Polygon(Statistics, pShape, pGrid); break; } } if( Statistics.Get_Count() > 0 ) { pShape->Set_Value(Field, Statistics.Get_Mean()); } else { pShape->Set_NoData(Field); } } } //----------------------------------------------------- if( pShapes == Parameters("SHAPES")->asShapes() ) { DataObject_Update(pShapes); } return( true ); }
//--------------------------------------------------------- bool CShapes_Extents::On_Execute(void) { CSG_Shapes *pShapes = Parameters("SHAPES" )->asShapes(); CSG_Shapes *pExtents = Parameters("EXTENTS")->asShapes(); //----------------------------------------------------- if( !pShapes->is_Valid() ) { Message_Add(_TL("invalid input")); return( false ); } //----------------------------------------------------- int iOutput = Parameters("OUTPUT")->asInt(); if( iOutput == 0 ) // all shapes { pExtents->Create(SHAPE_TYPE_Polygon, CSG_String::Format(SG_T("%s [%s]"), pShapes->Get_Name(), _TL("Extent"))); pExtents->Add_Field(SG_T("OID"), SG_DATATYPE_Int); TSG_Rect r = pShapes->Get_Extent(); CSG_Shape *pExtent = pExtents->Add_Shape(); pExtent->Set_Value(0, 1); pExtent->Add_Point(r.xMin, r.yMin); pExtent->Add_Point(r.xMin, r.yMax); pExtent->Add_Point(r.xMax, r.yMax); pExtent->Add_Point(r.xMax, r.yMin); return( true ); } //----------------------------------------------------- if( pShapes->Get_Type() == SHAPE_TYPE_Point ) { Message_Add(_TL("no 'get extents' support for single point layers")); return( false ); } //----------------------------------------------------- pExtents->Create(SHAPE_TYPE_Polygon, pShapes->Get_Name(), pShapes); for(int iShape=0; iShape<pShapes->Get_Count() && Set_Progress(iShape, pShapes->Get_Count()); iShape++) { CSG_Shape *pShape = pShapes->Get_Shape(iShape); if( iOutput == 1 ) // each shape { TSG_Rect r = pShape->Get_Extent(); CSG_Shape *pExtent = pExtents->Add_Shape(pShape, SHAPE_COPY_ATTR); pExtent->Add_Point(r.xMin, r.yMin); pExtent->Add_Point(r.xMin, r.yMax); pExtent->Add_Point(r.xMax, r.yMax); pExtent->Add_Point(r.xMax, r.yMin); } else // if( iOutput == 2 ) // each shape's part { for(int iPart=0; iPart<pShape->Get_Part_Count(); iPart++) { TSG_Rect r = pShape->Get_Extent(iPart); CSG_Shape *pExtent = pExtents->Add_Shape(pShape, SHAPE_COPY_ATTR); pExtent->Add_Point(r.xMin, r.yMin); pExtent->Add_Point(r.xMin, r.yMax); pExtent->Add_Point(r.xMax, r.yMax); pExtent->Add_Point(r.xMax, r.yMin); } } } //----------------------------------------------------- return( pExtents->is_Valid() ); }