ArcGIS Server中缓冲区分析的实现(点)

 

首先来看一下实现点缓冲区分析的效果图吧!

 

  

 

实现步骤如下:

1、 分别添加html控件:SelectText,两个 Button,;ID分别是selLayertxtDistancebtnDistanceClearBuffer。例外还添加一个DIV,并且在DIV中加入一个GridVview控件,ID分别是griddivGridView1,其中GridView1用来显示与缓冲区相交的图层要素的属性。如上图所示。

2、 在工具栏上添加一个tool工具,其javascript代码如下:

<esri:ToolDefaultImage="~/images/Buffer.jpg"JavaScriptFile="" DisabledImage="~/images/Buffer.gif"ServerActionAssembly="App_Code" HoverImage="~/images/Buffer.gif"ServerActionClass="BufferTool" ClientAction="Point" ToolTip="缓冲区" SelectedImage="~/images/Buffer.png" Name="Buffer" Text="Buffer"></esri:Tool>

3、其他的一些地图控件像Map,MapResourceManager,ToolBar,Toc,ScaleBar等,自己添加并设置关联即可。

4、分别单击连个button,其click事件的代码如下:

<script language="javascript" type="text/javascript">

function btnDistance_onclick()

{

 Var v=document.getElementById("txtDistance").value+"," +document.getElementById("selLayer").value;

         //生成请求字符串

         var argument = "ControlID=Map1&ControlType=Map&Type=Distance&EventArg=" + v;

         var context = "Map";

        //m_Callback 由服务端的Page_load事件中生成的,用于请求服务端的js代码

         var script=<%= m_Callback %>

        //用eval执行字符串

        eval(script);

}

 

function ClearBuffer_onclick()

{

         //生成请求字符串

        var argument = "ControlID=Map1&ControlType=Map&Type=BufferClear&EventArg=" + "";

         var context = "Map";

         //m_Callback 由服务端的Page_load事件中生成的,用于请求服务端的js代码

         var script=<%= m_Callback %>

        //用eval执行字符串

        eval(script);

}

 

 </script>

5、   后台的.cs文件,代码如下:   

public partial class _Default : System.Web.UI.Page, ICallbackEventHandler

{

 

    public string sADFCallBackFunctionInvocation;

     private string returnstring = "";

public string m_Callback = "";

   

protected void Page_Load(object sender, EventArgs e)

    {

  //生成客户端脚本段

        m_Callback = Page.ClientScript.GetCallbackEventReference(Page, "argument", "processCallbackResult", "context", true);

 

    }

 

 

   #region ICallbackEventHandler 成员

 

        private string _callbackArg;

        //负责把结果回传给客户端

        string ICallbackEventHandler.GetCallbackResult()

        {

           return RaiseCallbackEvent(_callbackArg);

           

            return returnstring;

        }

 

        //负责从客户端javascript传来的参数

        void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)

        {

            _callbackArg = eventArgument;

 

        }

 

 

 public void RaiseCallbackEvent( string _callbackArg)

    {

     string v = "";

  

     NameValueCollection keyValColl = CallbackUtility.ParseStringIntoNameValueCollection(_callbackArg);

 

          if (keyValColl["Type"].ToString() == "Distance")

             {

                    string str = keyValColl["EventArg"];

                       

                        string []str1=str.Split(',');

                        Session["BufferDistance"]=str1[0];

 

                        Session["Layer"] = str1[1];

 

                }

 

                else if (keyValColl["Type"].ToString() == "BufferClear")

                    {                        

 

                        IEnumerable gfc = Map1.GetFunctionalities();

                        ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource gResource = null;

              foreach (IGISFunctionality gfunc in gfc)

              {

                   if (gfunc.Resource is ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)

                            {

                                gResource = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)gfunc.Resource;

                                gResource.Graphics.Clear();

                            }

               }

 

                        Map1.Refresh();

 

                       object[] oa = new object[1];

                        string showtable = "'hidden'";

                        string sa = "var griddiv = document.getElementById('griddiv');";

                        sa += "griddiv.style.visibility = " + showtable + ";";

                        oa[0] = sa;

                        oa[0] = sa;

                        CallbackResult cr1 = new CallbackResult(null, null, "javascript", oa);

                        Map1.CallbackResults.Add(cr1);

 

                      

                      v = Map1.CallbackResults.ToString();

 

                      return v;

 

                    }

  

  

    }

 

 

6、接下来,添加一个类文件命名为:BufferTool.cs,其中的代码如下:

 

public class BufferTool : IMapServerToolAction

{

    #region IMapServerToolAction Members

 

    void IMapServerToolAction.ServerAction(ToolEventArgs args)

    {

        ESRI.ArcGIS.ADF.Web.UI.WebControls.Map mapctrl = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)args.Control;

 

        try

        {

            PointEventArgs pointargs = (PointEventArgs)args;

            System.Drawing.Point screenpoint = pointargs.ScreenPoint;

 

            ESRI.ArcGIS.ADF.Web.Geometry.Point mappoint = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(screenpoint.X, screenpoint.Y, mapctrl.Extent, (int)mapctrl.Width.Value, (int)mapctrl.Height.Value);

 

          string strbd = mapctrl.Page.Session["BufferDistance"].ToString();

 

        

            float bufferdistance;

            if (!Single.TryParse(strbd, out bufferdistance))

            {

                bufferdistance = 0.0F;

            }

         

            System.Drawing.Drawing2D.GraphicsPath gpath = new System.Drawing.Drawing2D.GraphicsPath();

            gpath.AddEllipse((float)mappoint.X - (bufferdistance / 2), (float)mappoint.Y - (bufferdistance / 2), bufferdistance, bufferdistance);

            System.Drawing.Drawing2D.Matrix translateMatrix = new System.Drawing.Drawing2D.Matrix();

            translateMatrix.Translate(0, 0);

 

            float flattening = bufferdistance / 1000; //0.005F; //decimal degrees --- 100F; //meters,feet

            gpath.Flatten(translateMatrix, flattening);

 

            ESRI.ArcGIS.ADF.Web.Geometry.PointCollection pc = new ESRI.ArcGIS.ADF.Web.Geometry.PointCollection();

            foreach (System.Drawing.PointF dpnt in gpath.PathPoints)

            {

                pc.Add(new ESRI.ArcGIS.ADF.Web.Geometry.Point(dpnt.X, dpnt.Y));

            }

 

            ESRI.ArcGIS.ADF.Web.Geometry.Ring ring = new ESRI.ArcGIS.ADF.Web.Geometry.Ring();

            ring.Points = pc;

            ESRI.ArcGIS.ADF.Web.Geometry.RingCollection rings = new ESRI.ArcGIS.ADF.Web.Geometry.RingCollection();

            rings.Add(ring);

            ESRI.ArcGIS.ADF.Web.Geometry.Polygon mappoly = new ESRI.ArcGIS.ADF.Web.Geometry.Polygon();

            mappoly.Rings = rings;

 

            IEnumerable gfc = mapctrl.GetFunctionalities();

            ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource gResource = null;

            foreach (IGISFunctionality gfunc in gfc)

            {

                if (gfunc.Resource.Name == "Buffer")

                {

                    gResource = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)gfunc.Resource;

                }

            }

 

            if (gResource == null)

                throw new Exception("Buffer Graphics layer not in MapResourceManager");

 

            ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer glayer = null;

 

            foreach (System.Data.DataTable dt in gResource.Graphics.Tables)

            {

                if (dt is ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)

                {

                    glayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)dt;

                    break;

                }

 

            }

 

            if (glayer == null)

            {

                glayer = new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer();

                gResource.Graphics.Tables.Add(glayer);

            }

 

            glayer.Clear();

 

            ESRI.ArcGIS.ADF.Web.Geometry.Geometry geom = (ESRI.ArcGIS.ADF.Web.Geometry.Geometry)mappoly;

            ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement ge = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(geom, System.Drawing.Color.Green);

            ge.Symbol.Transparency = 70.0;

            glayer.Add(ge);

 

            // Construct selection layer

            string targetlayername = (string)mapctrl.Page.Session["Layer"];

 

            int resource_index = 2;

            int layer_index = 0;

 

            ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mf = (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality)mapctrl.GetFunctionality(resource_index);

            ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisresource = mf.Resource;

            bool supported = gisresource.SupportsFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality));

 

            if (supported)

            {

                ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qfunc = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisresource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);

 

                string[] lids;

                string[] lnames;

                qfunc.GetQueryableLayers(null, out lids, out lnames);

 

                for (int i = 0; i < lnames.Length; i++)

                {

                    if (lnames[i] == targetlayername)

                    {

                        layer_index = i;

                        break;

                    }

                }

 

                ESRI.ArcGIS.ADF.Web.SpatialFilter spatialfilter = new ESRI.ArcGIS.ADF.Web.SpatialFilter();

                spatialfilter.ReturnADFGeometries = true;

                spatialfilter.MaxRecords = 1000;

                spatialfilter.Geometry = mappoly;

 

                System.Data.DataTable datatable = qfunc.Query(null, lids[layer_index], spatialfilter);

 

                #region Graphics Converter Usage

                /*** To use the ADF Converter to create a Graphics layer from DataTable, use the following code

                ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsLayer graphicslayer = Converter.ToGraphicsLayer(datatable, Color.Yellow, Color.Yellow);

                ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource selresource = null;

                foreach (IGISFunctionality gfunc in gfc)

                {

                    if (gfunc.Resource.Name == "Selection")

                    {

                        selresource = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)gfunc.Resource;

                        selresource.Graphics.Tables.Clear();

                    }

                }

                if (selresource == null)

                    return;

 

                selresource.Graphics.Tables.Add(graphicslayer);

                //*/

                #endregion

 

                #region Explicit Graphics Layer Usage

                //*** To work with a Graphics Layer explicitly, and manage it within a Graphics dataset, use the following code

 

                ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource selresource = null;

 

                foreach (IGISFunctionality gfunc in gfc)

                {

                    if (gfunc.Resource.Name == "Selection")

                    {

                        selresource = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)gfunc.Resource;

                    }

                }

 

                if (selresource == null)

                    return;

 

                ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer slayer = null;

 

                foreach (System.Data.DataTable dt in selresource.Graphics.Tables)

                {

                    if (dt is ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)

                    {

                        slayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)dt;

                        break;

                    }

                }

 

                if (slayer == null)

                {

                    slayer = new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer();

                    selresource.Graphics.Tables.Add(slayer);

                }

 

                slayer.Clear();

 

                DataRowCollection drs = datatable.Rows;

 

                int shpind = -1;

                for (int i = 0; i < datatable.Columns.Count; i++)

                {

                    if (datatable.Columns[i].DataType == typeof(ESRI.ArcGIS.ADF.Web.Geometry.Geometry))

                    {

                        shpind = i;

                        break;

                    }

                }

 

                try

                {

                    foreach (DataRow dr in drs)

                    {

                        ESRI.ArcGIS.ADF.Web.Geometry.Geometry sgeom = (ESRI.ArcGIS.ADF.Web.Geometry.Geometry)dr[shpind];

                        ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement sge = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(sgeom, System.Drawing.Color.Red);

                        sge.Symbol.Transparency = 50.0;

                        slayer.Add(sge);

                    }

                }

                catch

                {

                    throw new Exception("No geometry available in datatable");

                }

                //*/

                #endregion

 

                //string cbxvalue = (string)mapctrl.Page.Session["CheckBox1Value"];

                GridView gdview = (GridView)mapctrl.Page.FindControl("GridView1");

 

                // Is gridview div visible or hidden

                object[] oa = new object[1];

                string showtable = "'hidden'";

 

                //if (bool.Parse(cbxvalue))

                //{

                    // display table of selected attributes

                    gdview.DataSource = datatable;

                    gdview.DataBind();

 

                    string returnstring = null;

 

                    using (System.IO.StringWriter sw = new System.IO.StringWriter())

                    {

                        HtmlTextWriter htw = new HtmlTextWriter(sw);

                        gdview.RenderControl(htw);

                        htw.Flush();

                        returnstring = sw.ToString();

                    }

 

                    CallbackResult cr = new CallbackResult("div", "griddiv", "innercontent", returnstring);

                    mapctrl.CallbackResults.Add(cr);

 

                   

                      showtable = "'visible'";

 

               

 

                // set visibility of griddiv

                string sa = "var griddiv = document.getElementById('griddiv');";

                sa += "griddiv.style.visibility = " + showtable + ";";

                oa[0] = sa;

                CallbackResult cr1 = new CallbackResult(null, null, "javascript", oa);

                mapctrl.CallbackResults.Add(cr1);

 

                if (mapctrl.ImageBlendingMode == ImageBlendingMode.WebTier)

                { mapctrl.Refresh(); }

                else if (mapctrl.ImageBlendingMode == ImageBlendingMode.Browser)

                {

                    mapctrl.RefreshResource(gResource.Name);

                    mapctrl.RefreshResource(selresource.Name);

                }

            }

        }

        catch (Exception e)

        {

            mapctrl.Refresh();

            System.Diagnostics.Debug.WriteLine("Exception: " + e.Message);

            return;

        }

    }

 

    #endregion

}

7、差不多就是这些了,一些命名空间自己添加吧。运行下,看效果吧。

此例子版权归千秋一统哦!

 

 

 

 

 

 

  

 

 

    

一起学习GIS及其二次开发,一起进步!
原文地址:https://www.cnblogs.com/tuncaysanli/p/1336250.html