使用地图

A map 是OSGerath中的中心数据模型。它是图像、立面和要素图层的容器。

从地球文件加载地图

渲染地图的最简单方法是从 地球文件 . 由于osgearth使用OpenScenegraph插件,因此您可以使用一行代码来完成此操作:

osg::Node* globe = osgDB::readNodeFile("myglobe.earth");

你现在有一个 osg::Node 可以添加到场景图形和显示中。说真的,真的很简单!

加载映射的这种方法通常是应用程序需要做的全部工作。但是,如果您想使用API创建映射,请继续阅读。

程序化地图创建

OSGearth提供了一个用于在运行时创建映射的API。

使用API创建映射的基本步骤是:

  1. 创建地图对象

  2. 在地图中添加您认为合适的图像和立面图层

  3. 创建一个 MapNode 它将呈现地图对象

  4. 加上你的 MapNode 到你的场景图。

您可以随时向地图添加图层:

using namespace osgEarth;
using namespace osgEarth::Drivers;

#include <osgEarth/Map>
#include <osgEarth/MapNode>
#include <osgEarth/TMS>
#include <osgEarth/GDAL>

using namespace osgEarth;
...

// Create a Map and set it to Geocentric to display a globe
Map* map = new Map();

// Add an imagery layer (blue marble from a TMS source)
{
    TMSImageLayer* layer = new TMSImageLayer();
    layer->setName("ReadyMap");
    layer->setURL("http://readymap.org/readymap/tiles/1.0.0/7/");
    map->addLayer(layer);
}

// Add an elevation layer (SRTM from a local GeoTiff file)
{
    GDALElevationLayer* layer = new GDALElevationLayer();
    layer->setName("SRTM");
    layer->setURL("c:/data/srtm.tif");
    map->addLayer( layer );
}

// Create a MapNode to render this map:
MapNode* mapNode = new MapNode( map );
...

viewer->setSceneData( mapNode );

在运行时使用mapnode

A MapNode 是呈现 Map . 无论是从地球文件加载地图还是使用API创建地图,都可以访问地图及其 MapNode 在运行时进行更改。如果没有显式创建 MapNode 使用API,首先需要获得对 MapNode 一起工作。使用静态 get 功能:

// Load the map
osg::Node* loadedModel = osgDB::readNodeFile("mymap.earth");

// Find the MapNode
osgEarth::MapNode* mapNode = osgEarth::MapNode::get( loadedModel );

一旦你提到 MapNode ,你可以到地图:

// Add an OpenStreetMap image source
XYZImageLayer* osmLayer = new XYZImageLayer();
osmLayer->setName("OSM");
osmLayer->setURL("http://[abc].tile.openstreetmap.org/{z}/{x}/{y}.png");
osmLayer->setProfile(Profile::create("spherical-mercator"));
osmLayer->setAttribution("Copyright OpenStreetMap Contributors");

mapNode->getMap()->addImageLayer( osmLayer );

您还可以删除或重新排序层:

// Remove a layer from the map.  All other layers are repositioned accordingly
mapNode->getMap()->removeImageLayer( layer );

// Move a layer to position 1 in the image stack
mapNode->getMap()->moveImageLayer( layer, 1 );