osgearth二维地球显示

本文介绍了如何在osgEarth中使用GDAL库加载地图数据,包括设置投影、配置缓存、创建MapNode和ImageLayer,以及调整视图窗口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要用的GDAL

#include <osgEarth/MapNode>

#include <osgEarthDrivers/gdal/GDALOptions>
#include <osgEarthDrivers/cache_filesystem/FileSystemCache>
#include <osgEarth/ImageLayer>

#include <osgEarthUtil/EarthManipulator>        

        CPLSetConfigOption("GDAL_DATA", "D:\\OSG64\\osgearth\\gdal\\data");

		string wktString = "EPSG:3857";			//web墨卡托投影
		//string wktString = "EPSG:4326";			//wgs84
		osgEarth::ProfileOptions profileOpts;
		profileOpts.srsString() = wktString;
		
		//地图配置:设置缓存目录
		osgEarth::Drivers::FileSystemCacheOptions cacheOpts;
		string cacheDir = "D:/Work/OSGNewBuild/tmp";
		cacheOpts.rootPath() = cacheDir;
		//
		osgEarth::MapOptions mapOpts;
		mapOpts.cache() = cacheOpts;
		mapOpts.coordSysType() = osgEarth::MapOptions::CSTYPE_PROJECTED;

		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:\\OSG64\\osgearth\\osgearth\\data/world.tif";
		osg::ref_ptr<osgEarth::ImageLayer> layer = new osgEarth::ImageLayer("BlueMarble", gdal);
		map->addImageLayer(layer);
		m_root->addChild(mapNode);
		osg::ref_ptr< osgEarth::Util::EarthManipulator> mainManipulator = new osgEarth::Util::EarthManipulator;
		m_viewer->setCameraManipulator(mainManipulator);

		m_viewer->setUpViewInWindow(100, 100, 800, 600);

 

osgEarth中,加载二维瓦片地图需要进行以下步骤: 1. 创建地球模型(Earth Model):首先,你需要创建一个osgEarth地球模型,用于加载和渲染地图数据。你可以使用 osgEarth::Map 或者 osgEarth::MapNode 类来创建地球模型。 2. 添加图层(Layer):在地球模型中,你需要添加一个图层来加载二维瓦片地图数据。osgEarth支持多种图层类型,例如图片图层、矢量图层、高程图层等。对于二维瓦片地图,你可以使用 osgEarth::ImageLayer 类来创建和添加图层。 3. 配置瓦片源(TileSource):对于二维瓦片地图,你需要配置一个瓦片源来提供地图数据。osgEarth支持多种瓦片源类型,如WMS、TMS、WMTS等。你可以通过 osgEarth::TileSourceOptions 类来设置瓦片源的参数,并使用 osgEarth::ImageLayer 类的方法将瓦片源添加到地球模型中。 ``` osgEarth::TileSourceOptions options; options.url() = "http://example.com/tileserver/{z}/{x}/{y}.png"; osgEarth::ImageLayer* imageLayer = new osgEarth::ImageLayer("Tile Layer", options); map->addLayer(imageLayer); ``` 4. 设置视口(Viewpoint):在加载地图之前,你可以设置一个初始的视口(Viewpoint),用于指定地图的默认视角和位置。你可以使用 osgEarth::Viewpoint 类来定义视口,并使用 osgEarth::Map 或者 osgEarth::MapNode 类的方法设置初始视口。 ``` osgEarth::Viewpoint initialViewpoint("Initial View", 0.0, 0.0, 100000.0); map->setInitialViewpoint(initialViewpoint); ``` 5. 创建地球操纵器(EarthManipulator):osgEarth提供了地球操纵器用于交互式操作地球场景。你可以创建一个地球操纵器,并将其附加到 osgViewer::Viewer 或者 osgEarth::View 类上,以实现用户与地图的交互。 ``` osgEarth::Util::EarthManipulator* earthManipulator = new osgEarth::Util::EarthManipulator(); viewer->setCameraManipulator(earthManipulator); ``` 6. 加载和显示地图:最后,你可以使用 osgViewer::Viewer 或者 osgEarth::View 类的方法来加载和显示地图osgEarth会根据配置的图层、瓦片源等信息,自动加载并渲染地图数据。 ``` viewer->setSceneData(mapNode); viewer->run(); ``` 以上是一个基本的加载二维瓦片地图的流程,具体的实现细节会根据你的需求和使用的API有所不同。你可以参考osgEarth的文档和示例代码,了解更多关于加载二维瓦片地图的详细信息和具体用法。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值