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

1. 概述

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

2. 详论

2.1. 基本绘制

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

#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,也就是将这个矢量当成模型来加载。运行这段程序显示结果以下:
imglink1缓存

这个矢量加载的是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. 可见性

设置是否启用深度测试:ui

//可见性
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有三种设置高度的方式,分别是:贴地,相对高程和绝对高程。我这里是将其设置为贴地。
imglink2编码

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

2.2.3. 符号化

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

//设置矢量面样式(包括边界线)
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:3d

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中被提到了:
imglink4

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();
}

最后的显示结果:
imglink5

4. 问题

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

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

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

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