private void GetMap2(HttpContext context) { HttpRequest req = context.Request; HttpResponse resp = context.Response; string docName = req.QueryString["mapDoc"]; string mapName = req.QueryString["mapName"]; string strwidth = req.QueryString["width"]; string metadataId = req.QueryString["metadataid"]; string classpath = req.QueryString["classpath"]; string strlevel = req.QueryString["level"]; string strrow = req.QueryString["row"]; string strcol = req.QueryString["col"]; if (strlevel == null || strcol == null || strrow == null) { // string strbox = req.QueryString["box"]; string strheight = req.QueryString["height"]; strwidth = req.QueryString["width"]; if (strbox != null) { string[] tmp = strbox.Split(','); double xmin = Double.Parse(tmp[0]); double ymin = Double.Parse(tmp[1]); double xmax = Double.Parse(tmp[2]); double ymax = Double.Parse(tmp[3]); int h = Int32.Parse(strheight); int w = Int32.Parse(strwidth); string dcs = GetDCS(); Bitmap bitmap = null; if (docName != null && mapName != null) { bitmap = MapHelper.GetMapByBox(dcs, docName, mapName, xmin, xmax, ymin, ymax, w, h, 256, "Png"); } if (classpath == null) { classpath = req.Form["classpath"]; } if (classpath != null && metadataId != null) { bitmap = MapHelper.GetMetadataMapByBox(dcs, metadataId, classpath, xmin, xmax, ymin, ymax, w, h, 256, "Png"); } resp.StatusCode = 200; resp.ContentType = "image/jpeg"; resp.Buffer = true; bitmap.Save(resp.OutputStream, System.Drawing.Imaging.ImageFormat.Png); } return; } if (strwidth == null) { strwidth = "256"; } strwidth = "256"; int level = int.Parse(strlevel); int row = int.Parse(strrow); int col = int.Parse(strcol); int size = int.Parse(strwidth); //获取dws的URL string soapaddr = GetDCS(); string path = null; if ((mapName == null || docName == null) && metadataId != null) { if (classpath == null) { classpath = req.Form["classpath"]; } if (classpath != null) { path = MapHelper.GetMetadataMap(soapaddr, metadataId, classpath, level, row, col, 256, "png");// .GetMap(soapaddr, docName, mapName, level, row, col, size, "png"); } } else { //path = MapHelper.GetMap(soapaddr, docName, mapName, level, row, col, size, "png"); DWS.dwService dws = new DWS.dwService(); dws.Url = soapaddr; DWS.MapQuery query = new DWS.MapQuery(); query.format = "png"; query.classPath = ""; query.mapDoc = docName; query.col = col; query.row = row; query.level = (short)level; query.useTile = true; query.width = 256; query.height = 256; if (mapName == "NULL") { query.version = 1; query.mapName = ""; query.useTile = true; } //开始绘制 DWS.MapInfo mi = dws.getMap(query); //绘制成功 if (mi.isSuccess && mi.image != null) { resp.Clear(); resp.ClearHeaders(); resp.ClearContent(); resp.ContentType = "image/jpeg"; resp.Buffer = true; resp.StatusCode = 200; System.IO.MemoryStream ms = new System.IO.MemoryStream(mi.image); Bitmap fullimage = new Bitmap(ms); fullimage.Save(resp.OutputStream, System.Drawing.Imaging.ImageFormat.Png); //展现图片 ms.Dispose(); return; } } //resp.Clear(); //resp.ClearHeaders(); //resp.ClearContent(); //if (path == null) //{ // // // resp.StatusCode = 404; //} //else if (path == "empty") //{ // // // resp.StatusCode = 204; //} //else //{ // resp.StatusCode = 200; // resp.WriteFile(path); //} //long count = 0; //string openStr = docName + "|" + mapName; //try //{ // if (dataDic.ContainsKey(openStr)) // { // dataDic.TryGetValue(openStr, out count); // count++; // } // dataDic[openStr] = count; //} //catch (Exception countEx) //{ // dataDic = new System.Collections.Generic.Dictionary<string, long>(); // dataDic.Add(openStr, count); //} //string time = DateTime.Now.ToString(); //string respHead = "本次连接状态:" + resp.StatusCode + " " + resp.StatusDescription; //string remoteIp = req.UserHostAddress; //string requestData = "请求数据:" + openStr + "\r\n本次服务器开机该数据访问计数:" + count + "\r\n分块编码:" + strlevel + "级 " + strrow + "行 " + strcol + "列"; //string info = time + "\r\n远程用户:" + remoteIp + "\r\n" + requestData + "\r\n" + respHead; //try //{ // MapLog.WriteLine(info + "\r\n"); //} //catch (Exception) //{ //} }
// 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); }