在《地图下载1之天地图瓦片解析》中我们分析了天地图瓦片的结构;在《地图下载2之天超图瓦片格式》中我们分析了超图的WMTS瓦片的结构,本文,我们大致来讲一下实现下载的过程。
指定分辨率
分辨率是固定的,根据需要下载的级别(1-20)获取对应分辨率:
/// <summary> /// 获取某一级别下的分辨率 /// </summary> /// <param name="level">级别</param> /// <returns>分辨率</returns> public static double Resolution(int level) { switch (level) { case 1: return 0.70312500015485435; case 2: return 0.35156250007742718; case 3: return 0.17578125003871359; case 4: return 0.0878906250193568; case 5: return 0.0439453125096784; case 6: return 0.0219726562548392; case 7: return 0.0109863281274196; case 8: return 0.0054931640637098; case 9: return 0.0027465820318549957; case 10: return 0.0013732910159274978; case 11: return 0.00068664549607834132; case 12: return 0.00034332275992416907; case 13: return 0.00017166136807812298; case 14: return 8.5830684039061379E-05; case 15: return 4.2915342019530649E-05; case 16: return 2.1457682893727977E-05; case 17: return 1.0728841446864E-05; case 18: return 5.3644207234319882E-06; case 19: return 2.6822103617159941E-06; case 20: return 1.341105180858E-06; default: return 0; } }
获取下载范围
根据需要下载的范围、级别及分辨率,获取地图瓦片起始、结束行列值(其他地图如百度地图、谷歌地图算法可能 不一样)
/// <summary> /// 根据范围获取行列号范围 /// </summary> /// <param name="resolution">分辨率</param> /// <param name="level">级别</param> /// <param name="xMin">X最小</param> /// <param name="xMax">X最大</param> /// <param name="yMin">Y最小</param> /// <param name="yMax">Y最大</param> /// <returns>起始、结束行列值</returns> public static int[] GetRowCol(double resolution,int level,double xMin,double xMax,double yMin,double yMax) { //计算所选范围地图瓦片起始、结束行列值 (天地图范围(-180,90,180,90),瓦片大小256) double buffer = 0; //double buffer = resolution * 0.5; int startX = (int)Math.Floor((xMin + 180+ buffer) / (256 * resolution)); int startY = (int)Math.Floor((90 - yMax+ buffer) / (256 * resolution)); int endX = (int)Math.Floor((xMax + 180- buffer) / (256 * resolution)); int endY = (int)Math.Floor((90 - yMin- buffer) / (256 * resolution)); return new int[] { startX, startY, endX, endY }; }
确定下载的类型
/// <summary> /// 天地图底图类型 /// </summary> public enum TdType { /// <summary> /// 影像注记(墨卡托,WGS1984) /// </summary> cia_w, /// <summary> /// 影像(墨卡托,WGS1984) /// </summary> img_w, /// <summary> ///街道注记(墨卡托,WGS1984) /// </summary> cva_w, /// <summary> /// 街道(墨卡托,WGS1984) /// </summary> vec_w, /// <summary> /// 影像注记(经纬度,CGCS2000) /// </summary> cia_c, /// <summary> /// 影像(经纬度,CGCS2000) /// </summary> img_c, /// <summary> /// 天地图街道注记(经纬度,CGCS2000) /// </summary> cva_c, /// <summary> /// 天地图街道(经纬度,CGCS2000) /// </summary> vec_c }
构造下载链接
下载所需要的参数前面都提到了。
/// <summary> /// 构造下载地址 /// </summary> /// <param name="tdType">类型</param> /// <param name="x">列号(0~n)</param> /// <param name="y">行号(0~n)</param> /// <param name="level">级别(1~20)</param> /// <returns>下载地址</returns> public static string Url(TdType tdType,int x,int y,int level) { return string.Format("http://t0.tianditu.com/DataServer?T={0}&x={1}&y={2}&l={3}", tdType,x,y, level); }
构造输出路径
超图的瓦片路径构造:
/// <summary> /// 构造超图瓦片路径 /// </summary> /// <param name="level">级另</param> /// <param name="row">行号</param> /// <param name="col">列号</param> /// <returns>相对路径</returns> public static string GetTileFilePath(int level, int row, int col) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(@"/"); //stringBuilder.Append(@"/JP_256_/"); stringBuilder.Append(level); stringBuilder.Append(@"/"); //超图行号 int value = (row < 0) ? (row / 128 - 1) : (row / 128); stringBuilder.Append(value); stringBuilder.Append("/"); //超图列号 value = ((col < 0) ? (col / 128 - 1) : (col / 128)); stringBuilder.Append(value); stringBuilder.Append("/"); stringBuilder.Append(row); stringBuilder.Append("x"); stringBuilder.Append(col); stringBuilder.Append(".jpg"); return stringBuilder.ToString(); }
接下来就是遍历下载、多线程优化、界面设计等问题了。各方面还需优化,不过能解决问题了!