:
Map>map)
23.{
24.//
25.std:
:
stringfilePath="D:
/Work/OSGNewBuild/osgearth-2.10.1/data/world.shp";
26.osgEarth:
:
Drivers:
:
OGRFeatureOptionsfeatureData;
27.featureData.url()=filePath;
28.
29.//如果缺少空间参考,可以手动指定
30.//ifstreaminfile("C:
/Data/vector/hs/23.prj");
31.//stringline;
32.//getline(infile,line);
33.//featureData.profile()->srsString()=line;
34.
35.//MakeafeaturesourcelayerandaddittotheMap:
36.osgEarth:
:
Features:
:
FeatureSourceLayerOptionsogrLayer;
37.ogrLayer.name()=filePath+"_source";
38.ogrLayer.featureSource()=featureData;
39.osgEarth:
:
Features:
:
FeatureSourceLayer*featureSourceLayer=newosgEarth:
:
Features:
:
FeatureSourceLayer(ogrLayer);
40.map->addLayer(featureSourceLayer);
41.osgEarth:
:
Features:
:
FeatureSource*features=featureSourceLayer->getFeatureSource();
42.if(!
features)
43.{
44.printf(("无法打开该矢量文件!
"));
45.return;
46.}
47.
48.//
49.osgEarth:
:
Features:
:
FeatureModelLayerOptionsfmlOpt;
50.fmlOpt.name()=filePath;
51.fmlOpt.featureSourceLayer()=filePath+"_source";
52.fmlOpt.enableLighting()=false;
53.
54.osg:
:
ref_ptr:
Features:
:
FeatureModelLayer>fml=newosgEarth:
:
Features:
:
FeatureModelLayer(fmlOpt);
55.map->addLayer(fml);
56.}
57.
58.intmain()
59.{
60.osgEarth:
:
ProfileOptionsprofileOpts;
61.
62.//地图配置:
设置缓存目录
63.osgEarth:
:
Drivers:
:
FileSystemCacheOptionscacheOpts;
64.stringcacheDir="D:
/Work/OSGNewBuild/tmp";
65.cacheOpts.rootPath()=cacheDir;
66.
67.//
68.osgEarth:
:
MapOptionsmapOpts;
69.mapOpts.cache()=cacheOpts;
70.mapOpts.profile()=profileOpts;
71.
72.//创建地图节点
73.osg:
:
ref_ptr:
Map>map=newosgEarth:
:
Map(mapOpts);
74.osg:
:
ref_ptr:
MapNode>mapNode=newosgEarth:
:
MapNode(map);
75.
76.osgEarth:
:
Drivers:
:
GDALOptionsgdal;
77.gdal.url()="D:
/Work/OSGNewBuild/osgearth-2.10.1/data/world.tif";
78.osg:
:
ref_ptr:
ImageLayer>layer=newosgEarth:
:
ImageLayer("BlueMarble",gdal);
79.map->addLayer(layer);
80.
81.AddVector(map);
82.
83.osgViewer:
:
Viewerviewer;
84.viewer.setSceneData(mapNode);
85.
86.osg:
:
ref_ptr:
Util:
:
EarthManipulator>mainManipulator=newosgEarth:
:
Util:
:
EarthManipulator;
87.viewer.setCameraManipulator(mainManipulator);
88.
89.viewer.setUpViewInWindow(100,100,800,600);
90.
91.returnviewer.run();
92.}
osgEarth表达矢量的基本思路是,先将其读取到矢量源图层FeatureSourceLayer中,这个图层加载到osgEarth的图层列表中是不显示的,必须得再加载一个专门的符号化图层,将其符号号,才能正常显示。
这里使用的是FeatureModelLayer,也就是将这个矢量当成模型来加载。
运行这段程序显示结果如下:
这个矢量加载的是osgEarth自带的矢量地图world.shp,是一个面矢量,但是显示的效果却不太正确,也是因为没有设置合适的符号化方式。
2.2.矢量符号化
矢量符号化在osgEarth中被抽象成了类似于CSS中样式表StyleSheet,可以在其中加载样式Style:
1.//设置样式
2.osgEarth:
:
Symbology:
:
Stylestyle;
3.
4.//具体设置
5.//...
6.
7.//
8.osgEarth:
:
Features:
:
FeatureModelLayerOptionsfmlOpt;
9.fmlOpt.name()=filePath;
10.fmlOpt.featureSourceLayer()=filePath+"_source";
11.fmlOpt.enableLighting()=false;
12.fmlOpt.styles()=newosgEarth:
:
Symbology:
:
StyleSheet();
13.fmlOpt.styles()->addStyle(style);
14.
15.osg:
:
ref_ptr:
Features:
:
FeatureModelLayer>fml=newosgEarth:
:
Features:
:
FeatureModelLayer(fmlOpt);
16.map->addLayer(fml);
2.2.1.可见性
设置是否启用深度测试:
1.//可见性
2.osgEarth:
:
Symbology:
:
RenderSymbol*rs=style.getOrCreate:
Symbology:
:
RenderSymbol>();
3.rs->depthTest()=false;
2.2.2.高度设置
1.//贴地设置
2.osgEarth:
:
Symbology:
:
AltitudeSymbol*alt=style.getOrCreate:
Symbology:
:
AltitudeSymbol>();
3.alt->clamping()=alt->CLAMP_TO_TERRAIN;
4.alt->technique()=alt->TECHNIQUE_DRAPE;
osgEarth有三种设置高度的方式,分别是:
贴地,相对高程和绝对高程。
我这里是将其设置为贴地。
矢量贴地有多种技术实现方式,对每一种情况来说,并不存在一种最好的方式,需要根据实际的情况去设置,具体的技术说明可以参考osgEarth文档:
2.2.3.符号化
接下来就是设置具体的样式了。
这个矢量是个面矢量,所以给它设置一个面的样式,包含边界线和填充效果:
1.//设置矢量面样式(包括边界线)
2.osgEarth:
:
Symbology:
:
LineSymbol*ls=style.getOrCreateSymbol:
Symbology:
:
LineSymbol>();
3.ls->stroke()->color()=osgEarth:
:
Symbology:
:
Color("#FA8072");
4.ls->stroke()->width()=1.0;
5.ls->tessellationSize()->set(100,osgEarth:
:
Units:
:
KILOMETERS);
6.
7.osgEarth:
:
Symbology:
:
PolygonSymbol*polygonSymbol=style.getOrCreateSymbol:
Symbology:
:
PolygonSymbol>();
8.polygonSymbol->fill()->color()=osgEarth:
:
Symbology:
:
Color(152.0f/255,251.0f/255,152.0f/255,0.8f);//238230133
9.polygonSymbol->outline()=true;
2.2.4.显示标注
可以将矢量中存储的字段作为注记,标注在地图中。
这时可以另外新建一个FeatureModelLayer图层,并且还是会用到之间已经读取好的FeatureSourceLayer,只不过显示的样式修改为文字样式TextSymbol:
1.voidAddAnno(std:
:
stringfilePath,osg:
:
ref_ptr:
Map>map)
2.{
3.osgEarth:
:
Symbology:
:
StylelabelStyle;
4.
5.osgEarth:
:
Symbology:
:
TextSymbol*text=labelStyle.getOrCreateSymbol:
Symbology:
:
TextSymbol>();
6.stringname="[CNTRY_NAME]";//如果需要显示汉字,则需要转换成UTF-8编码
7.text->content()=osgEarth:
:
Symbology:
:
StringExpression(name);
8.text->priority()=osgEarth:
:
NumericExpression("[pop_cntry]");
9.text->size()=16.0f;
10.text->alignment()=osgEarth:
:
Symbology:
:
TextSymbol:
:
ALIGN_CENTER_CENTER;
11.text->fill()->color()=osgEarth:
:
Symbology:
:
Color:
:
White;
12.text->halo()->color()=osgEarth:
:
Symbology:
:
Color:
:
Red;
13.text->encoding()=osgEarth:
:
Symbology:
:
TextSymbol:
:
ENCODING_UTF8;
14.//stringfontFile=PathRef:
:
GetAppDir()+"/fonts/SourceHanSansCN-Regular.ttf";
15.//text->font()=fontFile;//如果显示汉字,需要支持中文字库的字体
16.
17.//andconfigureamodellayer:
18.osgEarth:
:
Features:
:
FeatureModelLayerOptionsfmlOpt;
19.fmlOpt.name()=filePath+"_labels";
20.fmlOpt.featureSourceLayer()=filePath+"_source";
21.fmlOpt.styles()=newosgEarth:
:
Symbology:
:
StyleSheet();
22.fmlOpt.styles()->addStyle(labelStyle);
23.
24.osg:
:
ref_ptr:
Features:
:
FeatureModelLayer>fml=newosgEarth:
:
Features:
:
FeatureModelLayer(fmlOpt);
25.map->addLayer(fml);
26.}
注意osgEarth中显示汉字还是很麻烦的,最好矢量和代码相关的设置都是UTF-8编码的。
2.3.其他
在最后的结果中如果线要素或者其他特征要素还是无法渲染,那么可能就是需要初始化状态设置:
1.//解决LinesorAnnotations(FeatureNode,etc.)不被渲染的问题
2.osgEarth:
:
GLUtils:
:
setGlobalDefaults(viewer.getCamera()->getOrCreateStateSet());
这一点在osgEarth中被提到了:
3.结果
整理的完整代码如下:
1.#include
2.#include
3.#include
4.
5.#include
6.#include
7.
8.#include
9.#include
10.#include
11.
12.#include
13.#include
14.#include
15.
16.#include
17.#include
18.
19.#include
20.
21.usingnamespacestd;
22.
23.voidAddAnno(std:
:
stringfilePath,osg:
:
ref_ptr:
Map>map)
24.{
25.osgEarth:
:
Symbology:
:
StylelabelStyle;
26.
27.osgEarth:
:
Symbology:
:
TextSymbol*text=labelStyle.getOrCreateSymbol:
Symbology:
:
TextSymbol>();
28.stringname="[CNTRY_NAME]";//如果需要显示汉字,则需要转换成UTF-8编码
29.text->content()=osgEarth:
:
Symbology:
:
StringExpression(name);
30.text->priority()=osgEarth:
:
NumericExpression("[pop_cntry]");
31.text->size()=16.0f;
32.text->alignment()=osgEarth:
:
Symbology:
:
TextSymbol:
:
ALIGN_CENTER_CENTER;
33.text->fill()->color()=osgEarth:
:
Symbology:
:
Color:
:
White;
34.text->halo()->color()=osgEarth:
:
Symbology:
:
Color:
:
Red;
35.text->encoding()=osgEarth:
:
Symbology:
:
TextSymbol:
:
ENCODING_UTF8;
36.//stringfontFile=PathRef:
:
GetAppDir()+"/fonts/SourceHanSansCN-Regular.ttf";
37.//text->font()=fontFile;//如果显示汉字,需要支持中文字库的字体
38.
39.//andconfigureamodellayer:
40.osgEarth:
:
Features:
:
FeatureModelLayerOptionsfmlOpt;
41.fmlOpt.name()=filePath+"_labels";
42.fmlOpt.featureSourceLayer()=filePath+"_source";
43.fmlOpt.styles()=newosgEarth:
:
Symbology:
:
StyleSheet();
44.fmlOpt.styles()->addStyle(labelStyle);
45.
46.osg:
:
ref_ptr:
Features:
:
FeatureModelLayer>fml=newosgEarth:
:
Features:
:
FeatureModelLayer(fmlOpt);
47.map->addLayer(fml);
48.}
49.
50.voidAddVector(osg:
:
ref_ptr:
Map>map)
51.{
52.//
53.std:
:
stringfilePath="D:
/Work/OSGNewBuild/osgearth-2.10.1/data/world.shp";
54.osgEarth:
:
Drivers:
:
OGRFeatureOptionsfeatureData;
55.featureData.url()=filePath;
56.
57.//如果缺少空间参考,可以手动指定
58.//ifstreaminfile("C:
/Data/vector/hs/23.prj");
59.//stringline;
60.//getline(infile,line);
61.//featureData.profile()->srsString()=line;
62.
63.//MakeafeaturesourcelayerandaddittotheMap:
64.osgEarth:
:
Features:
:
FeatureSourceLayerOptionsogrLayer;
65.ogrLayer.name()=filePath+"_source";
66.ogrLayer.featureSource()=featureData;
67.osgEarth:
:
Features:
:
FeatureSourceLayer*featureSourceLayer=newosgEarth:
:
Features:
:
FeatureSourceLayer(ogrLayer);
68.map->addLayer(featureSourceLayer);
69.osgEarth:
:
Features:
:
FeatureSource*features=featureSourceLayer->getFeatureSource();
70.if(!
features)
71.{
72.printf(("无法打开该矢量文件!
"));
73.return;
74.}
75.
76.//设置样式
77.osgEarth:
:
Symbology:
:
Stylestyle;
78.
79.//可见性
80.osgEarth:
:
Symbolo