ArcGIS.Server.9.2.DotNet网络分析之最短路径分析

ArcGIS.Server.9.2.DotNet网络分析之最短路径分析 收藏
目的:
1.arcgis server9.2 ADF实现短路径分析,输入起点的名称和终点的名称然后分析出最短路径进行显示。
准备工作:
1.用ArcGis Server Manager或者ArcCatalog发布一个叫citys的Map Service,citys这个必须包含可以分析的网络数据集,关于网络数据集可以查网络上的资料这里不详细说了,发布的时候一定勾上Network Analysis这个选项,并且把这个Service启动起来。
完成后的效果图:

开始:
1.新建名为NetworkSamples的ASP.NET Web应用程序,新建FindPath.aspx页面,在页面上添加MapResourceManager1、Map1、Toolbar1控件。
2.为MapResourceManager1控件添加2个MapResourceItem,由上到下分别为,第一个(显示分析后的路径)名称:pathLayer、DataSourceType:GraphicsLayer;第二个(citys的Map Service)名称为:citys、DataSourceType:ArcGIS Server Local。
3.按着上图设置好Map1、Toolbar1等控件,在页面的右边添加2个input的输入框控件用来输入起点和终点的名称,加一个input 的按钮并且添加onclick事件用来实现按钮功能。具体html代码如下:

1起点:<br />
2<input id="Text1" type="text" value="宁夏" /><br />
3终点:<br />
4<input id="Text2" type="text" value="安徽" /><br />
5<input id="Button1" type="button" value="查找最短路径" onclick="search()" />
4.切换到cs的代码视图,实现 ICallbackEventHandler接口,代码如下:
 1public partial class FindPath : System.Web.UI.Page, ICallbackEventHandler
 2    {
 3        public string m_Callback = "";
 4        protected void Page_Load(object sender, EventArgs e)
 5        {
 6            m_Callback = Page.ClientScript.GetCallbackEventReference(Page, "argument", "processCallbackResult", "context", "processCallbackError", true);
 7           
 8        }
 9
10        ICallbackEventHandler 成员#region ICallbackEventHandler 成员
11        private string _callbackArg;
12        string ICallbackEventHandler.GetCallbackResult()
13        {
14            return RaiseCallbackEvent(_callbackArg);
15        }
16
17        void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
18        {
19            _callbackArg = eventArgument;
20        }
21
22        #endregion
23
24        private string RaiseCallbackEvent(string _callbackArg)
25        {
26        }
27}
5.切换到html视图编写js功能,主要是编写按钮的onclick方法search(),在head标签之间输入如下代码:
 1<script>
 2    function search()
 3    {
 4       //起点的名称
 5       var v1=document.getElementById("Text1").value;
 6       //终点的名称
 7       var v2=document.getElementById("Text2").value;
 8       var argument = "ControlID=Map1&ControlType=Map&Type=findPath&p1="+v1+"&p2="+v2;
 9       var context = "Map";
10       var rv=<%= m_Callback %>;
11       eval(rv);
12
13    }
14   
15    function processCallbackError()
16    {
17       alert(66);
18    }
19    </script>
6.当点击按钮执行search()时,会把起点和终点的名称作为请求字符串像服务端的发起请求,切换到代码视图编写代码处理search()发起的请求,代码和说明如下:

  1ICallbackEventHandler 成员#region ICallbackEventHandler 成员
  2        private string _callbackArg;
  3        //
  4        string ICallbackEventHandler.GetCallbackResult()
  5        {
  6            return RaiseCallbackEvent(_callbackArg);
  7        }
  8
  9        void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
 10        {
 11            _callbackArg = eventArgument;
 12        }
 13
 14        #endregion
 15
 16        private string RaiseCallbackEvent(string _callbackArg)
 17        {
 18            string v = "";
 19            //请求字符串
 20            NameValueCollection keyValColl = CallbackUtility.ParseStringIntoNameValueCollection(_callbackArg);
 21            if (keyValColl["Type"].ToString() == "findPath")
 22            {
 23                System.Text.StringBuilder sb = new System.Text.StringBuilder();
 24                //起点名称
 25                string Input1 = keyValColl["p1"];
 26                //终点名称
 27                string Input2 = keyValColl["p2"];
 28                //路径分析
 29                doFindPath(Input1, Input2);
 30            }
 31            //
 32            v = Map1.CallbackResults.ToString();
 33            return v;
 34        }
 35       
 36        private void doFindPath(string name1,string name2)
 37        {
 38            //ags的服务器名
 39            string SERVER_NAME = "jh-53a435fbc0e8";
 40            //ags里发布的Map Service名
 41            string ROUTE_SERVICE_NAME = "citys";
 42            //创建NAServerProxy
 43            NAServerProxy naServerProxy = NAServerProxy.Create(SERVER_NAME, ROUTE_SERVICE_NAME, null);
 44            if (naServerProxy == null)
 45            {
 46                naServerProxy.Dispose();
 47                throw (new System.Exception("Error"));
 48            }
 49            else
 50            {
 51                //获取网络层的名称
 52                string[] nLayers = naServerProxy.GetNALayerNames(esriNAServerLayerType.esriNAServerRouteLayer);
 53                //
 54                NAServerSolverParams solverParams = naServerProxy.GetSolverParameters(nLayers[0]) as NAServerSolverParams;
 55                //路由分析参数
 56                NAServerRouteParams routeParams = solverParams as NAServerRouteParams;
 57                //不返回地图
 58                routeParams.ReturnMap = false;
 59                //返回RouteGeometries
 60                routeParams.ReturnRouteGeometries = true;
 61                routeParams.ReturnStops = true;
 62                routeParams.ReturnDirections = true;
 63
 64                //设置起点PropertySet参数
 65                PointN point = QueryPoint(name1);
 66                PropertySet propSet = new PropertySet();
 67                PropertySetProperty[] propSetProperty_new = new PropertySetProperty[2];
 68                propSet.PropertyArray = propSetProperty_new;
 69
 70                PropertySetProperty propSetProperty = new PropertySetProperty();
 71                propSetProperty.Key = "Shape";
 72                propSetProperty.Value = point;
 73
 74                PropertySetProperty propSetProperty2 = new PropertySetProperty();
 75                propSetProperty2.Key = "Name";
 76                propSetProperty2.Value = name1;
 77
 78                propSet.PropertyArray[0] = propSetProperty;
 79                propSet.PropertyArray[1] = propSetProperty2;
 80
 81                //设置终点PropertySet参数
 82                PointN point2 = QueryPoint(name2);
 83                PropertySet propSet2 = new PropertySet();
 84                PropertySetProperty[] propSetProperty_new2 = new PropertySetProperty[2];
 85                propSet2.PropertyArray = propSetProperty_new2;
 86
 87                PropertySetProperty propSetProperty3 = new PropertySetProperty();
 88                propSetProperty3.Key = "Shape";
 89                propSetProperty3.Value = point2;
 90
 91                PropertySetProperty propSetProperty4 = new PropertySetProperty();
 92                propSetProperty4.Key = "Name";
 93                propSetProperty4.Value = name2;
 94
 95                propSet2.PropertyArray[0] = propSetProperty3;
 96                propSet2.PropertyArray[1] = propSetProperty4;
 97
 98                //设置Stops参数
 99                PropertySet[] propSets = new PropertySet[2];
100                propSets[0] = propSet;
101                propSets[1] = propSet2;
102                NAServerPropertySets StopsPropSets = new NAServerPropertySets();
103                StopsPropSets.PropertySets = propSets;
104                routeParams.Stops = StopsPropSets;
105                NAServerSolverResults solverResults;
106                try
107                {
108                    //进行分析
109                    solverResults = naServerProxy.Solve(solverParams);
110                    NAServerRouteResults RouteSolverResults = solverResults as NAServerRouteResults;
111                    //显示分析结果
112                    ShowResults(solverResults);
113                    //释放naServerProxy
114                    naServerProxy.Dispose();
115                }
116                catch(Exception e)
117                {
118                    //释放naServerProxy
119                    naServerProxy.Dispose();
120                }
121               
122            }
123        }
124
125        public void ShowResults(NAServerSolverResults solverResults)
126        {
127            NAServerRouteResults RouteSolverResults = solverResults as NAServerRouteResults;
128            //开始点终点路径显示
129            AddRoutesAndStops(RouteSolverResults);
130            //路径区域全屏显示
131            PolylineN polylineN = RouteSolverResults.RouteGeometries[0] as PolylineN;
132            EnvelopeN envelopeN = polylineN.Extent as EnvelopeN;
133            double width = envelopeN.XMax - envelopeN.XMin;
134            double height = envelopeN.YMax - envelopeN.YMin;
135            double fivePercent;
136            if (width > height)
137            {
138                fivePercent = width * .05;
139            }
140            else
141            {
142                fivePercent = height * .05;
143            }
144            envelopeN.XMin = envelopeN.XMin - fivePercent;
145            envelopeN.YMin = envelopeN.YMin - fivePercent;
146            envelopeN.XMax = envelopeN.XMax + fivePercent;
147            envelopeN.YMax = envelopeN.YMax + fivePercent;
148            Map1.Extent = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.ToAdfEnvelope(envelopeN);
149            Map1.Refresh();
150        }
151
152        private void AddRoutesAndStops(NAServerRouteResults rResult)
153        {
154            //分析结果路径
155            Polyline[] lines = rResult.RouteGeometries;
156            RecordSet stops = rResult.Stops;
157            //获取Buffer的MapFunctionality
158            ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality mapFunct = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality)Map1.GetFunctionality("pathLayer");
159            //获取Buffer的MapResource
160            ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource gResource = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)mapFunct.Resource;
161
162            //把buffer结果范围进行显示
163            ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer glayer = null;
164            //查找ElementGraphicsLayer在Buffer中
165            foreach (System.Data.DataTable dt in gResource.Graphics.Tables)
166            {
167                if (dt is ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)
168                {
169                    glayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)dt;
170                    break;
171                }
172
173            }
174            //如果Buffer中没有ElementGraphicsLayer就新增加一个ElementGraphicsLayer
175            if (glayer == null)
176            {
177                glayer = new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer();
178                gResource.Graphics.Tables.Add(glayer);
179            }
180            //清除ElementGraphicsLayer中的内容
181            glayer.Clear();
182            ESRI.ArcGIS.ADF.Web.Geometry.Geometry geom = (ESRI.ArcGIS.ADF.Web.Geometry.Geometry)ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.ToAdfPolyline((PolylineN)lines[0]);
183            //设置点显示
184            ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement ge = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(geom, System.Drawing.Color.Red);
185            //设置透明度
186            ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleLineSymbol sls = new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleLineSymbol();
187            sls.Width = 3;
188            sls.Color = System.Drawing.Color.Red;
189            sls.Type = ESRI.ArcGIS.ADF.Web.Display.Symbol.LineType.Dash;
190            sls.Transparency = 50;
191            ge.Symbol = sls;
192           // ge.Symbol.Transparency = 50;
193            //添加到Buffer中进行显示
194            glayer.Add(ge);
195
196            Record[] stopRecords = stops.Records;
197            int stopCount = stopRecords.Length;
198            for (int iStop = 0; iStop < stopCount; iStop++)
199            {
200                ESRI.ArcGIS.ADF.Web.Geometry.Geometry geom2 = (ESRI.ArcGIS.ADF.Web.Geometry.Geometry)ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.ToAdfPoint(stopRecords[iStop].Values[1] as PointN);
201                //设置点显示
202                ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement ge2 = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(geom2, System.Drawing.Color.Red);
203                //设置透明度
204               
205                ge2.Symbol.Transparency = 50;
206               
207                //添加到Buffer中进行显示
208                glayer.Add(ge2);
209            }
210        }
211
212        //按名称查找点
213        private PointN QueryPoint(string name)
214        {
215            PointN point = new PointN();
216            IEnumerable func_enum = Map1.GetFunctionalities();
217            DataTable dt = null;
218            foreach (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality gisfunctionality in func_enum)
219            {
220                if (gisfunctionality.Resource.Name == "citys")
221                {
222                    bool supported = false;
223                    ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisresource  = gisfunctionality.Resource;
224                    supported = gisresource.SupportsFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality));
225                   
226                    if (supported)
227                    {
228                        ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality qfunc=(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisresource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);
229                        string[] lids;
230                        string[] lnames;
231                        qfunc.GetQueryableLayers(null, out lids, out lnames);
232                        ESRI.ArcGIS.ADF.Web.SpatialFilter spatialfilter = new ESRI.ArcGIS.ADF.Web.SpatialFilter();
233                        spatialfilter.ReturnADFGeometries = false;
234                        spatialfilter.MaxRecords = 1;
235                        spatialfilter.WhereClause = "NAME LIKE  '" + name + "'";
236                        spatialfilter.Geometry = Map1.GetFullExtent();
237                        dt = qfunc.Query(null, lids[3], spatialfilter);
238                    }
239                }
240            }
241
242            DataRowCollection drs = dt.Rows;
243
244            int shpind = -1;
245            for (int i = 0; i < dt.Columns.Count; i++)
246            {
247                if (dt.Columns[i].DataType == typeof(ESRI.ArcGIS.ADF.Web.Geometry.Geometry))
248                {
249                    shpind = i;
250                    break;
251                }
252            }
253            foreach (DataRow dr in drs)
254            {
255                ESRI.ArcGIS.ADF.Web.Geometry.Point geom = (ESRI.ArcGIS.ADF.Web.Geometry.Point)dr[shpind];
256                //ESRI.ArcGIS.ADF.Web.Geometry.PointCollection points = geom.Points;
257                point.X = geom.X;
258                point.Y = geom.Y;
259
260            }
261            return point;
262        }
7.这样就可以测试查看效果了。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ybgiser/archive/2009/03/08/3969642.aspx

你可能感兴趣的:(网络,String,service,buffer,input,callback)