osgEarth示例分析——osgearth_elevation

前言

osgearth_elevation示例,展示了如何通过点击地球获取不同定义下的高程数据。包括:MSL高程、HAE高程、EGM96高程。点击按钮,可以移除高程图层。

  • MSL高程:是mean sea level的简称,一般指平均海平面,是指水位高度等于观测结果平均值的平静的理想海面。观测时间范围不同,有不同概念的平均海平面,如日平均海平面、年平均海平面和多年平均海平面等等。一些验潮站常用18.6年或19年里每小时的观测值求出平均值,作为该站的平均海平面。简单记为:海平面高程。
  • HAE高程:WGS84坐标系下,定义椭球体,表示距椭圆体的高度的距离。简单记为:椭球体高程。
  • EFM96高程:EGM9模型是美国 BAI 推出的一种适用于全球范围,并综合利用现有全球大量重力数据所计算出来的高精度大地水准面模型。简单记为:米国自己定义的高程,好像在中国不太准确。

执行命令:

osgearth_elevationd.exe earth_image\world.earth

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>
	<!--文件缓存-->
	<options>
		<cache type="filesystem">
		  <path>./FileCache</path>
		</cache>
	</options>
</map>

执行效果

双击地形某处,或获取到高程等信息,并展示再ui面板上,同时会添加一个坐标模型。Scene graph interssection 是 与地形交点处的高程。Query resolution 是分辨率。

 代码分析

#include <osgGA/StateSetManipulator>
#include <osgGA/GUIEventHandler>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgUtil/LineSegmentIntersector>
#include <osgEarth/MapNode>
#include <osgEarth/TerrainEngineNode>
#include <osgEarth/StringUtils>
#include <osgEarth/Terrain>
#include <osgEarth/VerticalDatum>
#include <osgEarthUtil/EarthManipulator>
#include <osgEarthUtil/Controls>
#include <osgEarthUtil/LatLongFormatter>
#include <osgEarthUtil/ExampleResources>
#include <osgEarthAnnotation/ModelNode>
#include <iomanip>

using namespace osgEarth;
using namespace osgEarth::Util;
using namespace osgEarth::Util::Controls;
using namespace osgEarth::Annotation;

// 创建一些全局静态对象
static MapNode*       s_mapNode     = 0L;	// 地图根结点
static LabelControl*  s_posLabel    = 0L;	// 显示经纬度
static LabelControl*  s_vdaLabel    = 0L;	// 显示坐标系geodetic(wgs84)
static LabelControl*  s_mslLabel    = 0L;	// 圆球之上的高度
static LabelControl*  s_haeLabel    = 0L;	// 椭球之上的高度
static LabelControl*  s_egm96Label  = 0L;	// EGM96的高度
static LabelControl*  s_mapLabel    = 0L;	// 场景图交点的高度
static LabelControl*  s_resLabel    = 0L;	// 输出分辨率结果
static ModelNode*     s_marker      = 0L;	// 坐标模型节点


// An event handler that will print out the elevation at the clicked point
// 一个事件处理程序,它将打印出单击点处的高程
struct QueryElevationHandler : public osgGA::GUIEventHandler 
{
    QueryElevationHandler()
        : _mouseDown( false ),
          _terrain  ( s_mapNode->getTerrain() )
    {
        _map = s_mapNode->getMap();
        _path.push_back( s_mapNode->getTerrainEngine() );
        _envelope = _map->getElevationPool()->createEnvelope(_map->getSRS(), 20u);// 20级LOD
    }

	// 重写update方法
    void update( float x, float y, osgViewer::View* view )
    {
        bool yes = false;

        // look under the mouse:查看鼠标下方
        osg::Vec3d world;
        osgUtil::LineSegmentIntersector::Intersections hits;// 保存与场景图 相交点的 std::multiset 表。
        if ( view->computeIntersections(x, y, hits) )// 计算与(x,y)相交的点,并存入hits
        {
            world = hits.begin()->getWorldIntersectPoint();

            // convert to map coords: 将交点转换到地形坐标
            GeoPoint mapPoint;
            mapPoint.fromWorld( _terrain->getSRS(), world );

            // do an elevation query:执行高程查询
            double query_resolution  = 0.0;  // max.全局并没有被用到
            double actual_resolution = 0.0;
            float elevation          = 0.0f;

			// 获取单个高程以及采样数据的分辨率。
            std::pair<float, float> result = _envelope->getElevationAndResolution(
                mapPoint.x(), mapPoint.y());

            elevation = result.first;			// msl高程
            actual_resolution = result.second;	// 分辨率

            if ( elevation != NO_DATA_VALUE )
            {
                // convert to geodetic to get the HAE:计算HAE下的高度
                mapPoint.z() = elevation;
                GeoPoint mapPointGeodetic( s_mapNode->getMapSRS()->getGeodeticSRS(), mapPoint );

                static LatLongFormatter s_f;

                s_posLabel->setText( Stringify()
                    << std::fixed << std::setprecision(2) 
                    << s_f.format(mapPointGeodetic.y(), true)
                    << ", " 
                    << s_f.format(mapPointGeodetic.x(), false) );

                if (s_mapNode->getMapSRS()->isGeographic())// 判断是否为地心投影
                {
                    double metersPerDegree = s_mapNode->getMapSRS()->getEllipsoid()->getRadiusEquator() / 360.0;
                    actual_resolution *= metersPerDegree * cos(osg::DegreesToRadians(mapPoint.y()));
                }

                s_mslLabel->setText( Stringify() << elevation << " m" );
                s_haeLabel->setText( Stringify() << mapPointGeodetic.z() << " m" );
                s_resLabel->setText( Stringify() << actual_resolution << " m" );// 分辨率

				// 计算egm96下的高度
                double egm96z = mapPoint.z();

                VerticalDatum::transform(
                    mapPointGeodetic.getSRS()->getVerticalDatum(),
                    VerticalDatum::get("egm96"),
                    mapPointGeodetic.y(),
                    mapPointGeodetic.x(),
                    egm96z);
                
                s_egm96Label->setText(Stringify() << egm96z << " m");

                yes = true;
            }

            // now get a normal ISECT HAE point.ESECT规定的HAE高程
            GeoPoint isectPoint;
            isectPoint.fromWorld( _terrain->getSRS()->getGeodeticSRS(), world );
            s_mapLabel->setText( Stringify() << isectPoint.alt() << " m");

            // and move the marker.添加模型
            s_marker->setPosition(mapPoint);

            // normal test.法线测试
            osg::Quat q;
            q.makeRotate(osg::Vec3(0,0,1), hits.begin()->getLocalIntersectNormal());
            s_marker->setLocalRotation(q);
        }

		// 根据yes 决定是否要更新标签
        if (!yes)
        {
            s_posLabel->setText( "-" );
            s_mslLabel->setText( "-" );
            s_haeLabel->setText( "-" );
            s_resLabel->setText( "-" );
            s_egm96Label->setText("-");
        }
    }

	// 接收左键点击、双击事件
    bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
    {
        if (ea.getEventType() == ea.DOUBLECLICK &&
            ea.getButton() == ea.LEFT_MOUSE_BUTTON)
        {
            osgViewer::View* view = static_cast<osgViewer::View*>(aa.asView());
			// 获取到点击事件,则更新坐标
            update( ea.getX(), ea.getY(), view );
            return true;
        }

        return false;
    }

    const Map*       _map;		// 地图
    const Terrain*   _terrain;	// 地形
    bool             _mouseDown;// 鼠标是否按下
    osg::NodePath    _path;		// 路径
    osg::ref_ptr<ElevationEnvelope> _envelope;
};

// 点击移除高程
struct ClickToRemoveElevation : public ControlEventHandler
{
    void onClick(Control*)
    {
        Map* map = s_mapNode->getMap();
        ElevationLayerVector layers;
        map->getLayers(layers);// 获取高程图层组
        map->beginUpdate();
        for (ElevationLayerVector::iterator i = layers.begin(); i != layers.end(); ++i) {
            map->removeLayer(i->get());// 移除高程数据
        }
        map->endUpdate();
    }
};


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

    osgViewer::Viewer viewer(arguments);

	// mapNode 节点
    s_mapNode = 0L;
    osg::Node* earthFile = MapNodeHelper().load(arguments, &viewer);
    if (earthFile)
        s_mapNode = MapNode::get(earthFile);

    if ( !s_mapNode )
    {
        OE_WARN << "Unable to load earth file." << std::endl;
        return -1;
    }

    osg::Group* root = new osg::Group();
    viewer.setSceneData( root );
    
    // install the programmable manipulator.
    viewer.setCameraManipulator( new osgEarth::Util::EarthManipulator() );

    // The MapNode will render the Map object in the scene graph.
    root->addChild( earthFile );

    // Make the readout: 设置布局
    Grid* grid = new Grid();
    grid->setBackColor(osg::Vec4(0,0,0,0.5));
    int r=0;
	// 第一行,都是标签,仅1个按钮——移除地形事件
    grid->setControl(0,r++,new LabelControl("Double-click to sample elevation", osg::Vec4(1,1,0,1)));
    grid->setControl(0,r++,new LabelControl("Coords (Lat, Long):"));
    grid->setControl(0,r++,new LabelControl("Map vertical datum:"));
    grid->setControl(0,r++,new LabelControl("Height above geoid:"));
    grid->setControl(0,r++,new LabelControl("Height above ellipsoid:"));
    grid->setControl(0,r++,new LabelControl("Scene graph intersection:"));
    grid->setControl(0,r++,new LabelControl("EGM96 elevation:"));
    grid->setControl(0,r++,new LabelControl("Query resolution:"));
    grid->setControl(0, r++, new ButtonControl("Click to remove all elevation data", new ClickToRemoveElevation()));

	// 第二行,标签
    r = 1;
    s_posLabel = grid->setControl(1,r++,new LabelControl(""));	// 显示经纬度
    s_vdaLabel = grid->setControl(1,r++,new LabelControl(""));	// 显示坐标系geodetic(wgs84)
    s_mslLabel = grid->setControl(1,r++,new LabelControl(""));	// 圆球之上的高度
    s_haeLabel = grid->setControl(1,r++,new LabelControl(""));	// 椭球之上的高度
    s_mapLabel = grid->setControl(1,r++,new LabelControl(""));	// 场景图交点的高度
    s_egm96Label = grid->setControl(1,r++,new LabelControl(""));// EGM96的高度
    s_resLabel = grid->setControl(1,r++,new LabelControl(""));	// 请求的结果

    
    Style markerStyle;
    markerStyle.getOrCreate<ModelSymbol>()->url()->setLiteral("D:/FreeXGIS/osgearth_gch/data/axes.osgt.64.scale");
    markerStyle.getOrCreate<ModelSymbol>()->autoScale() = true;
    s_marker = new ModelNode(s_mapNode, markerStyle);// 模型对象
    //s_marker->setMapNode( s_mapNode );
    //s_marker->setIconImage(osgDB::readImageFile("../data/placemark32.png"));
    s_marker->setDynamic(true);
    s_mapNode->addChild( s_marker );

	// 获取当前坐标系
    const SpatialReference* mapSRS = s_mapNode->getMapSRS();
    s_vdaLabel->setText( mapSRS->getVerticalDatum() ? 
        mapSRS->getVerticalDatum()->getName() : 
        Stringify() << "geodetic (" << mapSRS->getEllipsoid()->getName() << ")" );

	// 画布控件
    ControlCanvas* canvas = ControlCanvas::get(&viewer);
    canvas->addControl( grid );

    // An event handler that will respond to mouse clicks:
    viewer.addEventHandler( new QueryElevationHandler() );

    // add some stock OSG handlers:
    viewer.addEventHandler(new osgViewer::StatsHandler());
    viewer.addEventHandler(new osgViewer::WindowSizeHandler());
    viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));

    return viewer.run();
}

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
osgEarth 的 121 个案例详解 osgEarth 的 121 个案例详解 ...........................................................................................................1 1. aeqd.earth.................................................................................................................................4 2. annotation.earth .......................................................................................................................5 3. annotation_dateline.earth........................................................................................................6 4. annotation_dateline_projected.earth ......................................................................................8 5. annotation_flat.earth................................................................................................................8 6. arcgisonline.earth .....................................................................................................................9 7. bing.earth................................................................................................................................10 8. boston.earth............................................................................................................................11 9. boston_buildings.earth ...........................................................................................................12 10. boston_projected.earth ..................................................................................................13 11. boston_tfs.earth..............................................................................................................14 12. boston-gpu.earth ............................................................................................................15 13. bumpmap.earth ..............................................................................................................16 14. clouds.earth ....................................................................................................................17 15. colorramp.earth ..............................................................................................................18 16. contourmap.earth ...........................................................................................................19 17. datum_override.earth.....................................................................................................20 18. day_night_mp.earth........................................................................................................21 19. day_night_rex.earth........................................................................................................21 20. detail_texture.earth ........................................................................................................21 21. errors.earth .....................................................................................................................22 22. fade_elevation.earth.......................................................................................................22 23. feature_clip_plane.earth.................................................................................................23 24. feature_country_boundaries.earth.................................................................................24 25. feature_custom_filters.earth ..........................................................................................25 26. feature_draped_lines.earth ............................................................................................26 27. feature_draped_polygons.earth .....................................................................................27 28. feature_elevation.earth ..................................................................................................28 29. feature_extrude.earth.....................................................................................................29 30. feature_geom.earth ........................................................................................................30 31. feature_gpx.earth............................................................................................................31 32. feature_inline_geometry.earth.......................................................................................32 33. feature_labels.earth........................................................................................................33 34. feature_labels_script.earth.............................................................................................35 35. feature_levels_and_selectors.earth................................................................................35 36. feature_model_scatter.earth ..........................................................................................36 37. feature_models.earth .....................................................................................................37 38. feature_occlusion_culling.earth......................................................................................38osgEarth 编辑器 SXEarth www.sxsim.com 2 39. feature_offset_polygons.earth .......................................................................................38 40. feature_overlay.earth......................................................................................................39 41. feature_poles.earth.........................................................................................................40 42. feature_population_cylinders.earth ...............................................................................40 43. feature_raster.earth ........................................................................................................41 44. feature_rasterize.earth ...................................................................................................41 45. feature_rasterize_2.earth ...............................................................................................42 46. feature_scripted_styling.earth........................................................................................43 47. feature_scripted_styling_2.earth....................................................................................43 48. feature_scripted_styling_3.earth....................................................................................43 49. feature_style_selector.earth ...........................................................................................44 50. feature_tfs.earth .............................................................................................................45 51. feature_tfs_scripting.earth .............................................................................................46 52. feature_wfs.earth............................................................................................................47 53. fractal_elevation.earth....................................................................................................47 54. gdal_multiple_files.earth ................................................................................................47 55. gdal_tiff.earth..................................................................................................................48 56. geomshader.earth ...........................................................................................................49 57. glsl.earth..........................................................................................................................50 58. glsl_filter.earth ................................................................................................................51 59. graticules.earth ...............................................................................................................52 60. hires-inset.earth..............................................................................................................53 61. intersect_filter.earth .......................................................................................................54 62. land_cover_mixed.earth .................................................................................................55 63. layer_opacity.earth .........................................................................................................55 64. ldb.earth..........................................................................................................................56 65. mapbox.earth..................................................................................................................56 66. mask.earth ......................................................................................................................57 67. mb_tiles.earth.................................................................................................................58 68. mercator_to_plate_carre.earth ......................................................................................59 69. mgrs_graticule.earth.......................................................................................................60 70. min_max_level.earth ......................................................................................................60 71. min_max_range.earth.....................................................................................................61 72. min_max_range_rex.earth..............................................................................................62 73. min_max_resolutions.earth............................................................................................62 74. multiple_heightfields.earth.............................................................................................64 75. night.earth.......................................................................................................................65 76. nodata.earth ...................................................................................................................65 77. noise.earth ......................................................................................................................68 78. normalmap.earth ............................................................................................................68 79. ocean.earth .....................................................................................................................69 80. ocean_no_elevation.earth ..............................................................................................69 81. openstreetmap.earth......................................................................................................69 82. openstreetmap_buildings.earth .....................................................................................70osgEarth 编辑器 SXEarth www.sxsim.com 3 83. openstreetmap_flat.earth...............................................................................................70 84. openstreetmap_full.earth...............................................................................................70 85. openweathermap_clouds.earth......................................................................................71 86. openweathermap_precipitation.earth ...........................................................................71 87. openweathermap_pressure.earth ..................................................................................71 88. photosphere1.earth ........................................................................................................71 89. photosphere2.earth ........................................................................................................72 90. readymap.earth...............................................................................................................73 91. readymap_flat.earth .......................................................................................................73 92. readymap_include.earth.................................................................................................74 93. readymap_template.earth..............................................................................................74 94. readymap-elevation-only.earth.......................................................................................74 95. readymap-osm.earth ......................................................................................................75 96. readymap-priority.earth..................................................................................................75 97. readymap-rex.earth ........................................................................................................75 98. roads.earth......................................................................................................................76 99. roads-flattened.earth......................................................................................................76 100. roads-test.earth...............................................................................................................76 101. scene_clamping.earth.....................................................................................................76 102. silverlining.earth..............................................................................................................78 103. simple_model.earth........................................................................................................78 104. skyview1.earth ................................................................................................................79 105. skyview2.earth ................................................................................................................80 106. splat.earth .......................................................................................................................81 107. splat-blended-with-imagery.earth ..................................................................................81 108. splat-with-mask-layer.earth.............................................................................................81 109. splat-with-multiple-zones.earth......................................................................................82 110. splat-with-rasterized-land-cover.earth............................................................................82 111. stamen_toner.earth ........................................................................................................82 112. stamen_watercolor.earth................................................................................................82 113. state_plane.earth............................................................................................................82 114. tess_screen_space.earth.................................................................................................82 115. tess-coastlines.earth .......................................................................................................82 116. tess-terrain.earth ............................................................................................................83 117. triton.earth......................................................................................................................83 118. triton_drop_shader.earth................................................................................................83 119. utm.earth ........................................................................................................................83 120. utm_graticule.earth ........................................................................................................83 121. vertical_datum.earth ......................................................................................................83 122. wms_nexrad.earth ..........................................................................................................84 123. wms-t_nexrad_animated.earth ......................................................................................84 124. 编辑问题总结....................................................................................错误!未定义书签。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值