/// <summary> /// 瓦片下载完成 /// </summary> /// <param name="downloader"></param> /// <param name="tile"></param> private void TileDownloadCompleted(Core.MapServerDownloader downloader, Core.Tile tile) { string logFormat = "当前线程:{0} 瓦片:{1},{2},{3} 时间:{4}"; Core.Log.Write(logFile, string.Format(logFormat, System.Threading.Thread.CurrentThread.Name , tile.X.ToString(), tile.Y.ToString(), tile.Zoom.ToString(), DateTime.Now.ToString("MM-dd HH:mm:ss ffff"))); statusbar_progress.Value = (int)((downloader.DownloadedTileCount / (decimal)downloader.DownloadTileCount) * 100); statusbar_status.Text = "下载中......" + downloader.DownloadedTileCount.ToString() + "/" + downloader.DownloadTileCount.ToString(); }
/// <summary> /// 经纬度所在的瓦片 /// </summary> /// <param name="zoom"></param> /// <param name="lon"></param> /// <param name="lat"></param> /// <returns></returns> public static Tile GetTile(int zoom, double lon, double lat) { Tile tile = new Tile(); //(经度+180)÷360×262144 = 经度图片编号 tile.X = (long)((lon + 180.0) / 360 * Math.Pow(2, zoom)); double siny = Math.Sin(lat * Math.PI / 180); double y = Math.Log((1 + siny) / (1 - siny)); tile.Y = (long)((128 * Math.Pow(2, zoom)) * (1 - y / (2 * Math.PI)) / 256); return tile; }
/// <summary> /// 下载 /// </summary> protected virtual void Download() { if (CurDownloadZoom == null) { cts.Cancel(); return; } CreateZoomDirectory(CurDownloadZoom.Zoom); #region 锁定下载 Tile tile; lock (lockObject) { tile = new Tile(CurDownloadZoom.CurTileX, CurDownloadZoom.CurTileY, CurDownloadZoom.Zoom); tile.DownloadZoom = CurDownloadZoom; GetTilePath(tile); if ((CurDownloadZoom.CurTileY + 1) > CurDownloadZoom.MaxTileY) { if ((CurDownloadZoom.CurTileX + 1) > CurDownloadZoom.MaxTileX) { CurDownloadZoom = CurDownloadZoom.Next; } else { ++CurDownloadZoom.CurTileX; CurDownloadZoom.CurTileY = CurDownloadZoom.MinTileY; } } else { ++tile.DownloadZoom.CurTileY; } } SaveTile(tile); Download(); #endregion if (task.IsCompleted && CurDownloadZoom != null) { task.Start(); } }
/// <summary> /// 保存Tile /// </summary> /// <param name="tile"></param> public virtual void SaveTile(Tile tile) { string row_1 = (tile.X / 1000000).ToString().PadLeft(3, '0'); string row_2 = ((tile.X % 1000000) / 1000).ToString().PadLeft(3, '0'); string row_3 = (tile.X % 1000).ToString().PadLeft(3, '0'); string col_1 = (tile.Y / 1000000).ToString().PadLeft(3, '0'); string col_2 = ((tile.Y % 1000000) / 1000).ToString().PadLeft(3, '0'); string col_3 = (tile.Y % 1000).ToString().PadLeft(3, '0'); string dir = savePath + "/" + tile.Zoom.ToString().PadLeft(2, '0') + "/" + row_1 + "/" + row_2 + "/" + row_3 + "/" + col_1 + "/" + col_2; if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } string fileName = dir + "/" + col_3 + ".png"; //DownloadFile(tile.UrlPath, fileName); DownloadFileWithHttpRequest(tile.UrlPath, fileName); #region 下载完成触发事件 if (TileDownloadCompleted != null) { TileDownloadCompleted(this, tile); ++downloadedTileCount; Core.DownloadConfig.UpdateDownloading(tile.DownloadZoom); } if (downloadedTileCount == downloadTileCount) { if (DownloadCompleted != null) { DownloadCompleted();//下载完成 } StopDownload();//停止下载 } #endregion }
/// <summary> /// 下载单张瓦片 /// </summary> /// <param name="tile"></param> public abstract void GetTilePath(Tile tile);