• osgEarth示例分析——osgearth_featurequery


    前言

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

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

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

    效果

     代码分析

    1. /**
    2. * Creates a simple user interface for the demo.
    3. * 创建一个简单的用户操作界面
    4. */
    5. Container* createUI()
    6. {
    7. VBox* vbox = new VBox();
    8. vbox->setVertAlign( Control::ALIGN_TOP );// 位置在右上角
    9. vbox->setHorizAlign( Control::ALIGN_RIGHT );
    10. vbox->addControl( new LabelControl("Feature Query Demo", Color::Yellow) );
    11. vbox->addControl( new LabelControl("Click on a feature to see its attributes.") );
    12. return vbox;
    13. }
    14. //-----------------------------------------------------------------------
    15. /**
    16. * Query Callback that displays the targeted feature's attributes in a
    17. * user interface grid control.
    18. * 用户操作接口面板,请求回调,进而展示目标特征属性。
    19. */
    20. // RTTPicker类:RTT相机的拾取类
    21. // ReadoutCallback重写的方法,均来自 RTTPicker::Callback
    22. class ReadoutCallback : public RTTPicker::Callback
    23. {
    24. public:
    25. ReadoutCallback(ControlCanvas* container) : _lastFID( ~0 )
    26. {
    27. _grid = new Grid();
    28. _grid->setBackColor( osg::Vec4(0,0,0,0.7f) );
    29. container->addControl( _grid );
    30. }
    31. // 拾取到的特征id
    32. void onHit(ObjectID id)
    33. {
    34. FeatureIndex* index = Registry::objectIndex()->get(id).get();
    35. Feature* feature = index ? index->getFeature( id ) : 0L;
    36. if ( feature && feature->getFID() != _lastFID )
    37. {
    38. _grid->clearControls();// 清空网格信息
    39. unsigned r=0;
    40. // 在网格中添加信息
    41. // 特征id = FID
    42. _grid->setControl( 0, r, new LabelControl("FID", Color::Red) );
    43. _grid->setControl( 1, r, new LabelControl(Stringify()<getFID(), Color::White) );
    44. ++r;
    45. // 输出特征信息(earth文件我也不太懂,所以特征信息也看不太明白)
    46. const AttributeTable& attrs = feature->getAttrs();
    47. for( AttributeTable::const_iterator i = attrs.begin(); i != attrs.end(); ++i, ++r )
    48. {
    49. _grid->setControl( 0, r, new LabelControl(i->first, 14.0f, Color::Yellow) );
    50. _grid->setControl( 1, r, new LabelControl(i->second.getString(), 14.0f, Color::White) );
    51. }
    52. if ( !_grid->visible() )// 如果网格不可见,则设置为可见
    53. _grid->setVisible( true );
    54. _lastFID = feature->getFID();
    55. }
    56. }
    57. // Called when a query results in nothing
    58. // 什么也拾取不到时,隐藏网格
    59. void onMiss()
    60. {
    61. _grid->setVisible(false);
    62. _lastFID = 0u;
    63. }
    64. // 接收点击释放事件
    65. bool accept(const osgGA::GUIEventAdapter& ea, const osgGA::GUIActionAdapter& aa)
    66. {
    67. return ea.getEventType() == ea.RELEASE; // click
    68. }
    69. Grid* _grid;
    70. FeatureID _lastFID;
    71. };
    72. //------------------------------------------------------------------------
    73. int
    74. main(int argc, char** argv)
    75. {
    76. osg::ArgumentParser arguments(&argc,argv);
    77. // a basic OSG viewer 基础osg视景器
    78. osgViewer::Viewer viewer(arguments);
    79. // install our default manipulator (do this before using MapNodeHelper)
    80. // 添加操作器
    81. viewer.setCameraManipulator( new EarthManipulator() );
    82. // load an earth file, and support all or our example command-line options
    83. // and earth file tags
    84. // 添加的earth文件,文件需要有feature这些信息
    85. osg::Group* root = MapNodeHelper().load( arguments, &viewer, createUI() );
    86. if ( root )
    87. {
    88. viewer.setSceneData( root );
    89. MapNode* mapNode = MapNode::findMapNode( root );
    90. if ( mapNode )
    91. {
    92. // Install the query tool.安装请求对象
    93. // RTTPicker 使用RTT摄影机和顶点属性拾取对象。
    94. RTTPicker* picker = new RTTPicker();
    95. viewer.addEventHandler( picker );// 添加请求事件
    96. picker->addChild( mapNode );
    97. // Install a readout for feature metadata.
    98. ControlCanvas* canvas = ControlCanvas::getOrCreate(&viewer);
    99. picker->setDefaultCallback( new ReadoutCallback(canvas) );
    100. }
    101. return viewer.run();
    102. }
    103. else
    104. {
    105. OE_NOTICE
    106. << "\nUsage: " << argv[0] << " file.earth" << std::endl
    107. << MapNodeHelper().usage() << std::endl;
    108. }
    109. }

    earth文件:

    1. <map name="Globe" type="geocentric" version = "2">
    2. <image name="GlobeImage" driver="gdal">
    3. <url>./globe/globel.tifurl>
    4. image>
    5. <heightfield name="GlobeHeightfiled" driver="gdal">
    6. <url>./heightfield/30m.tifurl>
    7. heightfield>
    8. <model name="world_boundaries" driver="feature_geom">
    9. <features name="world" driver="ogr">
    10. <url>./shpFile/world.shpurl>
    11. <build_spatial_index>truebuild_spatial_index>
    12. features>
    13. <geometry_type>linegeometry_type>
    14. <relative_line_size>truerelative_line_size>
    15. <styles>
    16. <style type="text/css">
    17. world{
    18. stroke: #ffff00;
    19. altitude-clamping:terrain-drape;
    20. stroke-width: 3px;
    21. }
    22. style>
    23. styles>
    24. model>
    25. <model name="china_boundaries" driver="feature_geom">
    26. <features name="world" driver="ogr">
    27. <url>./shpFile/china.shpurl>
    28. <build_spatial_index>truebuild_spatial_index>
    29. features>
    30. <geometry_type>linegeometry_type>
    31. <relative_line_size>truerelative_line_size>
    32. <styles>
    33. <style type="text/css">
    34. world{
    35. stroke: #ffffff;
    36. altitude-clamping:terrain-drape;
    37. stroke-width: 2px;
    38. }
    39. style>
    40. styles>
    41. model>
    42. <options>
    43. <cache type="filesystem">
    44. <path>./FileCachepath>
    45. cache>
    46. options>
    47. map>

     

  • 相关阅读:
    装饰器模式和 AOP 面向切片编程(设计模式与开发实践 P15)
    leetCode 125. 验证回文串 + 双指针
    mysql主从复制-使用心得
    【WxPusher消息推送平台】js版对接发送消息教程实战案例,JavaScript版调用axios发送消息实例。保姆级教程
    getBoundingClientRect使用场景(table固定表头)
    Vue3的学习
    手机怎么把几个PDF文件合并到一起?教你一分钟搞定
    9.14-广读最新研究方向论文核心思路汇总
    WPF Panel笔记
    Nacos集群搭建(图文教程)
  • 原文地址:https://blog.csdn.net/qq_34732729/article/details/128202908