ArcGIS.Server.9.2.DotNet自带例子分析(六、二)

目的:
1.arcgis server9.2 ADF实现Buffer Select功能

准备工作:
1.(六、一)的工程,具体见前篇。

开始:
1.在Toolbar1中新建一个Tool,ClientAction属性为"Point" ;Name属性为"BufferSelect" ;ServerActionAssembly属性为SelectTool;ServerActionClass属性为SelectTool.BufferTool;Text属性为"Buffer Point" ;ToolTip属性为"Buffer Point" 。
2.新建BufferTool.cs文件用来实现BufferSelect Tool的功能,BufferSelect实现IMapServerToolAction接口,具体代码和说明如下:

  1void IMapServerToolAction.ServerAction(ToolEventArgs args)
  2        {
  3            //获取Map控件
  4            ESRI.ArcGIS.ADF.Web.UI.WebControls.Map mapctrl = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)args.Control;
  5            //点参数
  6            PointEventArgs pointargs = (PointEventArgs)args;
  7            //像素点
  8            System.Drawing.Point screenpoint = pointargs.ScreenPoint;
  9            //像素坐标转换成地理坐标
 10            ESRI.ArcGIS.ADF.Web.Geometry.Point mappoint = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(screenpoint.X, screenpoint.Y, mapctrl.GetTransformationParams(ESRI.ArcGIS.ADF.Web.Geometry.TransformationDirection.ToMap));
 11            //从Session["BufferDistance"]中获取冗余值
 12            string strbd = (string)mapctrl.Page.Session["BufferDistance"];
 13            float bufferdistance;
 14            //转成float型
 15            if (!Single.TryParse(strbd, out bufferdistance))
 16            {
 17                bufferdistance = 0.0F;
 18            }

 19
 20            //相互连接的直线或曲线
 21            System.Drawing.Drawing2D.GraphicsPath gpath = new System.Drawing.Drawing2D.GraphicsPath();
 22            //绘制椭圆
 23            gpath.AddEllipse((float)mappoint.X - (bufferdistance / 2), (float)mappoint.Y - (bufferdistance / 2), bufferdistance, bufferdistance);
 24            //Flatten的参数
 25            System.Drawing.Drawing2D.Matrix translateMatrix = new System.Drawing.Drawing2D.Matrix();
 26            translateMatrix.Translate(00);
 27
 28            //误差值,Flatten的参数
 29            float flattening = bufferdistance / 1000;
 30            //将gpath曲线转换成相连接的线段序列
 31            gpath.Flatten(translateMatrix, flattening);
 32
 33            //点集合
 34            ESRI.ArcGIS.ADF.Web.Geometry.PointCollection pc = new ESRI.ArcGIS.ADF.Web.Geometry.PointCollection();
 35            foreach (System.Drawing.PointF dpnt in gpath.PathPoints)
 36            {
 37                pc.Add(new ESRI.ArcGIS.ADF.Web.Geometry.Point(dpnt.X, dpnt.Y));
 38            }

 39
 40            //环形
 41            ESRI.ArcGIS.ADF.Web.Geometry.Ring ring = new ESRI.ArcGIS.ADF.Web.Geometry.Ring();
 42            ring.Points = pc;
 43            ESRI.ArcGIS.ADF.Web.Geometry.RingCollection rings = new ESRI.ArcGIS.ADF.Web.Geometry.RingCollection();
 44            rings.Add(ring);
 45            //多边形
 46            ESRI.ArcGIS.ADF.Web.Geometry.Polygon mappoly = new ESRI.ArcGIS.ADF.Web.Geometry.Polygon();
 47            mappoly.Rings = rings;
 48
 49            
 50            IEnumerable gfc = mapctrl.GetFunctionalities();
 51            ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource gResource = null;
 52            foreach (IGISFunctionality gfunc in gfc)
 53            {
 54                if (gfunc.Resource.Name == "Buffer")
 55                {
 56                    //获取Buffer
 57                    gResource = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)gfunc.Resource;
 58                }

 59            }

 60            if (gResource == null)
 61            {
 62                throw new Exception("Buffer Graphics layer not in MapResourceManager");
 63            }

 64
 65            ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer glayer = null;
 66            //查找ElementGraphicsLayer在Buffer中
 67            foreach (System.Data.DataTable dt in gResource.Graphics.Tables)
 68            {
 69                if (dt is ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)
 70                {
 71                    glayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)dt;
 72                    break;
 73                }

 74
 75            }

 76            //如果Buffer中没有ElementGraphicsLayer就新增加一个ElementGraphicsLayer
 77            if (glayer == null)
 78            {
 79                glayer = new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer();
 80                gResource.Graphics.Tables.Add(glayer);
 81            }

 82            //清除ElementGraphicsLayer中的内容
 83            glayer.Clear();
 84            
 85            ESRI.ArcGIS.ADF.Web.Geometry.Geometry geom = (ESRI.ArcGIS.ADF.Web.Geometry.Geometry)mappoly;
 86            //设置点显示
 87            ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement ge = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(geom, System.Drawing.Color.Green);
 88            //设置透明度
 89            ge.Symbol.Transparency = 70.0;
 90            //添加到Buffer中进行显示
 91            glayer.Add(ge);
 92            //目标图层名称
 93            string targetlayername = (string)mapctrl.Page.Session["TargetLayer"];
 94            //Data Layers Resource index为2
 95            int resource_index = 2;
 96            int layer_index = 0;
 97            //获取Data Layers的MapFunctionality
 98            ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality mf = (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality)mapctrl.GetFunctionality(resource_index);
 99            ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisresource = mf.Resource;
100            //是否支持QueryFunctionality
101            bool supported = gisresource.SupportsFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality));
102            if (supported)
103            {
104                //创建QueryFunctionality
105                ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qfunc = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisresource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);
106                string[] lids;
107                string[] lnames;
108                //查询图层的id和名称
109                qfunc.GetQueryableLayers(nullout lids, out lnames);
110
111                //获取目标图层的index
112                for (int i = 0; i < lnames.Length; i++)
113                {
114                    if (lnames[i] == targetlayername)
115                    {
116                        layer_index = i;
117                        break;
118                    }

119                }

120
121                //空间过滤
122                ESRI.ArcGIS.ADF.Web.SpatialFilter spatialfilter = new ESRI.ArcGIS.ADF.Web.SpatialFilter();
123                //是否返回地理元素
124                spatialfilter.ReturnADFGeometries = true;
125                //返回最大记录数
126                spatialfilter.MaxRecords = 1000;
127                spatialfilter.Geometry = mappoly;
128                //查询目标图层并且把结果存入DataTable
129                System.Data.DataTable datatable = qfunc.Query(null, lids[layer_index], spatialfilter);
130
131                ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource selresource = null;
132                //获取Selection
133                foreach (IGISFunctionality gfunc in gfc)
134                {
135                    if (gfunc.Resource.Name == "Selection")
136                    {
137                        selresource = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)gfunc.Resource;
138                    }

139                }

140                if (selresource == null)
141                {
142                    return;
143                }

144                ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer slayer = null;
145                //获取Selection的ElementGraphicsLayer
146                foreach (System.Data.DataTable dt in selresource.Graphics.Tables)
147                {
148                    if (dt is ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)
149                    {
150                        slayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)dt;
151                        break;
152                    }

153                }

154                //如果Selection的ElementGraphicsLayer为bull就新建ElementGraphicsLayer
155                if (slayer == null)
156                {
157                    slayer = new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer();
158                    selresource.Graphics.Tables.Add(slayer);
159                }

160                //清除Selection的ElementGraphicsLayer的内容
161                slayer.Clear();
162
163                DataRowCollection drs = datatable.Rows;
164
165                int shpind = -1;
166                for (int i = 0; i < datatable.Columns.Count; i++)
167                {
168                    if (datatable.Columns[i].DataType == typeof(ESRI.ArcGIS.ADF.Web.Geometry.Geometry))
169                    {
170                        shpind = i;
171                        break;
172                    }

173                }

174
175                try
176                {
177                    foreach (DataRow dr in drs)
178                    {
179                        ESRI.ArcGIS.ADF.Web.Geometry.Geometry sgeom = (ESRI.ArcGIS.ADF.Web.Geometry.Geometry)dr[shpind];
180                        ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement sge = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(sgeom, System.Drawing.Color.Yellow);
181                        //设置透明度
182                        sge.Symbol.Transparency = 50.0;
183                        //添加到Selection的ElementGraphicsLayer进行显示
184                        slayer.Add(sge);
185                    }

186                }

187                catch
188                {
189                    throw new Exception("No geometry available in datatable");
190                }

191                //获取CheckBox1的值
192                string cbxvalue = (string)mapctrl.Page.Session["CheckBox1Value"];
193                //获取GridView1
194                GridView gdview = (GridView)mapctrl.Page.FindControl("GridView1");
195                object[] oa = new object[1];
196                string showtable = "'hidden'";
197
198                if (bool.Parse(cbxvalue))
199                {
200                    //把查询结果作为GridView1数据源进行绑定
201                    gdview.DataSource = datatable;
202                    gdview.DataBind();
203
204                    string returnstring = null;
205
206                    using (System.IO.StringWriter sw = new System.IO.StringWriter())
207                    {
208                        HtmlTextWriter htw = new HtmlTextWriter(sw);
209                        gdview.RenderControl(htw);
210                        htw.Flush();
211                        returnstring = sw.ToString();
212                    }

213
214                    CallbackResult cr = new CallbackResult("div""griddiv""innercontent", returnstring);
215                    mapctrl.CallbackResults.Add(cr);
216
217                    if (datatable.Rows.Count > 1)
218                        showtable = "'visible'";
219
220                }

221                string sa = "var griddiv = document.getElementById('griddiv');";
222                sa += "griddiv.style.visibility = " + showtable + ";";
223                oa[0= sa;
224                CallbackResult cr1 = new CallbackResult(nullnull"javascript", oa);
225                mapctrl.CallbackResults.Add(cr1);
226
227                if (mapctrl.ImageBlendingMode == ImageBlendingMode.WebTier)
228                { mapctrl.Refresh(); }
229                else if (mapctrl.ImageBlendingMode == ImageBlendingMode.Browser)
230                {
231                    mapctrl.RefreshResource(gResource.Name);
232                    mapctrl.RefreshResource(selresource.Name);
233                }

234
235            }

236        }
3.在Default.aspx页面上添加TextBox1,用来buffer值的输入,当值输入完成后把这个值存入到Session["BufferDistance"]中,在Page_Load事件中为这个TextBox1添加onkeyup的脚本方法名为ChangeBufferContext()。
1protected void Page_Load(object sender, EventArgs e)
2        {
3                        
4            TextBox1.Attributes.Add("onkeyup""ChangeBufferContext()");
5        }
4.接下在编写脚本方法ChangeBufferContext,切换到html视图添加这个方法,代码和说明具体如下:
 1function ChangeClient()
 2         {
 3            var message;
 4            
 5            //目标图层选择
 6            if (context == 'DDLContext')
 7            {
 8              //获取选择值
 9              var ddl1value = document.getElementById('DropDownList1').value;
10              message = 'ddl';
11              message += ',' + ddl1value;  
12            }
 
13            
14            //是否在表格中显示数据
15            if (context == 'CheckBox')
16            {
17                var checkboxvalue = document.getElementById('CheckBox1').checked;
18                
19                message = 'checkbox';
20                message += ',' + checkboxvalue;
21            }

22            
23            //buffer值
24             if (context == 'BufferTextBox')
25             {
26                var bufboxvalue = document.getElementById('TextBox1').value;
27                
28                message = 'bufbox';
29                message += ',' + bufboxvalue;
30            
31            }
     
32            
33            
34            //回调方法执行服务端是事情     
35            <%=sCallBackFunctionInvocation%>
36         }

37         
38         //TextBox1的onkeyup事件
39         function ChangeBufferContext()
40         {
41            context = 'BufferTextBox'
42            ChangeClient();        
43         }
5.完成客户的js代码后还需要在服务端的RaiseCallbackEvent方法中对请求进行处理,具体代码和说明如下:
 1 //接受客户端的请求进行处理
 2        public void RaiseCallbackEvent(string eventArgs)
 3        {
 4            if (eventArgs.Contains("ddl"))
 5            {
 6                ChangeDropDownListServer(eventArgs);
 7            }

 8            else if (eventArgs.Contains("checkbox"))
 9            {
10                ChangeCheckServer(eventArgs);
11            }

12            else if (eventArgs.Contains("bufbox"))
13            {
14                ChangeBufferDistanceServer(eventArgs);
15            }

16        }

17
18        //保存Buffer值到到Session["BufferDistance"]中
19        public void ChangeBufferDistanceServer(string ea)
20        {
21            char[] parser_char = ',' };
22            string[] messages = ea.Split(parser_char);
23            string bufbox = messages[1];
24            if (bufbox != string.Empty)
25            {
26                Session["BufferDistance"= bufbox;
27            }

28        }
6.接下来在添加一个button用来实现ClearSelection功能,具体的html代码如下:
1<input type="button" ID="Button1" Style="left: 575px; value="Clear Selection" onclick="ChangeClearSelection()" />
7.还需要编写ChangeClearSelection脚本方法来实现这个功能,具体代码和说明如下:
 1//清除选择
 2         function ChangeClearSelection()
 3         {
 4            context = "ClearSelectionButton";
 5            ChangeClientADF();
 6        }

 7         
 8        function ChangeClientADF()
 9        {              
10            if (context == "ClearSelectionButton"){
11                message = "clearselectionbutton";
12            }
                 
13            <%=sADFCallBackFunctionInvocation%>            
14         }
       
8.同样还需要在服务端的RaiseCallbackEvent方法中对请求进行处理,具体代码和说明如下:
 1//接受客户端的请求进行处理
 2        public void RaiseCallbackEvent(string eventArgs)
 3        {
 4            if (eventArgs.Contains("ddl"))
 5            {
 6                ChangeDropDownListServer(eventArgs);
 7            }

 8            else if (eventArgs.Contains("checkbox"))
 9            {
10                ChangeCheckServer(eventArgs);
11            }

12            else if (eventArgs.Contains("bufbox"))
13            {
14                ChangeBufferDistanceServer(eventArgs);
15            }

16            else if (eventArgs.Contains("clearselectionbutton"))
17            {
18                ChangeClearSelection();
19            }

20        }

21
22        //清除
23        public void ChangeClearSelection()
24        {
25            IEnumerable gfc = Map1.GetFunctionalities();
26            ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource gResource = null;
27            foreach (IGISFunctionality gfunc in gfc)
28            {
29                if (gfunc.Resource is ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)
30                {
31                    gResource = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)gfunc.Resource;
32                    gResource.Graphics.Clear();
33                }

34            }

35
36            Map1.Refresh();
37
38            object[] oa = new object[1];
39            string showtable = "'hidden'";
40            string sa = "var griddiv = document.getElementById('griddiv');";
41            sa += "griddiv.style.visibility = " + showtable + ";";
42            oa[0= sa;
43            oa[0= sa;
44            CallbackResult cr1 = new CallbackResult(nullnull"javascript", oa);
45            Map1.CallbackResults.Add(cr1);
46
47            returnstring = Map1.CallbackResults.ToString();
48        }

49
9.这样就完成了这个例子,可以运行查看效果。
原文地址:https://www.cnblogs.com/hll2008/p/1275141.html