📄 readerwriterogr.cpp
字号:
unsigned int total = 0; for (int i = 0; i < mpolygon->getNumGeometries(); i++ ) { OGRGeometry* ogrGeom = mpolygon->getGeometryRef(i); OGRwkbGeometryType ogrGeomType = ogrGeom->getGeometryType(); if (wkbPolygon != ogrGeomType && wkbPolygon25D != ogrGeomType) continue; // skip OGRPolygon* polygon = static_cast<OGRPolygon*>(ogrGeom); osg::ref_ptr<osg::Drawable> drw = polygonToDrawable(polygon); osg::ref_ptr<osg::Geometry> geometry = drw->asGeometry(); if (geometry.valid() && geometry->getVertexArray() && geometry->getVertexArray()->getNumElements() && geometry->getNumPrimitiveSets() && geometry->getVertexArray()->getType() == osg::Array::Vec3ArrayType ) { if (!geom->getVertexArray()) { // no yet data we put the first in osg::Vec3Array* arraySrc = static_cast<osg::Vec3Array*>(geom->getVertexArray()); geom->setVertexArray(geometry->getVertexArray()); geom->setPrimitiveSetList(geometry->getPrimitiveSetList()); } else { // already a polygon then append int size = geom->getVertexArray()->getNumElements(); osg::Vec3Array* arrayDst = static_cast<osg::Vec3Array*>(geom->getVertexArray()); osg::ref_ptr<osg::Vec3Array> triangulized = triangulizeGeometry(geometry.get()); if (triangulized.valid()) { arrayDst->insert(arrayDst->end(), triangulized->begin(), triangulized->end()); // shift index geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, size, triangulized->size())); } } } else osg::notify(osg::WARN) << "Warning something wrong with a polygon in a multi polygon" << std::endl; } if (geom->getVertexArray()) osg::notify() << "osgOgrFeature::multiPolygonToGeode " << geom->getVertexArray()->getNumElements() << " vertexes"<< std::endl; return geom; } 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; } osg::Geometry* multiLineStringToDrawable(OGRMultiLineString* mlineString) const { osg::Geometry* geom = new osg::Geometry; for (int i = 0; i < mlineString->getNumGeometries(); i++ ) { OGRGeometry* ogrGeom = mlineString->getGeometryRef(i); OGRwkbGeometryType ogrGeomType = ogrGeom->getGeometryType(); if (wkbLineString != ogrGeomType && wkbLineString25D != ogrGeomType) continue; // skip OGRLineString* lineString = static_cast<OGRLineString*>(ogrGeom); osg::ref_ptr<osg::Geometry> geometry = lineStringToDrawable(lineString); if (geometry.valid() && geometry->getVertexArray() && geometry->getNumPrimitiveSets() && geometry->getVertexArray()->getType() == osg::Array::Vec3ArrayType) { if (!geom->getVertexArray()) { geom->setVertexArray(geometry->getVertexArray()); geom->setPrimitiveSetList(geometry->getPrimitiveSetList()); } else { int size = geom->getVertexArray()->getNumElements(); osg::Vec3Array* arraySrc = static_cast<osg::Vec3Array*>(geometry->getVertexArray()); osg::Vec3Array* arrayDst = static_cast<osg::Vec3Array*>(geom->getVertexArray()); arrayDst->insert(arrayDst->end(), arraySrc->begin(), arraySrc->end()); // shift index geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP, size, arraySrc->size())); } } } return geom; } osg::Geode* readFeature(OGRFeature* ogrFeature, bool useRandomColorByFeature) const { if (!ogrFeature || !ogrFeature->GetGeometryRef()) return 0; osg::Geometry* drawable = 0; bool disableCulling = false; // Read the geometry switch(ogrFeature->GetGeometryRef()->getGeometryType()) { case wkbPoint: case wkbPoint25D: // point to drawable drawable = pointsToDrawable(static_cast<OGRPoint *>(ogrFeature->GetGeometryRef())); disableCulling = true; break; case wkbLinearRing: drawable = linearRingToDrawable(static_cast<OGRLinearRing *>(ogrFeature->GetGeometryRef())); break; case wkbLineString: case wkbLineString25D: drawable = lineStringToDrawable(static_cast<OGRLineString*>(ogrFeature->GetGeometryRef())); break; case wkbPolygon: case wkbPolygon25D: drawable = polygonToDrawable(static_cast<OGRPolygon*>(ogrFeature->GetGeometryRef())); break; case wkbMultiPoint: case wkbMultiPoint25D: break; case wkbMultiLineString: case wkbMultiLineString25D: drawable = multiLineStringToDrawable(static_cast<OGRMultiLineString*>(ogrFeature->GetGeometryRef())); break; case wkbMultiPolygon: case wkbMultiPolygon25D: drawable = multiPolygonToDrawable(static_cast<OGRMultiPolygon*>(ogrFeature->GetGeometryRef())); break; case wkbGeometryCollection: case wkbGeometryCollection25D: osg::notify(osg::WARN) << "This geometry is not yet implemented " << OGRGeometryTypeToName(ogrFeature->GetGeometryRef()->getGeometryType()) << std::endl; break; case wkbNone: osg::notify(osg::WARN) << "No WKB Geometry " << OGRGeometryTypeToName(ogrFeature->GetGeometryRef()->getGeometryType()) << std::endl; break; case wkbUnknown: default: osg::notify(osg::WARN) << "Unknown WKB Geometry " << OGRGeometryTypeToName(ogrFeature->GetGeometryRef()->getGeometryType()) << std::endl; break; } if (!drawable) return 0; osg::Geode* geode = new osg::Geode(); if (disableCulling) geode->setCullingActive(false); // because culling on one points geode is always true, so i disable it geode->addDrawable(drawable); if (useRandomColorByFeature) geode->getOrCreateStateSet()->setAttributeAndModes(createDefaultMaterial(),true); for(int i = 0; i < ogrFeature->GetFieldCount(); i++) { geode->addDescription(std::string(ogrFeature->GetFieldDefnRef(i)->GetNameRef()) + " : " + std::string(ogrFeature->GetFieldAsString(i))); } return geode; } mutable OpenThreads::ReentrantMutex _serializerMutex; };// now register with Registry to instantiate the above// reader/writer.osgDB::RegisterReaderWriterProxy<ReaderWriterOGR> g_readerWriter_OGR_Proxy;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -