osgEarth示例分析——osgearth_featurequery

前言

osgearth_featurequery示例,分析了如何拾取地球上的特征,并对特征信息进行提取与显示。

执行命令:osgearth_featurequeryd.exe earth_image\china-simple.earth

需要注意的是:earth文件,必须有特征内容。否则无法正常进行功能测试。earth文件见文末。

效果

 代码分析

/**
 * Creates a simple user interface for the demo.
 * 创建一个简单的用户操作界面
 */
Container* createUI()
{
    VBox* vbox = new VBox();
    vbox->setVertAlign( Control::ALIGN_TOP );// 位置在右上角
    vbox->setHorizAlign( Control::ALIGN_RIGHT );
    vbox->addControl( new LabelControl("Feature Query Demo", Color::Yellow) );
    vbox->addControl( new LabelControl("Click on a feature to see its attributes.") );
    return vbox;
}

//-----------------------------------------------------------------------

/**
 * Query Callback that displays the targeted feature's attributes in a
 * user interface grid control.
 * 用户操作接口面板,请求回调,进而展示目标特征属性。
 */
// RTTPicker类:RTT相机的拾取类
// ReadoutCallback重写的方法,均来自 RTTPicker::Callback
class ReadoutCallback : public RTTPicker::Callback
{
public:
    ReadoutCallback(ControlCanvas* container) : _lastFID( ~0 )
    {
        _grid = new Grid();
        _grid->setBackColor( osg::Vec4(0,0,0,0.7f) );
        container->addControl( _grid );
    }
	// 拾取到的特征id
    void onHit(ObjectID id)
    {
        FeatureIndex* index = Registry::objectIndex()->get<FeatureIndex>(id).get();
        Feature* feature = index ? index->getFeature( id ) : 0L;
        if ( feature && feature->getFID() != _lastFID )
        {
            _grid->clearControls();// 清空网格信息
            unsigned r=0;
			
			// 在网格中添加信息
			// 特征id = FID
            _grid->setControl( 0, r, new LabelControl("FID", Color::Red) );
            _grid->setControl( 1, r, new LabelControl(Stringify()<<feature->getFID(), Color::White) );
            ++r;

			// 输出特征信息(earth文件我也不太懂,所以特征信息也看不太明白)
            const AttributeTable& attrs = feature->getAttrs();
            for( AttributeTable::const_iterator i = attrs.begin(); i != attrs.end(); ++i, ++r )
            {
                _grid->setControl( 0, r, new LabelControl(i->first, 14.0f, Color::Yellow) );
                _grid->setControl( 1, r, new LabelControl(i->second.getString(), 14.0f, Color::White) );
            }
            if ( !_grid->visible() )// 如果网格不可见,则设置为可见
                _grid->setVisible( true );
        
            _lastFID = feature->getFID();
        }
    }

	// Called when a query results in nothing
	// 什么也拾取不到时,隐藏网格
    void onMiss()
    {
        _grid->setVisible(false);
        _lastFID = 0u;
    }

	// 接收点击释放事件
    bool accept(const osgGA::GUIEventAdapter& ea, const osgGA::GUIActionAdapter& aa) 
    {
        return ea.getEventType() == ea.RELEASE; // click
    }

    Grid*     _grid;
    FeatureID _lastFID;
};

//------------------------------------------------------------------------

int
main(int argc, char** argv)
{
    osg::ArgumentParser arguments(&argc,argv);

    // a basic OSG viewer 基础osg视景器
    osgViewer::Viewer viewer(arguments);

    // install our default manipulator (do this before using MapNodeHelper)
	// 添加操作器
    viewer.setCameraManipulator( new EarthManipulator() );

    // load an earth file, and support all or our example command-line options
    // and earth file <external> tags
	// 添加的earth文件,文件需要有feature这些信息
    osg::Group* root = MapNodeHelper().load( arguments, &viewer, createUI() );
    if ( root )
    {
        viewer.setSceneData( root );

        MapNode* mapNode = MapNode::findMapNode( root );
        if ( mapNode )
        {
            // Install the query tool.安装请求对象
			// RTTPicker 使用RTT摄影机和顶点属性拾取对象。
            RTTPicker* picker = new RTTPicker();
            viewer.addEventHandler( picker );// 添加请求事件
            picker->addChild( mapNode );

            // Install a readout for feature metadata.
            ControlCanvas* canvas = ControlCanvas::getOrCreate(&viewer);
            picker->setDefaultCallback( new ReadoutCallback(canvas) );
        }

        return viewer.run();
    }
    else
    {
        OE_NOTICE 
            << "\nUsage: " << argv[0] << " file.earth" << std::endl
            << MapNodeHelper().usage() << std::endl;
    }
}

earth文件:

<map name="Globe" type="geocentric" version = "2">
	<!--此为全球影像图-->
	<image name="GlobeImage" driver="gdal">
		<url>./globe/globel.tif</url>
	</image>
	
	<!--全球高程图-->
	<heightfield name="GlobeHeightfiled" driver="gdal">
		<url>./heightfield/30m.tif</url>
	</heightfield>
	
	<!--添加世界线(黄色)与国界线(白色)-->
	<model name="world_boundaries" driver="feature_geom">
		<features name="world" driver="ogr">
			<url>./shpFile/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;
	  			altitude-clamping:terrain-drape;
	  			stroke-width: 3px;
	  		}
	  	</style>
	  </styles>
	</model>
	
	<model name="china_boundaries" driver="feature_geom">
		<features name="world" driver="ogr">
			<url>./shpFile/china.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: #ffffff;
	  			altitude-clamping:terrain-drape;
	  			stroke-width: 2px;
	  		}
	  	</style>
	  </styles>
	</model>
		
	<!--文件缓存-->
	<options>
		<cache type="filesystem">
		  <path>./FileCache</path>
		</cache>
	</options>
</map>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值