osgEarth示例分析——osgearth_geodetic_graticule

前言

osgearth_geodetic_graticule示例,是展示网格控制的案例。可以修改网格的显隐、经纬度label的显隐及颜色、以及是否让经纬度label靠边缘显示、整个网格图层的显隐控制。

cmd 命令框输入:

osgearth_geodetic_graticuled.exe earth_image\china-simple.earth

效果

左上角是5个ui按钮

 代码分析

#include <osg/Notify>
#include <osgViewer/Viewer>
#include <osgEarth/MapNode>
#include <osgEarth/GLUtils>
#include <osgEarthUtil/EarthManipulator>
#include <osgEarthUtil/GeodeticGraticule>
#include <osgEarthUtil/Controls>
#include <osgEarthUtil/ExampleResources>
#include <osgEarthSymbology/Style>

#include <osgGA/StateSetManipulator>
#include <osgViewer/ViewerEventHandlers>

using namespace osgEarth;
using namespace osgEarth::Util;
using namespace osgEarth::Symbology;
namespace ui = osgEarth::Util::Controls;

// osgearth\src\osgEarthUtil\ExampleResources 文件中的宏定义如下:
#define OE_UI_HANDLER(X) \
    struct X : public osgEarth::Util::Controls::ControlEventHandler { \
        App& _app; X(App& app):_app(app) { } \
        void onValueChanged(osgEarth::Util::Controls::Control*) { _app. X (); } \
        void onClick(osgEarth::Util::Controls::Control*) { _app. X (); } }


int
usage( char** argv, const std::string& msg )
{
    OE_NOTICE 
        << msg << std::endl
        << "USAGE: " << argv[0] << " file.earth" << std::endl;
    return -1;
}

const osg::Vec4 colors[4] = { osg::Vec4(1,1,1,1), osg::Vec4(1,0,0,1), osg::Vec4(1,1,0,1), osg::Vec4(0,1,0,1) };

struct App
{
	// 显示经纬线,并自动放置label
    GeodeticGraticule* graticule;

    int gridColorIndex;
    int edgeColorIndex;

    App(GeodeticGraticule* g)
    {
        graticule = g;
        gridColorIndex = 0;// 网格颜色索引,用于更改网格颜色
        edgeColorIndex = 1;// 是否靠边显示经纬度的label
    }

	// 改变经纬度标签的颜色
    void cycleStyles()
    {
        // Could also use the get/setGridLabelStyle API here, but this demonstrates
        // changing the options and calling dirty() or apply().
		// 改变label标签颜色
        Style gridLabelStyle = graticule->options().gridLabelStyle().get();
        gridColorIndex = (gridColorIndex+1)%4;
        gridLabelStyle.getOrCreate<TextSymbol>()->fill()->color() = colors[gridColorIndex];
        graticule->options().gridLabelStyle() = gridLabelStyle;
		// 当label标签靠近边缘显示时,也同时改变边缘label的颜色
        Style edgeLabelStyle = graticule->options().edgeLabelStyle().get(); //graticule->getEdgeLabelStyle();
        edgeColorIndex = (edgeColorIndex+1)%4;
		edgeLabelStyle.getOrCreate<TextSymbol>()->fill()->color() = colors[edgeColorIndex];
		graticule->options().edgeLabelStyle() = edgeLabelStyle;


        graticule->dirty();
    }

	// 切换网格标签可见性
    void toggleGridLabels()
    {
		bool vis = graticule->getGridLabelsVisible();
		graticule->setGridLabelsVisible(!vis);
    }

	// 切换标签到边缘显示或者网格上显示
    void toggleEdgeLabels()
    {
		// 需要先判断是否地球放大到可以在边缘显示label
		bool vis = graticule->getEdgeLabelsVisible();
		graticule->setEdgeLabelsVisible(!vis);

    }

	// 切换网格可见性
    void toggleGrid()
    {
        bool vis = graticule->getGridLinesVisible();
        graticule->setGridLinesVisible(!vis);
    }

	// 切换图层可见性(网格和标签一起控制)
    void toggleLayer()
    {
        bool vis = graticule->getVisible();
        graticule->setVisible(!vis);
    }
};

// 定义一个宏,继承osgEarth::Util::Controls::ControlEventHandler,
// 实现以下结构体的点击方法
// new 每一个结构体时,需要传入app,且app对应的方法与该结构体同名。
OE_UI_HANDLER(cycleStyles);
OE_UI_HANDLER(toggleGridLabels);
OE_UI_HANDLER(toggleEdgeLabels);
OE_UI_HANDLER(toggleGrid);
OE_UI_HANDLER(toggleLayer);

ui::Control* makeUI(App& app)
{
    ui::VBox* b = new ui::VBox();// 垂直布局
    b->addChild(new ui::ButtonControl("Change styles", new cycleStyles(app)));// 改变经纬度标签的颜色
    b->addChild(new ui::ButtonControl("Toggle grid labels", new toggleGridLabels(app)));// 切换经纬度label显隐
    b->addChild(new ui::ButtonControl("Toggle edge labels", new toggleEdgeLabels(app)));// 当地球放大后,可以实现切换label在边缘显示和中间显示
    b->addChild(new ui::ButtonControl("Toggle grid visibility", new toggleGrid(app)));// 切换经纬网格显隐,label保留
    b->addChild(new ui::ButtonControl("Toggle layer visibility", new toggleLayer(app)));// 经纬网格和label一起显隐,即整个图层的显隐
    return b;
}

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

	// 初始化默认状态
    GLUtils::setGlobalDefaults(viewer.getCamera()->getOrCreateStateSet());
	// 设置图形操作以调用查看器图形窗口的实现。
    viewer.setRealizeOperation(new GL3RealizeOperation());
	// 所有示例均设置为-1.0,猜测应该是禁用 最小特征剔除像素尺寸 功能 
    viewer.getCamera()->setSmallFeatureCullingPixelSize(-1.0f);

    // load the .earth file from the command line.
    MapNode* mapNode = MapNode::load(arguments);
    if ( !mapNode )
        return usage( argv, "Failed to load a map from the .earth file" );

    viewer.setSceneData(mapNode);
    viewer.setCameraManipulator( new EarthManipulator() );

	// 经纬网格类
    GeodeticGraticule* graticule = new GeodeticGraticule();
	// 将此类加入到map图层中
    mapNode->getMap()->addLayer(graticule);

	// ui控制
    App app(graticule);
	// 将控件与OSG视图关联。
    ui::ControlCanvas* canvas = ui::ControlCanvas::getOrCreate(&viewer);
	// 将ui控件组加入到canvas中
    canvas->addControl(makeUI(app));

	// 设置事件处理器和操作器
	viewer.addEventHandler(new osgViewer::StatsHandler());
	viewer.addEventHandler(new osgViewer::WindowSizeHandler());

    return viewer.run();
}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 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. 编辑问题总结....................................................................................错误!未定义书签。
要对图层设置 `osgEarth::GeoExtent`,你需要使用 `osgEarth::Config` 对象来构建地理范围的配置数据,并将其传递给 `osgEarth::LayerOptions` 对象。然后,将 `LayerOptions` 传递给 `osgEarth::Map` 对象中的 `addLayer` 方法,以便将其添加到地图中。下面是一个示例代码: ```cpp #include <osgEarth/GeoExtent> #include <osgEarth/Layer> #include <osgEarth/Map> #include <osgEarth/Config> // 创建地理范围对象 osgEarth::GeoExtent extent(osgEarth::SpatialReference::create("epsg:4326"), -180.0, -90.0, 180.0, 90.0); // 构建地理范围的配置数据 osgEarth::Config conf; conf.add("type", "xyz"); // 图层类型 conf.add("url", "http://tile.openstreetmap.org/{z}/{x}/{y}.png"); // 图层数据源 conf.add("profile", "global-geodetic"); // 投影方式 conf.add("min_level", 0); // 最小级别 conf.add("max_level", 18); // 最大级别 conf.add("extent", extent.getConfig()); // 设置地理范围 // 创建图层选项对象 osgEarth::LayerOptions layerOptions("OpenStreetMap", conf); // 创建地图对象并添加图层 osgEarth::Map map; map.addLayer(new osgEarth::Layer(layerOptions)); ``` 在上面的代码中,我们首先创建了一个地理范围对象 `extent`,然后使用 `osgEarth::Config` 构建了一个包含图层配置信息的 `conf` 对象。接下来,我们创建了一个 `osgEarth::LayerOptions` 对象 `layerOptions`,并将 `conf` 作为参数传递给它。最后,我们通过调用 `map.addLayer` 方法向地图添加了一个新的图层。 注意,上面的示例代码只是一个简单的示例,实际应用中可能需要根据具体的需求进行更改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值