Google 地图下载工具 (C#)

来源:互联网 发布:cda数据分析一级好考吗 编辑:程序博客网 时间:2024/06/05 21:23
这是一个用于下载 Google 地图的小工具,相关内容参见 https://on4wp7.codeplex.com/ 。
[csharp] view plaincopyprint?
  1. //  
  2. // Google Map Tiles Downloader in C# by coolypf  
  3. // No rights reserved, neither warranty nor guarantee  
  4. //  
  5.   
  6. using System;  
  7. using System.Collections.Generic;  
  8. using System.Drawing;  
  9. using System.Net;  
  10.   
  11. namespace GMapsDownloader  
  12. {  
  13.     class Program  
  14.     {  
  15.         const double EarthRadius = 6378137;  
  16.         const double MinLatitude = -85.05112878;  
  17.         const double MaxLatitude = 85.05112878;  
  18.         const double MinLongitude = -180;  
  19.         const double MaxLongitude = 180;  
  20.   
  21.         const string NameFormat = "{0}-{1}-{2}.png";  
  22.         const string TileSource = "https://mts{0}.google.com/vt/lyrs=m@207000000&hl=zh-CN&gl=CN&src=app&x={1}&y={2}&z={3}&s={4}";  
  23.         const string SourceTail = "Galileo";  
  24.   
  25.         static Random rng = new Random();  
  26.   
  27.         static double Clip(double n, double minValue, double maxValue)  
  28.         {  
  29.             return Math.Min(Math.Max(n, minValue), maxValue);  
  30.         }  
  31.   
  32.         static void LatLongToTileXY(double latitude, double longitude, int levelOfDetail, out int tileX, out int tileY)  
  33.         {  
  34.             double x = (longitude + 180) / 360;  
  35.             double sinLatitude = Math.Sin(latitude * Math.PI / 180);  
  36.             double y = 0.5 - Math.Log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * Math.PI);  
  37.   
  38.             uint mapSize = 1u << levelOfDetail;  
  39.             tileX = (int)Clip(x * mapSize + 0.5, 0, mapSize - 1);  
  40.             tileY = (int)Clip(y * mapSize + 0.5, 0, mapSize - 1);  
  41.         }  
  42.   
  43.         static bool Validate(int x, int y, int l)  
  44.         {  
  45.             bool ret = false;  
  46.             try  
  47.             {  
  48.                 Bitmap bmp = new Bitmap(string.Format(NameFormat, x, y, l));  
  49.                 ret = (bmp.Height == 256 && bmp.Width == 256);  
  50.                 bmp.Dispose();  
  51.             }  
  52.             catch (Exception) { }  
  53.             return ret;  
  54.         }  
  55.   
  56.         static void Download(int x, int y, int l)  
  57.         {  
  58.             try  
  59.             {  
  60.                 WebClient client = new WebClient();  
  61.                 client.Headers.Add("user-agent""Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:21.0) Gecko/20130109 Firefox/21.0");  
  62.                 string loc = string.Format(TileSource, rng.Next(4), x, y, l,  
  63.                     SourceTail.Substring(0, rng.Next(SourceTail.Length)));  
  64.                 string name = string.Format(NameFormat, x, y, l);  
  65.                 client.DownloadFile(loc, name);  
  66.             }  
  67.             catch (Exception ex)  
  68.             {  
  69.                 Console.WriteLine();  
  70.                 Console.WriteLine(ex.Message);  
  71.             }  
  72.         }  
  73.   
  74.         static void Main(string[] args)  
  75.         {  
  76.             try  
  77.             {  
  78.                 Console.WriteLine("Google Map Tiles Downloader");  
  79.                 Console.WriteLine("lat1, long1   lat2, long2   level");  
  80.                 double[] array = new double[4];  
  81.                 int level = 1, i = 0;  
  82.                 string[] splits = Console.ReadLine().Split(' '',');  
  83.                 foreach (string s in splits)  
  84.                 {  
  85.                     if (s.Trim() == "")  
  86.                         continue;  
  87.                     if (i < 4) array[i++] = double.Parse(s);  
  88.                     else level = int.Parse(s);  
  89.                 }  
  90.                 double lat1 = Clip(array[0], MinLatitude, MaxLatitude);  
  91.                 double lat2 = Clip(array[2], MinLatitude, MaxLatitude);  
  92.                 double long1 = Clip(array[1], MinLongitude, MaxLongitude);  
  93.                 double long2 = Clip(array[3], MinLongitude, MaxLongitude);  
  94.                 if (level < 1) level = 1;  
  95.                 if (level > 19) level = 19;  
  96.   
  97.                 Console.WriteLine("Generating download list...");  
  98.                 List<int> list = new List<int>();  
  99.                 for (i = 1; i <= level; ++i)  
  100.                 {  
  101.                     int x1, y1, x2, y2;  
  102.                     LatLongToTileXY(lat1, long1, i, out x1, out y1);  
  103.                     LatLongToTileXY(lat2, long2, i, out x2, out y2);  
  104.                     for (int u = x1; u <= x2; ++u)  
  105.                         for (int v = y1; v <= y2; ++v)  
  106.                         {  
  107.                             list.Add(u);  
  108.                             list.Add(v);  
  109.                             list.Add(i);  
  110.                         }  
  111.                 }  
  112.                 Console.WriteLine(list.Count / 3 + " in list");  
  113.   
  114.                 Console.WriteLine("Validating existing tiles...");  
  115.                 List<int> dlist = new List<int>();  
  116.                 for (i = 0; i < list.Count; i += 3)  
  117.                 {  
  118.                     int x = list[i], y = list[i + 1], l = list[i + 2];  
  119.                     if (Validate(x, y, l))  
  120.                         continue;  
  121.                     dlist.Add(x);  
  122.                     dlist.Add(y);  
  123.                     dlist.Add(l);  
  124.                 }  
  125.                 Console.WriteLine(dlist.Count / 3 + " to download");  
  126.                 if (dlist.Count == 0)  
  127.                     return;  
  128.   
  129.                 Console.WriteLine("Press ENTER");  
  130.                 Console.ReadLine();  
  131.                 for (i = 0; i < dlist.Count; i += 3)  
  132.                 {  
  133.                     int x = dlist[i], y = dlist[i + 1], l = dlist[i + 2];  
  134.                     Console.Write("\rDownloading " + i / 3);  
  135.                     Download(x, y, l);  
  136.                 }  
  137.   
  138.                 Console.WriteLine();  
  139.                 Console.WriteLine("Done.");  
  140.             }  
  141.             catch (Exception ex)  
  142.             {  
  143.                 Console.WriteLine();  
  144.                 Console.WriteLine(ex.Message);  
  145.                 Console.WriteLine(ex.Source);  
  146.                 Console.WriteLine(ex.StackTrace);  
  147.                 Console.WriteLine();  
  148.             }  
  149.         }  
  150.     }  
  151. }  
原创粉丝点击