MapEasy开发体会(一)――切图

         MapEasy 是客户端程序,图片来源于服务器,之所以说 MapEasy 可以不依赖服务器而独立运行主要与她的取图方式有关。有两种方式,一是程序在运行时,适时的向服务器读取。二是一次性将所有图片读取到本地,然后便可不依赖服务器而独立运行。不管是哪种方式,图片来源都是服务器端,很多 GIS 厂商都有提供图片的服务器。
    介绍一个基于 IMS 服务器的取图程序。
首先简单说下 IMS 的出图方式
    向 IMS 服务器发送一个 HTTP 的请求( AJAX )―― > 然后发送一个已经构造好的 XML 串―― > 返回结果 XML 串―― > 解析―― > 得到当前图片地址―― > 将地址发送给服务器―― > 返回 BYTE 类型图片流
再说下总体思路
    初始化,获取配置参数―― > 根据参数构造返回图片的名称―― > 判断,若图片存在于本地(已生成过),则发送到浏览器―― > 若不存在,则向 IMS 请求取图―― > 将图片流保存到本地―― > 再发送到浏览器
    在网上有一个 ASP 写的取图程序,服务器就是 IMS 的。源代码在附件中,先在config.asp中配置好参数,然后共享arcims.aspconfig.asp这两个页面,访问arcims.asp共享页面便可出图。
如:"http://localhost/arcims.asp?z="+level+"&x="+column+"&y="+row
    下面是我基于以上取图程序的思路,用 ASP.NET 写的取图程序的源代码,以便于更好的学习和体会
注意:需要填加一个叫Interop.MSXML2.dll的引用
     访问地址时记得加参数
如:"http://localhost/ArcIMS.aspx?z="+level+"&x="+column+"&y="+row
 
ArcIMS.aspx
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;
  }

    }
}
Web.config
     < appSettings >
  <!-- 输出图片宽-->
   < add key ="image_width" value ="512" />
  <!-- 输出图片高-->
   < add key ="image_height" value ="512" />
  <!-- 主机名称-->
   < add key ="servername" value ="computer" />
  <!-- ims服务名称-->
   < add key ="mapservice" value ="transworld" />
  <!-- 图片类型-->
   < add key ="image_type" value ="gif" />
  <!-- 缓存图片名称-->
   < add key ="cache_name" value ="transworld" />
  <!-- 缓存图片路径-->
   < add key ="directory_path" value="e:\Inetpub\wwwroot\ArcIMS\pic\" />
  <!-- 缓存图片路径-->
   < add key ="coord" value ="longMin|0;latMin|-90;longMax|180;latMax|90" />
     </ appSettings >
 
 
 
 

你可能感兴趣的:(职场,休闲,切图,MapEasy,开发体会)