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. 如何修改矢量中某個或者某些特定要素的樣式?最好是不從新加載數據。

這兩個問題估計只能留待之後解決了。

相關文章
相關標籤/搜索