ArcGIS Server Web ADF缓冲区分析的开发

ArcGIS Server Web ADF开发不提供缓冲区分析的算法,但是ArcObject中提供了缓冲区相关接口ITopologicalOperator的Buffer方法。所以可以用ArcGIS Server远程调用ArcObject缓冲区分析功能。但是调用缓冲区分析功能后的,ADF需要实现缓冲区的表现,通常是采用通过Graphics Layer来实现。ADF通过MapResourceManage添加数据源的时候除了可以添加ArcGIS Server、ArcIMS、OGC等地图数据源外,还支持Graphics 数据源。ADF(或者说MapResourceManager)中的Graphics数据源可以支持两种图层,分别ElementGraphicsLayer 和 FeatureGraphicsLayer,两者都是System.Data.DataTable类型。用以下语句来添加要素图层Graphics数据源中:

  
  
  
  
  1. gResource.Graphics.Tables.Add(elementgraphicglayer); 

        ElementGraphicsLayer一般用来显示基本的图形元素,一个ElementGraphicsLayer可以同时存放不同几何类型(点、线、面等)的元素。一个典型的应用为用该图层来显示被选择的要素(Feature)。
        FeatureGraphicsLayer用来模拟要素图层并且存储要素的属性信息。所以可以在客户端根据属性来渲染不同的要素。一个FeatureGraphicsLayer图层只能存放一种几何类型的元素。FeatureGraphicsLayer也支持查询。
       Graphic图层中的元素都被存储在内存中,这点与服务器层的Graphic图层不同,后者将元素直接输出到图片上(可以在OutPut文件下查看)。
此外,MapResourceManager只能添加空的Graphics数据源,图层(GraphicsLayer)及元素(Graphic Element)的操作都必须通过编程实现。

       因此,首先要在VS中创建两个资源连接,一个是连接地图服务(ArcGIS Server Local),另一个是内存图像(GraphicsLayer),如下图,用来画缓冲图形。

 

然后添加MapResourceManager和Map控件,用代码添加文本框和缓冲区分析按钮如下:

  
  
  
  
  1. <table> 
  2.    <tr> 
  3.    <td>请输入城市名称</td> 
  4.    <td><input id ="TxtName" type ="text" /></td> 
  5.    <td><input id ="OkBut"  type ="button" value="缓冲分析" onclick ="GetAttriValue()"/> 
  6.        </td> 
  7.    </tr> 
  8.    </table> 

 下一步编写客户端向服务器传递消息,促使服务发生异步调用的Javascript函数,代码如下:

  
  
  
  
  1. <script language ="javascript" type="text/javascript">  
  2.         function GetAttriValue()  
  3.         {  
  4.             var name = document.getElementById("TxtName").value;  
  5.             if(name=='')  
  6.             {  
  7.                 alert('请输入城市名称');  
  8.                 document.getElementById("TxtName").focus();  
  9.                 return;  
  10.             }  
  11.         var message = name;    //传递消息  
  12.         var context = 'Map1';  
  13.         <%=m_ADFCallbackFunctionString%> //调用异步处理  
  14.           
  15.         }  
  16.     </script> 

下面是服务器端的相应源码:

  
  
  
  
  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13. using ESRI.ArcGIS.Carto;  
  14. using ESRI.ArcGIS.ADF;  
  15.  
  16. namespace BufferAanalyze  
  17. {  
  18.     public partial class _Default : System.Web.UI.Page, ICallbackEventHandler  
  19.     {  
  20.         public string m_ADFCallbackFunctionString;  
  21.         protected void Page_Load(object sender, EventArgs e)  
  22.         {  
  23.             m_ADFCallbackFunctionString = Page.ClientScript.GetCallbackEventReference(this"message",  
  24.            "processCallbackResult""context""postBackError"true);  
  25.         }  
  26.  
  27.         public void RaiseCallbackEvent(string eventargs)  
  28.         {  
  29.             System.Collections.IEnumerable func_enum = null;  
  30.             //获取当前map1控件中所有的functionality  
  31.             func_enum = Map1.GetFunctionalities();  
  32.  
  33.             System.Data.DataTable datatable;  
  34.             //对所有的functionality进行遍历  
  35.             foreach (ESRI.ArcGIS.ADF.Web.DataSources.IGISFunctionality gisfunctionality in func_enum)  
  36.             {  
  37.                 ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisresource = null;  
  38.                 //得到该functionality的resource  
  39.                 gisresource = gisfunctionality.Resource;  
  40.                 //判断该resource是否支持IQueryFunctionality   
  41.                 bool supported = false;  
  42.                 supported = gisresource.SupportsFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality));  
  43.                 if (supported)  
  44.                 {  
  45.                     ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qfunc;  
  46.                     qfunc = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisresource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);  
  47.  
  48.                     string[] lids;  
  49.                     string[] lnames;  
  50.                     //获得图层的layerId和layerName,GetQueryableLayers的重载方法可以指定图层类型  
  51.                     qfunc.GetQueryableLayers(nullout lids, out lnames);  
  52.  
  53.                     int selindex = -1;  
  54.                     for (int i = 0; i < lids.Length; i++)  
  55.                     {  
  56.                         if (lnames[i] == "行政区划")  
  57.                         {  
  58.                             //找到行政区划图层的序号  
  59.                             selindex = i;  
  60.                             break;  
  61.                         }  
  62.                     }  
  63.  
  64.                     if (selindex > -1)  
  65.                     {  
  66.                         //设置过滤器的过滤条件  
  67.                         ESRI.ArcGIS.ADF.Web.SpatialFilter spatialfilter = new ESRI.ArcGIS.ADF.Web.SpatialFilter();  
  68.                         spatialfilter.ReturnADFGeometries = true;  
  69.                         spatialfilter.MaxRecords = 100;  
  70.                         spatialfilter.WhereClause = "名称  like '%" + eventargs + "%'";  
  71.  
  72.                         //对指定的图层进行查询,查询的结果保存为  
  73.                         datatable = qfunc.Query(null, lids[selindex], spatialfilter);  
  74.                         if (datatable != null)  
  75.                         {  
  76.                             if (datatable.Rows.Count > 0)  
  77.                             {  
  78.                                 for (int j = 0; j < datatable.Columns.Count; j++)  
  79.                                 {  
  80.                                     if (datatable.Columns[j].DataType == typeof(ESRI.ArcGIS.ADF.Web.Geometry.Geometry))  
  81.                                     {  
  82.                                        //找到该点  
  83.                                         ESRI.ArcGIS.ADF.Web.Geometry.Geometry pnt = (ESRI.ArcGIS.ADF.Web.Geometry.Geometry)datatable.Rows[0][j];  
  84.                                           
  85.                                        //对该点进行缓冲分析  
  86.                                         //1,连接服务器  
  87.                                         ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection connection = new ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection();  
  88.                                         connection.Host = "localhost";  
  89.                                         connection.Connect();  
  90.  
  91.                                         //2获得服务器对象  
  92.                                         ESRI.ArcGIS.Server.IServerObjectManager pSOM = connection.ServerObjectManager;  
  93.                                         ESRI.ArcGIS.Server.IServerContext pSC = pSOM.CreateServerContext("地图""MapServer");//服务名和类型  
  94.                                         ESRI.ArcGIS.Carto.IMapServer mapserver = pSC.ServerObject as ESRI.ArcGIS.Carto.IMapServer;  
  95.                                           
  96.                                         //3使用服务器对象  
  97.                                         ESRI.ArcGIS.Carto.IMapServerObjects pMSO = (ESRI.ArcGIS.Carto.IMapServerObjects)mapserver;  
  98.                                         ESRI.ArcGIS.Geometry.IGeometry compnt = (ESRI.ArcGIS.Geometry.IGeometry)ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.ToIGeometry(pnt, pSC);//ValueObjectToComObject(pnt, pSC);  
  99.                                         ESRI.ArcGIS.Geometry.SpatialReferenceEnvironment sre = new ESRI.ArcGIS.Geometry.SpatialReferenceEnvironment();  
  100.                                           
  101.                                         ESRI.ArcGIS.Geometry.ISpatialReference pSR = sre.CreateGeographicCoordinateSystem(4326);  
  102.                                         compnt.SpatialReference = pSR;  
  103.                                         ESRI.ArcGIS.Geometry.ITopologicalOperator pTOPO = (ESRI.ArcGIS.Geometry.ITopologicalOperator)compnt;  
  104.                                           
  105.                                         pTOPO.Simplify();  
  106.                                         double bufdis = Map1.Extent.Width / 2;  
  107.                                         ESRI.ArcGIS.Geometry.IPolygon bufPoly = (ESRI.ArcGIS.Geometry.IPolygon)pTOPO.Buffer(1);  
  108.                                         ESRI.ArcGIS.ADF.ArcGISServer.PolygonN valuePoly = (ESRI.ArcGIS.ADF.ArcGISServer.PolygonN)ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.ComObjectToValueObject(bufPoly, pSC, typeof(ESRI.ArcGIS.ADF.ArcGISServer.PolygonN));  
  109.                                         ESRI.ArcGIS.ADF.Web.Geometry.Polygon adfpoly = (ESRI.ArcGIS.ADF.Web.Geometry.Polygon)ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.ToAdfPolygon(valuePoly);  
  110.                                          
  111.  
  112.                                         ESRI.ArcGIS.ADF.Web.Geometry.Geometry geom = adfpoly;  
  113.                                         ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement ge = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(geom, System.Drawing.Color.BlueViolet);  
  114.                                         Map1.Zoom(adfpoly);  
  115.                                          
  116.                                         ge.Symbol.Transparency = 70;  
  117.  
  118.                                         System.Collections.IEnumerable gfc = Map1.GetFunctionalities();  
  119.                                         ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource gmap = null;  
  120.  
  121.                                         foreach (ESRI.ArcGIS.ADF.Web.DataSources.IGISFunctionality gfunc in gfc)  
  122.                                         {  
  123.                                             if (gfunc.Resource.Name == "graph")  
  124.                                             {  
  125.                                                 gmap = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)gfunc.Resource;  
  126.                                             }  
  127.                                         }  
  128.  
  129.                                         if (gmap == nullreturn;  
  130.                                         ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer glayer = null;  
  131.                                         foreach (System.Data.DataTable dt in gmap.Graphics.Tables)  
  132.                                         {  
  133.                                             if (dt is ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)  
  134.                                             {  
  135.                                                 glayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)dt;  
  136.                                                 break;  
  137.                                             }  
  138.                                         }  
  139.  
  140.                                         if (glayer == null)  
  141.                                         {  
  142.                                             glayer = new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer();  
  143.                                             gmap.Graphics.Tables.Add(glayer);  
  144.                                         }  
  145.                                           
  146.                                         glayer.Clear();//清除以后数据  
  147.                                         glayer.Add(ge);  
  148.  
  149.                                         //4释放服务器对象  
  150.                                         pSC.ReleaseContext();  
  151.  
  152.                                         if (Map1.ImageBlendingMode == ESRI.ArcGIS.ADF.Web.UI.WebControls.ImageBlendingMode.WebTier)  
  153.                                             Map1.Refresh();  
  154.                                         else if (Map1.ImageBlendingMode == ESRI.ArcGIS.ADF.Web.UI.WebControls.ImageBlendingMode.Browser)  
  155.                                             Map1.RefreshResource(gmap.Name);  
  156.  
  157.  
  158.                                         m_ADFCallbackFunctionString = Map1.CallbackResults.ToString();  
  159.                                         return;  
  160.                                     }  
  161.                                 }  
  162.                             }  
  163.                             else 
  164.                             {  
  165.                                 object[] ob = new object[1];  
  166.                                 string sa = "alert('没有找到该城市');";  
  167.                                 ob[0] = sa;  
  168.                                 ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult callbackresult = new ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult(nullnull"JavaScript", ob);  
  169.                                 Map1.CallbackResults.Add(callbackresult);  
  170.                                 m_ADFCallbackFunctionString = Map1.CallbackResults.ToString();  
  171.  
  172.                             }  
  173.                         }  
  174.                     }  
  175.                 }  
  176.             }  
  177.  
  178.         }  
  179.  
  180.         public string GetCallbackResult()  
  181.         {  
  182.             return m_ADFCallbackFunctionString;  
  183.         }  
  184.  
  185.  
  186.     }  
  187. }  

运行结果如下图:

 

你可能感兴趣的:(Web,server,ADF,arcgis,缓冲区分析)