[osgearth]通过API创建一个earth模型

通过API的方式大体需要以下几个步骤:

创建map对象——

创建影像数据层——

创建高程数据层——

将影像数据层以及高程数据层加入到map对象——

根据前面创建的map对象创建mapNode节点——

将mapNode节点加入到场景;

我们可以对地形进行修改操作,如添加新的影像、高程数据,移除特定的影像、高程数据,重新制定影像、高程数据的顺序等;

赶紧进入正题,程序加载各种数据,首先介绍一下总体的方式

/*这里XXXOptions 是根据不同的数据类型选择不同驱动,比如加载本地数据可以使用GDALOptions ,加 载TMS数据可以使用TMSOptions(注意TMSOptions可以加载本地也可以加载网上数据),WMSOptions可以加载网上数据(注意这个options主要加载影像和图片数据),ArcGISOptions加载ArcGIS Server发布数据。*/

osgEarth::Drivers::XXXOptions XXXLayer;

 

/*这里就是加载的数据路径,如果加载的本地数据就是本地数据的路径,如果加载是网 上数据就是相应的网址*/

XXXLayer.url()=osgEarth::URI(".................................");

 

/*加载的数据是分层管理,每加载进来一个数据在earth上就是一个数据层,这里给数据层付个名字。*/

std::string LayerName="earth";

 

/*osgearth里layer主要有三种类型 ImageLayer、ElevationLayer和ModleLayer ,前两个大家从字面就可以知道第一个是加载影像和第二个是加载高程数据的,第三个是主要用来加载shp数据,至少我是这样用的,不知道还能否加载其他数据类型。确定加载用的驱动、数据源位置(路径)、数据层名和初始化了数据层,接下来就是把数据层加到地球中如下所示。*/

 osg::ref_ptr<osgEarth::XXXLayer> layer =new   osgEarth::XXXLayer(osgEarth::XXXLayerOptions(LayerName,XXXLayer));

 m_pMap->addImageLayer(layer.get());

(1)加载本地数据

a  本地影像数据,数据类型为tif

 osgEarth::Drivers::GDALOptions imagelayerOpt;//选择GDALOptions
 imagelayerOpt.url() = osgEarth::URI("E:\vs2010Progam Files\osgVR74\osgVR74\world.tif");//影像数据路径

 std::string imagelayerName = "worldimage"; //影像数据层名
 osg::ref_ptr<osgEarth::ImageLayer> imageLayer = new  osgEarth::ImageLayer(osgEarth::ImageLayerOptions(imagelayerName ,imagelayerOpt));

//初始数据层

 m_pMap->addImageLayer(imageLayer .get());

b 本地高程数据,数据类型为tif

 osgEarth::Drivers::GDALOptions demlayerOpt; //使用还是GDALOptions

 demlayerOpt.url() = osgEarth::URI("E:\vs2010Progam Files\osgVR74\osgVR74\worlddem.tif");//高程数据路径

 std::string demlayerName = "worlddem";//高程数据层名
 osg::ref_ptr<osgEarth::ImageLayer> demLayer = new osgEarth::ImageLayer(osgEarth::ImageLayerOptions(demlayerName,demlayerOpt));//初始数据层

 m_pMap->addImageLayer(demLayer.get());


加载本地经过package 切片的数据还可以用TMSOptions,

osgEarth::Drivers::TMSOptions tmsOpt;////选择TMSOptions 驱动
tmsOpt.url()=osgEarth::URI("//Edvis_-1/Layer_0/tms.xml");//package 切片生成金字塔文件下的 xml

std::stringDemtmslayerName="TmsDem";//图层名

osgEarth::ElevationLayerOptionstmslayerOpt(DemtmslayerName,tmsOpt);

osg::ref_ptr<osgEarth::ElevationLayer> TmsDemLayer = new osgEarth::ElevationLayer(tmslayerOpt);

m_pMap->addElevationLayer(TmsDemLayer.get());//初始化图层并加入到地球中

(2)加载网上数据

a 加载ArcGIS Server 发布的数据 加载方式与上面提到的类似

osgEarth::Drivers::ArcGISOptions MapImageLayer;
MapImageLayer.url()=osgEarth::URI("http://xxx.xxx.xxx.xxx.:xxxx/arcgis/rest/services/world/map003/MapServer");
std::string CdlayerName="worldimage";
osg::ref_ptr<osgEarth::ImageLayer> cdlayer =new osgEarth::ImageLayer(osgEarth::ImageLayerOptions(CdlayerName,MapImageLayer));
m_pMap->addImageLayer(cdlayer.get());

//这里注意,当osgearth访问ArcGIS Server 发布数据的时候有些问题很奇怪,用上面的方式访问ArcGIS Server 国外发布的数据没问题,但是访问自己发布的数据就会有问题,经过试验投影要设成3857才能正常访问。

b 加载网上数据还可以用WMSOptions 加载方式同上。

原文地址:https://www.cnblogs.com/lyggqm/p/6371557.html