osgEarth-2.10.2读取*.earth文件后,对其option内容(如Image和elevation等)属性参数进行修改的方法

osgEarth读取*.earth文件后,对其option内容(如Image和elevation等)属性进行修改的方法

一、编写一个earth文件

例如,有如下一个earth文件

    <!--添加世界线(黄色)与国界线-->
    <image name="test" driver="agglite">
    	<features name="world" driver="ogr">
    		<url>/home/test/work/osg/data/worldShp/world.shp</url>
    		<build_spatial_index>true</build_spatial_index>
    	</features>
    	
    	<geometry_type>line</geometry_type>
    	
    	<relative_line_size>true</relative_line_size>
    	
    	<styles>
    		<style type="text/css">
    			world{
    				stroke:#ffff00;
    				stroke-opacity:1.0;
    				stroke-width:1.5;
    			}
    		</style>
    	</styles>
    </image>

二、读取earth文件

从这部分开始就是在代码中实现了,首先把部分主要相关的头文件贴出来

	#include <osgEarth/MapNodeOptions>
	#include <osgEarth/MapNode>
	#include <osgEarth/ImageLayer>
	#include <osgEarth/MapModelChange>

然后开始操作。利用如下命令读取earth

	osg::ref_ptr<osg::Node> pNode = osgDB::readNodeFile("simple.earth", options);
	osg::ref_ptr<osgEarth::MapNode> mapNode = dynamic_cast<osgEarth::MapNode*>(pNode.get());

三、将指定的Layer转换至XXX_Layer

以转换ImageLayer为例,读取earth之后,我们利用getLayerByName("test")来获取指定名称的图层,即根据earth中的name属性的值获得图层。

    osgEarth::MapModelChange mMapModelChange(osgEarth::MapModelChange::ADD_LAYER,osgEarth::Revision(),mapNode.get()->getMap()->getLayerByName("test"));
    osg::ref_ptr<osgEarth::ImageLayer> test_layer=mMapModelChange.getImageLayer();

四、测试

以设置透明度为0.1测试,如果有效,其透明度会有明显变化

    if(test_layer)
    {
        test_layer.get()->setOpacity(0.1);//设置透明度为0.1
    }

五、其他

最后提醒一点,osgEarth有相应的帮助文档,比如osgEarth2.10版本的,但是建议不要只看2.10版本的帮助文档,要拿一些别的版本,比如2.8版本的帮助文档,比对着来看。就比如getImageLayer()这个函数,在2.10的帮助文档中,只写了这个函数,对这个函数的内容并没展开介绍,但是在2.8文档中有展开介绍。我一开始只看了2.10的帮助文档,所以理解有偏差,一直没注意到这个函数。


版权声明:本文为baidu_31788709原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。