using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
namespace MapEasy
{
/// <summary>
/// ArcIMS 的摘要说明。
/// </summary>
public partial class ArcIMS : System.Web.UI.Page
{
private int x;
private int y;
private int z;
private string imageWidth;
private string imageHeight;
private string serverName;
private string mapService;
private string imageType;
private string cacheName;
private string dirPath;
private string tileName;
private long longMin;
private long latMin;
private long longMax;
private long latMax;
protected void Page_Load( object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
GetParams();
CreateImages();
}
}
Web 窗体设计器生成的代码 #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
}
#endregion
/// <summary>
/// 获取地图加载前的重要参数
/// </summary>
private void GetParams()
{
x = Convert.ToInt32(Request.QueryString[ "x"]);
y = Convert.ToInt32(Request.QueryString[ "y"]);
z = Convert.ToInt32(Request.QueryString[ "z"]);
imageWidth = ConfigurationSettings.AppSettings[ "image_width"];
imageHeight = ConfigurationSettings.AppSettings[ "image_height"];
serverName = ConfigurationSettings.AppSettings[ "servername"];
mapService = ConfigurationSettings.AppSettings[ "mapservice"];
imageType = ConfigurationSettings.AppSettings[ "image_type"];
cacheName = ConfigurationSettings.AppSettings[ "cache_name"];
dirPath = ConfigurationSettings.AppSettings[ "directory_path"];
string coord = ConfigurationSettings.AppSettings[ "coord"];
string [] Arr = coord.Split(';');
long long_min = Convert.ToInt32(Arr[0].Split('|')[1]);
long lat_min = Convert.ToInt32(Arr[1].Split('|')[1]);
long long_max = Convert.ToInt32(Arr[2].Split('|')[1]);
long lat_max = Convert.ToInt32(Arr[3].Split('|')[1]);
long MapWidth,MapWidth1,MapWidth2;
MapWidth1 = long_max - long_min;
MapWidth2 = lat_max - lat_min;
if( MapWidth1 < MapWidth2 )
{
MapWidth = MapWidth1;
} else
{
MapWidth = MapWidth2;
}
//x=0;y=0;z=1;
longMin = MapWidth/(( long)(Math.Pow(2,z-1))) * x + long_min;
latMin = MapWidth / (( long)(Math.Pow(2,z-1))) * (( long)(Math.Pow(2,z-1))-y-1) + lat_min;
longMax = longMin + MapWidth / (( long)(Math.Pow(2,z-1)));
latMax = latMin + MapWidth / (( long)(Math.Pow(2,z-1)));
}
/// <summary>
/// 取图
/// </summary>
private void CreateImages()
{
tileName = cacheName + "_" + x + "_" + y + "_" + z + "." + imageType;
string ContentType = GetMIMEType(tileName);
if(!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
if(File.Exists(dirPath+tileName))
{
//文件存在则直接从文件取图
FileStream fs= new FileStream(dirPath+tileName,FileMode.Open);
SendStreamToBrowser(fs, tileName, ContentType, true);
}
else
{
//文件不存在则向ims请求取图
string curl = "http://" + serverName + "/servlet/com.esri.esrimap.Esrimap?ServiceName=" + mapService;
string axl =GetArcXML();
string strResult = GetResult(curl,axl);
XmlDocument doc = new XmlDocument();
doc.LoadXml(strResult);
XmlNode xn = doc.SelectSingleNode("ARCXML").SelectSingleNode("RESPONSE").SelectSingleNode("IMAGE").SelectSingleNode("OUTPUT");
curl = xn.Attributes["url"].Value;
byte[] content = GetResult(curl);
SendStreamToBrowser(content, tileName, ContentType, true);
}
}
/// <summary>
/// 向客户端输出
/// </summary>
/// <param name="content">图片流</param>
/// <param name="FileName">图片名</param>
/// <param name="ContentType">图片输出类型</param>
/// <param name="IsInline">输出形式:true直接输出 false附件输出</param>
private void SendStreamToBrowser(byte[] content,string FileName,string ContentType,bool IsInline)
{
Response.Clear();
Response.AddHeader("Connection", "keep-alive");
Response.AddHeader("Content-Length", content.Length.ToString());
if(IsInline)
{
Response.AddHeader("Content-Disposition", "inline; filename=" + FileName);
}
else
{
Response.AddHeader("Content-Disposition","attachment; filename=" + FileName);
}
Response.ContentType = ContentType;
Response.BinaryWrite(content);
Response.Flush();
Response.Close();
}
/// <summary>
/// 向客户端输出
/// </summary>
/// <param name="fs">指向图片流的文件流</param>
/// <param name="FileName">图片名</param>
/// <param name="ContentType">图片输出类型</param>
/// <param name="IsInline">输出形式:true直接输出 false附件输出</param>
private void SendStreamToBrowser(FileStream fs ,string FileName,string ContentType,bool IsInline)
{
Response.Clear();
Response.AddHeader("Connection", "keep-alive");
Response.AddHeader("Content-Length", fs.Length.ToString());
if(IsInline)
{
Response.AddHeader("Content-Disposition", "inline; filename=" + FileName);
}
else
{
Response.AddHeader("Content-Disposition","attachment; filename=" + FileName);
}
Response.ContentType = ContentType;
byte[] content = new byte[fs.Length];
fs.Read(content,0,content.Length);
Response.BinaryWrite(content);
fs.Flush();
fs.Close();
Response.Flush();
Response.Close();
}
/// <summary>
/// 获取图片流
/// </summary>
/// <param name="curl">当前图片地址</param>
/// <returns>图片流</returns>
private byte[] GetResult(string curl)
{
FileStream fs = new FileStream(dirPath+tileName,FileMode.OpenOrCreate);
MSXML2.XMLHTTP xmlHttp = new MSXML2.XMLHTTPClass();
xmlHttp.open("GET",curl,false,"","");
xmlHttp.setRequestHeader("Accept-Lauguage","zh-cn");
// object obj =new object();
xmlHttp.send(null);
byte[] content =new byte[((byte[])xmlHttp.responseBody).Length+1];
if(xmlHttp.status ==200)
{
content=(byte[])xmlHttp.responseBody;
fs.Write(content, 0,content.Length);
fs.Flush();
fs.Close();
}
return content;
}
/// <summary>
/// 获取包含图片路径的xml
/// </summary>
/// <param name="curl">请求ims的http地址</param>
/// <param name="axl">向ims发送的xml串</param>
/// <returns>从ims得到的xml串</returns>
private string GetResult(string curl , string axl)
{
string strResult ="";
MSXML2.XMLHTTP xmlHttp = new MSXML2.XMLHTTPClass();
xmlHttp.open("POST",curl,false,"","");
xmlHttp.setRequestHeader("Accept-Lauguage","zh-cn");
try
{
xmlHttp.send(axl);
if(xmlHttp.status ==200)
{
strResult= xmlHttp.responseText;
}
}
catch(Exception ex)
{
return ex.Message;
}
return strResult;
}
/// <summary>
/// 构造向ims发送的xml串
/// </summary>
/// <returns>向ims发送的xml串</returns>
private string GetArcXML()
{
StringBuilder sb =new StringBuilder();
sb.Append("");
sb.Append("<ARCXML version=\"1.1\">");
sb.Append("<REQUEST>");
sb.Append("<GET_IMAGE autoresize =\"true\">");
sb.Append("<PROPERTIES>");
sb.Append("<BACKGROUND color=\"255,255,255\" transcolor=\"255,255,255\"/>");
sb.Append("<ENVELOPE minx=\""+ longMin +"\" miny=\""+ latMin +"\" maxx=\""+ longMax +"\" maxy=\""+ latMax +"\" />");
sb.Append("<IMAGESIZE height=\""+ imageHeight +"\" width=\""+ imageWidth +"\" />");
sb.Append("<OUTPUT type=\""+ imageType +"\" />");
sb.Append("</PROPERTIES>");
sb.Append("</GET_IMAGE>");
sb.Append("</REQUEST>");
sb.Append("</ARCXML>");
sb.Append("");
return sb.ToString();
}
/// <summary>
/// 构造输出内容的类型
/// </summary>
/// <param name="fName">类型</param>
/// <returns>类型串</returns>
private string GetMIMEType( string fName )
{
string strMIME="";
string strFileExt = fName.Substring(fName.LastIndexOf("."));
strFileExt = strFileExt.Remove(0,1);
switch(strFileExt.ToUpper())
{
case "TXT":
strMIME = "text/plain";
break;
case "TEXT":
strMIME = "text/plain";
break;
case "JS":
strMIME = "text/javascript";
break;
case "VBS":
strMIME = "text/plain";
break;
case "ASP":
strMIME = "text/plain";
break;
case "CGI":
strMIME = "text/plain";
break;
case "PL":
strMIME = "text/plain";
break;
case "NFO":
strMIME = "text/plain";
break;
case "ME":
strMIME = "text/plain";
break;
case "DTD":
strMIME = "text/plain";
break;
case "HTM":
strMIME = "text/html";
break;
case "HTML":
strMIME = "text/html";
break;
case "HTA":
strMIME = "text/html";
break;
case "HTX":
strMIME = "text/html";
break;
case "MHT":
strMIME = "text/html";
break;
case "CSV":
strMIME = "text/comma-separated-values";
break;
case "CSS":
strMIME = "text/css";
break;
case "PDF":
strMIME = "application/pdf";
break;
case "RTF":
strMIME = "application/rtf";
break;
case "XML":
strMIME = "text/xml";
break;
case "XSL":
strMIME = "text/xml";
break;
case "XSLT":
strMIME = "text/xml";
break;
case "WPD":
strMIME = "application/wordperfect";
break;
case "WRI":
strMIME = "application/mswrite";
break;
case "XLS":
strMIME = "application/msexcel";
break;
case "XLS3":
strMIME = "application/msexcel";
break;
case "XLS5":
strMIME = "application/msexcel";
break;
case "XLW":
strMIME = "application/msexcel";
break;
case "DOC":
strMIME = "application/msword";
break;
case "PPT":
strMIME = "application/mspowerpoint";
break;
case "PPS":
strMIME = "application/mspowerpoint";
break;
case "WML":
strMIME = "text/vnd.wap.wml";
break;
case "WMLS":
strMIME = "text/vnd.wap.wmlscript";
break;
case "WBMP":
strMIME = "image/vnd.wap.wbmp";
break;
case "WMLC":
strMIME = "application/vnd.wap.wmlc";
break;
case "WMLSC":
strMIME = "application/vnd.wap.wmlscriptc";
break;
case "GIF":
strMIME = "image/gif";
break;
case "JPG":
strMIME = "image/jpeg";
break;
case "JPE":
strMIME = "image/jpeg";
break;
case "JPEG":
strMIME = "image/jpeg";
break;
case "PNG":
strMIME = "image/png";
break;
case "BMP":
strMIME = "image/bmp";
break;
case "TIF":
strMIME = "image/tiff";
break;
case "TIFF":
strMIME = "image/tiff";
break;
case "AI":
strMIME = "application/postscript";
break;
case "EPS":
strMIME = "application/postscript";
break;
case "PS":
strMIME = "application/postscript";
break;
//Sound files
case "AU":
strMIME = "audio/basic";
break;
case "SND":
strMIME = "audio/basic";
break;
case "WAV":
strMIME = "audio/wav";
break;
case "RA":
strMIME = "audio/x-pn-realaudio";
break;
case "RM":
strMIME = "audio/x-pn-realaudio";
break;
case "RAM":
strMIME = "audio/x-pn-realaudio";
break;
case "MID":
strMIME = "audio/x-midi";
break;
case "MIDI":
strMIME = "audio/x-midi";
break;
case "MP3":
strMIME = "audio/mp3";
break;
case "M3U":
strMIME = "audio/m3u";
break;
//Video/Multimedia files
case "ASF":
strMIME = "video/x-ms-asf";
break;
case "AVI":
strMIME = "video/avi";
break;
case "MPG":
strMIME = "video/mpeg";
break;
case "MPEG":
strMIME = "video/mpeg";
break;
case "QT":
strMIME = "video/quicktime";
break;
case "MOV":
strMIME = "video/quicktime";
break;
case "QTVR":
strMIME = "video/quicktime";
break;
case "SWA":
strMIME = "application/x-director";
break;
case "SWF":
strMIME = "application/x-shockwave-flash";
break;
//Compressed/archives
case "ZIP":
strMIME = "application/x-zip-compressed";
break;
case "GZ":
strMIME = "application/x-gzip";
break;
case "RAR":
strMIME = "application/x-rar-compressed";
break;
//Miscellaneous
case "COM":
strMIME = "application/octet-stream";
break;
case "EXE":
strMIME = "application/octet-stream";
break;
case "DLL":
strMIME = "application/octet-stream";
break;
case "OCX":
strMIME = "application/octet-stream";
break;
}
return strMIME;
}
}
}