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
(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()<
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
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.tifurl>
- image>
-
-
- <heightfield name="GlobeHeightfiled" driver="gdal">
- <url>./heightfield/30m.tifurl>
- heightfield>
-
-
- <model name="world_boundaries" driver="feature_geom">
- <features name="world" driver="ogr">
- <url>./shpFile/world.shpurl>
- <build_spatial_index>truebuild_spatial_index>
- features>
-
- <geometry_type>linegeometry_type>
-
- <relative_line_size>truerelative_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.shpurl>
- <build_spatial_index>truebuild_spatial_index>
- features>
-
- <geometry_type>linegeometry_type>
-
- <relative_line_size>truerelative_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>./FileCachepath>
- cache>
- options>
- map>