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

上传人:b****2 文档编号:2450579 上传时间:2023-05-03 格式:DOCX 页数:18 大小:328.58KB
下载 相关 举报
osgEarth使用笔记4加载矢量数据.docx_第1页
第1页 / 共18页
osgEarth使用笔记4加载矢量数据.docx_第2页
第2页 / 共18页
osgEarth使用笔记4加载矢量数据.docx_第3页
第3页 / 共18页
osgEarth使用笔记4加载矢量数据.docx_第4页
第4页 / 共18页
osgEarth使用笔记4加载矢量数据.docx_第5页
第5页 / 共18页
osgEarth使用笔记4加载矢量数据.docx_第6页
第6页 / 共18页
osgEarth使用笔记4加载矢量数据.docx_第7页
第7页 / 共18页
osgEarth使用笔记4加载矢量数据.docx_第8页
第8页 / 共18页
osgEarth使用笔记4加载矢量数据.docx_第9页
第9页 / 共18页
osgEarth使用笔记4加载矢量数据.docx_第10页
第10页 / 共18页
osgEarth使用笔记4加载矢量数据.docx_第11页
第11页 / 共18页
osgEarth使用笔记4加载矢量数据.docx_第12页
第12页 / 共18页
osgEarth使用笔记4加载矢量数据.docx_第13页
第13页 / 共18页
osgEarth使用笔记4加载矢量数据.docx_第14页
第14页 / 共18页
osgEarth使用笔记4加载矢量数据.docx_第15页
第15页 / 共18页
osgEarth使用笔记4加载矢量数据.docx_第16页
第16页 / 共18页
osgEarth使用笔记4加载矢量数据.docx_第17页
第17页 / 共18页
osgEarth使用笔记4加载矢量数据.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

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

《osgEarth使用笔记4加载矢量数据.docx》由会员分享,可在线阅读,更多相关《osgEarth使用笔记4加载矢量数据.docx(18页珍藏版)》请在冰点文库上搜索。

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

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

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

1.概述

2.详论

2.1.基本绘制

2.2.矢量符号化

2.2.1.可见性

2.2.2.高度设置

2.2.3.符号化

2.2.4.显示标注

2.3.其他

3.结果

4.问题

 

1.概述

前面文章加载的底图数据是一种栅格数据,还有一种很重要的地理信息表现形式是矢量数据。

在osgEarth中,这部分包含的内容还是很丰富的,这里就总结一二。

2.详论

2.1.基本绘制

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

1.#include

2.#include

3.#include

4.

5.#include

6.#include

7.

8.#include

9.#include

10.

11.#include

12.#include

13.#include

14.

15.#include

16.#include

17.

18.#include

19.

20.usingnamespacestd;

21.

22.voidAddVector(osg:

:

ref_ptr

:

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

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2