void ogr_converter::convert_polygon(OGRPolygon* geom, feature_ptr feature) { OGRLinearRing* exterior = geom->getExteriorRing(); int num_points = exterior->getNumPoints(); int num_interior = geom->getNumInteriorRings(); int capacity = 0; for (int r = 0; r < num_interior; ++r) { OGRLinearRing* interior = geom->getInteriorRing(r); capacity += interior->getNumPoints(); } std::auto_ptr<geometry_type> poly(new geometry_type(mapnik::Polygon)); poly->move_to(exterior->getX(0), exterior->getY(0)); for (int i = 1; i < num_points - 1; ++i) { poly->line_to(exterior->getX(i), exterior->getY(i)); } poly->close(exterior->getX(num_points-1), exterior->getY(num_points-1)); for (int r = 0; r < num_interior; ++r) { OGRLinearRing* interior = geom->getInteriorRing(r); num_points = interior->getNumPoints(); poly->move_to(interior->getX(0), interior->getY(0)); for (int i = 1; i < num_points - 1; ++i) { poly->line_to(interior->getX(i), interior->getY(i)); } poly->close(interior->getX(num_points-1), interior->getY(num_points-1)); } feature->paths().push_back(poly); }
void ogr_converter::convert_polygon(OGRPolygon* geom, feature_ptr feature) { OGRLinearRing* exterior = geom->getExteriorRing(); int num_points = exterior->getNumPoints(); int num_interior = geom->getNumInteriorRings(); int capacity = 0; for (int r = 0; r < num_interior; r++) { OGRLinearRing* interior = geom->getInteriorRing(r); capacity += interior->getNumPoints(); } geometry_type* poly = new geometry_type(mapnik::Polygon); poly->move_to(exterior->getX(0), exterior->getY(0)); for (int i = 1; i < num_points; ++i) { poly->line_to(exterior->getX(i), exterior->getY(i)); } for (int r = 0; r < num_interior; r++) { OGRLinearRing* interior = geom->getInteriorRing(r); num_points = interior->getNumPoints(); poly->move_to(interior->getX(0), interior->getY(0)); for (int i = 1; i < num_points; ++i) { poly->line_to(interior->getX(i), interior->getY(i)); } } feature->add_geometry(poly); }
Polygon GeomToPoly(OGRGeometry* geometry){ OGRLinearRing *ring = dynamic_cast<OGRLinearRing *>(dynamic_cast<OGRPolygon *>(geometry)->getExteriorRing()); Polygon skeleton; for(int i=0; i < ring->getNumPoints() - 1; i++) if(CGAL::is_finite(ring->getX(i)) && CGAL::is_finite(ring->getY(i))) skeleton.push_back(Point(ring->getX(i), ring->getY(i))); return skeleton; }
/*! \brief Set feature geometry Also checks if given geometry is valid \param poGeom pointer to OGRGeometry \return TRUE on valid feature or otherwise FALSE */ bool IVFKFeature::SetGeometry(OGRGeometry *poGeom) { m_bGeometry = TRUE; delete m_paGeom; m_paGeom = NULL; m_bValid = TRUE; if (!poGeom) { return m_bValid; } /* check empty geometries */ if (m_nGeometryType == wkbNone && poGeom->IsEmpty()) { CPLDebug("OGR-VFK", "%s: empty geometry fid = %ld", m_poDataBlock->GetName(), m_nFID); m_bValid = FALSE; } /* check coordinates */ if (m_nGeometryType == wkbPoint) { double x, y; x = ((OGRPoint *) poGeom)->getX(); y = ((OGRPoint *) poGeom)->getY(); if (x > -430000 || x < -910000 || y > -930000 || y < -1230000) { CPLDebug("OGR-VFK", "%s: invalid point fid = %ld", m_poDataBlock->GetName(), m_nFID); m_bValid = FALSE; } } /* check degenerated linestrings */ if (m_nGeometryType == wkbLineString && ((OGRLineString *) poGeom)->getNumPoints() < 2) { CPLDebug("OGR-VFK", "%s: invalid linestring fid = %ld", m_poDataBlock->GetName(), m_nFID); m_bValid = FALSE; } /* check degenerated polygons */ if (m_nGeometryType == wkbPolygon) { OGRLinearRing *poRing; poRing = ((OGRPolygon *) poGeom)->getExteriorRing(); if (!poRing || poRing->getNumPoints() < 3) { CPLDebug("OGR-VFK", "%s: invalid polygon fid = %ld", m_poDataBlock->GetName(), m_nFID); m_bValid = FALSE; } } if (m_bValid) m_paGeom = (OGRGeometry *) poGeom->clone(); /* make copy */ return m_bValid; }
osg::Geometry* polygonToDrawable(OGRPolygon* polygon) const { osg::Geometry* geom = new osg::Geometry(); osg::Vec3Array* vertices = new osg::Vec3Array(); geom->setVertexArray(vertices); { OGRLinearRing *ring = polygon->getExteriorRing(); OGRPoint point; for(int i = 0; i < ring->getNumPoints(); i++) { ring->getPoint(i, &point); vertices->push_back(osg::Vec3(point.getX(), point.getY(), point.getZ())); } geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, 0, vertices->size())); } if (polygon->getNumInteriorRings()) { for (int i = 0; i < polygon->getNumInteriorRings(); i++) { OGRLinearRing *ring = polygon->getInteriorRing(i); OGRPoint point; for (int j = 0; j < ring->getNumPoints(); j++) { ring->getPoint(j, &point); vertices->push_back(osg::Vec3(point.getX(), point.getY(), point.getZ())); } geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, vertices->size()-ring->getNumPoints() , ring->getNumPoints())); } } osgUtil::Tessellator tsl; tsl.setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); tsl.setBoundaryOnly(false); tsl.retessellatePolygons(*geom); osg::Vec3Array* array = triangulizeGeometry(geom); geom->setVertexArray(array); geom->removePrimitiveSet(0,geom->getNumPrimitiveSets()); geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 0, array->size())); return geom; }
Surface* Building::extrude_envelope() const { Surface* envelope = new Surface(SurfaceType::Envelope,0,_length,_width,_height); OGRLinearRing* ringFt = _footprint->getExteriorRing(); if(ringFt->isClockwise()) adjust_winding_ccw(ringFt); envelope->addChild(new Surface(SurfaceType::Footprint,_footprint,_length,_width,0.)); //extrude roof OGRLinearRing* ringRoof = new OGRLinearRing; for(int i=0; i<ringFt->getNumPoints(); i++) ringRoof->addPoint(ringFt->getX(i),ringFt->getY(i),_height); OGRPolygon plyRoof; plyRoof.addRingDirectly(ringRoof); envelope->addChild(new Surface(SurfaceType::Roof,&plyRoof,_length,_width,0.)); //extrude walls for(int i=0; i<ringFt->getNumPoints()-1; i++) { OGRLinearRing* ringWall = new OGRLinearRing; ringWall->addPoint(ringFt->getX(i),ringFt->getY(i),0); ringWall->addPoint(ringFt->getX(i+1),ringFt->getY(i+1),0); ringWall->addPoint(ringFt->getX(i+1),ringFt->getY(i+1),_height); ringWall->addPoint(ringFt->getX(i),ringFt->getY(i),_height); ringWall->addPoint(ringFt->getX(i),ringFt->getY(i),0); OGRPolygon plyWall; plyWall.addRingDirectly(ringWall); OGRPoint pt1(ringFt->getX(i),ringFt->getY(i),0); OGRPoint pt2(ringFt->getX(i+1),ringFt->getY(i+1),0); envelope->addChild(new Wall(&plyWall,pt1.Distance(&pt2),_height,&pt1,&pt2)); } return envelope; }
osg::ref_ptr<osg::Group> MapExtruder::osg_assemble() { osg::ref_ptr<osg::Group> osg = new osg::Group; osg::ref_ptr<osg::Node> surface = osgDB::readNodeFile(_map2dFile); ExtrudeVisitor extrude(_height); surface->accept(extrude); osg->addChild(surface); int n = _sides ->getNumGeometries(); osg::ref_ptr<osg::Geode> geode = new osg::Geode; for(int i=0;i<n;i++) { osg::ref_ptr<osg::Geometry> geom = new osg::Geometry; osg::ref_ptr<osg::Vec3Array> verts = new osg::Vec3Array; std::vector<OGRPoint> points; OGRLinearRing *ring = ((OGRPolygon*)_sides->getGeometryRef(i))->getExteriorRing(); int m = ring->getNumPoints(); for(int j=0;j<m;j++) { OGRPoint pt; ring->getPoint(j,&pt); verts->push_back(osg::Vec3(pt.getX(),pt.getY(),pt.getZ())); } geom->setVertexArray(verts); osg::Vec4Array* colors = new osg::Vec4Array; colors->push_back(YELLOW); geom->setColorArray(colors); geom->setColorBinding(osg::Geometry::BIND_OVERALL); geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,0,m)); geode->addDrawable(geom); } osg->addChild(geode); return osg; }
std::vector<Polygon::Ring> Polygon::interiorRings() const { std::vector<Ring> rings; OGRwkbGeometryType t = m_geom->getGeometryType(); if (t != wkbPolygon && t != wkbPolygon25D) throw pdal_error("Request for exterior ring on non-polygon."); // OGRPolygon *poly = m_geom->toPolygon(); OGRPolygon *poly = static_cast<OGRPolygon *>(m_geom.get()); for (int i = 0; i < poly->getNumInteriorRings(); ++i) { OGRLinearRing *er = poly->getInteriorRing(i); Ring r; for (int j = 0; j < er->getNumPoints(); ++j) r.push_back({er->getX(j), er->getY(j)}); rings.push_back(r); } return rings; }
void MapExtruder::extrusion() { _sides = new OGRMultiPolygon(); int n = _map2d->getNumGeometries(); for(int i=0;i<n;i++) { OGRLinearRing* ring = ((OGRPolygon*)_map2d->getGeometryRef(i))->getExteriorRing(); ring->closeRings(); int nPoints = ring->getNumPoints(); for(int j=0;j<nPoints-1;j++) { OGRPolygon side; OGRLinearRing* ring_side = new OGRLinearRing(); OGRPoint p1,p4; ring->getPoint(j,&p1); ring->getPoint(j+1,&p4); OGRPoint p2(p1.getX(),p1.getY(),_height); OGRPoint p3(p4.getX(),p4.getY(),_height); ring_side->addPoint(&p1); ring_side->addPoint(&p2); ring_side->addPoint(&p3); ring_side->addPoint(&p4); ring_side->addPoint(&p1); side.addRing(ring_side); _sides->addGeometry(&side); } } }
MultiPolygonGraphics::MultiPolygonGraphics(OGRMultiPolygon* OGRMultiPoly, const QPen& Pen, const QBrush& Brush): SurfacicGraphics(Brush.color()) { QPainterPath Path; for (int i=0; i<OGRMultiPoly->getNumGeometries();i++) { OGRLinearRing* LinearRing = dynamic_cast<OGRPolygon*>(OGRMultiPoly->getGeometryRef(i))->getExteriorRing(); QPolygonF Poly; for (int i=0; i < LinearRing->getNumPoints(); i++) Poly << QPointF(LinearRing->getX(i),LinearRing->getY(i)); Path.addPolygon(Poly); } setPen(Pen); setBrush(Brush); setPath(Path); }
Footprint::Footprint(OGRLinearRing* r):_index(-1),_visited(false),_degenerated(false),_altitude(0),_height(0) { if(r->IsEmpty()) std::cerr<<"Footprint constructor error: empty ring"<<std::endl; // create polygon with clockwise exterior ring OGRLinearRing * ring = new OGRLinearRing(r); remove_colinear_edges(ring); if(ring->getNumPoints()>3) adjust_winding(ring,ClockWise); else _degenerated = true; _polygon = new OGRPolygon(); _polygon->addRingDirectly(ring); _polygon = new OGRPolygon(); _polygon->addRing(r); _polygon->closeRings(); }
Polygon::Ring Polygon::exteriorRing() const { Ring r; OGRwkbGeometryType t = m_geom->getGeometryType(); if (t != wkbPolygon && t != wkbPolygon25D) throw pdal_error("Request for exterior ring on non-polygon."); // Not until GDAL 2.3 /** OGRLinearRing *er = m_geom->toPolygon()->getExteriorRing(); // For some reason there's no operator -> on an iterator. for (auto it = er->begin(); it != er->end(); ++it) r.push_back({(*it).getX(), (*it).getY()}); **/ OGRLinearRing *er = static_cast<OGRPolygon *>(m_geom.get())->getExteriorRing(); for (int i = 0; i < er->getNumPoints(); ++i) r.push_back({er->getX(i), er->getY(i)}); return r; }
int OGRGetCentroid(OGRPolygon *poPoly, OGRPoint *poCentroid) { int i,j; double cent_weight_x=0.0, cent_weight_y=0.0; double len, total_len=0; for(i=0; i<OGR_NUM_RINGS(poPoly); i++) { double x1, y1, x2, y2; OGRLinearRing *poRing = OGR_GET_RING(poPoly, i); x2 = poRing->getX(0); y2 = poRing->getY(0); for(j=1; j<poRing->getNumPoints(); j++) { x1 = x2; y1 = y2; x2 = poRing->getX(j); y2 = poRing->getY(j); len = sqrt( pow((x2-x1),2) + pow((y2-y1),2) ); cent_weight_x += len * ((x1 + x2)/2.0); cent_weight_y += len * ((y1 + y2)/2.0); total_len += len; } } if(total_len == 0) return(OGRERR_FAILURE); poCentroid->setX( cent_weight_x / total_len ); poCentroid->setY( cent_weight_y / total_len ); return OGRERR_NONE; }
OGRMultiPolygon* Building::extrude_box() const { OGRMultiPolygon* block = new OGRMultiPolygon; //extrude roof OGRPolygon roof; { roof.addRing(_footprint->getExteriorRing()); OGRLinearRing* ring = roof.getExteriorRing(); for(int i=0; i<ring->getNumPoints(); i++) ring->setPoint(i,ring->getX(i),ring->getY(i),_height); } if(int n = _footprint->getNumInteriorRings()) { for (int j=0; j<n; j++) { roof.addRing(_footprint->getInteriorRing(j)); OGRLinearRing* ring = roof.getInteriorRing(j); for(int i=0; i<ring->getNumPoints(); i++) ring->setPoint(i,ring->getX(i),ring->getY(i),_height); } } block->addGeometry(&roof); //extrude exter walls OGRLinearRing* ringEx = _footprint->getExteriorRing(); for(int i=0; i<ringEx->getNumPoints()-1; i++) { OGRPolygon wall; OGRLinearRing ring; ring.addPoint(ringEx->getX(i),ringEx->getY(i),0); ring.addPoint(ringEx->getX(i+1),ringEx->getY(i+1),0); ring.addPoint(ringEx->getX(i+1),ringEx->getY(i+1),_height); ring.addPoint(ringEx->getX(i),ringEx->getY(i),_height); ring.addPoint(ringEx->getX(i),ringEx->getY(i),0); wall.addRing(&ring); block->addGeometry(&wall); } //extrude inner walls if exist if(int n = _footprint->getNumInteriorRings()) { for (int i=0; i<n; i++) { OGRLinearRing* ringIn = _footprint->getInteriorRing(i); for(int j=0; j<ringIn->getNumPoints()-1; j++) { OGRPolygon wall; OGRLinearRing ring; ring.addPoint(ringIn->getX(j),ringIn->getY(j),0); ring.addPoint(ringIn->getX(j+1),ringIn->getY(j+1),0); ring.addPoint(ringIn->getX(j+1),ringIn->getY(j+1),_height); ring.addPoint(ringIn->getX(j),ringIn->getY(j),_height); ring.addPoint(ringIn->getX(j),ringIn->getY(j),0); wall.addRing(&ring); block->addGeometry(&wall); } } } return block; }
OGRGeometryH OGRBuildPolygonFromEdges( OGRGeometryH hLines, int bBestEffort, int bAutoClose, double dfTolerance, OGRErr * peErr ) { int bSuccess = TRUE; OGRGeometryCollection *poLines = (OGRGeometryCollection *) hLines; OGRPolygon *poPolygon = new OGRPolygon(); (void) bBestEffort; /* -------------------------------------------------------------------- */ /* Setup array of line markers indicating if they have been */ /* added to a ring yet. */ /* -------------------------------------------------------------------- */ int nEdges = poLines->getNumGeometries(); int *panEdgeConsumed, nRemainingEdges = nEdges; panEdgeConsumed = (int *) CPLCalloc(sizeof(int),nEdges); /* ==================================================================== */ /* Loop generating rings. */ /* ==================================================================== */ while( nRemainingEdges > 0 ) { int iEdge; OGRLineString *poLine; /* -------------------------------------------------------------------- */ /* Find the first unconsumed edge. */ /* -------------------------------------------------------------------- */ for( iEdge = 0; panEdgeConsumed[iEdge]; iEdge++ ) {} poLine = (OGRLineString *) poLines->getGeometryRef(iEdge); /* -------------------------------------------------------------------- */ /* Start a new ring, copying in the current line directly */ /* -------------------------------------------------------------------- */ OGRLinearRing *poRing = new OGRLinearRing(); AddEdgeToRing( poRing, poLine, FALSE ); panEdgeConsumed[iEdge] = TRUE; nRemainingEdges--; /* ==================================================================== */ /* Loop adding edges to this ring until we make a whole pass */ /* within finding anything to add. */ /* ==================================================================== */ int bWorkDone = TRUE; double dfBestDist = dfTolerance; while( !CheckPoints(poRing,0,poRing,poRing->getNumPoints()-1,NULL) && nRemainingEdges > 0 && bWorkDone ) { int iBestEdge = -1, bReverse = FALSE; bWorkDone = FALSE; dfBestDist = dfTolerance; // We consider linking the end to the beginning. If this is // closer than any other option we will just close the loop. //CheckPoints(poRing,0,poRing,poRing->getNumPoints()-1,&dfBestDist); // Find unused edge with end point closest to our loose end. for( iEdge = 0; iEdge < nEdges; iEdge++ ) { if( panEdgeConsumed[iEdge] ) continue; poLine = (OGRLineString *) poLines->getGeometryRef(iEdge); if( CheckPoints(poLine,0,poRing,poRing->getNumPoints()-1, &dfBestDist) ) { iBestEdge = iEdge; bReverse = FALSE; } if( CheckPoints(poLine,poLine->getNumPoints()-1, poRing,poRing->getNumPoints()-1, &dfBestDist) ) { iBestEdge = iEdge; bReverse = TRUE; } } // We found one within tolerance - add it. if( iBestEdge != -1 ) { poLine = (OGRLineString *) poLines->getGeometryRef(iBestEdge); AddEdgeToRing( poRing, poLine, bReverse ); panEdgeConsumed[iBestEdge] = TRUE; nRemainingEdges--; bWorkDone = TRUE; } } /* -------------------------------------------------------------------- */ /* Did we fail to complete the ring? */ /* -------------------------------------------------------------------- */ dfBestDist = dfTolerance; if( !CheckPoints(poRing,0,poRing,poRing->getNumPoints()-1, &dfBestDist) ) { CPLDebug( "OGR", "Failed to close ring %d.\n" "End Points are: (%.8f,%.7f) and (%.7f,%.7f)\n", poPolygon->getNumInteriorRings()+1, poRing->getX(0), poRing->getY(0), poRing->getX(poRing->getNumPoints()-1), poRing->getY(poRing->getNumPoints()-1) ); bSuccess = FALSE; } /* -------------------------------------------------------------------- */ /* Do we need to auto-close this ring? */ /* -------------------------------------------------------------------- */ if( bAutoClose && !CheckPoints(poRing,0,poRing,poRing->getNumPoints()-1,NULL) ) { poRing->addPoint( poRing->getX(0), poRing->getY(0), poRing->getZ(0)); } poPolygon->addRingDirectly( poRing ); } /* next ring */ /* -------------------------------------------------------------------- */ /* Cleanup. */ /* -------------------------------------------------------------------- */ CPLFree( panEdgeConsumed ); // Eventually we should at least identify the external ring properly, // perhaps even ordering the direction of rings, though this isn't // required by the OGC geometry model. if( peErr != NULL ) { if( bSuccess ) *peErr = OGRERR_NONE; else *peErr = OGRERR_FAILURE; } return (OGRGeometryH) poPolygon; }
bool ShpReader::Open(std::wstring fullPath, StudyControllerPtr studyController, VectorMapControllerPtr vectorMapController, ProgressDlgPtr progressDlg) { // Registers all format drivers built into GDAL/OGR. OGRRegisterAll(); OGRDataSource *OGRDataset; // Open vector file path std::string tempStr( fullPath.begin(), fullPath.end() ); OGRDataset = OGRSFDriverRegistrar::Open( tempStr.c_str(), FALSE ); // Return if no vector files are found if( OGRDataset == NULL ) { Log::Inst().Warning("(Warning) Failed to open file at " + tempStr + "."); return false; } if ( App::Inst().GetLayerTreeController()->GetNumMapLayers() >0 ) MapControllerPtr mapController= App::Inst().GetLayerTreeController()->GetLayerTreeModel()->GetStudy(0)->GetMapLayer(0)->GetMapController(); // It appears that shapefiles (*.SHP) only support up to one layer per file // This will need to be further investigated for other vector filetypes (e.g., KML) // For now just grab layer at position 0 OGRLayer *poLayer = OGRDataset->GetLayer( 0 ); // Determine the XY boundaries for the entire vector dataset OGREnvelope psEnvelope; VectorMapModelPtr vectorMapModel = vectorMapController->GetVectorMapModel(); poLayer->GetExtent( &psEnvelope ); vectorMapModel->SetVectorBoundary(psEnvelope); if(!SetupVectorProjection(OGRDataset,studyController,poLayer,vectorMapController )) { OGRDataset->DestroyDataSource(OGRDataset); return false; } if(progressDlg) { if(!progressDlg->Update(0, _T("Reading Vector Map Information..."))) { OGRDataset->DestroyDataSource(OGRDataset); return false; } } GLdouble minX, minY, maxX, maxY; minX = minY = std::numeric_limits<float>::max(); maxX = maxY = -std::numeric_limits<float>::max(); // Retrieve features from the dataset OGRFeature *poFeature; poLayer->ResetReading(); int numFeatures = poLayer->GetFeatureCount(); int count=0; //Log::Inst().Write("Loading shapefile with the following meta data:"); while ( ( poFeature = poLayer->GetNextFeature() ) != NULL ) { ///////////////////////////////////////////////// // PHASE 1: Retrieve METADATA from the dataset // ///////////////////////////////////////////////// OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn(); int iField; for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ ) { OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField ); //if( poFieldDefn->GetType() == OFTInteger ) // printf( "%d,", poFeature->GetFieldAsInteger( iField ) ); //else if( poFieldDefn->GetType() == OFTReal ) // printf( "%.3f,", poFeature->GetFieldAsDouble(iField) ); //else if( poFieldDefn->GetType() == OFTString ) // printf( "%s,", poFeature->GetFieldAsString(iField) ); //else // printf( "%s,", poFeature->GetFieldAsString(iField) ); //ofs << poFeature->GetFieldAsString(iField) << ","; std::string metaData = poFeature->GetFieldAsString(iField); // do something with the meta data... //Log::Inst().Write(metaData); } count++; if(progressDlg) { if(!progressDlg->Update(int(50 + (float(count)/numFeatures)*50))) return false; } /////////////////////////////////////////////////// // PHASE 2: Retrieve GEOMETRIES from the dataset // /////////////////////////////////////////////////// OGRGeometry *poGeometry; poGeometry = poFeature->GetGeometryRef(); // Move to the next feature in the set if no geometry present if( poGeometry == NULL ) { OGRFeature::DestroyFeature( poFeature ); continue; } OGRwkbGeometryType whatisit = poGeometry->getGeometryType(); // Handle POINTS if ( wkbFlatten( poGeometry->getGeometryType() ) == wkbPoint ) { GeoVector* geoVector = new GeoVector(); OGRPoint *poPoint = (OGRPoint *) poGeometry; geoVector->SetGeometryType( wkbPoint ); geoVector->SetNumberOfPoints( 1 ); if(needProjection) { double x,y; x= poPoint->getX(); y= poPoint->getY(); if(!poTransform->Transform(1, &x, &y)) { Log::Inst().Warning("(Warning) Failed to project vector map."); OGRDataset->DestroyDataSource(OGRDataset); return false; } // project and store the points geoVector->pointX[0] = x; geoVector->pointY[0] = y; if(x < minX) minX=x; if(y < minY) minY=y; if(x > maxX) maxX=x; if(y > maxY) maxY=y; } else { geoVector->pointX[0] = poPoint->getX(); geoVector->pointY[0] = poPoint->getY(); } vectorMapController->GetVectorMapModel()->AddGeoVector( geoVector ); } //Handle MultiPoint else if ( wkbFlatten( poGeometry->getGeometryType() ) == wkbMultiPoint ) { OGRMultiPoint *poMultiPoint = (OGRMultiPoint *) poGeometry; for ( int currGeometry = 0; currGeometry < poMultiPoint->getNumGeometries(); currGeometry++ ) { GeoVector* geoVector = new GeoVector(); OGRPoint *poPoint = ( OGRPoint* )poMultiPoint->getGeometryRef( currGeometry ); geoVector->SetGeometryType( wkbPoint ); geoVector->SetNumberOfPoints( 1 ); if(needProjection) { double x,y; x= poPoint->getX(); y= poPoint->getY(); if(!poTransform->Transform(1, &x, &y)) { Log::Inst().Warning("(Warning) Failed to project vector map."); OGRDataset->DestroyDataSource(OGRDataset); return false; } // project and store the points geoVector->pointX[0] = x; geoVector->pointY[0] = y; if(x < minX) minX=x; if(y < minY) minY=y; if(x > maxX) maxX=x; if(y > maxY) maxY=y; } else { geoVector->pointX[0] = poPoint->getX(); geoVector->pointY[0] = poPoint->getY(); } vectorMapController->GetVectorMapModel()->AddGeoVector( geoVector ); } } //Handle Polylines else if ( wkbFlatten( poGeometry->getGeometryType() ) == wkbLineString ) { GeoVector* geoVector = new GeoVector(); OGRLineString *poLine = (OGRLineString *) poGeometry; geoVector->SetGeometryType( wkbLineString ); geoVector->SetNumberOfPoints( poLine->getNumPoints() ); // Convert and store the points for ( int currentPoint = 0; currentPoint < poLine->getNumPoints(); currentPoint++ ) { // Convert and store the points if(needProjection) { double x,y; x= poLine->getX( currentPoint ); y= poLine->getY( currentPoint ); if(!poTransform->Transform(1, &x, &y)) { Log::Inst().Warning("(Warning) Failed to project vector map."); OGRDataset->DestroyDataSource(OGRDataset); return false; } // project and store the points geoVector->pointX[currentPoint] = x; geoVector->pointY[currentPoint] = y; if(x < minX) minX=x; if(y < minY) minY=y; if(x > maxX) maxX=x; if(y > maxY) maxY=y; } else { geoVector->pointX[currentPoint] = poLine->getX( currentPoint ); geoVector->pointY[currentPoint] = poLine->getY( currentPoint ); } } vectorMapController->GetVectorMapModel()->AddGeoVector( geoVector ); } // Handle MultiPolyLine else if ( wkbFlatten( poGeometry->getGeometryType() ) == wkbMultiLineString ) { OGRMultiLineString *poMultiLine = (OGRMultiLineString *) poGeometry; for ( int currGeometry = 0; currGeometry < poMultiLine->getNumGeometries(); currGeometry++ ) { GeoVector* geoVector = new GeoVector(); OGRLineString *poLine = ( OGRLineString* )poMultiLine->getGeometryRef( currGeometry ); geoVector->SetGeometryType( wkbLineString ); geoVector->SetNumberOfPoints( poLine->getNumPoints() ); for ( int currentPoint = 0; currentPoint < poLine ->getNumPoints(); currentPoint++ ) { if(needProjection) { double x,y; x= poLine->getX( currentPoint ); y= poLine->getY( currentPoint ); if(!poTransform->Transform(1, &x, &y)) { Log::Inst().Warning("(Warning) Failed to project vector map."); OGRDataset->DestroyDataSource(OGRDataset); return false; } // project and store the points geoVector->pointX[currentPoint] = x; geoVector->pointY[currentPoint] = y; if(x < minX) minX=x; if(y < minY) minY=y; if(x > maxX) maxX=x; if(y > maxY) maxY=y; } else { geoVector->pointX[currentPoint] = poLine->getX( currentPoint ); geoVector->pointY[currentPoint] = poLine->getY( currentPoint ); } } vectorMapController->GetVectorMapModel()->AddGeoVector( geoVector ); } } // Handle POLYGONS else if ( wkbFlatten( poGeometry->getGeometryType() ) == wkbPolygon ) { GeoVector* geoVector = new GeoVector(); OGRPolygon *poPolygon = ( OGRPolygon* )poGeometry; OGRLinearRing *poLinearRing = poPolygon->getExteriorRing(); geoVector->SetGeometryType( wkbLinearRing ); geoVector->SetNumberOfPoints( poLinearRing->getNumPoints() ); for ( int currentPoint = 0; currentPoint < poLinearRing->getNumPoints(); currentPoint++ ) { if(needProjection) { double x,y; x= poLinearRing->getX( currentPoint ); y= poLinearRing->getY( currentPoint ); if(!poTransform->Transform(1, &x, &y)) { Log::Inst().Warning("(Warning) Failed to project vector map."); OGRDataset->DestroyDataSource(OGRDataset); return false; } // project and store the points geoVector->pointX[currentPoint] = x; geoVector->pointY[currentPoint] = y; if(x < minX) minX=x; if(y < minY) minY=y; if(x > maxX) maxX=x; if(y > maxY) maxY=y; } else { geoVector->pointX[currentPoint] = poLinearRing->getX( currentPoint ); geoVector->pointY[currentPoint] = poLinearRing->getY( currentPoint ); } } vectorMapController->GetVectorMapModel()->AddGeoVector( geoVector ); } // Handle MULTIPOLYGONS else if ( wkbFlatten( poGeometry->getGeometryType() ) == wkbMultiPolygon ) { OGRMultiPolygon *poMultiPolygon = (OGRMultiPolygon *) poGeometry; for ( int currGeometry = 0; currGeometry < poMultiPolygon->getNumGeometries(); currGeometry++ ) { GeoVector* geoVector = new GeoVector(); // OGRPolygon http://www.gdal.org/ogr/classOGRPolygon.html OGRPolygon *poPolygon = ( OGRPolygon* )poMultiPolygon->getGeometryRef( currGeometry ); // Retrieve the EXTERNAL ring of the multipolygon OGRLinearRing *poLinearRing = poPolygon->getExteriorRing(); geoVector->SetGeometryType( wkbLinearRing ); geoVector->SetNumberOfPoints( poLinearRing->getNumPoints() ); for ( int currentPoint = 0; currentPoint < poLinearRing->getNumPoints(); currentPoint++ ) { if(needProjection) { double x,y; x= poLinearRing->getX( currentPoint ); y= poLinearRing->getY( currentPoint ); if(!poTransform->Transform(1, &x, &y)) { Log::Inst().Warning("(Warning) Failed to project vector map."); OGRDataset->DestroyDataSource(OGRDataset); return false; } // project and store the points geoVector->pointX[currentPoint] = x; geoVector->pointY[currentPoint] = y; if(x < minX) minX=x; if(y < minY) minY=y; if(x > maxX) maxX=x; if(y > maxY) maxY=y; } else { geoVector->pointX[currentPoint] = poLinearRing->getX( currentPoint ); geoVector->pointY[currentPoint] = poLinearRing->getY( currentPoint ); } } vectorMapController->GetVectorMapModel()->AddGeoVector( geoVector ); // Retrieve all the INTERNAL rings of the multipolygon for ( int currentRing = 0; currentRing < poPolygon->getNumInteriorRings(); currentRing++ ) { GeoVector* geoVector2 = new GeoVector(); poLinearRing = poPolygon->getInteriorRing( currentRing ); geoVector2->SetGeometryType( wkbLinearRing ); geoVector2->SetNumberOfPoints( poLinearRing->getNumPoints() ); for ( int currentPoint = 0; currentPoint < poLinearRing->getNumPoints(); currentPoint++ ) { if(needProjection) { double x,y; x= poLinearRing->getX( currentPoint ); y= poLinearRing->getY( currentPoint ); if(!poTransform->Transform(1, &x, &y)) { Log::Inst().Warning("(Warning) Failed to project vector map."); OGRDataset->DestroyDataSource(OGRDataset); return false; } // project and store the points geoVector2->pointX[currentPoint] = x; geoVector2->pointY[currentPoint] = y; if(x < minX) minX=x; if(y < minY) minY=y; if(x > maxX) maxX=x; if(y > maxY) maxY=y; } else { geoVector2->pointX[currentPoint] = poLinearRing->getX( currentPoint ); geoVector2->pointY[currentPoint] = poLinearRing->getY( currentPoint ); } } vectorMapController->GetVectorMapModel()->AddGeoVector( geoVector2 ); } } } // Report a warning message for unhandled geometries else { Log::Inst().Warning("(Warning) Could not load vector data: unsupported geometry."); } OGRFeature::DestroyFeature( poFeature ); } if (float(minX) == float(maxX) && float(minY) == float(maxY)) { Log::Inst().Warning("(Warning) Failed to project vector map."); OGRDataset->DestroyDataSource(OGRDataset); return false; } if(needProjection) { vectorMapModel->SetVectorBoundary_MinX(minX); vectorMapModel->SetVectorBoundary_MaxX(maxX); vectorMapModel->SetVectorBoundary_MinY(minY); vectorMapModel->SetVectorBoundary_MaxY(maxY); } if(!SetupVectorScaling(vectorMapModel,progressDlg)) { OGRDataset->DestroyDataSource(OGRDataset); return false; } VectorMetaDataInfo(OGRDataset, studyController, vectorMapController); OGRDataSource::DestroyDataSource( OGRDataset ); return true; }
OGRErr OGRPolygon::exportToWkt( char ** ppszDstText, OGRwkbVariant eWkbVariant ) const { bool bMustWriteComma = false; /* -------------------------------------------------------------------- */ /* If we have no valid exterior ring, return POLYGON EMPTY. */ /* -------------------------------------------------------------------- */ if( getExteriorRing() == NULL || getExteriorRing()->IsEmpty() ) { if( eWkbVariant == wkbVariantIso ) { if( (flags & OGR_G_3D) && (flags & OGR_G_MEASURED) ) *ppszDstText = CPLStrdup((CPLString(getGeometryName()) + " ZM EMPTY").c_str()); else if( flags & OGR_G_MEASURED ) *ppszDstText = CPLStrdup((CPLString(getGeometryName()) + " M EMPTY").c_str()); else if( flags & OGR_G_3D ) *ppszDstText = CPLStrdup((CPLString(getGeometryName()) + " Z EMPTY").c_str()); else *ppszDstText = CPLStrdup((CPLString(getGeometryName()) + " EMPTY").c_str()); } else *ppszDstText = CPLStrdup((CPLString(getGeometryName()) + " EMPTY").c_str()); return OGRERR_NONE; } /* -------------------------------------------------------------------- */ /* Build a list of strings containing the stuff for each ring. */ /* -------------------------------------------------------------------- */ char **papszRings = static_cast<char **>(CPLCalloc(sizeof(char *), oCC.nCurveCount)); size_t nCumulativeLength = 0; size_t nNonEmptyRings = 0; size_t *pnRingBeginning = static_cast<size_t *>(CPLCalloc(sizeof(size_t), oCC.nCurveCount)); OGRErr eErr; for( int iRing = 0; iRing < oCC.nCurveCount; iRing++ ) { OGRLinearRing* poLR = (OGRLinearRing*) oCC.papoCurves[iRing]; //poLR->setFlags( getFlags() ); poLR->set3D(Is3D()); poLR->setMeasured(IsMeasured()); if( poLR->getNumPoints() == 0 ) { papszRings[iRing] = NULL; continue; } eErr = poLR->exportToWkt( &(papszRings[iRing]), eWkbVariant ); if( eErr != OGRERR_NONE ) goto error; if( STARTS_WITH_CI(papszRings[iRing], "LINEARRING ZM (") ) pnRingBeginning[iRing] = 14; else if( STARTS_WITH_CI(papszRings[iRing], "LINEARRING M (") ) pnRingBeginning[iRing] = 13; else if( STARTS_WITH_CI(papszRings[iRing], "LINEARRING Z (") ) pnRingBeginning[iRing] = 13; else if( STARTS_WITH_CI(papszRings[iRing], "LINEARRING (") ) pnRingBeginning[iRing] = 11; else { CPLAssert(false); } nCumulativeLength += strlen(papszRings[iRing] + pnRingBeginning[iRing]); nNonEmptyRings++; } /* -------------------------------------------------------------------- */ /* Allocate exactly the right amount of space for the */ /* aggregated string. */ /* -------------------------------------------------------------------- */ *ppszDstText = (char *) VSI_MALLOC_VERBOSE( nCumulativeLength + nNonEmptyRings + strlen(getGeometryName()) + strlen(" ZM ()") + 1); if( *ppszDstText == NULL ) { eErr = OGRERR_NOT_ENOUGH_MEMORY; goto error; } /* -------------------------------------------------------------------- */ /* Build up the string, freeing temporary strings as we go. */ /* -------------------------------------------------------------------- */ if( eWkbVariant == wkbVariantIso ) { if( (flags & OGR_G_3D) && (flags & OGR_G_MEASURED) ) strcpy( *ppszDstText, (CPLString(getGeometryName()) + " ZM (").c_str() ); else if( flags & OGR_G_MEASURED ) strcpy( *ppszDstText, (CPLString(getGeometryName()) + " M (").c_str() ); else if( flags & OGR_G_3D ) strcpy( *ppszDstText, (CPLString(getGeometryName()) + " Z (").c_str() ); else strcpy( *ppszDstText, (CPLString(getGeometryName()) + " (").c_str() ); } else strcpy( *ppszDstText, (CPLString(getGeometryName()) + " (").c_str() ); nCumulativeLength = strlen(*ppszDstText); for( int iRing = 0; iRing < oCC.nCurveCount; iRing++ ) { if( papszRings[iRing] == NULL ) { CPLDebug( "OGR", "OGRPolygon::exportToWkt() - skipping empty ring."); continue; } if( bMustWriteComma ) (*ppszDstText)[nCumulativeLength++] = ','; bMustWriteComma = true; size_t nRingLen = strlen(papszRings[iRing] + pnRingBeginning[iRing]); memcpy( *ppszDstText + nCumulativeLength, papszRings[iRing] + pnRingBeginning[iRing], nRingLen ); nCumulativeLength += nRingLen; VSIFree( papszRings[iRing] ); } (*ppszDstText)[nCumulativeLength++] = ')'; (*ppszDstText)[nCumulativeLength] = '\0'; CPLFree( papszRings ); CPLFree( pnRingBeginning ); return OGRERR_NONE; error: for( int iRing = 0; iRing < oCC.nCurveCount; iRing++ ) CPLFree(papszRings[iRing]); CPLFree(papszRings); return eErr; }
OGRErr OGRIngresTableLayer::PrepareOldStyleGeometry( OGRGeometry *poGeom, CPLString &osRetGeomText ) { osRetGeomText = ""; if( poGeom == NULL ) return OGRERR_FAILURE; /* -------------------------------------------------------------------- */ /* Point */ /* -------------------------------------------------------------------- */ if( EQUAL(osIngresGeomType,"POINT") && wkbFlatten(poGeom->getGeometryType()) == wkbPoint ) { OGRPoint *poPoint = (OGRPoint *) poGeom; osRetGeomText.Printf( "(%.15g,%.15g)", poPoint->getX(), poPoint->getY() ); return OGRERR_NONE; } if( EQUAL(osIngresGeomType,"IPOINT") && wkbFlatten(poGeom->getGeometryType()) == wkbPoint ) { OGRPoint *poPoint = (OGRPoint *) poGeom; osRetGeomText.Printf( "(%d,%d)", (int) floor(poPoint->getX()), (int) floor(poPoint->getY()) ); return OGRERR_NONE; } /* -------------------------------------------------------------------- */ /* Line */ /* -------------------------------------------------------------------- */ if( wkbFlatten(poGeom->getGeometryType()) == wkbLineString ) { OGRLineString *poLS = (OGRLineString *) poGeom; CPLString osLastPoint; int i; if( (EQUAL(osIngresGeomType,"LSEG") || EQUAL(osIngresGeomType,"ILSEG")) && poLS->getNumPoints() != 2 ) { CPLError( CE_Failure, CPLE_AppDefined, "Attempt to place %d vertex linestring in %s field.", poLS->getNumPoints(), osIngresGeomType.c_str() ); return OGRERR_FAILURE; } else if( EQUAL(osIngresGeomType,"LINESTRING") && poLS->getNumPoints() > 124 ) { CPLError( CE_Failure, CPLE_AppDefined, "Attempt to place %d vertex linestring in %s field.", poLS->getNumPoints(), osIngresGeomType.c_str() ); return OGRERR_FAILURE; } else if( EQUAL(osIngresGeomType,"ILINESTRING") && poLS->getNumPoints() > 248 ) { CPLError( CE_Failure, CPLE_AppDefined, "Attempt to place %d vertex linestring in %s field.", poLS->getNumPoints(), osIngresGeomType.c_str() ); return OGRERR_FAILURE; } osRetGeomText = "("; for( i = 0; i < poLS->getNumPoints(); i++ ) { CPLString osPoint; if( i > 0 && poLS->getX(i) == poLS->getX(i-1) && poLS->getY(i) == poLS->getY(i-1) ) { CPLDebug( "INGRES", "Dropping duplicate point in linestring."); continue; } if( EQUALN(osIngresGeomType,"I",1) ) osPoint.Printf( "(%d,%d)", (int) floor(poLS->getX(i)), (int) floor(poLS->getY(i)) ); else osPoint.Printf( "(%.15g,%.15g)", poLS->getX(i), poLS->getY(i) ); if( osPoint == osLastPoint ) { CPLDebug( "INGRES", "Dropping duplicate point in linestring(2)."); continue; } osLastPoint = osPoint; if( osRetGeomText.size() > 1 ) osRetGeomText += "," + osPoint; else osRetGeomText += osPoint; } osRetGeomText += ")"; return OGRERR_NONE; } /* -------------------------------------------------------------------- */ /* Polygon */ /* -------------------------------------------------------------------- */ if( wkbFlatten(poGeom->getGeometryType()) == wkbPolygon ) { OGRPolygon *poPoly = (OGRPolygon *) poGeom; OGRLinearRing *poLS = poPoly->getExteriorRing(); int i, nPoints; if( poLS == NULL ) return OGRERR_FAILURE; if( poPoly->getNumInteriorRings() > 0 ) { CPLError( CE_Warning, CPLE_AppDefined, "%d inner rings discarded from polygon being converted\n" "to old ingres spatial data type '%s'.", poPoly->getNumInteriorRings(), osIngresGeomType.c_str() ); } if( EQUAL(osIngresGeomType,"POLYGON") && poLS->getNumPoints() > 124 ) { CPLError( CE_Failure, CPLE_AppDefined, "Attempt to place %d vertex linestring in %s field.", poLS->getNumPoints(), osIngresGeomType.c_str() ); return OGRERR_FAILURE; } else if( EQUAL(osIngresGeomType,"IPOLYGON") && poLS->getNumPoints() > 248 ) { CPLError( CE_Failure, CPLE_AppDefined, "Attempt to place %d vertex linestring in %s field.", poLS->getNumPoints(), osIngresGeomType.c_str() ); return OGRERR_FAILURE; } // INGRES geometries use *implied* closure of rings. nPoints = poLS->getNumPoints(); if( poLS->getX(0) == poLS->getX(nPoints-1) && poLS->getY(0) == poLS->getY(nPoints-1) && nPoints > 1 ) nPoints--; osRetGeomText = "("; for( i = 0; i < nPoints; i++ ) { CPLString osPoint; if( i > 0 && poLS->getX(i) == poLS->getX(i-1) && poLS->getY(i) == poLS->getY(i-1) ) { CPLDebug( "INGRES", "Dropping duplicate point in linestring."); continue; } if( EQUALN(osIngresGeomType,"I",1) ) osPoint.Printf( "(%d,%d)", (int) floor(poLS->getX(i)), (int) floor(poLS->getY(i)) ); else osPoint.Printf( "(%.15g,%.15g)", poLS->getX(i), poLS->getY(i) ); if( osRetGeomText.size() > 1 ) osRetGeomText += "," + osPoint; else osRetGeomText += osPoint; } osRetGeomText += ")"; return OGRERR_NONE; } return OGRERR_FAILURE; }
OGRFeature *OGROpenAirLayer::GetNextRawFeature() { const char* pszLine; CPLString osCLASS, osNAME, osFLOOR, osCEILING; OGRLinearRing oLR; /* double dfLastLat = 0, dfLastLon = 0; */ int bFirst = TRUE; int bClockWise = TRUE; double dfCenterLat = 0, dfCenterLon = 0; int bHasCenter = FALSE; OpenAirStyle sStyle; sStyle.penStyle = -1; sStyle.penWidth = -1; sStyle.penR = sStyle.penG = sStyle.penB = -1; sStyle.fillR = sStyle.fillG = sStyle.fillB = -1; if (bEOF) return NULL; while(TRUE) { if (bFirst && bHasLastLine) { pszLine = osLastLine.c_str(); bFirst = FALSE; } else { pszLine = CPLReadLine2L(fpOpenAir, 1024, NULL); if (pszLine == NULL) { bEOF = TRUE; if (oLR.getNumPoints() == 0) return NULL; if (osCLASS.size() != 0 && oStyleMap.find(osCLASS) != oStyleMap.end()) { memcpy(&sStyle, oStyleMap[osCLASS], sizeof(sStyle)); } break; } osLastLine = pszLine; bHasLastLine = TRUE; } if (pszLine[0] == '*' || pszLine[0] == '\0') continue; if (EQUALN(pszLine, "AC ", 3) || EQUALN(pszLine, "AC,", 3)) { if (osCLASS.size() != 0) { if (sStyle.penStyle != -1 || sStyle.fillR != -1) { if (oLR.getNumPoints() == 0) { OpenAirStyle* psStyle; if (oStyleMap.find(osCLASS) == oStyleMap.end()) { psStyle = (OpenAirStyle*)CPLMalloc( sizeof(OpenAirStyle)); oStyleMap[osCLASS] = psStyle; } else psStyle = oStyleMap[osCLASS]; memcpy(psStyle, &sStyle, sizeof(sStyle)); } else break; } else if (oStyleMap.find(osCLASS) != oStyleMap.end()) { memcpy(&sStyle, oStyleMap[osCLASS], sizeof(sStyle)); break; } else break; } sStyle.penStyle = -1; sStyle.penWidth = -1; sStyle.penR = sStyle.penG = sStyle.penB = -1; sStyle.fillR = sStyle.fillG = sStyle.fillB = -1; osCLASS = pszLine + 3; bClockWise = TRUE; bHasCenter = FALSE; } else if (EQUALN(pszLine, "AN ", 3)) { if (osNAME.size() != 0) break; osNAME = pszLine + 3; } else if (EQUALN(pszLine, "AH ", 3)) osCEILING = pszLine + 3; else if (EQUALN(pszLine, "AL ", 3)) osFLOOR = pszLine + 3; else if (EQUALN(pszLine, "AT ", 3)) { /* Ignored for that layer*/ } else if (EQUALN(pszLine, "SP ", 3)) { if (osCLASS.size() != 0) { char** papszTokens = CSLTokenizeString2(pszLine+3, ", ", 0); if (CSLCount(papszTokens) == 5) { sStyle.penStyle = atoi(papszTokens[0]); sStyle.penWidth = atoi(papszTokens[1]); sStyle.penR = atoi(papszTokens[2]); sStyle.penG = atoi(papszTokens[3]); sStyle.penB = atoi(papszTokens[4]); } CSLDestroy(papszTokens); } } else if (EQUALN(pszLine, "SB ", 3)) { if (osCLASS.size() != 0) { char** papszTokens = CSLTokenizeString2(pszLine+3, ", ", 0); if (CSLCount(papszTokens) == 3) { sStyle.fillR = atoi(papszTokens[0]); sStyle.fillG = atoi(papszTokens[1]); sStyle.fillB = atoi(papszTokens[2]); } CSLDestroy(papszTokens); } } else if (EQUALN(pszLine, "DP ", 3)) { pszLine += 3; double dfLat, dfLon; if (!OGROpenAirGetLatLon(pszLine, dfLat, dfLon)) continue; oLR.addPoint(dfLon, dfLat); /* dfLastLat = dfLat; */ /* dfLastLon = dfLon; */ } else if (EQUALN(pszLine, "DA ", 3)) { pszLine += 3; char* pszStar = strchr((char*)pszLine, '*'); if (pszStar) *pszStar = 0; char** papszTokens = CSLTokenizeString2(pszLine, ",", 0); if (bHasCenter && CSLCount(papszTokens) == 3) { double dfRadius = CPLAtof(papszTokens[0]) * 1852; double dfStartAngle = CPLAtof(papszTokens[1]); double dfEndAngle = CPLAtof(papszTokens[2]); if (bClockWise && dfEndAngle < dfStartAngle) dfEndAngle += 360; else if (!bClockWise && dfStartAngle < dfEndAngle) dfEndAngle -= 360; double dfStartDistance = dfRadius; double dfEndDistance = dfRadius; int nSign = (bClockWise) ? 1 : -1; double dfAngle; double dfLat, dfLon; for(dfAngle = dfStartAngle; (dfAngle - dfEndAngle) * nSign < 0; dfAngle += nSign) { double pct = (dfAngle - dfStartAngle) / (dfEndAngle - dfStartAngle); double dfDist = dfStartDistance * (1-pct) + dfEndDistance * pct; OGRXPlane_ExtendPosition(dfCenterLat, dfCenterLon, dfDist, dfAngle, &dfLat, &dfLon); oLR.addPoint(dfLon, dfLat); } OGRXPlane_ExtendPosition(dfCenterLat, dfCenterLon, dfEndDistance, dfEndAngle, &dfLat, &dfLon); oLR.addPoint(dfLon, dfLat); /* dfLastLat = oLR.getY(oLR.getNumPoints() - 1); */ /* dfLastLon = oLR.getX(oLR.getNumPoints() - 1); */ } CSLDestroy(papszTokens); } else if (EQUALN(pszLine, "DB ", 3)) { pszLine += 3; char* pszStar = strchr((char*)pszLine, '*'); if (pszStar) *pszStar = 0; char** papszTokens = CSLTokenizeString2(pszLine, ",", 0); double dfFirstLat, dfFirstLon; double dfSecondLat, dfSecondLon; if (bHasCenter && CSLCount(papszTokens) == 2 && OGROpenAirGetLatLon(papszTokens[0], dfFirstLat, dfFirstLon) && OGROpenAirGetLatLon(papszTokens[1], dfSecondLat, dfSecondLon)) { double dfStartDistance =OGRXPlane_Distance(dfCenterLat, dfCenterLon, dfFirstLat, dfFirstLon); double dfEndDistance = OGRXPlane_Distance(dfCenterLat, dfCenterLon, dfSecondLat, dfSecondLon); double dfStartAngle = OGRXPlane_Track(dfCenterLat, dfCenterLon, dfFirstLat, dfFirstLon); double dfEndAngle = OGRXPlane_Track(dfCenterLat, dfCenterLon, dfSecondLat, dfSecondLon); if (bClockWise && dfEndAngle < dfStartAngle) dfEndAngle += 360; else if (!bClockWise && dfStartAngle < dfEndAngle) dfEndAngle -= 360; int nSign = (bClockWise) ? 1 : -1; double dfAngle; for(dfAngle = dfStartAngle; (dfAngle - dfEndAngle) * nSign < 0; dfAngle += nSign) { double dfLat, dfLon; double pct = (dfAngle - dfStartAngle) / (dfEndAngle - dfStartAngle); double dfDist = dfStartDistance * (1-pct) + dfEndDistance * pct; OGRXPlane_ExtendPosition(dfCenterLat, dfCenterLon, dfDist, dfAngle, &dfLat, &dfLon); oLR.addPoint(dfLon, dfLat); } oLR.addPoint(dfSecondLon, dfSecondLat); /* dfLastLat = oLR.getY(oLR.getNumPoints() - 1); */ /* dfLastLon = oLR.getX(oLR.getNumPoints() - 1); */ } CSLDestroy(papszTokens); } else if ((EQUALN(pszLine, "DC ", 3) || EQUALN(pszLine, "DC=", 3)) && (bHasCenter || strstr(pszLine, "V X=") != NULL)) { if (!bHasCenter) { const char* pszVX = strstr(pszLine, "V X="); bHasCenter = OGROpenAirGetLatLon(pszVX, dfCenterLat, dfCenterLon); } if (bHasCenter) { pszLine += 3; double dfRADIUS = CPLAtof(pszLine) * 1852; double dfAngle; double dfLat, dfLon; for(dfAngle = 0; dfAngle < 360; dfAngle += 1) { OGRXPlane_ExtendPosition(dfCenterLat, dfCenterLon, dfRADIUS, dfAngle, &dfLat, &dfLon); oLR.addPoint(dfLon, dfLat); } OGRXPlane_ExtendPosition(dfCenterLat, dfCenterLon, dfRADIUS, 0, &dfLat, &dfLon); oLR.addPoint(dfLon, dfLat); /* dfLastLat = oLR.getY(oLR.getNumPoints() - 1); */ /* dfLastLon = oLR.getX(oLR.getNumPoints() - 1); */ } } else if (EQUALN(pszLine, "V X=", 4)) { bHasCenter = OGROpenAirGetLatLon(pszLine + 4, dfCenterLat, dfCenterLon); } else if (EQUALN(pszLine, "V D=-", 5)) { bClockWise = FALSE; } else if (EQUALN(pszLine, "V D=+", 5)) { bClockWise = TRUE; } else { //CPLDebug("OpenAir", "Unexpected content : %s", pszLine); } } OGRFeature* poFeature = new OGRFeature(poFeatureDefn); poFeature->SetField(0, osCLASS.c_str()); poFeature->SetField(1, osNAME.c_str()); poFeature->SetField(2, osFLOOR.c_str()); poFeature->SetField(3, osCEILING.c_str()); if (sStyle.penStyle != -1 || sStyle.fillR != -1) { CPLString osStyle; if (sStyle.penStyle != -1) { osStyle += CPLString().Printf("PEN(c:#%02X%02X%02X,w:%dpt", sStyle.penR, sStyle.penG, sStyle.penB, sStyle.penWidth); if (sStyle.penStyle == 1) osStyle += ",p:\"5px 5px\""; osStyle += ")"; } if (sStyle.fillR != -1) { if (osStyle.size() != 0) osStyle += ";"; osStyle += CPLString().Printf("BRUSH(fc:#%02X%02X%02X)", sStyle.fillR, sStyle.fillG, sStyle.fillB); } else { if (osStyle.size() != 0) osStyle += ";"; osStyle += "BRUSH(fc:#00000000,id:\"ogr-brush-1\")"; } if (osStyle.size() != 0) poFeature->SetStyleString(osStyle); } OGRPolygon* poPoly = new OGRPolygon(); oLR.closeRings(); poPoly->addRing(&oLR); poPoly->assignSpatialReference(poSRS); poFeature->SetGeometryDirectly(poPoly); poFeature->SetFID(nNextFID++); return poFeature; }
OGRFeature *OGRHTFPolygonLayer::GetNextRawFeature() { OGRFeature* poFeature = new OGRFeature(poFeatureDefn); const char* pszLine; OGRLinearRing oLR; int bHastFirstCoord = FALSE; double dfFirstEasting = 0, dfFirstNorthing = 0; double dfIslandEasting = 0, dfIslandNorthing = 0; int bInIsland = FALSE; OGRPolygon* poPoly = new OGRPolygon(); while( (pszLine = CPLReadLine2L(fpHTF, 1024, NULL)) != NULL) { if (pszLine[0] == ';') { /* comment */ ; } else if (pszLine[0] == 0) { /* end of polygon is marked by a blank line */ break; } else if (strncmp(pszLine, "POLYGON DESCRIPTION: ", strlen("POLYGON DESCRIPTION: ")) == 0) { poFeature->SetField(0, pszLine + strlen("POLYGON DESCRIPTION: ")); } else if (strncmp(pszLine, "POLYGON IDENTIFIER: ", strlen("POLYGON IDENTIFIER: ")) == 0) { poFeature->SetField(1, pszLine + strlen("POLYGON IDENTIFIER: ")); } else if (strncmp(pszLine, "SEAFLOOR COVERAGE: ", strlen("SEAFLOOR COVERAGE:")) == 0) { const char* pszVal = pszLine + strlen("SEAFLOOR COVERAGE: "); if (*pszVal != '*') poFeature->SetField(2, pszVal); } else if (strncmp(pszLine, "POSITION ACCURACY: ", strlen("POSITION ACCURACY:")) == 0) { const char* pszVal = pszLine + strlen("POSITION ACCURACY: "); if (*pszVal != '*') poFeature->SetField(3, pszVal); } else if (strncmp(pszLine, "DEPTH ACCURACY: ", strlen("DEPTH ACCURACY:")) == 0) { const char* pszVal = pszLine + strlen("DEPTH ACCURACY: "); if (*pszVal != '*') poFeature->SetField(4, pszVal); } else if (strcmp(pszLine, "END OF POLYGON DATA") == 0) { bEOF = TRUE; break; } else { char** papszTokens = CSLTokenizeString(pszLine); if (CSLCount(papszTokens) == 4) { double dfEasting = atof(papszTokens[2]); double dfNorthing = atof(papszTokens[3]); if (!bHastFirstCoord) { bHastFirstCoord = TRUE; dfFirstEasting = dfEasting; dfFirstNorthing = dfNorthing; oLR.addPoint(dfEasting, dfNorthing); } else if (dfFirstEasting == dfEasting && dfFirstNorthing == dfNorthing) { if (!bInIsland) { oLR.addPoint(dfEasting, dfNorthing); poPoly->addRing(&oLR); oLR.empty(); bInIsland = TRUE; } } else if (bInIsland && oLR.getNumPoints() == 0) { dfIslandEasting = dfEasting; dfIslandNorthing = dfNorthing; oLR.addPoint(dfEasting, dfNorthing); } else if (bInIsland && dfIslandEasting == dfEasting && dfIslandNorthing == dfNorthing) { oLR.addPoint(dfEasting, dfNorthing); poPoly->addRing(&oLR); oLR.empty(); } else { oLR.addPoint(dfEasting, dfNorthing); } } CSLDestroy(papszTokens); } } if (pszLine == NULL) bEOF = TRUE; if (oLR.getNumPoints() >= 3) { oLR.closeRings(); poPoly->addRing(&oLR); } poPoly->assignSpatialReference(poSRS); poFeature->SetGeometryDirectly(poPoly); poFeature->SetFID(nNextFID++); return poFeature; }
OGRErr OGRBNALayer::CreateFeature( OGRFeature *poFeature ) { int i,j,k,n; OGRGeometry *poGeom = poFeature->GetGeometryRef(); char eol[3]; const char* partialEol = (poDS->GetMultiLine()) ? eol : poDS->GetCoordinateSeparator(); if (poGeom == NULL || poGeom->IsEmpty() ) { CPLError(CE_Failure, CPLE_AppDefined, "OGR BNA driver cannot write features with empty geometries."); return OGRERR_FAILURE; } if (poDS->GetUseCRLF()) { eol[0] = 13; eol[1] = 10; eol[2] = 0; } else { eol[0] = 10; eol[1] = 0; } if ( ! bWriter ) { return OGRERR_FAILURE; } if( poFeature->GetFID() == OGRNullFID ) poFeature->SetFID( nFeatures++ ); VSILFILE* fp = poDS->GetOutputFP(); int nbPairPerLine = poDS->GetNbPairPerLine(); switch( poGeom->getGeometryType() ) { case wkbPoint: case wkbPoint25D: { OGRPoint* point = (OGRPoint*)poGeom; WriteFeatureAttributes(fp, poFeature); VSIFPrintfL( fp, "1"); VSIFPrintfL( fp, "%s", partialEol); WriteCoord(fp, point->getX(), point->getY()); VSIFPrintfL( fp, "%s", eol); break; } case wkbPolygon: case wkbPolygon25D: { OGRPolygon* polygon = (OGRPolygon*)poGeom; OGRLinearRing* ring = polygon->getExteriorRing(); if (ring == NULL) { return OGRERR_FAILURE; } double firstX = ring->getX(0); double firstY = ring->getY(0); int nBNAPoints = ring->getNumPoints(); int is_ellipse = FALSE; /* This code tries to detect an ellipse in a polygon geometry */ /* This will only work presumably on ellipses already read from a BNA file */ /* Mostly a BNA to BNA feature... */ if (poDS->GetEllipsesAsEllipses() && polygon->getNumInteriorRings() == 0 && nBNAPoints == 361) { double oppositeX = ring->getX(180); double oppositeY = ring->getY(180); double quarterX = ring->getX(90); double quarterY = ring->getY(90); double antiquarterX = ring->getX(270); double antiquarterY = ring->getY(270); double center1X = 0.5*(firstX + oppositeX); double center1Y = 0.5*(firstY + oppositeY); double center2X = 0.5*(quarterX + antiquarterX); double center2Y = 0.5*(quarterY + antiquarterY); if (fabs(center1X - center2X) < 1e-5 && fabs(center1Y - center2Y) < 1e-5 && fabs(oppositeY - firstY) < 1e-5 && fabs(quarterX - antiquarterX) < 1e-5) { double major_radius = fabs(firstX - center1X); double minor_radius = fabs(quarterY - center1Y); is_ellipse = TRUE; for(i=0;i<360;i++) { if (!(fabs(center1X + major_radius * cos(i * (M_PI / 180)) - ring->getX(i)) < 1e-5 && fabs(center1Y + minor_radius * sin(i * (M_PI / 180)) - ring->getY(i)) < 1e-5)) { is_ellipse = FALSE; break; } } if ( is_ellipse == TRUE ) { WriteFeatureAttributes(fp, poFeature); VSIFPrintfL( fp, "2"); VSIFPrintfL( fp, "%s", partialEol); WriteCoord(fp, center1X, center1Y); VSIFPrintfL( fp, "%s", partialEol); WriteCoord(fp, major_radius, minor_radius); VSIFPrintfL( fp, "%s", eol); } } } if ( is_ellipse == FALSE) { int nInteriorRings = polygon->getNumInteriorRings(); for(i=0;i<nInteriorRings;i++) { nBNAPoints += polygon->getInteriorRing(i)->getNumPoints() + 1; } if (nBNAPoints <= 3) { CPLError( CE_Failure, CPLE_AppDefined, "Invalid geometry" ); return OGRERR_FAILURE; } WriteFeatureAttributes(fp, poFeature); VSIFPrintfL( fp, "%d", nBNAPoints); n = ring->getNumPoints(); int nbPair = 0; for(i=0;i<n;i++) { VSIFPrintfL( fp, "%s", ((nbPair % nbPairPerLine) == 0) ? partialEol : " "); WriteCoord(fp, ring->getX(i), ring->getY(i)); nbPair++; } for(i=0;i<nInteriorRings;i++) { ring = polygon->getInteriorRing(i); n = ring->getNumPoints(); for(j=0;j<n;j++) { VSIFPrintfL( fp, "%s", ((nbPair % nbPairPerLine) == 0) ? partialEol : " "); WriteCoord(fp, ring->getX(j), ring->getY(j)); nbPair++; } VSIFPrintfL( fp, "%s", ((nbPair % nbPairPerLine) == 0) ? partialEol : " "); WriteCoord(fp, firstX, firstY); nbPair++; } VSIFPrintfL( fp, "%s", eol); } break; } case wkbMultiPolygon: case wkbMultiPolygon25D: { OGRMultiPolygon* multipolygon = (OGRMultiPolygon*)poGeom; int N = multipolygon->getNumGeometries(); int nBNAPoints = 0; double firstX = 0, firstY = 0; for(i=0;i<N;i++) { OGRPolygon* polygon = (OGRPolygon*)multipolygon->getGeometryRef(i); OGRLinearRing* ring = polygon->getExteriorRing(); if (ring == NULL) continue; if (nBNAPoints) nBNAPoints ++; else { firstX = ring->getX(0); firstY = ring->getY(0); } nBNAPoints += ring->getNumPoints(); int nInteriorRings = polygon->getNumInteriorRings(); for(j=0;j<nInteriorRings;j++) { nBNAPoints += polygon->getInteriorRing(j)->getNumPoints() + 1; } } if (nBNAPoints <= 3) { CPLError( CE_Failure, CPLE_AppDefined, "Invalid geometry" ); return OGRERR_FAILURE; } WriteFeatureAttributes(fp, poFeature); VSIFPrintfL( fp, "%d", nBNAPoints); int nbPair = 0; for(i=0;i<N;i++) { OGRPolygon* polygon = (OGRPolygon*)multipolygon->getGeometryRef(i); OGRLinearRing* ring = polygon->getExteriorRing(); if (ring == NULL) continue; n = ring->getNumPoints(); int nInteriorRings = polygon->getNumInteriorRings(); for(j=0;j<n;j++) { VSIFPrintfL( fp, "%s", ((nbPair % nbPairPerLine) == 0) ? partialEol : " "); WriteCoord(fp, ring->getX(j), ring->getY(j)); nbPair++; } if (i != 0) { VSIFPrintfL( fp, "%s", ((nbPair % nbPairPerLine) == 0) ? partialEol : " "); WriteCoord(fp, firstX, firstY); nbPair++; } for(j=0;j<nInteriorRings;j++) { ring = polygon->getInteriorRing(j); n = ring->getNumPoints(); for(k=0;k<n;k++) { VSIFPrintfL( fp, "%s", ((nbPair % nbPairPerLine) == 0) ? partialEol : " "); WriteCoord(fp, ring->getX(k), ring->getY(k)); nbPair++; } VSIFPrintfL( fp, "%s", ((nbPair % nbPairPerLine) == 0) ? partialEol : " "); WriteCoord(fp, firstX, firstY); nbPair++; } } VSIFPrintfL( fp, "%s", eol); break; } case wkbLineString: case wkbLineString25D: { OGRLineString* line = (OGRLineString*)poGeom; int n = line->getNumPoints(); int i; if (n < 2) { CPLError( CE_Failure, CPLE_AppDefined, "Invalid geometry" ); return OGRERR_FAILURE; } WriteFeatureAttributes(fp, poFeature); VSIFPrintfL( fp, "-%d", n); int nbPair = 0; for(i=0;i<n;i++) { VSIFPrintfL( fp, "%s", partialEol); WriteCoord(fp, line->getX(i), line->getY(i)); nbPair++; } VSIFPrintfL( fp, "%s", eol); break; } default: { CPLError( CE_Failure, CPLE_AppDefined, "Unsupported geometry type : %s.", poGeom->getGeometryName() ); return OGRERR_UNSUPPORTED_GEOMETRY_TYPE; } } return OGRERR_NONE; }
int OGRLayer::InstallFilter( OGRGeometry * poFilter ) { if( m_poFilterGeom == NULL && poFilter == NULL ) return FALSE; /* -------------------------------------------------------------------- */ /* Replace the existing filter. */ /* -------------------------------------------------------------------- */ if( m_poFilterGeom != NULL ) { delete m_poFilterGeom; m_poFilterGeom = NULL; } if( poFilter != NULL ) m_poFilterGeom = poFilter->clone(); m_bFilterIsEnvelope = FALSE; if( m_poFilterGeom == NULL ) return TRUE; if( m_poFilterGeom != NULL ) m_poFilterGeom->getEnvelope( &m_sFilterEnvelope ); /* -------------------------------------------------------------------- */ /* Now try to determine if the filter is really a rectangle. */ /* -------------------------------------------------------------------- */ if( wkbFlatten(m_poFilterGeom->getGeometryType()) != wkbPolygon ) return TRUE; OGRPolygon *poPoly = (OGRPolygon *) m_poFilterGeom; if( poPoly->getNumInteriorRings() != 0 ) return TRUE; OGRLinearRing *poRing = poPoly->getExteriorRing(); if (poRing == NULL) return TRUE; if( poRing->getNumPoints() > 5 || poRing->getNumPoints() < 4 ) return TRUE; // If the ring has 5 points, the last should be the first. if( poRing->getNumPoints() == 5 && ( poRing->getX(0) != poRing->getX(4) || poRing->getY(0) != poRing->getY(4) ) ) return TRUE; // Polygon with first segment in "y" direction. if( poRing->getX(0) == poRing->getX(1) && poRing->getY(1) == poRing->getY(2) && poRing->getX(2) == poRing->getX(3) && poRing->getY(3) == poRing->getY(0) ) m_bFilterIsEnvelope = TRUE; // Polygon with first segment in "x" direction. if( poRing->getY(0) == poRing->getY(1) && poRing->getX(1) == poRing->getX(2) && poRing->getY(2) == poRing->getY(3) && poRing->getX(3) == poRing->getX(0) ) m_bFilterIsEnvelope = TRUE; return TRUE; }
bool MapWidget::loadCountyShapes() { OGRRegisterAll(); std::string filename = g_dataDirectory + "/counties/tl_2009_48_county00.shp"; OGRDataSource * dataSource = OGRSFDriverRegistrar::Open(filename.c_str(), false); if(dataSource == NULL) { put_flog(LOG_ERROR, "could not open %s", filename.c_str()); return false; } OGRLayer * layer = dataSource->GetLayerByName("tl_2009_48_county00"); layer->ResetReading(); OGRFeature * feature; while((feature = layer->GetNextFeature()) != NULL) { // get county FIPS code int nodeId = feature->GetFieldAsInteger("COUNTYFP00"); if(nodeId == 0) { put_flog(LOG_WARN, "invalid county"); } // add a new county to the counties map corresponding to this nodeId boost::shared_ptr<MapShape> county(new MapShape()); counties_[nodeId] = county; OGRGeometry * geometry = feature->GetGeometryRef(); if(geometry != NULL && geometry->getGeometryType() == wkbPolygon) { OGRPolygon * polygon = (OGRPolygon *)geometry; OGRLinearRing * ring = polygon->getExteriorRing(); for(int i=0; i<ring->getNumPoints(); i++) { // x is longitude, y latitude county->addVertex(ring->getY(i), ring->getX(i)); } // set the centroid OGRPoint centroidPoint; if(polygon->Centroid(¢roidPoint) == OGRERR_NONE) { county->setCentroid(centroidPoint.getY(), centroidPoint.getX()); } else { put_flog(LOG_WARN, "no polygon centroid"); } } else { put_flog(LOG_WARN, "no polygon geometry"); } OGRFeature::DestroyFeature(feature); } OGRDataSource::DestroyDataSource(dataSource); return true; }
void PCLoaderArcView::load(const std::string& file, OptionsCont& oc, PCPolyContainer& toFill, PCTypeMap&) { #ifdef HAVE_GDAL GeoConvHelper& geoConvHelper = GeoConvHelper::getProcessing(); // get defaults std::string prefix = oc.getString("prefix"); std::string type = oc.getString("type"); RGBColor color = RGBColor::parseColor(oc.getString("color")); int layer = oc.getInt("layer"); std::string idField = oc.getString("shapefile.id-column"); bool useRunningID = oc.getBool("shapefile.use-running-id"); // start parsing std::string shpName = file + ".shp"; OGRRegisterAll(); OGRDataSource* poDS = OGRSFDriverRegistrar::Open(shpName.c_str(), FALSE); if (poDS == NULL) { throw ProcessError("Could not open shape description '" + shpName + "'."); } // begin file parsing OGRLayer* poLayer = poDS->GetLayer(0); poLayer->ResetReading(); // build coordinate transformation OGRSpatialReference* origTransf = poLayer->GetSpatialRef(); OGRSpatialReference destTransf; // use wgs84 as destination destTransf.SetWellKnownGeogCS("WGS84"); OGRCoordinateTransformation* poCT = OGRCreateCoordinateTransformation(origTransf, &destTransf); if (poCT == NULL) { if (oc.isSet("shapefile.guess-projection")) { OGRSpatialReference origTransf2; origTransf2.SetWellKnownGeogCS("WGS84"); poCT = OGRCreateCoordinateTransformation(&origTransf2, &destTransf); } if (poCT == 0) { WRITE_WARNING("Could not create geocoordinates converter; check whether proj.4 is installed."); } } OGRFeature* poFeature; poLayer->ResetReading(); unsigned int runningID = 0; while ((poFeature = poLayer->GetNextFeature()) != NULL) { // read in edge attributes std::string id = useRunningID ? toString(runningID) : poFeature->GetFieldAsString(idField.c_str()); ++runningID; id = StringUtils::prune(id); if (id == "") { throw ProcessError("Missing id under '" + idField + "'"); } id = prefix + id; // read in the geometry OGRGeometry* poGeometry = poFeature->GetGeometryRef(); if (poGeometry == 0) { OGRFeature::DestroyFeature(poFeature); continue; } // try transform to wgs84 poGeometry->transform(poCT); OGRwkbGeometryType gtype = poGeometry->getGeometryType(); switch (gtype) { case wkbPoint: { OGRPoint* cgeom = (OGRPoint*) poGeometry; Position pos((SUMOReal) cgeom->getX(), (SUMOReal) cgeom->getY()); if (!geoConvHelper.x2cartesian(pos)) { WRITE_ERROR("Unable to project coordinates for POI '" + id + "'."); } PointOfInterest* poi = new PointOfInterest(id, type, color, pos, (SUMOReal)layer); if (!toFill.insert(id, poi, layer)) { WRITE_ERROR("POI '" + id + "' could not be added."); delete poi; } } break; case wkbLineString: { OGRLineString* cgeom = (OGRLineString*) poGeometry; PositionVector shape; for (int j = 0; j < cgeom->getNumPoints(); j++) { Position pos((SUMOReal) cgeom->getX(j), (SUMOReal) cgeom->getY(j)); if (!geoConvHelper.x2cartesian(pos)) { WRITE_ERROR("Unable to project coordinates for polygon '" + id + "'."); } shape.push_back_noDoublePos(pos); } Polygon* poly = new Polygon(id, type, color, shape, false, (SUMOReal)layer); if (!toFill.insert(id, poly, layer)) { WRITE_ERROR("Polygon '" + id + "' could not be added."); delete poly; } } break; case wkbPolygon: { OGRLinearRing* cgeom = ((OGRPolygon*) poGeometry)->getExteriorRing(); PositionVector shape; for (int j = 0; j < cgeom->getNumPoints(); j++) { Position pos((SUMOReal) cgeom->getX(j), (SUMOReal) cgeom->getY(j)); if (!geoConvHelper.x2cartesian(pos)) { WRITE_ERROR("Unable to project coordinates for polygon '" + id + "'."); } shape.push_back_noDoublePos(pos); } Polygon* poly = new Polygon(id, type, color, shape, true, (SUMOReal)layer); if (!toFill.insert(id, poly, layer)) { WRITE_ERROR("Polygon '" + id + "' could not be added."); delete poly; } } break; case wkbMultiPoint: { OGRMultiPoint* cgeom = (OGRMultiPoint*) poGeometry; for (int i = 0; i < cgeom->getNumGeometries(); ++i) { OGRPoint* cgeom2 = (OGRPoint*) cgeom->getGeometryRef(i); Position pos((SUMOReal) cgeom2->getX(), (SUMOReal) cgeom2->getY()); std::string tid = id + "#" + toString(i); if (!geoConvHelper.x2cartesian(pos)) { WRITE_ERROR("Unable to project coordinates for POI '" + tid + "'."); } PointOfInterest* poi = new PointOfInterest(tid, type, color, pos, (SUMOReal)layer); if (!toFill.insert(tid, poi, layer)) { WRITE_ERROR("POI '" + tid + "' could not be added."); delete poi; } } } break; case wkbMultiLineString: { OGRMultiLineString* cgeom = (OGRMultiLineString*) poGeometry; for (int i = 0; i < cgeom->getNumGeometries(); ++i) { OGRLineString* cgeom2 = (OGRLineString*) cgeom->getGeometryRef(i); PositionVector shape; std::string tid = id + "#" + toString(i); for (int j = 0; j < cgeom2->getNumPoints(); j++) { Position pos((SUMOReal) cgeom2->getX(j), (SUMOReal) cgeom2->getY(j)); if (!geoConvHelper.x2cartesian(pos)) { WRITE_ERROR("Unable to project coordinates for polygon '" + tid + "'."); } shape.push_back_noDoublePos(pos); } Polygon* poly = new Polygon(tid, type, color, shape, false, (SUMOReal)layer); if (!toFill.insert(tid, poly, layer)) { WRITE_ERROR("Polygon '" + tid + "' could not be added."); delete poly; } } } break; case wkbMultiPolygon: { OGRMultiPolygon* cgeom = (OGRMultiPolygon*) poGeometry; for (int i = 0; i < cgeom->getNumGeometries(); ++i) { OGRLinearRing* cgeom2 = ((OGRPolygon*) cgeom->getGeometryRef(i))->getExteriorRing(); PositionVector shape; std::string tid = id + "#" + toString(i); for (int j = 0; j < cgeom2->getNumPoints(); j++) { Position pos((SUMOReal) cgeom2->getX(j), (SUMOReal) cgeom2->getY(j)); if (!geoConvHelper.x2cartesian(pos)) { WRITE_ERROR("Unable to project coordinates for polygon '" + tid + "'."); } shape.push_back_noDoublePos(pos); } Polygon* poly = new Polygon(tid, type, color, shape, true, (SUMOReal)layer); if (!toFill.insert(tid, poly, layer)) { WRITE_ERROR("Polygon '" + tid + "' could not be added."); delete poly; } } } break; default: WRITE_WARNING("Unsupported shape type occured (id='" + id + "')."); break; } OGRFeature::DestroyFeature(poFeature); } PROGRESS_DONE_MESSAGE(); #else WRITE_ERROR("SUMO was compiled without GDAL support."); #endif }
OGRErr OGROCIWritableLayer::TranslateElementGroup( OGRGeometry *poGeometry ) { switch( wkbFlatten(poGeometry->getGeometryType()) ) { case wkbPoint: { OGRPoint *poPoint = (OGRPoint *) poGeometry; PushElemInfo( nOrdinalCount+1, 1, 1 ); PushOrdinal( poPoint->getX() ); PushOrdinal( poPoint->getY() ); if( nDimension == 3 ) PushOrdinal( poPoint->getZ() ); return OGRERR_NONE; } case wkbLineString: { OGRLineString *poLine = (OGRLineString *) poGeometry; int iVert; PushElemInfo( nOrdinalCount+1, 2, 1 ); for( iVert = 0; iVert < poLine->getNumPoints(); iVert++ ) { PushOrdinal( poLine->getX(iVert) ); PushOrdinal( poLine->getY(iVert) ); if( nDimension == 3 ) PushOrdinal( poLine->getZ(iVert) ); } return OGRERR_NONE; } case wkbPolygon: { OGRPolygon *poPoly = (OGRPolygon *) poGeometry; int iRing; for( iRing = -1; iRing < poPoly->getNumInteriorRings(); iRing++ ) { OGRLinearRing *poRing; int iVert; if( iRing == -1 ) poRing = poPoly->getExteriorRing(); else poRing = poPoly->getInteriorRing(iRing); if( iRing == -1 ) PushElemInfo( nOrdinalCount+1, 1003, 1 ); else PushElemInfo( nOrdinalCount+1, 2003, 1 ); if( (iRing == -1 && poRing->isClockwise()) || (iRing != -1 && !poRing->isClockwise()) ) { for( iVert = poRing->getNumPoints()-1; iVert >= 0; iVert-- ) { PushOrdinal( poRing->getX(iVert) ); PushOrdinal( poRing->getY(iVert) ); if( nDimension == 3 ) PushOrdinal( poRing->getZ(iVert) ); } } else { for( iVert = 0; iVert < poRing->getNumPoints(); iVert++ ) { PushOrdinal( poRing->getX(iVert) ); PushOrdinal( poRing->getY(iVert) ); if( nDimension == 3 ) PushOrdinal( poRing->getZ(iVert) ); } } } return OGRERR_NONE; } default: { return OGRERR_FAILURE; } } }
static void GDALCollectRingsFromGeometry( OGRGeometry *poShape, std::vector<double> &aPointX, std::vector<double> &aPointY, std::vector<double> &aPointVariant, std::vector<int> &aPartSize, GDALBurnValueSrc eBurnValueSrc) { if( poShape == NULL ) return; OGRwkbGeometryType eFlatType = wkbFlatten(poShape->getGeometryType()); int i; if ( eFlatType == wkbPoint ) { OGRPoint *poPoint = (OGRPoint *) poShape; int nNewCount = aPointX.size() + 1; aPointX.reserve( nNewCount ); aPointY.reserve( nNewCount ); aPointX.push_back( poPoint->getX() ); aPointY.push_back( poPoint->getY() ); aPartSize.push_back( 1 ); if( eBurnValueSrc != GBV_UserBurnValue ) { /*switch( eBurnValueSrc ) { case GBV_Z:*/ aPointVariant.reserve( nNewCount ); aPointVariant.push_back( poPoint->getZ() ); /*break; case GBV_M: aPointVariant.reserve( nNewCount ); aPointVariant.push_back( poPoint->getM() ); }*/ } } else if ( eFlatType == wkbLineString ) { OGRLineString *poLine = (OGRLineString *) poShape; int nCount = poLine->getNumPoints(); int nNewCount = aPointX.size() + nCount; aPointX.reserve( nNewCount ); aPointY.reserve( nNewCount ); if( eBurnValueSrc != GBV_UserBurnValue ) aPointVariant.reserve( nNewCount ); for ( i = nCount - 1; i >= 0; i-- ) { aPointX.push_back( poLine->getX(i) ); aPointY.push_back( poLine->getY(i) ); if( eBurnValueSrc != GBV_UserBurnValue ) { /*switch( eBurnValueSrc ) { case GBV_Z:*/ aPointVariant.push_back( poLine->getZ(i) ); /*break; case GBV_M: aPointVariant.push_back( poLine->getM(i) ); }*/ } } aPartSize.push_back( nCount ); } else if ( EQUAL(poShape->getGeometryName(),"LINEARRING") ) { OGRLinearRing *poRing = (OGRLinearRing *) poShape; int nCount = poRing->getNumPoints(); int nNewCount = aPointX.size() + nCount; aPointX.reserve( nNewCount ); aPointY.reserve( nNewCount ); if( eBurnValueSrc != GBV_UserBurnValue ) aPointVariant.reserve( nNewCount ); for ( i = nCount - 1; i >= 0; i-- ) { aPointX.push_back( poRing->getX(i) ); aPointY.push_back( poRing->getY(i) ); } if( eBurnValueSrc != GBV_UserBurnValue ) { /*switch( eBurnValueSrc ) { case GBV_Z:*/ aPointVariant.push_back( poRing->getZ(i) ); /*break; case GBV_M: aPointVariant.push_back( poRing->getM(i) ); }*/ } aPartSize.push_back( nCount ); } else if( eFlatType == wkbPolygon ) { OGRPolygon *poPolygon = (OGRPolygon *) poShape; GDALCollectRingsFromGeometry( poPolygon->getExteriorRing(), aPointX, aPointY, aPointVariant, aPartSize, eBurnValueSrc ); for( i = 0; i < poPolygon->getNumInteriorRings(); i++ ) GDALCollectRingsFromGeometry( poPolygon->getInteriorRing(i), aPointX, aPointY, aPointVariant, aPartSize, eBurnValueSrc ); } else if( eFlatType == wkbMultiPoint || eFlatType == wkbMultiLineString || eFlatType == wkbMultiPolygon || eFlatType == wkbGeometryCollection ) { OGRGeometryCollection *poGC = (OGRGeometryCollection *) poShape; for( i = 0; i < poGC->getNumGeometries(); i++ ) GDALCollectRingsFromGeometry( poGC->getGeometryRef(i), aPointX, aPointY, aPointVariant, aPartSize, eBurnValueSrc ); } else { CPLDebug( "GDAL", "Rasterizer ignoring non-polygonal geometry." ); } }
OGRFeature *OGRSUALayer::GetNextRawFeature() { if( bEOF ) return nullptr; CPLString osTYPE; CPLString osCLASS; CPLString osTITLE; CPLString osTOPS; CPLString osBASE; OGRLinearRing oLR; double dfLastLat = 0.0; double dfLastLon = 0.0; bool bFirst = true; while( true ) { const char* pszLine = nullptr; if( bFirst && bHasLastLine ) { pszLine = osLastLine.c_str(); bFirst = false; } else { pszLine = CPLReadLine2L(fpSUA, 1024, nullptr); if (pszLine == nullptr) { bEOF = true; if (oLR.getNumPoints() == 0) return nullptr; break; } osLastLine = pszLine; bHasLastLine = true; } if (pszLine[0] == '#' || pszLine[0] == '\0') continue; if (STARTS_WITH_CI(pszLine, "TYPE=")) { if (!osTYPE.empty()) break; osTYPE = pszLine + 5; } else if (STARTS_WITH_CI(pszLine, "CLASS=")) { if (!osCLASS.empty()) break; osCLASS = pszLine + 6; } else if (STARTS_WITH_CI(pszLine, "TITLE=")) { if (!osTITLE.empty()) break; osTITLE = pszLine + 6; } else if (STARTS_WITH_CI(pszLine, "TOPS=")) osTOPS = pszLine + 5; else if (STARTS_WITH_CI(pszLine, "BASE=")) osBASE = pszLine + 5; else if (STARTS_WITH_CI(pszLine, "POINT=")) { pszLine += 6; if (strlen(pszLine) != 16) continue; double dfLat = 0.0; double dfLon = 0.0; if (!GetLatLon(pszLine, dfLat, dfLon)) continue; oLR.addPoint(dfLon, dfLat); dfLastLat = dfLat; dfLastLon = dfLon; } else if (STARTS_WITH_CI(pszLine, "CLOCKWISE") || STARTS_WITH_CI(pszLine, "ANTI-CLOCKWISE")) { if (oLR.getNumPoints() == 0) continue; int bClockWise = STARTS_WITH_CI(pszLine, "CLOCKWISE"); /*const char* pszRADIUS = strstr(pszLine, "RADIUS="); if (pszRADIUS == NULL) continue; double dfRADIUS = CPLAtof(pszRADIUS + 7) * 1852;*/ const char* pszCENTRE = strstr(pszLine, "CENTRE="); if (pszCENTRE == nullptr) continue; pszCENTRE += 7; if (strlen(pszCENTRE) < 17 || pszCENTRE[16] != ' ') continue; double dfCenterLat = 0.0; double dfCenterLon = 0.0; if (!GetLatLon(pszCENTRE, dfCenterLat, dfCenterLon)) continue; const char* pszTO = strstr(pszLine, "TO="); if (pszTO == nullptr) continue; pszTO += 3; if (strlen(pszTO) != 16) continue; double dfToLat = 0.0; double dfToLon = 0.0; if (!GetLatLon(pszTO, dfToLat, dfToLon)) continue; const double dfStartDistance = OGR_GreatCircle_Distance(dfCenterLat, dfCenterLon, dfLastLat, dfLastLon); const double dfEndDistance = OGR_GreatCircle_Distance(dfCenterLat, dfCenterLon, dfToLat, dfToLon); const double dfStartAngle = OGR_GreatCircle_InitialHeading(dfCenterLat, dfCenterLon, dfLastLat, dfLastLon); double dfEndAngle = OGR_GreatCircle_InitialHeading(dfCenterLat, dfCenterLon, dfToLat, dfToLon); if( bClockWise && dfEndAngle < dfStartAngle ) dfEndAngle += 360; else if (!bClockWise && dfStartAngle < dfEndAngle) dfEndAngle -= 360; int nSign = (bClockWise) ? 1 : -1; for( double dfAngle = dfStartAngle; (dfAngle - dfEndAngle) * nSign < 0; dfAngle += nSign ) { const double pct = (dfAngle - dfStartAngle) / (dfEndAngle - dfStartAngle); const double dfDist = dfStartDistance * (1-pct) + dfEndDistance * pct; double dfLat = 0.0; double dfLon = 0.0; OGR_GreatCircle_ExtendPosition(dfCenterLat, dfCenterLon, dfDist, dfAngle, &dfLat, &dfLon); oLR.addPoint(dfLon, dfLat); } oLR.addPoint(dfToLon, dfToLat); dfLastLat = oLR.getY(oLR.getNumPoints() - 1); dfLastLon = oLR.getX(oLR.getNumPoints() - 1); } else if (STARTS_WITH_CI(pszLine, "CIRCLE")) { const char* pszRADIUS = strstr(pszLine, "RADIUS="); if (pszRADIUS == nullptr) continue; double dfRADIUS = CPLAtof(pszRADIUS + 7) * 1852; const char* pszCENTRE = strstr(pszLine, "CENTRE="); if (pszCENTRE == nullptr) continue; pszCENTRE += 7; if (strlen(pszCENTRE) != 16) continue; double dfCenterLat = 0.0; double dfCenterLon = 0.0; if (!GetLatLon(pszCENTRE, dfCenterLat, dfCenterLon)) continue; double dfLat = 0.0; double dfLon = 0.0; for( double dfAngle = 0; dfAngle < 360; dfAngle += 1 ) { OGR_GreatCircle_ExtendPosition(dfCenterLat, dfCenterLon, dfRADIUS, dfAngle, &dfLat, &dfLon); oLR.addPoint(dfLon, dfLat); } OGR_GreatCircle_ExtendPosition(dfCenterLat, dfCenterLon, dfRADIUS, 0, &dfLat, &dfLon); oLR.addPoint(dfLon, dfLat); dfLastLat = oLR.getY(oLR.getNumPoints() - 1); dfLastLon = oLR.getX(oLR.getNumPoints() - 1); } else if (STARTS_WITH_CI(pszLine, "INCLUDE") || STARTS_WITH_CI(pszLine, "END")) { } else { CPLDebug("SUA", "Unexpected content : %s", pszLine); } } OGRFeature* poFeature = new OGRFeature(poFeatureDefn); poFeature->SetField(0, osTYPE.c_str()); poFeature->SetField(1, osCLASS.c_str()); poFeature->SetField(2, osTITLE.c_str()); poFeature->SetField(3, osTOPS.c_str()); poFeature->SetField(4, osBASE.c_str()); OGRPolygon* poPoly = new OGRPolygon(); poPoly->assignSpatialReference(poSRS); oLR.closeRings(); poPoly->addRing(&oLR); poFeature->SetGeometryDirectly(poPoly); poFeature->SetFID(nNextFID++); return poFeature; }
int OGRPolygonLabelPoint(OGRPolygon *poPoly, OGRPoint *poLabelPoint) { double slope; OGRRawPoint point1, point2; int i, j, k, nfound; double x, y, *xintersect, temp; double hi_y, lo_y; int wrong_order, n; double len, max_len=0; double skip; OGREnvelope oEnv; if (poPoly == NULL) return OGRERR_FAILURE; poPoly->getEnvelope(&oEnv); poLabelPoint->setX((oEnv.MaxX + oEnv.MinX)/2.0); poLabelPoint->setY((oEnv.MaxY + oEnv.MinY)/2.0); //if(get_centroid(p, lp, &miny, &maxy) == -1) return(-1); if(OGRIntersectPointPolygon(poLabelPoint, poPoly) == TRUE) /* cool, done */ return OGRERR_NONE; /* do it the hard way - scanline */ skip = (oEnv.MaxY - oEnv.MinY)/NUM_SCANLINES; n=0; for(j=0; j<OGR_NUM_RINGS(poPoly); j++) { /* count total number of points */ n += OGR_GET_RING(poPoly, j)->getNumPoints(); } xintersect = (double *)calloc(n, sizeof(double)); if (xintersect == NULL) return OGRERR_FAILURE; for(k=1; k<=NUM_SCANLINES; k++) { /* sample the shape in the y direction */ y = oEnv.MaxY - k*skip; /* need to find a y that won't intersect any vertices exactly */ hi_y = y - 1; /* first initializing lo_y, hi_y to be any 2 pnts on either side of lp->y */ lo_y = y + 1; for(j=0; j<OGR_NUM_RINGS(poPoly); j++) { OGRLinearRing *poRing = OGR_GET_RING(poPoly,j); if((lo_y < y) && (hi_y >= y)) break; /* already initialized */ for(i=0; i < poRing->getNumPoints(); i++) { if((lo_y < y) && (hi_y >= y)) break; /* already initialized */ if(poRing->getY(i) < y) lo_y = poRing->getY(i); if(poRing->getY(i) >= y) hi_y = poRing->getY(i); } } n=0; for(j=0; j<OGR_NUM_RINGS(poPoly); j++) { OGRLinearRing *poRing = OGR_GET_RING(poPoly,j); for(i=0; i < poRing->getNumPoints(); i++) { if((poRing->getY(i) < y) && ((y - poRing->getY(i)) < (y - lo_y))) lo_y = poRing->getY(i); if((poRing->getY(i) >= y) && ((poRing->getY(i) - y) < (hi_y - y))) hi_y = poRing->getY(i); } } if(lo_y == hi_y) return OGRERR_FAILURE; else y = (hi_y + lo_y)/2.0; nfound = 0; for(j=0; j<OGR_NUM_RINGS(poPoly); j++) /* for each line */ { OGRLinearRing *poRing = OGR_GET_RING(poPoly,j); point1.x = poRing->getX(poRing->getNumPoints()-1); point1.y = poRing->getY(poRing->getNumPoints()-1); for(i=0; i < poRing->getNumPoints(); i++) { point2.x = poRing->getX(i); point2.y = poRing->getY(i); if(EDGE_CHECK(point1.y, y, point2.y) == CLIP_MIDDLE) { if(point1.y == point2.y) continue; /* ignore horizontal edges */ else slope = (point2.x - point1.x) / (point2.y - point1.y); x = point1.x + (y - point1.y)*slope; xintersect[nfound++] = x; } /* End of checking this edge */ point1 = point2; /* Go on to next edge */ } } /* Finished the scanline */ /* First, sort the intersections */ do { wrong_order = 0; for(i=0; i < nfound-1; i++) { if(xintersect[i] > xintersect[i+1]) { wrong_order = 1; SWAP(xintersect[i], xintersect[i+1], temp); } } } while(wrong_order); /* Great, now find longest span */ point1.y = point2.y = y; for(i=0; i < nfound; i += 2) { point1.x = xintersect[i]; point2.x = xintersect[i+1]; /* len = length(point1, point2); */ len = ABS((point2.x - point1.x)); if(len > max_len) { max_len = len; poLabelPoint->setX( (point1.x + point2.x)/2 ); poLabelPoint->setY( y ); } } } free(xintersect); /* __TODO__ Bug 673 * There seem to be some polygons for which the label is returned * completely outside of the polygon's MBR and this messes the * file bounds, etc. * Until we find the source of the problem, we'll at least validate * the label point to make sure that it overlaps the polygon MBR. */ if( poLabelPoint->getX() < oEnv.MinX || poLabelPoint->getY() < oEnv.MinY || poLabelPoint->getX() > oEnv.MaxX || poLabelPoint->getY() > oEnv.MaxY ) { // Reset label coordinates to center of MBR, just in case poLabelPoint->setX((oEnv.MaxX + oEnv.MinX)/2.0); poLabelPoint->setY((oEnv.MaxY + oEnv.MinY)/2.0); // And return an error return OGRERR_FAILURE; } if(max_len > 0) return OGRERR_NONE; else return OGRERR_FAILURE; }
/*! \brief Load geometry (polygon BUD/PAR layers) \return number of invalid features */ int VFKDataBlockDB::LoadGeometryPolygon() { int nInvalidNoLines, nInvalidNoRings, nGeometries, nBridges; int rowId, nCount, nCountMax; size_t nLines; GIntBig iFID; bool bIsPar, bNewRing, bFound; CPLString osSQL; const char *vrColumn[2]; GUIntBig vrValue[2]; GUIntBig id, idOb; sqlite3_stmt *hStmt; VFKReaderDB *poReader; VFKDataBlockDB *poDataBlockLines1, *poDataBlockLines2; VFKFeatureDB *poFeature; VFKFeatureDBList poLineList; /* first is to be considered as exterior */ PointListArray poRingList; std::vector<OGRLinearRing *> poLinearRingList; OGRPolygon ogrPolygon; OGRLinearRing *poOgrRing; nInvalidNoLines = nInvalidNoRings = nGeometries = 0; poReader = (VFKReaderDB*) m_poReader; if (EQUAL (m_pszName, "PAR")) { poDataBlockLines1 = (VFKDataBlockDB *) m_poReader->GetDataBlock("HP"); poDataBlockLines2 = poDataBlockLines1; bIsPar = TRUE; } else { poDataBlockLines1 = (VFKDataBlockDB *) m_poReader->GetDataBlock("OB"); poDataBlockLines2 = (VFKDataBlockDB *) m_poReader->GetDataBlock("SBP"); bIsPar = FALSE; } if (NULL == poDataBlockLines1) { CPLError(CE_Warning, CPLE_FileIO, "Data block %s not found. Unable to build geometry for %s.", bIsPar ? "HP" : "OB", m_pszName); return -1; } if (NULL == poDataBlockLines2) { CPLError(CE_Warning, CPLE_FileIO, "Data block %s not found. Unable to build geometry for %s.", "SBP", m_pszName); return -1; } poDataBlockLines1->LoadGeometry(); poDataBlockLines2->LoadGeometry(); if (LoadGeometryFromDB()) /* try to load geometry from DB */ return 0; if (bIsPar) { vrColumn[0] = "PAR_ID_1"; vrColumn[1] = "PAR_ID_2"; } else { vrColumn[0] = "OB_ID"; vrColumn[1] = "PORADOVE_CISLO_BODU"; vrValue[1] = 1; } osSQL.Printf("SELECT ID,%s,rowid FROM %s", FID_COLUMN, m_pszName); if (poReader->IsSpatial()) poReader->ExecuteSQL("BEGIN"); std::vector<VFKDbValue> record; record.push_back(VFKDbValue(DT_BIGINT)); record.push_back(VFKDbValue(DT_BIGINT)); record.push_back(VFKDbValue(DT_INT)); poReader->PrepareStatement(osSQL.c_str()); while(poReader->ExecuteSQL(record) == OGRERR_NONE) { nBridges = 0; /* read values */ id = static_cast<GIntBig> (record[0]); iFID = static_cast<GIntBig> (record[1]); rowId = static_cast<int> (record[2]); poFeature = (VFKFeatureDB *) GetFeatureByIndex(rowId - 1); CPLAssert(NULL != poFeature && poFeature->GetFID() == iFID); if (bIsPar) { vrValue[0] = vrValue[1] = id; poLineList = poDataBlockLines1->GetFeatures(vrColumn, vrValue, 2); } else { VFKFeatureDB *poLineSbp; std::vector<VFKFeatureDB *> poLineListOb; sqlite3_stmt *hStmtOb; osSQL.Printf("SELECT ID FROM %s WHERE BUD_ID = " CPL_FRMT_GUIB, poDataBlockLines1->GetName(), id); if (poReader->IsSpatial()) { CPLString osColumn; osColumn.Printf(" AND %s IS NULL", GEOM_COLUMN); osSQL += osColumn; } std::vector<VFKDbValue> record1; record1.push_back(VFKDbValue(DT_BIGINT)); poReader->PrepareStatement(osSQL.c_str(), 1); while(poReader->ExecuteSQL(record, 1) == OGRERR_NONE) { idOb = static_cast<GIntBig> (record[0]); vrValue[0] = idOb; poLineSbp = poDataBlockLines2->GetFeature(vrColumn, vrValue, 2); if (poLineSbp) poLineList.push_back(poLineSbp); } } nLines = poLineList.size(); if (nLines < 1) { CPLDebug("OGR-VFK", "%s: unable to collect rings for polygon fid = %ld (no lines)", m_pszName, iFID); nInvalidNoLines++; continue; } /* clear */ ogrPolygon.empty(); poRingList.clear(); /* collect rings from lines */ bFound = FALSE; nCount = 0; nCountMax = static_cast<int>(nLines) * 2; while (poLineList.size() > 0 && nCount < nCountMax) { bNewRing = !bFound ? TRUE : FALSE; bFound = FALSE; int i = 1; for (VFKFeatureDBList::iterator iHp = poLineList.begin(), eHp = poLineList.end(); iHp != eHp; ++iHp, ++i) { const OGRLineString *pLine = (OGRLineString *) (*iHp)->GetGeometry(); if (pLine && AppendLineToRing(&poRingList, pLine, bNewRing)) { bFound = TRUE; poLineList.erase(iHp); break; } } nCount++; } CPLDebug("OGR-VFK", "%s: fid = %ld nlines = %d -> nrings = %d", m_pszName, iFID, (int)nLines, (int)poRingList.size()); if (poLineList.size() > 0) { CPLDebug("OGR-VFK", "%s: unable to collect rings for polygon fid = %ld", m_pszName, iFID); nInvalidNoRings++; continue; } /* build rings */ poLinearRingList.clear(); int i = 1; for (PointListArray::const_iterator iRing = poRingList.begin(), eRing = poRingList.end(); iRing != eRing; ++iRing) { OGRPoint *poPoint; PointList *poList = *iRing; poLinearRingList.push_back(new OGRLinearRing()); poOgrRing = poLinearRingList.back(); CPLAssert(NULL != poOgrRing); for (PointList::iterator iPoint = poList->begin(), ePoint = poList->end(); iPoint != ePoint; ++iPoint) { poPoint = &(*iPoint); poOgrRing->addPoint(poPoint); } i++; } /* find exterior ring */ if (poLinearRingList.size() > 1) { double dArea, dMaxArea; std::vector<OGRLinearRing *>::iterator exteriorRing; exteriorRing = poLinearRingList.begin(); dMaxArea = -1.; for (std::vector<OGRLinearRing *>::iterator iRing = poLinearRingList.begin(), eRing = poLinearRingList.end(); iRing != eRing; ++iRing) { poOgrRing = *iRing; if (!IsRingClosed(poOgrRing)) continue; /* skip unclosed rings */ dArea = poOgrRing->get_Area(); if (dArea > dMaxArea) { dMaxArea = dArea; exteriorRing = iRing; } } if (exteriorRing != poLinearRingList.begin()) { std::swap(*poLinearRingList.begin(), *exteriorRing); } } /* build polygon from rings */ for (std::vector<OGRLinearRing *>::iterator iRing = poLinearRingList.begin(), eRing = poLinearRingList.end(); iRing != eRing; ++iRing) { poOgrRing = *iRing; /* check if ring is closed */ if (IsRingClosed(poOgrRing)) { ogrPolygon.addRing(poOgrRing); } else { if (poOgrRing->getNumPoints() == 2) { CPLDebug("OGR-VFK", "%s: Polygon (fid = %ld) bridge removed", m_pszName, iFID); nBridges++; } else { CPLDebug("OGR-VFK", "%s: Polygon (fid = %ld) unclosed ring skipped", m_pszName, iFID); } } delete poOgrRing; *iRing = NULL; } /* set polygon */ ogrPolygon.setCoordinateDimension(2); /* force 2D */ if (ogrPolygon.getNumInteriorRings() + nBridges != (int) poLinearRingList.size() - 1 || !poFeature->SetGeometry(&ogrPolygon)) { nInvalidNoRings++; continue; } /* store also geometry in DB */ if (poReader->IsSpatial() && SaveGeometryToDB(&ogrPolygon, rowId) != OGRERR_FAILURE) nGeometries++; } /* free ring list */ for (PointListArray::iterator iRing = poRingList.begin(), eRing = poRingList.end(); iRing != eRing; ++iRing) { delete (*iRing); *iRing = NULL; } CPLDebug("OGR-VFK", "%s: nolines = %d norings = %d", m_pszName, nInvalidNoLines, nInvalidNoRings); /* update number of geometries in VFK_DB_TABLE table */ UpdateVfkBlocks(nGeometries); if (poReader->IsSpatial()) poReader->ExecuteSQL("COMMIT"); return nInvalidNoLines + nInvalidNoRings; }
/*! \brief Set feature geometry Also checks if given geometry is valid \param poGeom pointer to OGRGeometry \param ftype geometry VFK type \return TRUE on valid feature or otherwise FALSE */ bool IVFKFeature::SetGeometry(OGRGeometry *poGeom, const char *ftype) { m_bGeometry = TRUE; delete m_paGeom; m_paGeom = NULL; m_bValid = TRUE; if (!poGeom) { return m_bValid; } /* check empty geometries */ if (m_nGeometryType == wkbNone && poGeom->IsEmpty()) { CPLError(CE_Warning, CPLE_AppDefined, "%s: empty geometry fid = " CPL_FRMT_GIB, m_poDataBlock->GetName(), m_nFID); m_bValid = FALSE; } /* check coordinates */ if (m_nGeometryType == wkbPoint) { double x, y; x = ((OGRPoint *) poGeom)->getX(); y = ((OGRPoint *) poGeom)->getY(); if (x > -430000 || x < -910000 || y > -930000 || y < -1230000) { CPLDebug("OGR-VFK", "%s: invalid point fid = " CPL_FRMT_GIB, m_poDataBlock->GetName(), m_nFID); m_bValid = FALSE; } } /* check degenerated polygons */ if (m_nGeometryType == wkbPolygon) { OGRLinearRing *poRing; poRing = ((OGRPolygon *) poGeom)->getExteriorRing(); if (!poRing || poRing->getNumPoints() < 3) { CPLDebug("OGR-VFK", "%s: invalid polygon fid = " CPL_FRMT_GIB, m_poDataBlock->GetName(), m_nFID); m_bValid = FALSE; } } if (m_bValid) { if (ftype) { OGRPoint pt; OGRGeometry *poGeomCurved; OGRCircularString poGeomString; poGeomCurved = NULL; if (EQUAL(ftype, "15") || EQUAL(ftype, "16")) { /* -> circle or arc */ int npoints; npoints = ((OGRLineString *) poGeom)->getNumPoints(); for (int i = 0; i < npoints; i++) { ((OGRLineString *) poGeom)->getPoint(i, &pt); poGeomString.addPoint(&pt); } if (EQUAL(ftype, "15")) { /* compute center and radius of a circle */ double x[3], y[3]; double m1, n1, m2, n2, c1, c2, mx; double c_x, c_y; for (int i = 0; i < npoints; i++) { ((OGRLineString *) poGeom)->getPoint(i, &pt); x[i] = pt.getX(); y[i] = pt.getY(); } m1 = (x[0] + x[1]) / 2.0; n1 = (y[0] + y[1]) / 2.0; m2 = (x[0] + x[2]) / 2.0; n2 = (y[0] + y[2]) / 2.0; c1 = (x[1] - x[0]) * m1 + (y[1] - y[0]) * n1; c2 = (x[2] - x[0]) * m2 + (y[2] - y[0]) * n2; mx = (x[1] - x[0]) * (y[2] - y[0]) + (y[1] - y[0]) * (x[0] - x[2]); c_x = (c1 * (y[2] - y[0]) + c2 * (y[0] - y[1])) / mx; c_y = (c1 * (x[0] - x[2]) + c2 * (x[1] - x[0])) / mx; /* compute a new intermediate point */ pt.setX(c_x - (x[1] - c_x)); pt.setY(c_y - (y[1] - c_y)); poGeomString.addPoint(&pt); /* add last point */ ((OGRLineString *) poGeom)->getPoint(0, &pt); poGeomString.addPoint(&pt); } } else if (strlen(ftype) > 2 && EQUALN(ftype, "15", 2)) { /* -> circle with radius */ float r; char s[3]; /* 15 */ r = 0; if (2 != sscanf(ftype, "%s %f", s, &r) || r < 0) { CPLDebug("OGR-VFK", "%s: invalid circle (unknown or negative radius) " "fid = " CPL_FRMT_GIB, m_poDataBlock->GetName(), m_nFID); m_bValid = FALSE; } else { double c_x, c_y; ((OGRLineString *) poGeom)->getPoint(0, &pt); c_x = pt.getX(); c_y = pt.getY(); /* define first point on a circle */ pt.setX(c_x + r); pt.setY(c_y); poGeomString.addPoint(&pt); /* define second point on a circle */ pt.setX(c_x); pt.setY(c_y + r); poGeomString.addPoint(&pt); /* define third point on a circle */ pt.setX(c_x - r); pt.setY(c_y); poGeomString.addPoint(&pt); /* define fourth point on a circle */ pt.setX(c_x); pt.setY(c_y - r); poGeomString.addPoint(&pt); /* define last point (=first) on a circle */ pt.setX(c_x + r); pt.setY(c_y); poGeomString.addPoint(&pt); } } else if (EQUAL(ftype, "11")) { /* curve */ int npoints; npoints = ((OGRLineString *) poGeom)->getNumPoints(); if (npoints > 2) { /* circular otherwise line string */ for (int i = 0; i < npoints; i++) { ((OGRLineString *) poGeom)->getPoint(i, &pt); poGeomString.addPoint(&pt); } } } if (!poGeomString.IsEmpty()) poGeomCurved = poGeomString.CurveToLine(); if (poGeomCurved) { int npoints; npoints = ((OGRLineString *) poGeomCurved)->getNumPoints(); CPLDebug("OGR-VFK", "%s: curve (type=%s) to linestring (npoints=%d) fid = " CPL_FRMT_GIB, m_poDataBlock->GetName(), ftype, npoints, m_nFID); if (npoints > 1) m_paGeom = (OGRGeometry *) poGeomCurved->clone(); delete poGeomCurved; } } if (!m_paGeom) { /* check degenerated linestrings */ if (m_nGeometryType == wkbLineString) { int npoints; npoints = ((OGRLineString *) poGeom)->getNumPoints(); if (npoints < 2) { CPLError(CE_Warning, CPLE_AppDefined, "%s: invalid linestring (%d vertices) fid = " CPL_FRMT_GIB, m_poDataBlock->GetName(), npoints, m_nFID); m_bValid = FALSE; } } if (m_bValid) m_paGeom = (OGRGeometry *) poGeom->clone(); /* make copy */ } } return m_bValid; }