1. 概述

osgEarth支持.earth格式的文件,里面保存了数字地球相关信息的配置XML,只需要读取这个配置文件,就可以直接得到相应的数字地球相关效果。但实际使用中还是感觉到有些不便,有些效果没办法保存下来,所以很多时候还是使用代码实现比较好。osgEarth最基础的就是显示一个数字地球了。

2. 实现

2.1. 三维显示

具体的实现代码如下:

#include <Windows.h>
#include <iostream>
#include <string> #include <osgViewer/Viewer>
#include <osgDB/ReadFile> #include <osgEarth/MapNode> #include <osgEarthDrivers/gdal/GDALOptions>
#include <osgEarthDrivers/cache_filesystem/FileSystemCache>
#include <osgEarth/ImageLayer> #include <osgEarthUtil/EarthManipulator> using namespace std; int main()
{
osgEarth::ProfileOptions profileOpts; //地图配置:设置缓存目录
osgEarth::Drivers::FileSystemCacheOptions cacheOpts;
string cacheDir = "D:/Work/OSGNewBuild/tmp";
cacheOpts.rootPath() = cacheDir; //
osgEarth::MapOptions mapOpts;
mapOpts.cache() = cacheOpts;
mapOpts.profile() = profileOpts; //创建地图节点
osg::ref_ptr<osgEarth::Map> map = new osgEarth::Map(mapOpts);
osg::ref_ptr<osgEarth::MapNode> mapNode = new osgEarth::MapNode(map); osgEarth::Drivers::GDALOptions gdal;
gdal.url() = "D:/Work/OSGNewBuild/osgearth-2.10.1/data/world.tif";
osg::ref_ptr<osgEarth::ImageLayer> layer = new osgEarth::ImageLayer("BlueMarble", gdal);
map->addLayer(layer); osgViewer::Viewer viewer;
viewer.setSceneData(mapNode); osg::ref_ptr< osgEarth::Util::EarthManipulator> mainManipulator = new osgEarth::Util::EarthManipulator;
viewer.setCameraManipulator(mainManipulator); viewer.setUpViewInWindow(100, 100, 800, 600); return viewer.run();
}

这里有两个点值得注意,其一是使用了缓存机制,可以在浏览的时候变浏览边生成缓存,所以设置了一个缓存目录;其二是加载了一个底图数据,是osgEarth中自带的。运行的效果如下:

2.2. 二维显示

除了显示三维数字地球之外,osgEarth其实还可以显示成平面地图,只需要设置具体的参数就可以了。例如这里显示成web墨卡托投影的二维平面地图:

#include <Windows.h>
#include <iostream>
#include <string> #include <osgViewer/Viewer>
#include <osgDB/ReadFile> #include <osgEarth/MapNode> #include <osgEarthDrivers/gdal/GDALOptions>
#include <osgEarthDrivers/cache_filesystem/FileSystemCache>
#include <osgEarth/ImageLayer> #include <osgEarthUtil/EarthManipulator> #include <gdal_priv.h> using namespace std; int main()
{
CPLSetConfigOption("GDAL_DATA", "D:/Work/OSGNewBuild/OpenSceneGraph-3.6.4/3rdParty/x64/gdal-data"); string wktString = "EPSG:3857"; //web墨卡托投影
//string wktString = "EPSG:4326"; //wgs84
osgEarth::ProfileOptions profileOpts;
profileOpts.srsString() = wktString; //osgEarth::Bounds bs(535139, 3365107, 545139, 3375107);
//osgEarth::Bounds bs(73, 3, 135, 53);
//profileOpts.bounds() = bs; //地图配置:设置缓存目录
osgEarth::Drivers::FileSystemCacheOptions cacheOpts;
string cacheDir = "D:/Work/OSGNewBuild/tmp";
cacheOpts.rootPath() = cacheDir; //
osgEarth::MapOptions mapOpts;
mapOpts.cache() = cacheOpts;
mapOpts.coordSysType() = osgEarth::MapOptions::CSTYPE_PROJECTED; mapOpts.profile() = profileOpts; //创建地图节点
osg::ref_ptr<osgEarth::Map> map = new osgEarth::Map(mapOpts);
osg::ref_ptr<osgEarth::MapNode> mapNode = new osgEarth::MapNode(map); osgEarth::Drivers::GDALOptions gdal;
gdal.url() = "D:/Work/OSGNewBuild/osgearth-2.10.1/data/world.tif";
osg::ref_ptr<osgEarth::ImageLayer> layer = new osgEarth::ImageLayer("BlueMarble", gdal);
map->addLayer(layer); osgViewer::Viewer viewer;
viewer.setSceneData(mapNode); osg::ref_ptr< osgEarth::Util::EarthManipulator> mainManipulator = new osgEarth::Util::EarthManipulator;
viewer.setCameraManipulator(mainManipulator); viewer.setUpViewInWindow(100, 100, 800, 600); return viewer.run();
}

Web墨卡托投影平面坐标系的EPSG代码是3857,所以只需要直接传入相应的代码就行了。对于比较复杂或者自定义的坐标系,其实也可以直接传入wkt字符串,因为osgEarth是通过GDAL来处理空间坐标参考的,GDAL又是通过proj4来处理空间坐标参考的,所以这个时候需要通过GDAL设置一下环境变量GDAL_DATA(具体可以参见《GDAL坐标转换》)。

显示的效果如下所示:

显然,跟Web墨卡托投影的特性一样,椭球被投影成了方形的平面地图。

osgEarth使用笔记1——显示一个数字地球的更多相关文章

  1. osgEarth使用笔记4——加载矢量数据

    目录 1. 概述 2. 详论 2.1. 基本绘制 2.2. 矢量符号化 2.2.1. 可见性 2.2.2. 高度设置 2.2.3. 符号化 2.2.4. 显示标注 2.3. 其他 3. 结果 4. 问 ...

  2. osgEarth学习笔记(转载)

    osgEarth学习笔记1.        通过earth文件创建图层时,可以指定多个影像数据源和多个高程数据源,数据源的顺序决定渲染顺序,在earth文件中处于最前的在渲染时处于最底层渲染:所以如果 ...

  3. C#.NET学习笔记2---C#.第一个C#程序

    C#.NET学习笔记2---C#.第一个C#程序 技术qq交流群:JavaDream:251572072  教程下载,在线交流:创梦IT社区:www.credream.com 6.第一个C#程序:   ...

  4. The Pragmatic Programmer 读书笔记之中的一个 DRY-Don’t Repeat Youself

     The Pragmatic Programmer读书笔记之中的一个 DRY-Don't Repeat Youself 尽管自己买了非常多软件project方面的书,可是由于时间的问题.一直没有静 ...

  5. OsgEarth开发笔记(一):Osg3.6.3+OsgEarth3.1+vs2019x64开发环境搭建(上)

    前言   OSG研究之后,做地理GIS显示了地球:<项目实战:Qt+OSG教育学科工具之地理三维星球>,这一文章是基于OSG做的,而基于OsgEarth是可以进一步对地球进行深度操作,所以 ...

  6. Unity3D学习笔记2——绘制一个带纹理的面

    目录 1. 概述 2. 详论 2.1. 网格(Mesh) 2.1.1. 顶点 2.1.2. 顶点索引 2.2. 材质(Material) 2.2.1. 创建材质 2.2.2. 使用材质 2.3. 光照 ...

  7. win7系统的右键菜单只显示一个白色框不显示菜单项 解决办法

    如上图所示,桌面或其他大部分地方点击右键菜单,都只显示一个白色框,鼠标移上去才有菜单项看,并且效果很丑 解决办法: 计算机-右键-属性-高级-性能-设置-视觉效果-淡入淡出或滑动菜单到视图,将其前面的 ...

  8. ios显示一个下载banner

    <meta name="apple-itunes-app" content="app-id=432274380" /> 这个标签是告诉iphone的 ...

  9. linux 如何显示一个文件的某几行(中间几行)

    linux 如何显示一个文件的某几行(中间几行) [一]从第3000行开始,显示1000行.即显示3000~3999行 cat filename | tail -n +3000 | head -n 1 ...

随机推荐

  1. 每天定时下载gfs资料shell脚本

    在数值天气预报应用中,经常需要下载一些输入资料,美国ncep的gfs资料是常用的一种分析场资料.业务运行,需要每天定时从ncep网站上下载,所以写了一个Shell脚本实现这一功能.脚本内容如下: #! ...

  2. 架构设计 | 基于电商交易流程,图解TCC事务分段提交

    本文源码:GitHub·点这里 || GitEE·点这里 一.场景案例简介 1.场景描述 分布式事务在业务系统中是十分常见的,最经典的场景就是电商架构中的交易业务,如图: 客户端通过请求订单服务,执行 ...

  3. Laravel Reponse 响应客户端

    Laravel Response 响应客户端 本篇文章逻辑较长,只说明和响应生命周期相关的必要代码. 本文主要内容顺序为: 1.执行上文管道中的then方法指定的闭包,路由的分发 2.在路由器中(Ro ...

  4. AndroidStudio中利用git下载github或者git.oschina的代码时报错:repository test has failed解决方法

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 微博:http://weibo.com/mcxiaobing AndroidS ...

  5. 流光shader 和 流光+扭曲shader

    我认为这种shader能通过简单的方式呈现出不错的效果. 1.流光shader: Shader "Unlit/StreamShader" { //流光shader Properti ...

  6. 【平台开发】— 2.前端:vue-element-admin

    前端我虽然没怎么写过,但也并不陌生.之前做web自动化,网页结构没少看,html,css,js也都断断续续了解过. 如果从空白开始写,肯定还是需要花不少时间的. 好在网上有了不少成熟的后台系统的前端脚 ...

  7. P1090 合并果子(哈弗曼树)

    题目描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和.可 ...

  8. vue 在使用数组的时候,数组内部数据发生变化,视图却没有改变

    data(){ return{ todos: [ {name: 'aa', age: 22}, {name: 'bb', age: 23} ] } } methods:{ changeTodos(){ ...

  9. Spring Boot 整合Redis 实现缓存

      本文提纲 一.缓存的应用场景 二.更新缓存的策略 三.运行 springboot-mybatis-redis 工程案例 四.springboot-mybatis-redis 工程代码配置详解   ...

  10. leetcode刷题-57插入区间

    题目 给出一个无重叠的 ,按照区间起始端点排序的区间列表. 在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间). 示例 1: 输入:intervals = ...