osgEarth使用筆記4——載入向量資料

charlee44發表於2020-10-02

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. 如何修改向量中某個或者某些特定要素的樣式?最好是不重新載入資料。

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

相關文章