原文参考:http://docs.osgearth.org/en/latest/developer/maps.html#programmatic-map-creation
本人翻译水平有限。。。
加载earth地图文件
osg::Node* globe = osgDB::readNodeFile("myglobe.earth");
最简单的方式
API编程式的地图创建
用API创建一个地图的基本步骤是:
1.创建一个地图对象(Map object)2.在你认为合适的地方添加图像层和高程层3.创建一个可以绘制“地图对象”的MapNode4.将MapNode添加进场景图。
你可以在任何时候向地图添加层:
#include#include #include #include using namespace osgEarth;using namespace osgEarth::Drivers;...// Create a Map and set it to Geocentric to display a globeMap* map = new Map();// Add an imagery layer (blue marble from a TMS source){ TMSOptions tms; tms.url() = "http://labs.metacarta.com/wms-c/Basic.py/1.0.0/satellite/"; ImageLayer* layer = new ImageLayer( "NASA", tms ); map->addImageLayer( layer );}// Add an elevationlayer (SRTM from a local GeoTiff file){ GDALOptions gdal; gdal.url() = "c:/data/srtm.tif"; ElevationLayer* layer = new ElevationLayer( "SRTM", gdal ); map->addElevationLayer( layer );}// Create a MapNode to render this map:MapNode* mapNode = new MapNode( map );...viewer->setSceneData( mapNode );
(注意:官方的文档是OE2.4的,目前的API是OE2.9喽,时间:2017年2月7日10:27:15)
在运行时使用MapNode
MapNode是在场景图中绘制地图的节点(node)
如果,你不是用API创建的MapNode,你需要先使用静态函数get来获取它:
// Load the maposg::Node* loadedModel = osgDB::readNodeFile("mymap.earth");// Find the MapNodeosgEarth::MapNode* mapNode = MapNode::get( loadedModel );
如果你有了MapNode,你就可以使用地图了:
// Add an OpenStreetMap image sourceTMSOptions driverOpt;driverOpt.url() = "http://tile.openstreetmap.org/";driverOpt.tmsType() = "google";ImageLayerOptions layerOpt( "OSM", driverOpt );layerOpt.profile() = ProfileOptions( "global-mercator" );ImageLayer* osmLayer = new ImageLayer( layerOpt );mapNode->getMap()->addImageLayer( osmLayer );
你也可以删除或重新订制图层:
// Remove a layer from the map. All other layers are repositioned accordinglymapNode->getMap()->removeImageLayer( layer );// Move a layer to position 1 in the image stackmapNode->getMap()->moveImageLayer( layer, 1 );
处理图层
地图包含纹理层和高程层对象,这些层包含一些可以在运行时调整的属性
例如,你可以使用API打开,关闭,或者调整纹理层透明度等
ImageLayer* layer;...layer->setOpacity( 0.5 ); // makes the layer partially transparent
另加:
完整的osgearthviewer工具关于api加载的函数
/** @brief 创建场景 */int Create(){ // create the map.创建一个地图 Map* map = new Map(); // add a TMS imager layer:添加一个TMS图片层 TMSOptions imagery; imagery.url() = "http://readymap.org/readymap/tiles/1.0.0/7/"; map->addImageLayer(new ImageLayer("Imagery", imagery)); // add a TMS elevation layer:添加一个高程图 TMSOptions elevation; elevation.url() = "http://readymap.org/readymap/tiles/1.0.0/9/"; map->addElevationLayer(new ElevationLayer("Elevation", elevation)); // make the map scene graph: MapNode* node = new MapNode(map); // initialize a viewer: osgViewer::Viewer viewer; viewer.setCameraManipulator(new EarthManipulator); viewer.setSceneData(node); // add some stock OSG handlers: viewer.addEventHandler(new osgViewer::StatsHandler()); viewer.addEventHandler(new osgViewer::WindowSizeHandler()); viewer.addEventHandler(new osgViewer::ThreadingHandler()); viewer.addEventHandler(new osgViewer::LODScaleHandler()); viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet())); viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage())); return viewer.run();}