osgearth3.2平面模式,加载多块本地地形

问题描述:osgearth3.2平面模式,同时加载多个具有相同srs的地形和影像文件

解决思路:

1.首先给map指定一个srs信息,指定一个bound信息,即要显示的范围,加载多块地形时,bound的范围为所有影像的minx,miny,maxx,maxy;

2.使用map->addlayer()依次添加所有的影像层和高程层。

具体实现见代码,此代码中拥有完整的实现流程。

#include <osg/Notify>
#include <osgGA/StateSetManipulator> 
#include <osgViewer/Viewer> 
#include <osgViewer/ViewerEventHandlers>
#include <osgDB/WriteFile> 
#include <osgEarth/MapNode> 
#include <osgEarth/GDAL> 
#include <osgEarth/ExampleResources> 
#include <osgEarth/EarthManipulator> 
#include <osgEarth/Style> 
#include <osgEarth/OGRFeatureSource> 
#include <osgEarth/FeatureModelLayer> 
#include <osgEarth/FeatureImageLayer> 
#include<osgEarth/TerrainConstraintLayer>

#include<osgEarth/FeatureNode>
#include <osgEarth/CircleNode>
#include <osgEarth/GLUtils>
#include<osgEarth/LabelNode>

#define DEBUG
#ifdef DEBUG
#pragma comment(lib,"osgd.lib")
#pragma comment(lib,"osgGAd.lib")
#pragma comment(lib,"osgViewerd.lib")
#pragma comment(lib,"osgEarthd.lib")
#pragma comment(lib,"osgDBd.lib")
#else
#pragma comment(lib,"osg.lib")
#pragma comment(lib,"osgGA.lib")
#pragma comment(lib,"osgViewer.lib")
#pragma comment(lib,"osgEarth.lib")
#pragma comment(lib,"osgDB.lib")
#endif // DEBUG

struct Extents
{
	double minx, miny, maxx, maxy;
	Extents() { minx = 0.0, miny = 0.0, maxx = 0.0, maxy = 0.0; };
};
Extents getExtents(std::string inputfilepath)
{
	GDALDataset* poDataset;
	GDALAllRegister();  //注册所有的驱动
	CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
	poDataset = (GDALDataset*)GDALOpen(inputfilepath.c_str(), GA_ReadOnly);//打开数据集

	if (poDataset == NULL)
	{
		cout << "fail in open files!!!" << endl;
		exit(1);
	}

	//获取图像的尺寸
	int nImgSizeX = poDataset->GetRasterXSize();
	int nImgSizeY = poDataset->GetRasterYSize();

	//获取坐标变换系数
	double trans[6];
	CPLErr aaa = poDataset->GetGeoTransform(trans);
	//计算范围
	Extents extents;
	extents.minx = trans[0];
	extents.maxx = trans[0] + nImgSizeX * trans[1];
	extents.miny = trans[3] + nImgSizeY * trans[5];
	extents.maxy = trans[3];
	return extents;
}
std::string getProjectInformation(std::string inputfilepath)
int main()
{
	osgEarth::initialize();
	osgViewer::Viewer viewer;
	osgEarth::GLUtils::setGlobalDefaults(viewer.getCamera()->getOrCreateStateSet());
	osg::ref_ptr<Map> map = new Map();
	MapNode* mapNode;
	//map->addLayer(basemap);
	{
		//加载多块地形 
		string strimage, imagePathStr;
		//声明地形高程、影像名称
		string image1, image2, height1, height2;
		image1 = "E:\\LocalRepos_2022\\terrain\\多地形1_影像.tif";
		image2 = "E:\\LocalRepos_2022\\terrain\\多地形2_影像.tif";
		height1 = "E:\\LocalRepos_2022\\terrain\\多地形1_高程.tif";
		height2 = "E:\\LocalRepos_2022\\terrain\\多地形2_高程.tif";
		string srs;
		osgEarth::Map::Options options;
		options.cachePolicy() = osgEarth::CachePolicy::USAGE_READ_WRITE;
		srs = getProjectInformation(image1);

		osgEarth::ProfileOptions options1;
		options1.srsString() = srs;
		Extents bound;
		Extents bound2;
		bound = getExtents(image1);
		bound2 = getExtents(image2);
		osgEarth::Bounds bs(min(bound.minx,bound2.minx), min(bound.miny, bound2.miny), max(bound.maxx, bound2.maxx), max(bound.maxy, bound2.maxy));
		options1.bounds() = bs;
		options.profile() = options1;
		osg::ref_ptr<osgEarth::Map> map = new osgEarth::Map(options, nullptr); 
		mapNode = new MapNode(map.get());

		GDALImageLayer* terraini1 = new GDALImageLayer();
		terraini1->setURL(image1);
		map->addLayer(terraini1);
		
		GDALElevationLayer* terrainh1 = new GDALElevationLayer();
		terrainh1->setURL(height1);
		map->addLayer(terrainh1);

		GDALImageLayer* terraini2 = new GDALImageLayer();
		terraini2->setURL(image2);
		map->addLayer(terraini2);

		GDALElevationLayer* terrainh2 = new GDALElevationLayer();
		terrainh2->setURL(height2);
		map->addLayer(terrainh2);
	}

	osg::ref_ptr < osg::Group > root = new osg::Group;
	root->addChild(mapNode);
	viewer.setSceneData(root);
	viewer.setCameraManipulator(new EarthManipulator());
	MapNodeHelper().configureView(&viewer);
	return viewer.run();
}
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值