static public string GetTDT(string layer, int level, int row, int col) { //过滤平面8-13列的请求 int maxCol = 8 * (int)Math.Pow(2, level); if (col >= maxCol) { return(null); } int tdtlevel = level + 3; string tdtlayer = layer; //string tdtlayer = GetTDTLayerName(layer, tdtlevel); if (tdtlayer == null) { return(null); } // string FolderPath = ConfigurationManager.AppSettings["CachePath"] + "\\256\\TDTSource" + "\\" + layer + "\\" + level; string path = FolderPath + "\\" + row + "_" + col + ".png"; if (File.Exists(path)) { return(path); } //构造绘制块Key string drawKey = "/TDTImage/256" + "/" + layer + "/" + level + "/" + row + "/" + col; try { // LockKey.Lock(drawKey); //创建文件夹 if (!Directory.Exists(FolderPath)) { Directory.CreateDirectory(FolderPath); } //天地图第3级行列号 int tdtrownum = 4; int tdtcolnum = 8; int tl = level; while (tl-- > 0) { tdtrownum *= 2; tdtcolnum *= 2; } int tdtrow = tdtrownum - row - 1; int tdtcol = col; Random r = new Random(); int ser = r.Next(8); //string url1 = "http://tile" + ser + ".tianditu.com/DataServer?T=" + tdtlayer + "&X=" + tdtcol + "&Y=" + tdtrow + "&L=" + tdtlevel; string url1 = ""; switch (tdtlayer) { case "img_c": url1 = "http://t" + ser + ".tianditu.cn/" + tdtlayer + "/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=c&TILEMATRIX=" + tdtlevel + "&TILEROW=" + tdtrow + "&TILECOL=" + tdtcol + "&FORMAT=tiles"; break; case "vec_c": url1 = "http://t" + ser + ".tianditu.cn/" + tdtlayer + "/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=c&TILEMATRIX=" + tdtlevel + "&TILEROW=" + tdtrow + "&TILECOL=" + tdtcol + "&FORMAT=tiles"; break; case "cva_c": url1 = "http://t" + ser + ".tianditu.cn/" + tdtlayer + "/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=c&TILEMATRIX=" + tdtlevel + "&TILEROW=" + tdtrow + "&TILECOL=" + tdtcol + "&FORMAT=tiles"; break; case "X0110_ZL_GJ": case "X0110_ZL_SJ": url1 = "http://t" + ser + ".tianditu.cn/DataServer?T=bou_c&X=" + tdtcol + "&Y=" + tdtrow + "&L=" + tdtlevel + ""; break; case "W1103_TMap": url1 = "http://t" + ser + ".tianditu.cn/DataServer?T=wat_c&X=" + tdtcol + "&Y=" + tdtrow + "&L=" + tdtlevel + ""; break; } try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url1); request.Timeout = Timeout; HttpWebResponse response = null; using (response = request.GetResponse() as HttpWebResponse) { if (response.StatusCode == HttpStatusCode.OK) { int len = (int)response.ContentLength; if (len == 2407) { path = null; //return "empty"; } else { using (Stream responseStream = response.GetResponseStream()) { BinaryReader reader = new BinaryReader(responseStream); byte[] byteArry = reader.ReadBytes(len); FileStream fs = File.Open(path, FileMode.Create); fs.Write(byteArry, 0, byteArry.Length); fs.Close(); } } } } } catch (Exception) { path = null; } } catch (Exception) { path = null; } finally { LockKey.UnLock(drawKey); } return(path); }
static public string GetAltitude(string dwsUrl, string classpath, string filter) { string path = null; try { string[] tmp = filter.Split(','); string level = tmp[2].Split(':')[1]; string row = tmp[0].Split(':')[1]; string col = tmp[1].Split(':')[1]; string sample = tmp[7].Split(':')[1]; string xmax = tmp[3].Split(':')[1]; string xmin = tmp[4].Split(':')[1]; string ymin = tmp[5].Split(':')[1]; string ymax = tmp[6].Split(':')[1]; filter = "row:" + row + ",col:" + col + ",level:" + level + ",xmax:" + xmax + ",xmin:" + xmin + ",ymin:" + ymin + ",ymax:" + ymax + ",sample:" + sample; string safeclasspath = Uri.EscapeDataString(classpath); string FolderPath = ConfigurationManager.AppSettings["DEMCachePath"].ToString() + "/" + sample + "/" + safeclasspath + "/" + level; path = FolderPath + "/" + row + "_" + col + ".db"; if (File.Exists(path)) { return(path); } //创建文件夹 if (!Directory.Exists(FolderPath)) { Directory.CreateDirectory(FolderPath); } string getKey = "/DCSDem/" + sample + "/" + classpath + "/" + level + "/" + row + "/" + col; LockKey.Lock(getKey); try { byte[] dem = GetTDData(dwsUrl, "altitude", classpath, filter); if (dem != null) { FileStream fs = File.Open(path, FileMode.Create); fs.Write(dem, 0, dem.Length); fs.Close(); return(path); } else { return(null); } } catch (Exception) { path = null; } finally { LockKey.UnLock(getKey); } } catch (Exception) { path = null; } return(path); }
// static public string GetMap(string dwsUrl, string docName, string mapName, int level, int row, int col, int size, string format) { string FolderPath = ConfigurationManager.AppSettings["CachePath"].ToString() + "\\" + size + "\\" + docName + "\\" + mapName + "\\" + level; string path = FolderPath + "\\" + row + "_" + col + ".png"; //查找缓存 FileInfo f = new FileInfo(path); if (f.Exists) { if (f.Length == 0 || f.Length == 1275) { return("empty"); } return(path); } //计算绘制等级,行列号,和范围 int uplevel = UP_LEVLE; int drawLevel = level - uplevel; double drawSize = LEVEL0_TILE_SIZE / Math.Pow(2, level); int drawPixelSize = size; int drawRow = row; int drawCol = col; while (uplevel-- > 0) { drawRow /= 2; drawCol /= 2; drawSize *= 2; drawPixelSize *= 2; } //构造绘制块Key string drawKey = "/DCSMap/" + size + "/" + docName + "/" + mapName + "/" + drawLevel + "/" + drawRow + "/" + drawCol; // string filepath = null; // try { LockKey.Lock(drawKey); //计算范围 double xmin = -180 + (drawCol * drawSize); double ymin = -90 + (drawRow * drawSize); double xmax = xmin + drawSize; double ymax = ymin + drawSize; // string box = xmin + "," + ymin + "," + xmax + "," + ymax; // DWS.dwService dws = new DWS.dwService(); dws.Url = dwsUrl; //DWS.MapQuery query = new DWS.MapQuery(); //query.box = box; //query.format = format; //query.classPath = ""; //query.mapDoc = docName; //query.mapName = mapName; //query.col = -1; //query.row = -1; //query.level = 0; //query.useTile = false; //query.width = drawPixelSize; //query.height = drawPixelSize; DWS.MapQuery query = new DWS.MapQuery(); query.format = "png"; query.classPath = ""; query.mapDoc = docName; query.mapName = mapName; query.col = col; query.row = row; query.level = (short)level; query.useTile = true; query.width = 256; query.height = 256; //开始绘制 DWS.MapInfo mi = dws.getMap(query); //创建文件夹 if (!Directory.Exists(FolderPath)) { Directory.CreateDirectory(FolderPath); } //绘制成功 if (mi.isSuccess && mi.image != null) { //开始的行列号 int sRow = drawRow; int sCol = drawCol; //瓦块行列数目 int rowNum = 1; int colNum = 1; uplevel = UP_LEVLE; while (uplevel-- > 0) { sRow *= 2; sCol *= 2; rowNum *= 2; colNum *= 2; } // hack, 850 byte and 6120 byte is empty if (mi.image.Length == 850 && mi.image.Length == 24072) { // 记录缓存 for (int nr = 0; nr < rowNum; ++nr) { for (int nc = 0; nc < colNum; ++nc) { //string tilepath = FolderPath + "/" + row + "_" + col + ".png"; string tilepath = FolderPath + "/" + (sRow + nr) + "_" + (sCol + nc) + ".png"; FileStream fs = File.Open(tilepath, FileMode.CreateNew); fs.Close(); } } } else { //请求到的图片 MemoryStream stream = new MemoryStream(mi.image); Bitmap fullimage = new Bitmap(stream); // Bitmap bmp = new Bitmap(size, size); Graphics g = Graphics.FromImage(bmp); // Rectangle destRect = new Rectangle(0, 0, size, size); Rectangle srcRect = new Rectangle(0, 0, size, size); //分割图片 for (int nr = 0; nr < rowNum; ++nr) { for (int nc = 0; nc < colNum; ++nc) { g.Clear(System.Drawing.Color.Transparent); srcRect.X = nc * size; srcRect.Y = drawPixelSize - (nr * size) - size; g.DrawImage(fullimage, destRect, srcRect, GraphicsUnit.Pixel); // string tilepath = FolderPath + "/" + (sRow + nr) + "_" + (sCol + nc) + ".png"; try { File.Delete(tilepath); bmp.Save(tilepath); } catch (Exception) { } } } } filepath = path; } else { // error filepath = null; } } catch (Exception) { filepath = null; } finally { LockKey.UnLock(drawKey); } return(filepath); }