//--------------------------------------------------------- CSG_Shapes * COGR_DataSource::Read_Shapes(int iLayer) { OGRLayer *pLayer = Get_Layer(iLayer); //----------------------------------------------------- if( pLayer && Get_Type(iLayer) != SHAPE_TYPE_Undefined ) { int iField; OGRFeature *pFeature; OGRFeatureDefn *pDef = pLayer->GetLayerDefn(); CSG_Shapes *pShapes = SG_Create_Shapes(Get_Type(iLayer), CSG_String(pDef->GetName())); for(iField=0; iField<pDef->GetFieldCount(); iField++) { OGRFieldDefn *pDefField = pDef->GetFieldDefn(iField); pShapes->Add_Field(pDefField->GetNameRef(), COGR_Driver::Get_Type(pDefField->GetType())); } pLayer->ResetReading(); //------------------------------------------------- while( (pFeature = pLayer->GetNextFeature()) != NULL && SG_UI_Process_Get_Okay(false) ) { OGRGeometry *pGeometry = pFeature->GetGeometryRef(); if( pGeometry != NULL ) { CSG_Shape *pShape = pShapes->Add_Shape(); for(iField=0; iField<pDef->GetFieldCount(); iField++) { OGRFieldDefn *pDefField = pDef->GetFieldDefn(iField); switch( pDefField->GetType() ) { default: pShape->Set_Value(iField, SG_STR_MBTOSG(pFeature->GetFieldAsString (iField))); break; case OFTString: pShape->Set_Value(iField, SG_STR_MBTOSG(pFeature->GetFieldAsString (iField))); break; case OFTInteger: pShape->Set_Value(iField, pFeature->GetFieldAsInteger(iField)); break; case OFTReal: pShape->Set_Value(iField, pFeature->GetFieldAsDouble (iField)); break; } } //----------------------------------------- if( _Read_Geometry(pShape, pGeometry) == false ) { pShapes->Del_Shape(pShape); } } OGRFeature::DestroyFeature(pFeature); } return( pShapes ); } //----------------------------------------------------- return( NULL ); }
//--------------------------------------------------------- bool CSG_Network::Update(void) { int iEdge; //----------------------------------------------------- for(iEdge=m_Edges.Get_Count()-1; iEdge>=0; iEdge--) { CSG_Shape *pEdge = m_Edges.Get_Shape(iEdge); if( !(((CSG_Shape_Line *)pEdge)->Get_Length() > 0.0) ) { m_Edges.Del_Shape(iEdge); } } //----------------------------------------------------- for(int i=0; i<Get_Node_Count(); i++) { delete(&Get_Node(i)); } m_Nodes.Set_Array(0); //----------------------------------------------------- CSG_PRQuadTree Search(m_Edges.Get_Extent()); for(iEdge=0; iEdge<m_Edges.Get_Count(); iEdge++) { CSG_Shape *pEdge = m_Edges.Get_Shape(iEdge); pEdge->Set_Value(0, iEdge); pEdge->Set_Value(1, _Add_Node(Search, iEdge, pEdge->Get_Point(0), pEdge->Get_Point(1) )); pEdge->Set_Value(2, _Add_Node(Search, iEdge, pEdge->Get_Point(pEdge->Get_Point_Count(0) - 1), pEdge->Get_Point(pEdge->Get_Point_Count(0) - 2) )); } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CGCS_Graticule::Get_Graticule(const CSG_Rect &Extent) { double x, y, Interval; CSG_Rect r; if( !Get_Extent(Extent, r) || (Interval = Get_Interval(r)) <= 0.0 ) { return( false ); } //----------------------------------------------------- r.m_rect.xMin = Interval * floor(r.Get_XMin() / Interval); r.m_rect.xMax = Interval * ceil (r.Get_XMax() / Interval); r.m_rect.yMin = Interval * floor(r.Get_YMin() / Interval); r.m_rect.yMax = Interval * ceil (r.Get_YMax() / Interval); r.Inflate(Interval, false); if( r.Get_XMin() < -180.0 ) r.m_rect.xMin = -180.0; if( r.Get_XMax() > 180.0 ) r.m_rect.xMax = 180.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; //----------------------------------------------------- double Resolution = Parameters("RESOLUTION")->asDouble(); if( Resolution <= 0.0 ) Resolution = Interval; if( Interval > Resolution ) { Resolution = Interval / ceil(Interval / Resolution); } //----------------------------------------------------- CSG_Shapes *pGraticule = Parameters("GRATICULE")->asShapes(); pGraticule->Create(SHAPE_TYPE_Line); pGraticule->Set_Name(_TL("Graticule")); pGraticule->Add_Field("TYPE" , SG_DATATYPE_String); pGraticule->Add_Field("LABEL" , SG_DATATYPE_String); pGraticule->Add_Field("DEGREE", SG_DATATYPE_Double); //----------------------------------------------------- CSG_Shapes *pCoordinates = Parameters("COORDS")->asShapes(); if( pCoordinates ) { pCoordinates->Create(SHAPE_TYPE_Point); pCoordinates->Set_Name(_TL("Coordinates")); pCoordinates->Add_Field("TYPE" , SG_DATATYPE_String); pCoordinates->Add_Field("LABEL", SG_DATATYPE_String); } //----------------------------------------------------- CSG_Shapes Clip(SHAPE_TYPE_Polygon); CSG_Shape *pClip = Clip.Add_Shape(); pClip->Add_Point(Extent.Get_XMin(), Extent.Get_YMin()); pClip->Add_Point(Extent.Get_XMin(), Extent.Get_YMax()); pClip->Add_Point(Extent.Get_XMax(), Extent.Get_YMax()); pClip->Add_Point(Extent.Get_XMax(), Extent.Get_YMin()); pClip->Add_Point(Extent.Get_XMin(), Extent.Get_YMin()); //----------------------------------------------------- for(y=r.Get_YMin(); y<=r.Get_YMax(); y+=Interval) { CSG_Shape *pLine = pGraticule->Add_Shape(); pLine->Set_Value(0, "LAT"); pLine->Set_Value(1, Get_Degree(y, DEG_PREC_DEG)); pLine->Set_Value(2, y); for(x=r.Get_XMin(); x<=r.Get_XMax(); x+=Interval) { CSG_Point p(x, y); m_Projector.Get_Projection(p); pLine->Add_Point(p); if( Resolution < Interval && x < r.Get_XMax() ) { for(double i=x+Resolution; i<x+Interval; i+=Resolution) { CSG_Point p(i, y); m_Projector.Get_Projection(p); pLine->Add_Point(p); } } } Get_Coordinate(Extent, pCoordinates, pLine, AXIS_LEFT); Get_Coordinate(Extent, pCoordinates, pLine, AXIS_RIGHT); if( !SG_Polygon_Intersection(pLine, pClip) ) { pGraticule->Del_Shape(pLine); } } //----------------------------------------------------- for(x=r.Get_XMin(); x<=r.Get_XMax(); x+=Interval) { CSG_Shape *pLine = pGraticule->Add_Shape(); pLine->Set_Value(0, "LON"); pLine->Set_Value(1, Get_Degree(x, DEG_PREC_DEG)); pLine->Set_Value(2, x); for(y=r.Get_YMin(); y<=r.Get_YMax(); y+=Interval) { CSG_Point p(x, y); m_Projector.Get_Projection(p); pLine->Add_Point(p); if( Resolution < Interval && y < r.Get_YMax() ) { for(double i=y+Resolution; i<y+Interval; i+=Resolution) { CSG_Point p(x, i); m_Projector.Get_Projection(p); pLine->Add_Point(p); } } } Get_Coordinate(Extent, pCoordinates, pLine, AXIS_BOTTOM); Get_Coordinate(Extent, pCoordinates, pLine, AXIS_TOP); if( !SG_Polygon_Intersection(pLine, pClip) ) { pGraticule->Del_Shape(pLine); } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CSG_Network::Remove_End_Nodes(void) { int iEdge, n; //----------------------------------------------------- for(iEdge=0; iEdge<m_Edges.Get_Count(); iEdge++) { CSG_Shape *pEdge = m_Edges.Get_Shape(iEdge); if( pEdge->asInt(3) == SHAPE_TYPE_Line ) { bool bRemove = false; for(int iNode=1; iNode<=2 && !bRemove; iNode++) { CSG_Network_Node &Node = Get_Node(pEdge->asInt(iNode)); CSG_Point Point = Node.Get_Point(); CSG_Shape *pNext; if( ( (pNext = m_Edges.Get_Shape(Node.Get_Edge_Next(pEdge->asInt(0), true))) != NULL && pNext->asInt(3) == SHAPE_TYPE_Polygon && !Point.is_Equal(pNext->Get_Point(0, 0, false)) ) || ( (pNext = m_Edges.Get_Shape(Node.Get_Edge_Next(pEdge->asInt(0), false))) != NULL && pNext->asInt(3) == SHAPE_TYPE_Polygon && !Point.is_Equal(pNext->Get_Point(0, 0, true)) ) ) { bRemove = true; } } if( bRemove ) { Get_Node(pEdge->asInt(1)).Del_Edge(pEdge->asInt(0)); Get_Node(pEdge->asInt(2)).Del_Edge(pEdge->asInt(0)); pEdge->Set_Value(4, 1); } } } //----------------------------------------------------- do { for(n=0, iEdge=0; iEdge<m_Edges.Get_Count(); iEdge++) { CSG_Shape *pEdge = m_Edges.Get_Shape(iEdge); if( pEdge->asInt(3) == SHAPE_TYPE_Line && pEdge->asInt(4) == 0 ) { if( Get_Node(pEdge->asInt(1)).Get_Edge_Count() <= 1 || Get_Node(pEdge->asInt(2)).Get_Edge_Count() <= 1 ) { Get_Node(pEdge->asInt(1)).Del_Edge(pEdge->asInt(0)); Get_Node(pEdge->asInt(2)).Del_Edge(pEdge->asInt(0)); pEdge->Set_Value(4, 1); n++; } } } } while( n > 0 ); //----------------------------------------------------- for(iEdge=m_Edges.Get_Count()-1; iEdge>=0; iEdge--) { if( m_Edges[iEdge][4] ) { m_Edges.Del_Shape(iEdge); } } //----------------------------------------------------- return( Update() ); }
//--------------------------------------------------------- bool CSG_Network::_Add_Line(CSG_Shape *pLine, int ID) { int iEdge, iPoint, iCrossing; CSG_Shape *pEdge, *pCrossing; CSG_Shapes Crossings(SHAPE_TYPE_Point); //----------------------------------------------------- // 1. find crossings Crossings.Add_Field(SG_T("LINE_POINT") , SG_DATATYPE_Int); Crossings.Add_Field(SG_T("EDGE_ID") , SG_DATATYPE_Int); Crossings.Add_Field(SG_T("EDGE_POINT") , SG_DATATYPE_Int); Crossings.Add_Field(SG_T("EDGE_DIST") , SG_DATATYPE_Double); for(iEdge=0; iEdge<m_Edges.Get_Count(); iEdge++) { pEdge = m_Edges.Get_Shape(iEdge); if( pEdge->Intersects(pLine) ) { TSG_Point a = pEdge->Get_Point(0); for(int iEdge_Point=1; iEdge_Point<pEdge->Get_Point_Count(0); iEdge_Point++) { TSG_Point b = a; a = pEdge->Get_Point(iEdge_Point); TSG_Point A = pLine->Get_Point(0); for(iPoint=1; iPoint<pLine->Get_Point_Count(0); iPoint++) { TSG_Point C, B = A; A = pLine->Get_Point(iPoint); if( SG_Get_Crossing(C, A, B, a, b) ) { pCrossing = Crossings.Add_Shape(); pCrossing->Add_Point(C); pCrossing->Set_Value(0, iPoint); pCrossing->Set_Value(1, iEdge); pCrossing->Set_Value(2, iEdge_Point); pCrossing->Set_Value(3, SG_Get_Distance(C, b)); } } } } } //----------------------------------------------------- // 2. add new line's vertices Crossings.Set_Index(0, TABLE_INDEX_Ascending); pEdge = m_Edges.Add_Shape(); pEdge ->Set_Value(3, ID); for(iCrossing=0, iPoint=0; iCrossing<Crossings.Get_Count(); iCrossing++) { pCrossing = Crossings.Get_Shape_byIndex(iCrossing); while( iPoint < pCrossing->asInt(0) ) { pEdge->Add_Point(pLine->Get_Point(iPoint++)); } pEdge->Add_Point(pCrossing->Get_Point(0)); pEdge = m_Edges.Add_Shape(); pEdge ->Set_Value(3, ID); pEdge ->Add_Point(pCrossing->Get_Point(0)); } while( iPoint < pLine->Get_Point_Count(0) ) { pEdge->Add_Point(pLine->Get_Point(iPoint++)); } //----------------------------------------------------- // 3. split edges, if necessary Crossings.Set_Index(1, TABLE_INDEX_Descending, 2, TABLE_INDEX_Ascending, 3, TABLE_INDEX_Ascending); for(iCrossing=0; iCrossing<Crossings.Get_Count(); ) { pCrossing = Crossings.Get_Shape_byIndex(iCrossing); iEdge = pCrossing->asInt(1); pLine = m_Edges.Get_Shape(iEdge); ID = pLine->asInt(0); iPoint = 0; pEdge = m_Edges.Add_Shape(); pEdge ->Set_Value(3, pLine->asInt(3)); while( 1 ) { while( iPoint < pCrossing->asInt(2) ) { pEdge->Add_Point(pLine->Get_Point(iPoint++)); } pEdge->Add_Point(pCrossing->Get_Point(0)); if( ++iCrossing < Crossings.Get_Count() && iEdge == Crossings.Get_Shape_byIndex(iCrossing)->asInt(1) ) { pEdge = m_Edges.Add_Shape(); pEdge ->Set_Value(3, pLine->asInt(3)); pEdge->Add_Point(pCrossing->Get_Point(0)); pCrossing = Crossings.Get_Shape_byIndex(iCrossing); } else { if( iPoint < pLine->Get_Point_Count() ) { pEdge = m_Edges.Add_Shape(); pEdge ->Set_Value(3, pLine->asInt(3)); pEdge->Add_Point(pCrossing->Get_Point(0)); while( iPoint < pLine->Get_Point_Count() ) { pEdge->Add_Point(pLine->Get_Point(iPoint++)); } } break; } } m_Edges.Del_Shape(iEdge); } return( true ); }