osgearth中加载二维地图,由于用的数据源格式为*.tif,所有这里需要用到GDAL。
核心代码如下:
std::string wktString = "EPSG:4326"; //wgs84
osgEarth::ProfileOptions profileOpts;
profileOpts.srsString() = wktString;
// 设置缓存目录
osgEarth::Drivers::FileSystemCacheOptions cacheOpts;
std::string cacheDir = "E:/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() = (QApplication::applicationDirPath() + "/data/world.tif").toStdString().c_str(); // 二维地图资源路径
osg::ref_ptr<osgEarth::ImageLayer> layer = new osgEarth::ImageLayer("mapImage", gdal);
map->addImageLayer(layer);
m_root->addChild(mapNode);