private void btnGenPNG_Click(object sender, EventArgs e) { clearMsg(); TilesBounds tilesBounds = GetTilesBound(); StringBuilder cmdBuilder = new StringBuilder(); string outPath = textTiffPath.Text; try { string tmpPath = outPath + "_tmp"; Directory.CreateDirectory(tmpPath); setMsg("开始合并png图片!"); ivProcessCount = tilesBounds.maxCol - tilesBounds.minCol + 1; for (int col = tilesBounds.minCol; col <= tilesBounds.maxCol; col++) { string picPath = Path.Combine(textTilePath.Text, numZoomLevel.Text, col.ToString(), "*" + fileExt); string outPngfile = Path.Combine(tmpPath, col + ".png"); string cmd = string.Format("/panorama=(2,{0}) /convert={1}", picPath, outPngfile); Process ivProcess = new Process(); ivProcess.EnableRaisingEvents = true; ivProcess.StartInfo.FileName = irfanViewPath; ivProcess.StartInfo.Arguments = cmd; ivProcess.Exited += ivProcess_Exited; ivProcess.Start(); } } catch (Exception ex) { setMsg(ex.Message); } }
private void WriteTfwFile(TilesBounds tilesBounds, string outTfwFileName) { StringBuilder sb = new StringBuilder(); decimal resolution = EarthCircumference / 256 / (decimal)Math.Pow(2, tilesBounds.zoomLevel); //EPSG:3857 sb.AppendLine(resolution.ToString()); //X方向上的象素分辨素 sb.AppendLine("0"); //X方向的旋转系数 sb.AppendLine("0"); //Y方向的旋转系数 sb.AppendLine("-" + resolution.ToString()); //Y方向上的象素分辨率 sb.AppendLine(((tilesBounds.minCol * 256m + 0.5m) * resolution - EarthCircumference / 2).ToString()); //左上角象素中心X坐标 sb.AppendLine((EarthCircumference / 2 - (tilesBounds.minRow * 256m + 0.5m) * resolution).ToString()); //左上角象素中心Y坐标 /* EPSG:4326 * int imageWidth = 256 * (tilesBounds.maxCol - tilesBounds.minCol + 1); * int imageHeight = 256 * (tilesBounds.maxRow - tilesBounds.minRow + 1); * * PointD p1 = TileToWorldPos(tilesBounds.minCol,tilesBounds.minRow,tilesBounds.zoomLevel); //左上 * PointD p2 = TileToWorldPos(tilesBounds.maxCol + 1, tilesBounds.maxRow + 1, tilesBounds.zoomLevel);//右下 * * sb.AppendLine(((p2.X - p1.X) / imageWidth).ToString());//X方向上的象素分辨素 * sb.AppendLine("0");//X方向的旋转系数 * sb.AppendLine("0");//Y方向的旋转系数 * sb.AppendLine(((p2.Y - p1.Y) / imageHeight).ToString());//Y方向上的象素分辨率 * sb.AppendLine((p1.X).ToString());//左上角象素中心X坐标 * sb.AppendLine((p1.Y).ToString());//左上角象素中心Y坐标 */ File.WriteAllText(outTfwFileName, sb.ToString()); }
private void btnDownloadMissFile_Click(object sender, EventArgs e) { DisableButton(false); clearMsg(); TilesBounds tilesBounds = GetTilesBound(); backgroundWorker1.RunWorkerAsync(new WorkerParams { Type = 0, DownloadUseProxy = false, TilesBound = tilesBounds, MapType = cmbMapType.SelectedIndex, GoogleRasterV = txtGoogleRasterV.Text }); }
private void btnGenerateTiFF_Click(object sender, EventArgs e) { clearMsg(); DisableButton(false); //调用 TilesBounds tilesBounds = GetTilesBound(); backgroundWorker1.RunWorkerAsync(new WorkerParams { Type = 2, TilesBound = tilesBounds, OutPutFilePath = textTiffPath.Text, TilePath = textTilePath.Text }); }
private TilesBounds GetTilesBound() { TilesBounds tilesBounds = new TilesBounds(); tilesBounds.minCol = int.Parse(textMinCol.Text); tilesBounds.maxCol = int.Parse(textMaxCol.Text); tilesBounds.minRow = int.Parse(textMinRow.Text); tilesBounds.maxRow = int.Parse(textMaxRow.Text); tilesBounds.zoomLevel = int.Parse(numZoomLevel.Text); return(tilesBounds); }
//合并单个Tiff文件 private void CombineSingleTiff(BackgroundWorker worker, TilesBounds tilesBounds, string tilePath, string outPutFilePath) { string outPutFileName = Path.Combine(outPutFilePath, string.Format("x{2}-{3}_y{0}-{1}.tif", tilesBounds.minRow, tilesBounds.maxRow, tilesBounds.minCol, tilesBounds.maxCol)); worker.ReportProgress(tilesBounds.minCol, string.Format("开始生成TIFF文件:{0}", outPutFileName)); if (File.Exists(outPutFileName)) { //存在则跳过 return; } int imageWidth = 256 * (tilesBounds.maxCol - tilesBounds.minCol + 1); int imageHeight = 256 * (tilesBounds.maxRow - tilesBounds.minRow + 1); TiffFileWriter writer = new TiffFileWriter(); writer.CreateFile(outPutFileName, (uint)imageWidth, (uint)imageHeight); writer.WriteTiffHeader(); for (int row = tilesBounds.minRow; row <= tilesBounds.maxRow; row++) { List <Bitmap> bitmapLs = new List <Bitmap>(tilesBounds.maxCol - tilesBounds.minCol + 1); try { for (int col = tilesBounds.minCol; col <= tilesBounds.maxCol; col++) { string sourceFileName = Path.Combine(tilePath, tilesBounds.zoomLevel.ToString(), col.ToString(), row.ToString() + fileExt); if (File.Exists(sourceFileName)) { bitmapLs.Add(new Bitmap(sourceFileName)); } else { bitmapLs.Add(new Bitmap(256, 256)); } } writer.WriteImageData(bitmapLs); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } writer.CloseFile(); string outTfwFileName = Path.Combine(outPutFilePath, Path.GetFileNameWithoutExtension(outPutFileName) + ".tfw"); WriteTfwFile(tilesBounds, outTfwFileName); }
/// <summary> /// 拼接瓦片 /// </summary> /// <param name="tilesBounds"></param> /// <param name="tilePath"></param> /// <param name="outPutFileName"></param> private void CombineTilesByTiffWriter(BackgroundWorker worker, DoWorkEventArgs e) { try { WorkerParams param = e.Argument as WorkerParams; TilesBounds tb = param.TilesBound; string tilePath = param.TilePath; string outPutFilePath = param.OutPutFilePath; int minBlockColIdx = tb.minCol / param.TiffTileCount; int maxBlockColIdx = tb.maxCol / param.TiffTileCount; int minBlockRowIdx = tb.minRow / param.TiffTileCount; int maxBlockRowIdx = tb.maxRow / param.TiffTileCount; int totalFileCount = (maxBlockColIdx - minBlockColIdx + 1) * (maxBlockRowIdx - minBlockRowIdx + 1); worker.ReportProgress(0, string.Format("生成的TIFF共有{0}个,总大小估计为:{1} G", totalFileCount, totalFileCount * 0.75)); for (int i = minBlockColIdx; i <= maxBlockColIdx; i++) { for (int j = minBlockRowIdx; j <= maxBlockRowIdx; j++) { if (worker.CancellationPending) { e.Cancel = true; return; } TilesBounds tilesBounds = new TilesBounds(); tilesBounds.zoomLevel = tb.zoomLevel; //修正瓦片边界,防止周围黑边 tilesBounds.minCol = Math.Max(i * param.TiffTileCount, tb.minCol); tilesBounds.maxCol = Math.Min(i * param.TiffTileCount + param.TiffTileCount - 1, tb.maxCol); tilesBounds.minRow = Math.Max(j * param.TiffTileCount, tb.minRow); tilesBounds.maxRow = Math.Min(j * param.TiffTileCount + param.TiffTileCount - 1, tb.maxRow); CombineSingleTiff(worker, tilesBounds, tilePath, outPutFilePath); } } } catch (Exception ex) { worker.ReportProgress(0, string.Format("合并图片生成TIFF时出现异常!msg:{0}", ex.Message)); } }
private void btnSearchMiss_Click(object sender, EventArgs e) { clearMsg(); TilesBounds tilesBounds = GetTilesBound(); try { int i = 0; int allMissTile = 0; for (int col = tilesBounds.minCol; col <= tilesBounds.maxCol; col++) { int missTileCount = 0; for (int row = tilesBounds.minRow; row <= tilesBounds.maxRow; row++) { string sourceFileName = Path.Combine(textTilePath.Text, numZoomLevel.Text, col.ToString(), row.ToString()) + fileExt; if (!File.Exists(sourceFileName)) { string fileUrl = string.Format("http://khm{0}.google.com/kh/v=189&hl=en&x={1}&y={2}&z={3}&s={4}", i, col, row, tilesBounds.zoomLevel, SourceTail.Substring(0, rng.Next(SourceTail.Length))); //sb.AppendLine(fileUrl); missTileCount++; allMissTile++; i++; i = i % 4; } } if (missTileCount > 0) { setMsg(string.Format("{0}:{1}", col, missTileCount)); } } setMsg(string.Format("Total:{0}", allMissTile)); } catch (Exception ex) { setMsg(ex.Message); } }
void ivProcess_Exited(object sender, EventArgs e) { string outPath = textTiffPath.Text; string tmpPath = outPath + "_tmp"; TilesBounds tilesBounds = GetTilesBound(); lock (this) { ivProcessCount--; if (ivProcessCount == 0) { setMsg("完成行合并,开始进行列合并!"); string arguments = string.Format("/panorama=(1,{0}) /convert={1}", Path.Combine(tmpPath, "*.png"), outPath); Process p = new Process(); p.EnableRaisingEvents = true; p.StartInfo.FileName = irfanViewPath; p.StartInfo.Arguments = arguments; p.Exited += ivProcess_Exited; p.Start(); p.Exited += p_Exited; } } }
private void WriteTfwFile(TilesBounds tilesBounds, string outTfwFileName) { StringBuilder sb = new StringBuilder(); decimal resolution = EarthCircumference / 256 / (decimal)Math.Pow(2, tilesBounds.zoomLevel); //EPSG:3857 sb.AppendLine(resolution.ToString());//X方向上的象素分辨素 sb.AppendLine("0");//X方向的旋转系数 sb.AppendLine("0");//Y方向的旋转系数 sb.AppendLine( "-" + resolution.ToString());//Y方向上的象素分辨率 sb.AppendLine(((tilesBounds.minCol * 256m + 0.5m) * resolution - EarthCircumference / 2).ToString());//左上角象素中心X坐标 sb.AppendLine((EarthCircumference / 2 - (tilesBounds.minRow * 256m + 0.5m) * resolution).ToString());//左上角象素中心Y坐标 /* EPSG:4326 int imageWidth = 256 * (tilesBounds.maxCol - tilesBounds.minCol + 1); int imageHeight = 256 * (tilesBounds.maxRow - tilesBounds.minRow + 1); PointD p1 = TileToWorldPos(tilesBounds.minCol,tilesBounds.minRow,tilesBounds.zoomLevel); //左上 PointD p2 = TileToWorldPos(tilesBounds.maxCol + 1, tilesBounds.maxRow + 1, tilesBounds.zoomLevel);//右下 sb.AppendLine(((p2.X - p1.X) / imageWidth).ToString());//X方向上的象素分辨素 sb.AppendLine("0");//X方向的旋转系数 sb.AppendLine("0");//Y方向的旋转系数 sb.AppendLine(((p2.Y - p1.Y) / imageHeight).ToString());//Y方向上的象素分辨率 sb.AppendLine((p1.X).ToString());//左上角象素中心X坐标 sb.AppendLine((p1.Y).ToString());//左上角象素中心Y坐标 */ File.WriteAllText(outTfwFileName,sb.ToString()); }
private TilesBounds GetTilesBound() { TilesBounds tilesBounds = new TilesBounds(); tilesBounds.minCol = int.Parse(textMinCol.Text); tilesBounds.maxCol = int.Parse(textMaxCol.Text); tilesBounds.minRow = int.Parse(textMinRow.Text); tilesBounds.maxRow = int.Parse(textMaxRow.Text); tilesBounds.zoomLevel = int.Parse(numZoomLevel.Text); return tilesBounds; }
/// <summary> /// 拼接瓦片 /// </summary> /// <param name="tilesBounds"></param> /// <param name="tilePath"></param> /// <param name="outPutFileName"></param> private void CombineTilesByTiffWriter(BackgroundWorker worker, DoWorkEventArgs e) { try { WorkerParams param = e.Argument as WorkerParams; TilesBounds tb = param.TilesBound; string tilePath = param.TilePath; string outPutFilePath = param.OutPutFilePath; int minBlockColIdx = tb.minCol / param.TiffTileCount; int maxBlockColIdx = tb.maxCol / param.TiffTileCount; int minBlockRowIdx = tb.minRow / param.TiffTileCount; int maxBlockRowIdx = tb.maxRow / param.TiffTileCount; int totalFileCount = (maxBlockColIdx - minBlockColIdx + 1) * (maxBlockRowIdx - minBlockRowIdx + 1); worker.ReportProgress(0, string.Format("生成的TIFF共有{0}个,总大小估计为:{1} G", totalFileCount, totalFileCount * 0.75)); for (int i = minBlockColIdx; i <= maxBlockColIdx; i++) { for (int j = minBlockRowIdx; j <= maxBlockRowIdx; j++) { if (worker.CancellationPending) { e.Cancel = true; return; } TilesBounds tilesBounds = new TilesBounds(); tilesBounds.zoomLevel = tb.zoomLevel; //修正瓦片边界,防止周围黑边 tilesBounds.minCol = Math.Max(i * param.TiffTileCount,tb.minCol); tilesBounds.maxCol = Math.Min(i * param.TiffTileCount + param.TiffTileCount - 1,tb.maxCol); tilesBounds.minRow = Math.Max(j * param.TiffTileCount,tb.minRow); tilesBounds.maxRow = Math.Min(j * param.TiffTileCount + param.TiffTileCount - 1,tb.maxRow); CombineSingleTiff(worker, tilesBounds, tilePath, outPutFilePath); } } } catch (Exception ex) { worker.ReportProgress(0, string.Format("合并图片生成TIFF时出现异常!msg:{0}", ex.Message)); } }
//合并单个Tiff文件 private void CombineSingleTiff(BackgroundWorker worker, TilesBounds tilesBounds, string tilePath, string outPutFilePath) { string outPutFileName = Path.Combine(outPutFilePath, string.Format("x{2}-{3}_y{0}-{1}.tif", tilesBounds.minRow, tilesBounds.maxRow, tilesBounds.minCol, tilesBounds.maxCol)); worker.ReportProgress(tilesBounds.minCol, string.Format("开始生成TIFF文件:{0}", outPutFileName)); if (File.Exists(outPutFileName)) { //存在则跳过 return; } int imageWidth = 256 * (tilesBounds.maxCol - tilesBounds.minCol + 1); int imageHeight = 256 * (tilesBounds.maxRow - tilesBounds.minRow + 1); TiffFileWriter writer = new TiffFileWriter(); writer.CreateFile(outPutFileName, (uint)imageWidth, (uint)imageHeight); writer.WriteTiffHeader(); for (int row = tilesBounds.minRow; row <= tilesBounds.maxRow; row++) { List<Bitmap> bitmapLs = new List<Bitmap>(tilesBounds.maxCol - tilesBounds.minCol + 1); try { for (int col = tilesBounds.minCol; col <= tilesBounds.maxCol; col++) { string sourceFileName = Path.Combine(tilePath, tilesBounds.zoomLevel.ToString(), col.ToString(), row.ToString() + fileExt); if (File.Exists(sourceFileName)) { bitmapLs.Add(new Bitmap(sourceFileName)); } else { bitmapLs.Add(new Bitmap(256, 256)); } } writer.WriteImageData(bitmapLs); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } writer.CloseFile(); string outTfwFileName = Path.Combine(outPutFilePath, Path.GetFileNameWithoutExtension(outPutFileName) + ".tfw"); WriteTfwFile(tilesBounds, outTfwFileName); }
private void DownLoadMissFiles(BackgroundWorker worker, DoWorkEventArgs e) { try { WorkerParams param = e.Argument as WorkerParams; TilesBounds tilesBounds = param.TilesBound; int i = 0; for (int col = tilesBounds.minCol; col <= tilesBounds.maxCol; col++) { worker.ReportProgress(col, string.Format("开始下载{0}目录图片!", col)); string colDir = Path.Combine(textTilePath.Text, numZoomLevel.Text, col.ToString()); if (!Directory.Exists(colDir)) { Directory.CreateDirectory(colDir); } for (int row = tilesBounds.minRow; row <= tilesBounds.maxRow; row++) { if (worker.CancellationPending) { e.Cancel = true; return; } string sourceFileName = Path.Combine(colDir, row.ToString()) + fileExt; if (!File.Exists(sourceFileName)) { string fileUrl = string.Empty; if (param.MapType == 0) { fileUrl = string.Format("http://khm{0}.google.com/kh/v={5}&hl=en&x={1}&y={2}&z={3}&s={4}", i, col, row, tilesBounds.zoomLevel, SourceTail.Substring(0, rng.Next(SourceTail.Length)), param.GoogleRasterV); } else if (param.MapType == 1) { fileUrl = string.Format("http://mt{0}.google.cn/vt/lyrs=t@132&x={1}&y={2}&z={3}&s={4}", i, col, row, tilesBounds.zoomLevel, SourceTail.Substring(0, rng.Next(SourceTail.Length))); } else if (param.MapType == 2) { fileUrl = string.Format("http://mt{0}.google.cn/vt/lyrs=m@292000000&hl=zh-CN&gl=cn&x={1}&y={2}&z={3}&s={4}", i, col, row, tilesBounds.zoomLevel, SourceTail.Substring(0, rng.Next(SourceTail.Length))); } else if (param.MapType == 3) { fileUrl = string.Format("http://10.55.13.160/ArcGIS/rest/services/water_baseMap/MapServer/tile/{3}/{2}/{1}", i, col, row, tilesBounds.zoomLevel, SourceTail.Substring(0, rng.Next(SourceTail.Length))); } try { WebClient wc = new WebClient(); wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:21.0) Gecko/20130109 Firefox/21.0"); if (param.DownloadUseProxy) { wc.Proxy = new WebProxy("127.0.0.1", 41080); } wc.DownloadFile(fileUrl, sourceFileName); Thread.Sleep(10); } catch (Exception ex) { worker.ReportProgress(col, string.Format("下载图片{0}_{1}时出现错误!msg:{2} ,url:{3}", col, row, ex.Message, fileUrl)); } i++; i = i % 4; } } } } catch (Exception ex) { worker.ReportProgress(0, string.Format("下载图片时出现异常!msg:{0}", ex.Message)); } }