#include "GoogleTile.h" #include #include #include #include #include #include using std::string; namespace fs = std::filesystem; #define M_PI 3.1415926 googleTile::googleTile() { } googleTile::~googleTile() { } void googleTile::ExportGeoPng(cv::Mat _pan, TileInfo info, std::string dir) { // 创建目录 if (!fs::exists(dir)) { fs::create_directories(dir); } // 保存全景图 string png_path = dir + "/" + info.tileName + ".png"; cv::imwrite(png_path, _pan); // 写入kml string kml_path = dir + "/" + info.tileName + ".kml"; vector taskTiles; taskTiles.push_back(info); WriteKml(taskTiles, kml_path); } void googleTile::ExportTile(cv::Mat _pan, TileInfo panInfo, std::string dir,std::string fileString) { int zoom = panInfo.ind.z; TileBox panBox = panInfo.box; // 计算四至的瓦片编号 TileIndex WN = LatLonToTile(panBox.north, panBox.west, zoom); TileIndex WS = LatLonToTile(panBox.south, panBox.west, zoom); TileIndex EN = LatLonToTile(panBox.north, panBox.east, zoom); TileIndex ES = LatLonToTile(panBox.south, panBox.east, zoom); int xStart = WN.x; int xEnd = EN.x; int yStart = EN.y; int yEnd = ES.y; int panWidth = _pan.cols; int panHeight = _pan.rows; vector taskTiles; for (size_t i = yStart; i < yEnd; i++) { for (size_t j = xStart; j < xEnd; j++) { TileIndex id = {j,i,zoom}; TileBox tilebox = GetTileBox(id); // 填充 TileInfo info = { 0 }; info.box = tilebox; info.ind = id; // 计算瓦片在全景图中的像素位置 int x1 = (tilebox.west - panBox.west) / (panBox.east - panBox.west) * panWidth; int y1 = (panBox.north - tilebox.north) / (panBox.north - panBox.south) * panHeight; int x2 = (tilebox.east - panBox.west) / (panBox.east - panBox.west) * panWidth; int y2 = (panBox.north - tilebox.south) / (panBox.north - panBox.south) * panHeight; // 有不完整瓦片,不保存 if (x1 < 0 || y1 < 0 || x2 > panWidth - 1 || y2 > panHeight - 1) { continue; } // 确保坐标在图像范围内 x1 = std::max(0, std::min(x1, panWidth - 1)); y1 = std::max(0, std::min(y1, panHeight - 1)); x2 = std::max(0, std::min(x2, panWidth - 1)); y2 = std::max(0, std::min(y2, panHeight - 1)); // 提取瓦片 cv::Rect tileRect(x1, y1, x2 - x1, y2 - y1); cv::Mat tile = _pan(tileRect); // 标准web瓦片尺寸 // if (tile.cols != 256 || tile.rows != 256) // { // cv::resize(tile, tile, cv::Size(256, 256)); // } // 生成文件名 std::string tileDir = dir + "/" + std::to_string(zoom); std::string fileName = tileDir + "/" + std::to_string(j) + "_" + std::to_string(i) + ".png"; // 创建目录 if (!fs::exists(tileDir)) { fs::create_directories(tileDir); } // 保存瓦片为图像文件 cv::imwrite(fileName, tile); // 补充瓦片信息 info.tileName = std::to_string(j) + "_" + std::to_string(i) + "_" + std::to_string(zoom); info.href = std::to_string(zoom) + "/" + std::to_string(j) + "_" + std::to_string(i) + ".png"; taskTiles.push_back(info); } } // 输出KML WriteKml(taskTiles,dir + "/" + fileString + "_z" + std::to_string(zoom) + ".kml"); } int googleTile::getZoomLevel(float mp) { int nLev = 0; // 计算单位瓦片的实际覆盖 int meters_cover = TILE_SIZE * 1.0/mp; int earthLen = 40075017; nLev = std::floor(std::log2(earthLen / meters_cover)); return nLev; } TileIndex googleTile::LatLonToTile(double latitude, double longitude, int zoomLevel) { // 将纬度转换为墨卡托投影 double latRad = latitude * M_PI / 180.0; double sinLat = sin(latRad); // 计算经度的瓦片编号(0-2^zoomLevel) int xTile = static_cast((longitude + 180.0) / 360.0 * pow(2.0, zoomLevel)); // 计算纬度的瓦片编号(0-2^zoomLevel) int yTile = static_cast((1.0 - log((1.0 + sinLat) / (1.0 - sinLat)) / (2.0 * M_PI)) / 2.0 * pow(2.0, zoomLevel)); return TileIndex{ xTile, yTile, zoomLevel }; } // 将瓦片编号转换为经纬度 void TileToLatLon(int x, int y, int z, double& lat, double& lon) { double n = std::pow(2.0, z); lon = x / n * 360.0 - 180.0; double lat_rad = std::atan(std::sinh(M_PI * (1 - 2 * y / n))); lat = lat_rad * 180.0 / M_PI; } TileBox googleTile::GetTileBox(TileIndex ind) { TileBox box = {0}; // 计算瓦片的西北角经纬度 TileToLatLon(ind.x, ind.y, ind.z, box.north, box.west); // 计算瓦片的东南角经纬度 TileToLatLon(ind.x + 1, ind.y + 1, ind.z, box.south, box.east); return box; } void googleTile::WriteKml(vector tiles, string filePath) { // Open the KML file for writing std::ofstream kml_file(filePath); if (!kml_file.is_open()) { std::cerr << "Failed to open KML file for writing: " << filePath << std::endl; return; } // Write the KML header kml_file << "" << std::endl; kml_file << "" << std::endl; kml_file << "" << std::endl; // Loop through each tile and write a GroundOverlay element for (const auto& tile : tiles) { kml_file << "" << std::endl; kml_file << " " << tile.tileName << "" << std::endl; kml_file << " " << std::endl; kml_file << " " << tile.href << "" << std::endl; kml_file << " " << std::endl; kml_file << " " << std::endl; kml_file << " " << tile.box.north << "" << std::endl; kml_file << " " << tile.box.south << "" << std::endl; kml_file << " " << tile.box.east << "" << std::endl; kml_file << " " << tile.box.west << "" << std::endl; kml_file << " " << std::endl; kml_file << "" << std::endl; } // Write the KML footer kml_file << "" << std::endl; kml_file << "" << std::endl; // Close the file kml_file.close(); }