osgEarth使用筆記4——加載矢量數據


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,也就是將這個矢量當成模型來加載。運行這段程序顯示結果如下:
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. 可見性

設置是否啟用深度測試:

//可見性
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文檔:
imglink3

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

這兩個問題估計只能留待以后解決了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM