用osgEarth加载tif地形及影像类

头文件
结构体中存储的是地形及影像的范围和SRS投影信息

#pragma once
#include 
#include 
#include
struct mapInformation {
	double maxx;
	double maxy;
	double minx;
	double miny;
	std::string SRS;
};

class CAddTerrain {
public:
	CAddTerrain(std::vector<std::string>vechight, std::vector<std::string>vecimage);
	~CAddTerrain();
	void ansMapInf();//解析需要的信息
	void makeMapNode();//制作mapnode
	osgEarth::MapNode* getmapnode() { return mapnode; }
private:
	std::vector<std::string>heightVec;
	std::vector<std::string>imageVec;
	mapInformation MapInfo;
	osgEarth::MapNode* mapnode;
};

实现cpp文件

#include "CAddTerrain.h"
#include 
#include 
#include 

CAddTerrain::CAddTerrain(std::vector<std::string > vechight, std::vector<std::string>vecimage) {
	heightVec = vechight;
	imageVec = vecimage;
	ansMapInf();
	makeMapNode();
}

CAddTerrain::~CAddTerrain() {

}

void CAddTerrain::ansMapInf() {
	GDALDataset* poDataset = nullptr;
	GDALAllRegister(); 
	CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");//支持中文
	CPLSetConfigOption("GDAL_DATA", NULL);
	poDataset = (GDALDataset*)GDALOpen(heightVec[0].c_str(), GA_ReadOnly);
	const char* ch = poDataset->GetProjectionRef();
	std::string str(ch);
	MapInfo.SRS = str;

	int SizeX = poDataset->GetRasterXSize();
	int SizeY = poDataset->GetRasterYSize();
	
	double trans[6];
	CPLErr aaa = poDataset->GetGeoTransform(trans);
	//计算单个tif的范围
	MapInfo.minx = trans[0];
	MapInfo.maxx = trans[0] + (SizeX - 1) * trans[1];
	MapInfo.miny = trans[3] + (SizeY - 1) * trans[5];
	MapInfo.maxy = trans[3];

	for (int i = 1; i < heightVec.size(); i++) {
		poDataset = (GDALDataset*)GDALOpen(heightVec[i].c_str(), GA_ReadOnly);

		SizeX = poDataset->GetRasterXSize();
		SizeY = poDataset->GetRasterYSize();

		double maxx, maxy, minx, miny;

		CPLErr aaa = poDataset->GetGeoTransform(trans);
		//计算单个tif的范围
		minx = trans[0];
		maxx = trans[0] + (SizeX - 1) * trans[1];
		miny = trans[3] + (SizeY - 1) * trans[5];
		maxy = trans[3];
		MapInfo.minx = std::min(MapInfo.minx, minx);//合并范围
		MapInfo.maxx = std::max(MapInfo.maxx, maxx);
		MapInfo.miny = std::min(MapInfo.miny, miny);
		MapInfo.maxy = std::max(MapInfo.maxy, maxy);
	}

	for (int i = 0; i < imageVec.size(); i++) {
		poDataset = (GDALDataset*)GDALOpen(imageVec[i].c_str(), GA_ReadOnly);

		SizeX = poDataset->GetRasterXSize();
		SizeY = poDataset->GetRasterYSize();

		double maxx, maxy, minx, miny;

		CPLErr aaa = poDataset->GetGeoTransform(trans);
		//计算单个tif的范围
		minx = trans[0];
		maxx = trans[0] + (SizeX - 1) * trans[1];
		miny = trans[3] + (SizeY - 1) * trans[5];
		maxy = trans[3];
		MapInfo.minx = std::min(MapInfo.minx, minx);//合并范围
		MapInfo.maxx = std::max(MapInfo.maxx, maxx);
		MapInfo.miny = std::min(MapInfo.miny, miny);
		MapInfo.maxy = std::max(MapInfo.maxy, maxy);
	}
	
}

void CAddTerrain::makeMapNode() {
	osgEarth::Drivers::FileSystemCacheOptions fileOptions;
	fileOptions.rootPath() = "E:\\cachepath";
	osgEarth::Map::Options options;
	options.cache() = fileOptions;
	options.cachePolicy() = osgEarth::CachePolicy::USAGE_READ_WRITE;
	osgEarth::Bounds bs(MapInfo.minx, MapInfo.miny, MapInfo.maxx, MapInfo.maxy);
	osgEarth::ProfileOptions profileoptions;
	profileoptions.srsString() = MapInfo.SRS;
	profileoptions.bounds() = bs;
	options.profile() = profileoptions;

	osg::ref_ptr<osgEarth::Map> map = new osgEarth::Map(options, nullptr);
	for (int i = 0; i < heightVec.size(); i++)//添加高程
	{
		osg::ref_ptr<osgEarth::GDALElevationLayer>gdalE = new osgEarth::GDALElevationLayer();
		gdalE->setURL(heightVec[i]);
		map->addLayer(gdalE);
	}
	for (int i = 0; i < imageVec.size(); i++)//添加影像
	{
		osg::ref_ptr<osgEarth::GDALImageLayer>gdalI = new osgEarth::GDALImageLayer();
		gdalI->setURL(imageVec[i]);
		map->addLayer(gdalI);
	}
	mapnode = new osgEarth::MapNode(map);
}

主函数

int main() {
	CAddTerrain* caddt = new CAddTerrain(HeightVec, ImageVec);//此处的参数为两个tif数组
	osg::ref_ptr<osg::Group>root = new osg::Group;
	root->addChild(caddt->getmapnode());
	osg::ref_ptr<osgViewer::Viewer>viewer = new osgViewer::Viewer;
	viewer->setSceneData(root);
	viewer->setCameraManipulator(new osgEarth::EarthManipulator);
	return viewer->run();
}

你可能感兴趣的:(c++,c语言,算法)