C++ 如何根据地理坐标范围获取瓦片地图并使用CImage库实现多张图片(瓦片地图)的快速合并

作者:虚坏叔叔
博客:https://xuhss.com

早餐店不会开到晚上,想吃的人早就来了!😄

C++ 如何获取瓦片地图并使用CImage库实现多张图片(瓦片地图)的快速合并

一、C++ 如何获取瓦片数据

C++ 如何获取瓦片数据,这里通过向谷歌地图发起http请求,获取多个地图保存到本地,部分关键源码如下所示

int CGenerateJpgAccessImpl::FetchTiles(int zoomLevel, int x, int y, int width, int height, int nMaxThreads, bool yReversed, bool bNewBuild)
{
	int failedCount = 0;

	CString strTilePath = _strFullPath + L"瓦片";
	if (CFileToolkit::FileExist(strTilePath))
		CFileToolkit::DeleteToRecycle(strTilePath);
	CFileToolkit::CreateDirectory(strTilePath);

	vector<StructUrl2Path> vctUrl2Path;
	int nJpgI = 0;
	int nJpgJ = 0;
	if (yReversed)
	{
		// 下载图片
		for (int row = y - height + 1; row <= y; row++)
		{
			nJpgI++;
			nJpgJ = 0;
			for (int col = x; col <= x + width - 1; col++)
			{
				CString strJpgBwUrl;
				CString strWorkSpace = CPathConfig::GetWorksapce();
				strJpgBwUrl.Format(_T("http://tiles.google.com.cn:8510/TileService/Tile?type=Mapbox&layerStyle=Satellite&x=%d&y=%d&z=%d"), col, row, zoomLevel);
				CString strFullPath;
				strFullPath.Format(L"%s\\%s#%s.jpg", strTilePath, CStringToolkit::IntToStr(nJpgI), CStringToolkit::IntToStr(nJpgJ));
				StructUrl2Path sup;
				sup.strPat = strFullPath;
				sup.strUrl = strJpgBwUrl;
				vctUrl2Path.push_back(sup);
				nJpgJ++;
			}
		}
	}
	else
	{
		// 下载图片
		for (int row = y; row >= y - height + 1; row--)
		{
			nJpgI++;
			nJpgJ = 0;
			for (int col = x; col <= x + width - 1; col++)
			{
				CString strJpgBwUrl;
				CString strWorkSpace = CPathConfig::GetWorksapce();
				strJpgBwUrl.Format(_T("http://tiles.google.com.cn:8510/TileService/Tile?type=Mapbox&layerStyle=Satellite&x=%d&y=%d&z=%d"), col, row, zoomLevel);
				CString strFullPath;
				strFullPath.Format(L"%s\\%s#%s.jpg", strTilePath, CStringToolkit::IntToStr(nJpgI), CStringToolkit::IntToStr(nJpgJ));
				StructUrl2Path sup;
				sup.strPat = strFullPath;
				sup.strUrl = strJpgBwUrl;
				vctUrl2Path.push_back(sup);
				nJpgJ++;
			}
		}
	}
	
	#pragma omp parallel for num_threads(2 * omp_get_num_procs() - 1)
	for (int i = 0; i < (int)vctUrl2Path.size();i++)
	{
		StructUrl2Path sup = vctUrl2Path[i];
		DownLoadTile(sup.strUrl, sup.strPat);
	}

	return failedCount;
}
bool CGenerateJpgAccessImpl::DownLoadTile(CString strUrl, CString strSaveFile)
{
	std::string strUrlA = CW2A(strUrl);;

	CURL *curl;
	curl_global_init(CURL_GLOBAL_ALL);
	const std::string url = strUrlA;
	curl = curl_easy_init();
	curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

	std::string full_path = CW2A(strSaveFile);
	FILE *fp;
	if ((fp = fopen(/*"C:/00zbb/test.jfif"*/full_path.c_str(), "ab")) == NULL)
	{
		curl_easy_cleanup(curl);
		return false;
	}

	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
	curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");//这个要加不然文件传输不完全,字符串内容可自定义
	double downloadFileLenth = 0;

	if (CURLE_OK == curl_easy_perform(curl))
	{
		curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &downloadFileLenth);
	}
	fclose(fp);
	curl_easy_cleanup(curl);

	return true;
}

下载完成后 图片如下文件夹:

在这里插入图片描述

二、使用CImage库实现多张图片(瓦片地图)的快速合并

对于多个瓦片数据,想要实现瓦片数据的合并,应该如何实现呢,这里假设已经下载到了瓦片数据,并且瓦片的格式如下图

	int nPicWidth = 256; //瓦片宽
	int nPicHeight = 256; //瓦片高
	int nTotalPicSize = nPicWidth*nPicHeight;

	CImage dst;
	dst.Create(width, height, 24, 0);//创建一个dst对象;参数意义分别为dst宽;dst高;没有alpha通道;

	HDC hDC = dst.GetDC();
	// #pragma omp parallel for num_threads(2 * omp_get_num_procs() - 1)
	for (int nCur = 0; nCur < (int)vctFile.size(); nCur++)
	{
		int nCurMapRow = nCur / nFirRowSize;
		int nCurMapCol = nCur % nFirRowSize;

		CString strFileName = vctFile[nCur];

		CImage curImageData;
		curImageData.Load(strFileName);
		HDC hDestDC = curImageData.GetDC();
		
		int xDest = 0;
		int yDest = 0;
		int nDestWidth = 256;
		int nDestHeight = 256;
		int xSrc = 0;
		int ySrc = 0;

		if (nCurMapCol == 0)
		{
			xDest = 0;
			nDestWidth = nPicWidth - leftTopPixelX;
			xSrc = leftTopPixelX;
		}
		if (nCurMapCol == 1)
		{
			xDest = (nPicWidth - leftTopPixelX);
		}
		if (nCurMapCol >1)
		{
			xDest = (nPicWidth - leftTopPixelX) + (nCurMapCol - 1) * nPicWidth;
		}

		if (nCurMapRow == 0)
		{
			yDest = 0;
			nDestHeight = nPicHeight - leftTopPixelY;
			ySrc = leftTopPixelY;
		}
		if (nCurMapRow == 1)
		{
			yDest = (nPicHeight - leftTopPixelY);
		}
		if (nCurMapRow > 1)
		{
			yDest = (nPicHeight - leftTopPixelY) + (nCurMapRow - 1) * nPicHeight;
		}


		if (xDest + nDestWidth > width)
		{
			nDestWidth = width - xDest;
		}
		if (yDest + nDestHeight > height)
		{
			nDestHeight = height - yDest;
		}

		BOOL bResult = ::BitBlt(hDC, xDest, yDest, nDestWidth, nDestHeight, hDestDC,
			xSrc, ySrc, SRCCOPY);

		curImageData.ReleaseDC();
	}
	dst.ReleaseDC();

	CString strSaveFilePath = _strFullPath + _strProjectBaseName + L"\\";
	if (!CFileToolkit::DirectoryExist(strSaveFilePath))
		CFileToolkit::CreateDirectory(strSaveFilePath);

	CString strSaveFileName = strSaveFilePath + _strProjectBaseName + L".jpg";
	dst.Save(strSaveFileName);

只需要不到1秒的时间就能合并100多张图片:

在这里插入图片描述

三、完整的代码下载

下面是计算完整的流程:根据多个地理坐标(经纬度),计算这些地理坐标的范围内。去谷歌地图上获取范围内的所有瓦片,并完成最终的瓦片合并

	// 读取json获取所有的地理坐标(根据自己的途径获取)
	vector<AcGePoint3d> vcP = GetAllCoords(strFileName);
	if (vcP.empty())
		return;

	// 计算包围框
	CalcBox(strFileName, vcP);

	_nZoomLevel = CalcZoomLevel();

	// 计算填充 方便调整经纬度和层级
	CalcPad(basePad, minSideLength);

	_nZoomLevel = CalcZoomLevel();

	// 下载合并所有瓦片的流程方法
	Merge();

提供代码下载,代码包含完整核心步骤和算法,不能够独立运行,下载请慎重

下载地址

四、总结

  • 本文主要介绍C++ 如何获取瓦片地图并使用CImage库实现多张图片(瓦片地图)的快速合并
  • 如果觉得文章对你有用处,记得 点赞 收藏 转发 一波哦~

💬 往期优质文章分享

🚀 优质教程分享 🚀

  • 🎄如果感觉文章看完了不过瘾,可以来我的其他 专栏 看一下哦~
  • 🎄比如以下几个专栏:Python实战微信订餐小程序、Python量化交易实战、C++ QT实战类项目 和 算法学习专栏
  • 🎄可以学习更多的关于C++/Python的相关内容哦!直接点击下面颜色字体就可以跳转啦!
学习路线指引(点击解锁)知识定位人群定位
🧡 Python实战微信订餐小程序 🧡进阶级本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。
💛Python量化交易实战 💛入门级手把手带你打造一个易扩展、更安全、效率更高的量化交易系统
❤️ C++ QT结合FFmpeg实战开发视频播放器❤️难度偏高分享学习QT成品的视频播放器源码,需要有扎实的C++知识!
💚 游戏爱好者九万人社区💚互助/吹水九万人游戏爱好者社区,聊天互助,白嫖奖品
💙 Python零基础到入门 💙Python初学者针对没有经过系统学习的小伙伴,核心目的就是让我们能够快速学习Python的知识以达到入门

🚀 资料白嫖,温馨提示 🚀

关注下面卡片即刻获取更多编程知识,包括各种语言学习资料,上千套PPT模板和各种游戏源码素材等等资料。更多内容可自行查看哦!

请添加图片描述

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
要将 BMP 图像转换为 PNG 格式并添加透明度,您可以使用 C++ 的第三方,例如 libpng 和 zlib。以下是一个简单的示例代码,它使用这些来执行所需的 BMP 到 PNG 转换和透明度添加: ``` #include <iostream> #include <fstream> #include <cstring> #include <png.h> #include "CImage.h" void write_png_file(char* file_name, int width, int height, png_bytep* row_pointers) { FILE* fp = fopen(file_name, "wb"); if (!fp) { std::cerr << "Error: Failed to open PNG file for writing\n"; return; } png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); if (!png_ptr) { std::cerr << "Error: Failed to create PNG write structure\n"; fclose(fp); return; } png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { std::cerr << "Error: Failed to create PNG info structure\n"; png_destroy_write_struct(&png_ptr, nullptr); fclose(fp); return; } if (setjmp(png_jmpbuf(png_ptr))) { std::cerr << "Error: Failed to initialize PNG write\n"; png_destroy_write_struct(&png_ptr, &info_ptr); fclose(fp); return; } png_init_io(png_ptr, fp); if (setjmp(png_jmpbuf(png_ptr))) { std::cerr << "Error: Failed to write PNG image header\n"; png_destroy_write_struct(&png_ptr, &info_ptr); fclose(fp); return; } png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); png_write_info(png_ptr, info_ptr); if (setjmp(png_jmpbuf(png_ptr))) { std::cerr << "Error: Failed to write PNG image data\n"; png_destroy_write_struct(&png_ptr, &info_ptr); fclose(fp); return; } png_write_image(png_ptr, row_pointers); if (setjmp(png_jmpbuf(png_ptr))) { std::cerr << "Error: Failed to complete PNG write\n"; png_destroy_write_struct(&png_ptr, &info_ptr); fclose(fp); return; } png_write_end(png_ptr, nullptr); png_destroy_write_struct(&png_ptr, &info_ptr); fclose(fp); } void bmp_to_png_with_alpha(const char* bmp_file, const char* png_file) { CImage img; if (!img.Load(bmp_file)) { std::cerr << "Error: Failed to load BMP file\n"; return; } int width = img.GetWidth(); int height = img.GetHeight(); png_bytep* row_pointers = new png_bytep[height]; for (int y = 0; y < height; ++y) { row_pointers[y] = new png_byte[4 * width]; for (int x = 0; x < width; ++x) { COLORREF color = img.GetPixel(x, y); png_byte red = GetRValue(color); png_byte green = GetGValue(color); png_byte blue = GetBValue(color); png_byte alpha = (color != RGB(255, 0, 255)) ? 0xFF : 0x00; png_bytep pixel = &(row_pointers[y][4 * x]); pixel[0] = red; pixel[1] = green; pixel[2] = blue; pixel[3] = alpha; } } write_png_file(png_file, width, height, row_pointers); for (int y = 0; y < height; ++y) { delete[] row_pointers[y]; } delete[] row_pointers; } int main() { const char* bmp_file = "test.bmp"; const char* png_file = "test.png"; bmp_to_png_with_alpha(bmp_file, png_file); return 0; } ``` 在这个示例中,我们首先使用 CImage 类加载 BMP 图像文件。然后,我们遍历每个像素,并将其值转换为 PNG 格式,同时添加透明度(即,如果像素值为 RGB(255, 0, 255),则将其 alpha 值设置为 0,否则将其 alpha 值设置为 255)。最后,我们使用 write_png_file() 函数将转换后的 PNG 图像写入磁盘。 请注意,该示例仅提供了一个基本的转换功能。如果您需要处理大量图像或需要更高级的功能(例如,调整大小、旋转或滤镜等),则可能需要使用更强大的图像处理,例如 OpenCV 或 ImageMagick。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虚坏叔叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值