1. 概述

前面文章加载的底图数据是一种栅格数据,还有一种很重要的地理信息表现形式是矢量数据。在osgEarth中,这部分包含的内容还是很丰富的,这里就总结一二。

2. 详论

2.1. 基本绘制

《osgEarth使用笔记1——显示一个数字地球》这篇文章中代码的基础之上,添加加载显示矢量的代码:

#include <Windows.h>
#include <iostream>
#include <string> #include <osgViewer/Viewer>
#include <osgDB/ReadFile> #include <osgEarth/MapNode>
#include <osgEarth/ImageLayer> #include <osgEarthDrivers/gdal/GDALOptions>
#include <osgEarthDrivers/cache_filesystem/FileSystemCache>
#include <osgEarthDrivers/feature_ogr/OGRFeatureOptions> #include <osgEarthFeatures/FeatureSourceLayer>
#include <osgEarthFeatures/FeatureModelLayer> #include <osgEarthUtil/EarthManipulator> using namespace std; void AddVector(osg::ref_ptr<osgEarth::Map> map)
{
//
std::string filePath = "D:/Work/OSGNewBuild/osgearth-2.10.1/data/world.shp";
osgEarth::Drivers::OGRFeatureOptions featureData;
featureData.url() = filePath; // 如果缺少空间参考,可以手动指定
// ifstream infile("C:/Data/vector/hs/23.prj");
// string line;
// getline(infile, line);
// featureData.profile()->srsString() = line; // Make a feature source layer and add it to the Map:
osgEarth::Features::FeatureSourceLayerOptions ogrLayer;
ogrLayer.name() = filePath + "_source";
ogrLayer.featureSource() = featureData;
osgEarth::Features::FeatureSourceLayer* featureSourceLayer = new osgEarth::Features::FeatureSourceLayer(ogrLayer);
map->addLayer(featureSourceLayer);
osgEarth::Features::FeatureSource *features = featureSourceLayer->getFeatureSource();
if (!features)
{
printf(("无法打开该矢量文件!"));
return;
} //
osgEarth::Features::FeatureModelLayerOptions fmlOpt;
fmlOpt.name() = filePath;
fmlOpt.featureSourceLayer() = filePath + "_source";
fmlOpt.enableLighting() = false; osg::ref_ptr<osgEarth::Features::FeatureModelLayer> fml = new osgEarth::Features::FeatureModelLayer(fmlOpt);
map->addLayer(fml);
} 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); AddVector(map); 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表达矢量的基本思路是,先将其读取到矢量源图层FeatureSourceLayer中,这个图层加载到osgEarth的图层列表中是不显示的,必须得再加载一个专门的符号化图层,将其符号号,才能正常显示。这里使用的是FeatureModelLayer,也就是将这个矢量当成模型来加载。运行这段程序显示结果如下:

这个矢量加载的是osgEarth自带的矢量地图world.shp,是一个面矢量,但是显示的效果却不太正确,也是因为没有设置合适的符号化方式。

2.2. 矢量符号化

矢量符号化在osgEarth中被抽象成了类似于CSS中样式表StyleSheet,可以在其中加载样式Style:

//设置样式
osgEarth::Symbology::Style style; //具体设置
//... //
osgEarth::Features::FeatureModelLayerOptions fmlOpt;
fmlOpt.name() = filePath;
fmlOpt.featureSourceLayer() = filePath + "_source";
fmlOpt.enableLighting() = false;
fmlOpt.styles() = new osgEarth::Symbology::StyleSheet();
fmlOpt.styles()->addStyle(style); osg::ref_ptr<osgEarth::Features::FeatureModelLayer> fml = new osgEarth::Features::FeatureModelLayer(fmlOpt);
map->addLayer(fml);

2.2.1. 可见性

设置是否启用深度测试:

//可见性
osgEarth::Symbology::RenderSymbol* rs = style.getOrCreate<osgEarth::Symbology::RenderSymbol>();
rs->depthTest() = false;

2.2.2. 高度设置

//贴地设置
osgEarth::Symbology::AltitudeSymbol* alt = style.getOrCreate<osgEarth::Symbology::AltitudeSymbol>();
alt->clamping() = alt->CLAMP_TO_TERRAIN;
alt->technique() = alt->TECHNIQUE_DRAPE;

osgEarth有三种设置高度的方式,分别是:贴地,相对高程和绝对高程。我这里是将其设置为贴地。

矢量贴地有多种技术实现方式,对每一种情况来说,并不存在一种最好的方式,需要根据实际的情况去设置,具体的技术说明可以参考osgEarth文档:

2.2.3. 符号化

接下来就是设置具体的样式了。这个矢量是个面矢量,所以给它设置一个面的样式,包含边界线和填充效果:

//设置矢量面样式(包括边界线)
osgEarth::Symbology::LineSymbol* ls = style.getOrCreateSymbol<osgEarth::Symbology::LineSymbol>();
ls->stroke()->color() = osgEarth::Symbology::Color("#FA8072");
ls->stroke()->width() = 1.0;
ls->tessellationSize()->set(100, osgEarth::Units::KILOMETERS); osgEarth::Symbology::PolygonSymbol *polygonSymbol = style.getOrCreateSymbol<osgEarth::Symbology::PolygonSymbol>();
polygonSymbol->fill()->color() = osgEarth::Symbology::Color(152.0f / 255, 251.0f / 255, 152.0f / 255, 0.8f); //238 230 133
polygonSymbol->outline() = true;

2.2.4. 显示标注

可以将矢量中存储的字段作为注记,标注在地图中。这时可以另外新建一个FeatureModelLayer图层,并且还是会用到之间已经读取好的FeatureSourceLayer,只不过显示的样式修改为文字样式TextSymbol:

void AddAnno(std::string filePath, osg::ref_ptr<osgEarth::Map> map)
{
osgEarth::Symbology::Style labelStyle; osgEarth::Symbology::TextSymbol* text = labelStyle.getOrCreateSymbol<osgEarth::Symbology::TextSymbol>();
string name = "[CNTRY_NAME]"; //如果需要显示汉字,则需要转换成UTF-8编码
text->content() = osgEarth::Symbology::StringExpression(name);
text->priority() = osgEarth::NumericExpression( "[pop_cntry]" );
text->size() = 16.0f;
text->alignment() = osgEarth::Symbology::TextSymbol::ALIGN_CENTER_CENTER;
text->fill()->color() = osgEarth::Symbology::Color::White;
text->halo()->color() = osgEarth::Symbology::Color::Red;
text->encoding() = osgEarth::Symbology::TextSymbol::ENCODING_UTF8;
//string fontFile = PathRef::GetAppDir() + "/fonts/SourceHanSansCN-Regular.ttf";
//text->font() = fontFile; //如果显示汉字,需要支持中文字库的字体 // and configure a model layer:
osgEarth::Features::FeatureModelLayerOptions fmlOpt;
fmlOpt.name() = filePath + "_labels";
fmlOpt.featureSourceLayer() = filePath + "_source";
fmlOpt.styles() = new osgEarth::Symbology::StyleSheet();
fmlOpt.styles()->addStyle(labelStyle); osg::ref_ptr<osgEarth::Features::FeatureModelLayer> fml = new osgEarth::Features::FeatureModelLayer(fmlOpt);
map->addLayer(fml);
}

注意osgEarth中显示汉字还是很麻烦的,最好矢量和代码相关的设置都是UTF-8编码的。

2.3. 其他

在最后的结果中如果线要素或者其他特征要素还是无法渲染,那么可能就是需要初始化状态设置:

//解决Lines or Annotations (FeatureNode, etc.) 不被渲染的问题
osgEarth::GLUtils::setGlobalDefaults(viewer.getCamera()->getOrCreateStateSet());

这一点在osgEarth中被提到了:

3. 结果

整理的完整代码如下:

#include <Windows.h>
#include <iostream>
#include <string> #include <osgViewer/Viewer>
#include <osgDB/ReadFile> #include <osgEarth/MapNode>
#include <osgEarth/ImageLayer>
#include <osgEarth/GLUtils> #include <osgEarthDrivers/gdal/GDALOptions>
#include <osgEarthDrivers/cache_filesystem/FileSystemCache>
#include <osgEarthDrivers/feature_ogr/OGRFeatureOptions> #include <osgEarthFeatures/FeatureSourceLayer>
#include <osgEarthFeatures/FeatureModelLayer> #include <osgEarthUtil/EarthManipulator> using namespace std; void AddAnno(std::string filePath, osg::ref_ptr<osgEarth::Map> map)
{
osgEarth::Symbology::Style labelStyle; osgEarth::Symbology::TextSymbol* text = labelStyle.getOrCreateSymbol<osgEarth::Symbology::TextSymbol>();
string name = "[CNTRY_NAME]"; //如果需要显示汉字,则需要转换成UTF-8编码
text->content() = osgEarth::Symbology::StringExpression(name);
text->priority() = osgEarth::NumericExpression( "[pop_cntry]" );
text->size() = 16.0f;
text->alignment() = osgEarth::Symbology::TextSymbol::ALIGN_CENTER_CENTER;
text->fill()->color() = osgEarth::Symbology::Color::White;
text->halo()->color() = osgEarth::Symbology::Color::Red;
text->encoding() = osgEarth::Symbology::TextSymbol::ENCODING_UTF8;
//string fontFile = PathRef::GetAppDir() + "/fonts/SourceHanSansCN-Regular.ttf";
//text->font() = fontFile; //如果显示汉字,需要支持中文字库的字体 // and configure a model layer:
osgEarth::Features::FeatureModelLayerOptions fmlOpt;
fmlOpt.name() = filePath + "_labels";
fmlOpt.featureSourceLayer() = filePath + "_source";
fmlOpt.styles() = new osgEarth::Symbology::StyleSheet();
fmlOpt.styles()->addStyle(labelStyle); osg::ref_ptr<osgEarth::Features::FeatureModelLayer> fml = new osgEarth::Features::FeatureModelLayer(fmlOpt);
map->addLayer(fml);
} void AddVector(osg::ref_ptr<osgEarth::Map> map)
{
//
std::string filePath = "D:/Work/OSGNewBuild/osgearth-2.10.1/data/world.shp";
osgEarth::Drivers::OGRFeatureOptions featureData;
featureData.url() = filePath; // 如果缺少空间参考,可以手动指定
// ifstream infile("C:/Data/vector/hs/23.prj");
// string line;
// getline(infile, line);
// featureData.profile()->srsString() = line; // Make a feature source layer and add it to the Map:
osgEarth::Features::FeatureSourceLayerOptions ogrLayer;
ogrLayer.name() = filePath + "_source";
ogrLayer.featureSource() = featureData;
osgEarth::Features::FeatureSourceLayer* featureSourceLayer = new osgEarth::Features::FeatureSourceLayer(ogrLayer);
map->addLayer(featureSourceLayer);
osgEarth::Features::FeatureSource *features = featureSourceLayer->getFeatureSource();
if (!features)
{
printf(("无法打开该矢量文件!"));
return;
} //设置样式
osgEarth::Symbology::Style style; //可见性
osgEarth::Symbology::RenderSymbol* rs = style.getOrCreate<osgEarth::Symbology::RenderSymbol>();
rs->depthTest() = false; //贴地设置
osgEarth::Symbology::AltitudeSymbol* alt = style.getOrCreate<osgEarth::Symbology::AltitudeSymbol>();
alt->clamping() = alt->CLAMP_TO_TERRAIN;
alt->technique() = alt->TECHNIQUE_DRAPE; //设置矢量面样式(包括边界线)
osgEarth::Symbology::LineSymbol* ls = style.getOrCreateSymbol<osgEarth::Symbology::LineSymbol>();
ls->stroke()->color() = osgEarth::Symbology::Color("#FA8072");
ls->stroke()->width() = 1.0;
ls->tessellationSize()->set(100, osgEarth::Units::KILOMETERS); osgEarth::Symbology::PolygonSymbol *polygonSymbol = style.getOrCreateSymbol<osgEarth::Symbology::PolygonSymbol>();
polygonSymbol->fill()->color() = osgEarth::Symbology::Color(152.0f / 255, 251.0f / 255, 152.0f / 255, 0.8f); //238 230 133
polygonSymbol->outline() = true; //
osgEarth::Features::FeatureModelLayerOptions fmlOpt;
fmlOpt.name() = filePath;
fmlOpt.featureSourceLayer() = filePath + "_source";
fmlOpt.enableLighting() = false;
fmlOpt.styles() = new osgEarth::Symbology::StyleSheet();
fmlOpt.styles()->addStyle(style); osg::ref_ptr<osgEarth::Features::FeatureModelLayer> fml = new osgEarth::Features::FeatureModelLayer(fmlOpt);
map->addLayer(fml); AddAnno(filePath, map);
} 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); AddVector(map); osgViewer::Viewer viewer;
viewer.setSceneData(mapNode); osg::ref_ptr< osgEarth::Util::EarthManipulator> mainManipulator = new osgEarth::Util::EarthManipulator;
viewer.setCameraManipulator(mainManipulator); //解决Lines or Annotations (FeatureNode, etc.) 不被渲染的问题
osgEarth::GLUtils::setGlobalDefaults(viewer.getCamera()->getOrCreateStateSet()); viewer.setUpViewInWindow(100, 100, 800, 600);
return viewer.run();
}

最后的显示结果:

4. 问题

osgEarth中矢量符号化的样式机制非常强大,甚至可以将面按照线绘制,线按照点来绘制。但是这样就会造成一个问题,那就是矢量类型如果判断不正确,渲染的效果就不正确,除非事先知道是点、线或者面。可以从矢量图层中获取到FeatureSource这个类,存在的getGeometryType()接口获取的类型有时候不太正确(有时候返回成osgEarth::Symbology::Geometry::TYPE_UNKNOWN)。

一直困扰的两个问题就来了:

  1. 对于DXF这种可能包含点、线、面三种类型的矢量加载之后,如何设置样式,保证点按照点样式渲染,线按照线样式渲染,面按照面样式渲染呢?
  2. 如何修改矢量中某个或者某些特定要素的样式?最好是不重新加载数据。

这两个问题估计只能留待以后解决了。

osgEarth使用笔记4——加载矢量数据的更多相关文章

  1. python数据分析笔记——数据加载与整理]

    [ python数据分析笔记——数据加载与整理] https://mp.weixin.qq.com/s?__biz=MjM5MDM3Nzg0NA==&mid=2651588899&id ...

  2. 高性能JS笔记1——加载执行

    一.脚本位置 1.Script标签尽可能放到Body底部,以减少脚本文件下载对整个页面UI渲染的影响. 2.Script标签永远不要紧跟Link标签后面. 二.组织脚本 1.合并多个文件在一个Scri ...

  3. [WPF学习笔记]动态加载XAML

    好久没写Blogs了,现在在看[WPF编程宝典],决定开始重新写博客,和大家一起分享技术. 在编程时我们常希望界面是动态的,可以随时变换而不需要重新编译自己的代码. 以下是动态加载XAML的一个事例代 ...

  4. ember.js:使用笔记8 加载测试与集成测试

    emberjs使用的测试工具为qunit.js: 加载:将runner.js添加到Index.html:大致内容: if (window.location.search.indexOf("? ...

  5. javascrip笔记——图片加载

    var t_img; // 定时器 var isLoad = true; // 控制变量 // 判断图片加载状况,加载完成后回调 isImgLoad(function(){ // 加载完成 }); / ...

  6. Spring读书笔记——bean加载

    我们的日常开发几乎离不开Spring,他为我们的开发带来了很大的便捷,那么Spring框架是如何做到方便他人的呢.今天就来说说bean如何被加载加载. 我们在xml文件中写过太多类似这样的bean声明 ...

  7. 学习笔记TF015:加载图像、图像格式、图像操作、颜色

    TensorFlow支持JPG.PNG图像格式,RGB.RGBA颜色空间.图像用与图像尺寸相同(height*width*chnanel)张量表示.通道表示为包含每个通道颜色数量标量秩1张量.图像所有 ...

  8. JVM笔记11-类加载器和OSGI

    一.JVM 类加载器: 一个类在使用前,如何通过类调用静态字段,静态方法,或者new一个实例对象,第一步就是需要类加载,然后是连接和初始化,最后才能使用. 类从被加载到虚拟机内存中开始,到卸载出内存为 ...

  9. 安卓开发笔记——ListView加载性能优化ViewHolder

    在前不久做安卓项目的时候,其中有个功能是爬取某网站上的新闻信息,用ListView展示,虽然做了分页,但还是觉得达不到理想流畅效果. 上网查阅了些资料,发现一些挺不错的总结,这里记录下,便于复习. 当 ...

随机推荐

  1. Java高级特性———Java注解

    什么是注解(Annotation)? 注解是放在Java源码的类.方法.字段.参数上的一种标签,在Java SE 5.0版本中开始引入.注解同class和interface一样,也属于一种类型. 如何 ...

  2. 常被问到的八个 Java 面试题

    想要找到一份好的工作,面试是少不了的,可能你觉得自己技术应该不错了,但是面试却是一团糟.下面我收集了八个常常被问到的Java面试题.   1. 阐述 Java 7 和 Java 8 的区别. 实话说, ...

  3. python os库的使用方法 + 自动化安装第三方库脚本

    一.os库基本介绍 os库提供通用的.基本的操作系统交互功能,包括windows.Mac os.linux os库是python标准库,包含几百个函数 常用路径操作.进程管理.环境参数等几类 路径操作 ...

  4. Redis秒杀实战-微信抢红包-秒杀库存,附案例源码(Jmeter压测)

    导读 前二天我写了一篇,Redis高级项目实战(点我直达),SpringBoot整合Redis附源码(点我直达),今天我们来做一下Redis秒杀系统的设计.当然啦,Redis基础知识还不过关的,先去加 ...

  5. aop思想以及环境搭建(记录自己的一小步)1.1

    ##什么是aop思想? 首先老办法我们一起先看看官网是咋描述的? 它利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其名为 ...

  6. 蒲公英 · JELLY技术周刊 Vol.20: Vue3 极致优化——分析 Vue3 Compiler 告诉你为什么这么快

    蒲公英 · JELLY技术周刊 Vol.20 性能优化是一条无尽的路,我们总是可以找到各种途径去提升体验,不论是响应时间还是按需加载,亦或是根据框架或者组件有针对性的优化都会是不错的方法.如果你在使用 ...

  7. java服务端实现微信小程序内容安全

    请参考微信官方文档:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.i ...

  8. Photon PUN 二 大厅 & 房间

    一, 简介 玩过 LOL , dota2, 王者荣耀 等MOBA类的游戏,就很容易理解大厅和房间了. LOL中一个服务器就相当与一个大厅; 什么电一,电二 ,,, 联通一区等 每一个区就相当于一个大厅 ...

  9. Vue官方文档Vue.extend、Vue.component、createElement、$attrs/$listeners、插槽的深入理解

    一.Vue.extend({}). 看官网文档介绍,Vue.extend({})返回一个Vue的子类,那么这个Vue子类是啥玩意儿呢?我直观感觉它就是创建出一个组件而已啊,那么它又和Vue.compo ...

  10. 网站远程附件存储到 OSS

    参考:链接  链接  链接 简介 网站远程附件功能是指将用户上传的附件直接存储到远端的存储服务器,一般是通过FTP的方式存储到远程的FTP服务器,将论坛附件保存在 OSS 上有以下好处: 附件将拥有更 ...