前言

osgearth_splat示例,展示如何加载图片贴在地球上,且通过分类,贴文理绘制森林、草地等。

执行命令:osgearth_splatd.exe

效果

整个地球的影像,加载的一张贴图。黑白点点,是不同的贴图。当拉进放大后,才可以看清楚加载的是哪张纹理图。代码设置了15级。

代码分析

1. 创建 土地覆盖字典选项 LandCoverDictionaryOptions options;
 将xml文件中的字典配置,读入options中,主要是name。

2. 用options初始化土地覆盖字典 LandCoverDictionary dictionary;

3. 设置gdal驱动 GDALOptions coverageDriver;,加载地形。

4. 创建LandCover的可序列化配置选项 LandCoverCoverageLayerOptions coverage;
主要包括驱动 coverageDriver 以及 map表(从xml读入,指定某位置放某类贴图)。

5. 为地图创建土地覆盖层:LandCoverLayer *landCover; 并将coverage设置到此土地覆盖层,指定最大层级。

6. 加载实际纹理映射定义,即map表中对应的分类,需要覆盖哪张图。创建 Surface* surface;通过 SplatCatalog* catalog 加载xml文件,完成name与纹理图的实际对应。

7. 创建Zone* splatZone;来指定surface图要覆盖到地图的哪个区域。

8. 创建 SplatLayer* splatLayer;将dictionary、landCover、splatZone设置到splatLayer 图层.

9. 有了上面的基础内容,可以通过直接加载image的方式创建图层。读取图片osg::ref_ptr<osg::Image> tree; 创建标牌 BillboardSymbol* treeSymbol ;将tree传入treeSymbol;

10. 创建土地覆盖种群选项,GroundCoverBiomeOptions forestBiome;
将 新物种 treeSymbol ,放置到 "forest" 种群中。

11. 创建 GroundCoverOptions treeOptions 地面覆盖选项,将生物种群 forestBiome 加入到 treeOptions中。同时设置 GroundCover* trees = new GroundCover(treeOptions);

12. 为要添加的 树 ,新增一个区域 Zone* treeZone; treeZone->setGroundCover(trees);

13. 为这些树,创建地面覆盖图层 GroundCoverLayer* treeLayer;并将内容设置进去。

  • treeLayer->setLandCoverDictionary(dictionary);
  • treeLayer->setLandCoverLayer(landCover);
  • treeLayer->zones().push_back(treeZone);

14. 最后将上述设置,组装到map中。

  • Map* map = new Map();
  • map->addLayer(dictionary);
  • map->addLayer(landCover);
  • map->addLayer(splatLayer);

【注】其中9-13步骤,是概述不通过xml文件,加载纹理图的方式。

完整代码:

#include <osgViewer/Viewer>#include <osgEarth/MapNode>
#include <osgEarth/Registry>
#include <osgEarth/LandCoverLayer>#include <osgEarthSplat/SplatLayer>
#include <osgEarthSplat/GroundCoverLayer>#include <osgEarthDrivers/gdal/GDALOptions>#include <osgEarthUtil/ExampleResources>
#include <osgEarthUtil/EarthManipulator>#include <osgEarthSymbology/BillboardSymbol>#include <osgGA/StateSetManipulator>
#include <osgViewer/ViewerEventHandlers>#define LC "[splat] "using namespace osgEarth;
using namespace osgEarth::Util;
using namespace osgEarth::Splat;
using namespace osgEarth::Drivers;
using namespace osgEarth::Symbology;int
failed(const std::string& s) {OE_WARN << "FAILED: " << s << "\n";return -1;
}int
main(int argc, char** argv)
{osg::ArgumentParser arguments(&argc,argv);bool fromXML = arguments.find("--xml") >= 0;// Create a land cover dictionary.创建 陆地覆盖词典 对象LandCoverDictionary* dictionary;if (fromXML){LandCoverDictionaryOptions options;if (options.loadFromXML("D:/FreeXGIS/osgearth_gch/data/land_cover_dictionary.xml") == false)return failed("Cannot find XML land cover dictionary");dictionary = new LandCoverDictionary(options);}else{// 如果没有找到,则新建一个对象。下面的内容同xml中标签一致dictionary = new LandCoverDictionary();dictionary->setName("Land Cover Dictionary");dictionary->addClass("forest");dictionary->addClass("cropland");dictionary->addClass("grassland");dictionary->addClass("savanna");dictionary->addClass("swamp");dictionary->addClass("desert");dictionary->addClass("rock");dictionary->addClass("water");    dictionary->addClass("tundra");dictionary->addClass("urban");}// Create the data source for our land cover data and// map each value to a class in the dictionary.// This example uses the ESA GLOBCOVER data set from// http://due.esrin.esa.int/page_globcover.phpGDALOptions coverageDriver;// gdal驱动//coverageDriver.url() = "D:/FreeXGIS/osgearth_gch/data/splat/GLOBCOVER_L4_200901_200912_V2.3_Ant_tiled.tif"; // 未找到,换一个coverageDriver.url() = "earth_image/heightfield/30m.tif";// 30m高程数据//coverageDriver.url() = "earth_image/globe/globel.tif";// 加载影像,拉进的过程中,控制台会输出错误信息coverageDriver.profile() = ProfileOptions("global-geodetic");// 创建地球// landCover 可序列化配置选项LandCoverCoverageLayerOptions coverage;coverage.driver() = coverageDriver;// 指定驱动coverage.warp() = 0.035;// 弯曲率?if (fromXML){if (coverage.loadMappingsFromXML("D:/FreeXGIS/osgearth_gch/data/land_cover_ESA_GLOBCOVER.xml") == false)return failed("Cannot find coverage mappings XML\n");}else{// 如果读取不到xml数据则创建。以下name值与dictionary的class值保持一致coverage.map(11, "cropland");// 农田coverage.map(14, "cropland");coverage.map(20, "cropland");coverage.map(30, "cropland");coverage.map(40, "forest");// 森林coverage.map(50, "forest");coverage.map(60, "forest");coverage.map(70, "forest");coverage.map(80, "forest");coverage.map(90, "forest");coverage.map(100, "forest");coverage.map(110, "grassland");// 草场coverage.map(120, "grassland");coverage.map(130, "savanna");// 稀树草原coverage.map(140, "savanna");coverage.map(150, "savanna");coverage.map(160, "swamp");// 沼泽coverage.map(170, "swamp");coverage.map(180, "swamp");coverage.map(190, "urban");// 城市coverage.map(200, "desert");// 沙漠coverage.map(210, "water");// 水coverage.map(220, "tundra");// 冻土带coverage.map(230, "water");}// Create the land cover layer for the map:// 为地图创建土地覆盖层:LandCoverLayer* landCover = new LandCoverLayer();landCover->setName("LandCover");landCover->options().cachePolicy() = CachePolicy::NO_CACHE;landCover->options().coverages().push_back(coverage);landCover->options().maxDataLevel() = 15u;// 最大层级// Next, load the definitions that map land cover classes to actual textures.// 接下来,加载将土地覆盖类映射到实际纹理的定义。Surface* surface = new Surface();// 在catalog中,指明了 每一个class对应的图,比如forest对应森林图SplatCatalog* catalog = SplatCatalog::read("D:/FreeXGIS/osgearth_gch/data/splat/splat_catalog.xml");if (catalog == 0L)return failed("Reading splat catalog");// 将读取到的策略,应用到surface中surface->setCatalog(catalog);// The zone designates the geographic area over which to apply the surface.// At least one zone is required and by default it covers the entire map.// 区域指定要应用曲面的地理区域。// 至少需要一个区域,默认情况下它覆盖整个地图。Zone* splatZone = new Zone();//定义一个区域,将特定表面或土地覆盖层限制在一组地理边界内。splatZone->setSurface(surface);// Create an imagery splatting layer that uses the configured land cover.// 使用配置的土地覆盖图 创建 图像splatting层。即将纹理贴在地形上SplatLayer* splatLayer = new SplatLayer();splatLayer->setName("Splat imagery");splatLayer->options().cachePolicy() = CachePolicy::NO_CACHE;splatLayer->setLandCoverDictionary(dictionary);splatLayer->setLandCoverLayer(landCover);splatLayer->zones().push_back(splatZone);// Now, the trees:// Load a tree image and make a billboard symbol from it:osg::ref_ptr<osg::Image> tree = URI("D:/FreeXGIS/osgearth_gch/data/splat/pine2.png").getImage();if (tree.valid() == false)return failed("Loading tree image");// 标牌BillboardSymbol* treeSymbol = new BillboardSymbol();treeSymbol->setImage(tree.get());treeSymbol->width() = 12.0f;treeSymbol->height() = 16.0f;// Add this symbol to a "frest" biome.// 将此符号添加到“新”生物群落中。GroundCoverBiomeOptions forestBiome;forestBiome.biomeClasses() = "forest";forestBiome.symbols().push_back(treeSymbol);// Assemble the ground cover coniguration:// 组装覆盖地面的配置GroundCoverOptions treeOptions;treeOptions.biomes().push_back(forestBiome);treeOptions.maxDistance() = 15000.0;treeOptions.density() = 4.0;treeOptions.fill() = 0.85;treeOptions.brightness() = 2.0;// 亮度treeOptions.contrast() = 1.0;// 对比度GroundCover* trees = new GroundCover(treeOptions);//控制地面覆盖物外观的接口。// 新增一个区域,绘制treesZone* treeZone = new Zone();treeZone->setGroundCover(trees);// Now, create a ground cover layer for some trees.// 现在,为这些树创建地面覆盖层。GroundCoverLayer* treeLayer = new GroundCoverLayer();treeLayer->setName("Ground cover");treeLayer->options().lod() = 13;treeLayer->setLandCoverDictionary(dictionary);treeLayer->setLandCoverLayer(landCover);treeLayer->zones().push_back(treeZone);// Assemble the Map.组装地图Map* map = new Map();map->addLayer(dictionary);map->addLayer(landCover);map->addLayer(splatLayer);map->addLayer(treeLayer);// Activate the REX terrain engine (required for splatting)激活REX地形引擎osgEarth::Registry::instance()->overrideTerrainEngineDriverName() = "rex";// create a viewer:osgViewer::Viewer viewer(arguments);viewer.getDatabasePager()->setUnrefImageDataAfterApplyPolicy( false, false );viewer.setCameraManipulator( new EarthManipulator(arguments) );viewer.setSceneData(new MapNode(map));viewer.addEventHandler(new osgViewer::StatsHandler());viewer.addEventHandler(new osgViewer::WindowSizeHandler());return viewer.run();
}

osgEarth示例分析——osgearth_splat相关推荐

  1. osgEarth示例分析——osgearth_annotation

    前言 本章为osgearth_annotation示例分析,示例中采用osgEarth提供的类,绘制标签.线.billboard.遮盖图.墙等内容. 运行时,在生成的可执行路径下,打开命令框,输入: ...

  2. osgEarth示例分析——osgearth_skyview

    前言 本示例分析osgearth操作深空场景,或者是银河系场景,可以想象人拿着相机站在地球表面上观看天空/银河系的场景. 重点是相机操作器的使用. 在命令框输入执行程序,在data路径下有加载的图,且 ...

  3. osgEarth示例分析——osgearth_manip

    前言 本示例主要演示osgEarth的事件处理的用法,内容比较多,这部分功能也很重要. 输入命令依然采用china-simple.earth的示例,加上了模型,但是模型并没有看到,可能是因为模型没有放 ...

  4. osgEarth示例分析——osgearth_elevation

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

  5. osgEarth示例分析——osgearth_graticule

    前言 本示例最具有借鉴的功能:绘制网格.网格上的文字显示.拾取地球的坐标.在地球网格示例中,可以设置4种网格.执行命令如下: // --geodetic osgearth_graticuled.exe ...

  6. osgEarth示例分析——osgearth_srstest

    前言 osgearth_srstest示例,主要涉及到两个坐标系转换,wgs84→egm96  wgs84→plate-carre wgs84:World Geodetic System 1984,是 ...

  7. osgEarth示例分析——osgearth_eci

    前言 osgearth_eci示例,展示了J2000的天体坐标系和ECEF地固系的转换,绘制坐标系,以及读取卫星参数绘制卫星的功能.绘制卫星轨迹,添加差值效果和未添加差值的效果. 关于卫星两行根数的数 ...

  8. osgEarth示例分析——osgearth_terrainprofile

    前言 osgearth_terrainprofile示例,涉及到一个新的类 TerrainProfileCalculator(地形轮廓计算器类),用来计算两个点连线之间的地形数据.左下角会根据点击的起 ...

  9. osgEarth示例分析——osgearth_features

    前言 osgearth_features示例,主要演示如何通过代码方式加载shp文件,并设置其样式.在执行时,通过不同 的命令,得到不一样的效果. cmd执行命令: // rasterize 光栅化, ...

最新文章

  1. Linux 执行文件查找命令 which 详解
  2. 顶级程序员的心得 Coders at Work (IV)
  3. 使用Powerdesigner的逆向工程生成PDM(主要是注释可以放进去)
  4. Rocketmq基于docker部署并在Springboot中接入
  5. 第二季-专题15-快车道DMA
  6. 一次使用针式打印机打印异常问题的处理
  7. 手把手会教你搭建微信小程序服务器node!!!
  8. 美信科技冲刺A股上市:拟募资4亿元,公司及董事长张定珍曾遭处罚
  9. 八皇后问题(回溯算法)
  10. C语言之图书管理系统(功能齐全!!!)
  11. python中批量将矩形图片转化为正方形图片,并且缩放
  12. 并发编程——ConcurrentHashMap#transfer() 扩容逐行分析
  13. 关于路由器中家长控制和上网控制的设置
  14. SDP(Session Description Protocol)模型介绍(RFC3264)
  15. MFC添加加瓦系列一MFC编写的增量更新软件
  16. No constructor found in com.kuang.springcloud.pojo.Dept matching [java.lang.Long, java.lang.String
  17. mysql联合索引案例_mysql多个联合索引的案例分析
  18. java 播放swf_java文档在线播放实现
  19. html创建盒子,HTML 盒子
  20. NTLDR用途和常见故障分析方法

热门文章

  1. yolov6训练自己的数据记录+yolov5对比测试
  2. 罗马仕 php30充电,同时支持PD及QC的平价充电宝 罗马仕 WA10 10000mAh
  3. Opencv - python快速入门
  4. 基于单片机的频率计设计
  5. 私有云Mariadb集群搭建
  6. (c++)a+aa+...+aaa..aa表达式输出
  7. 算丰边缘计算开发板人脸检测识别-实现原理与代码介绍
  8. 自媒体数据运营工具--做标签
  9. English Learning - L3 作业打卡 Lesson1 Day5 2023.5.9 周二
  10. 原神台式电脑配置要求2021适合玩原神游戏电脑清单