阅读:2015回复:0
C#2008+ArcGIS Mobile实现路口定位
第一步:在道路图层中,通过模糊查询获取道路列表,并选取所需的道路,C#代码如下:<br><br> <br>代码<br> 1 /// <summary><br> 2 /// 模糊查询道路<br> 3 /// </summary><br> 4 /// <param name="_name">道路名关键字</param><br> 5 /// <param name="_ms">待操作的mobileservice</param><br> 6 /// <param name="_layername">操作的图层名</param><br> 7 /// <param name="_lb">填充结果的ListBox</param><br> 8 /// <param name="_lg">获取的geometry集合</param><br> 9 public static void SearchRoadList(string _name, MobileService _ms, string _layername, ListBox _lb,out List<Geometry> _lg)<br>10 {<br>11 _lb.Items.Clear();<br>12 if (String.IsNullOrEmpty(_name))<br>13 {<br>14 _lg = null;<br>15 return;<br>16 }<br>17 <br>18 MobileServiceLayer msl = _ms.Layers[_layername] as MobileServiceLayer;<br>19 FeatureLayer fl = msl as ESRI.ArcGIS.Mobile.MobileServices.FeatureLayer;<br>20 string qstr = "NAME Like '%" + _name + "%'";//过滤内容<br>21 <br>22 QueryFilter qf = new QueryFilter(qstr, true);//过滤器<br>23 <br>24 using (FeatureDataReader fdr = fl.GetDataReader(qf))<br>25 {<br>26 _lg = new List<Geometry>();<br>27 while (fdr.Read())<br>28 {<br>29 Geometry geo = fdr.GetGeometry() as ESRI.ArcGIS.Mobile.Geometries.Geometry;<br>30 _lg.Add(geo);<br>31 string id = Convert.ToString(fdr["OBJECTID"]);<br>32 string name = Convert.ToString(fdr["NAME"]);<br>33 _lb.Items.Add(id + "-" + name);<br>34 }<br>35 }<br>36 if (_lb.Items.Count == 0)<br>37 {<br>38 MessageBox.Show("搜寻结果为空!", "提示");<br>39 }<br>40 <br>41 <br>42 }<br><br> <br><br>这里除了获取名称以外,也获取了道路的ID,这是因为在实际的数据中可能会有多条道路Polyline的Name相同,但ID不同,下面主要是通过 ID定位道路<br><br> 在ListBox有查询结果,选择道路后,可以从上面的List<Geometry> _lg中获取对应的Geometry,下面另外附了一段代码,是通过ID获取道路的Geometry的:<br><br> <br>代码<br> 1 /// <summary><br> 2 /// 根据ID获取道路的Geometry<br> 3 /// </summary><br> 4 /// <param name="_roadid">道路ID</param><br> 5 /// <param name="_layername">道路操作图层</param><br> 6 /// <param name="_ms">要操作的MobileService</param><br> 7 /// <returns></returns><br> 8 public static ESRI.ArcGIS.Mobile.Geometries.Geometry GetGeometry(string _roadid, string _layername, MobileService _ms)<br> 9 {<br>10 MobileServiceLayer msl = _ms.Layers[_layername] as MobileServiceLayer;<br>11 FeatureLayer fl = msl as FeatureLayer;<br>12 string qstr = "OBJECTID=" + _roadid;<br>13 QueryFilter qf = new QueryFilter(qstr, true);<br>14 ESRI.ArcGIS.Mobile.Geometries.Geometry G = null;<br>15 try<br>16 {<br>17 using (FeatureDataReader fdr = fl.GetDataReader(qf))<br>18 {<br>19 while (fdr.Read())<br>20 {<br>21 G = fdr.GetGeometry() as ESRI.ArcGIS.Mobile.Geometries.Geometry;<br>22 return G;<br>23 }<br>24 }<br>25 return G = null;<br>26 }<br>27 catch (Exception ex)<br>28 {<br>29 MessageBox.Show(ex.Message, "错误");<br>30 return G = null;<br>31 }<br>32 }<br><br> <br><br>第二步,分析处理两个道路的Geometry,得到路口,并在图中高亮显示出来,C#代码如下,我的表述能力不怎么好,希望通过代码来弥补<br><br> <br><br> <br>代码<br> 1 /// <summary><br> 2 /// 高亮显示路口<br> 3 /// </summary><br> 4 /// <param name="mainroadid">主路的道路ID</param><br> 5 /// <param name="crossroadid">跨越道路ID</param><br> 6 /// <param name="layername">操作的图层名</param><br> 7 /// <param name="ms">操作的MobileService</param><br> 8 /// <param name="mp">操作的MobileMap</param><br> 9 /// <param name="cc">搜集到的CoordinateCollection</param><br>10 /// <param name="IsMacth">是否超过一个路口</param><br>11 public static void HightLightRoadCorner(Geometry mainGeo,Geometry crossGeo, string layername, MobileService ms, ESRI.ArcGIS.Mobile.Map mp, out CoordinateCollection cc, bool IsTouch)<br>12 {<br>13 try<br>14 {<br>15 //Geometry mainGeo = GetGeometry(mainroadid, layername, ms);<br>16 if (mainGeo == null)<br>17 {<br>18 MessageBox.Show("不能获取对应道路,请检查", "提示");<br>19 cc = null;<br>20 return;<br>21 }<br>22 <br>23 //Geometry crossGeo = GetGeometry(crossroadid, layername, ms);<br>24 if (crossGeo == null)<br>25 {<br>26 MessageBox.Show("不能获取第二条道路, 请检查", "提示");<br>27 cc = null;<br>28 return;<br>29 }<br>30 GeometricRelationshipType _type;<br>31 if (IsTouch == true)<br>32 _type = GeometricRelationshipType.Touch;//两条道路有两个或以上的路口<br>33 else<br>34 _type = GeometricRelationshipType.Cross;//两条道路有一个路口<br>35 <br>36 bool IsCross = mainGeo.Relate(crossGeo, _type);//判断两者是否相交<br>37 <br>38 if (IsCross == true)<br>39 {<br>40 IList<CoordinateCollection> crossRoadParts = crossGeo.Parts;<br>41 IList<Coordinate> crossRoadCoordinate = crossRoadParts[0];<br>42 CoordinateCollection tmpCollect = new CoordinateCollection();<br>43 <br>44 if (crossRoadCoordinate.Count != 0)<br>45 {<br>46 for (int i = 0; i < crossRoadCoordinate.Count; i++)<br>47 {<br>48 ESRI.ArcGIS.Mobile.Geometries.Point tmppoint = new ESRI.ArcGIS.Mobile.Geometries.Point(crossRoadCoordinate);<br>49 <br>50 if (tmppoint.Within(mainGeo))<br>51 {<br>52 if (IsTouch == true)//有两个或以上交叉路口<br>53 {<br>54 Coordinate c = tmppoint.GetExtent().GetCenter();<br>55 tmpCollect.Add(c);<br>56 HighLightOnlyRoadCorner(tmppoint, mp, ms, layername);<br>57 }<br>58 else<br>59 {<br>60 cc=tmpCollect = null;<br>61 HighLightOnlyRoadCorner(tmppoint, mp, ms, layername);<br>62 return; <br>63 } <br>64 <br>65 }<br>66 <br>67 }<br>68 }<br>69 //if (tmpCollect.Count == 0)<br>70 // cc = null;<br>71 //else<br>72 // cc = tmpCollect;<br>73 cc = tmpCollect;<br>74 <br>75 }<br>76 else<br>77 {<br>78 MessageBox.Show("两条道路不能相交,不能确定路口!", "提示");<br>79 cc = null;<br>80 return;<br>81 }<br>82 }<br>83 catch<br>84 {<br>85 cc = null;<br>86 }<br>87 <br>88 }<br><br> <br><br>下面是写的两个相关的函数<br><br> <br>代码<br> 1 /// <summary><br> 2 /// 判断点是否在指定区域内<br> 3 /// </summary><br> 4 /// <param name="_g">geometry</param><br> 5 /// <param name="_ev">指定区域</param><br> 6 /// <returns></returns><br> 7 public static bool PointInEnvelop(Geometry _g, Envelope _ev)<br> 8 {<br> 9 Coordinate cd = _g.GetExtent().GetCenter();<br>10 double evcenterx = _ev.XCenter;<br>11 double evcentery = _ev.YCenter;<br>12 double minx = evcenterx - _ev.Width / 2;<br>13 double maxx = evcenterx + _ev.Width / 2;<br>14 double miny = evcentery - _ev.Height / 2;<br>15 double maxy = evcentery + _ev.Height / 2;<br>16 <br>17 if (cd.X > minx ;; cd.X < maxx ;; cd.Y > miny ;; cd.Y < maxy)<br>18 {<br>19 return true;<br>20 }<br>21 else<br>22 {<br>23 return false;<br>24 }<br>25 }<br>26 <br>27 /// <summary><br>28 /// 定位路口单点<br>29 /// </summary><br>30 /// <param name="cornerpoint">路口点</param><br>31 /// <param name="mp">地图控件</param><br>32 /// <param name="layername">图层名</param><br>33 public static void HighLightOnlyRoadCorner(ESRI.ArcGIS.Mobile.Geometries.Point cornerpoint, ESRI.ArcGIS.Mobile.Map mp,MobileService ms,string layername)<br>34 {<br>35 Envelope newEnvelope = mp.GetExtent();<br>36 MobileServiceLayer msl = ms.Layers[layername] as MobileServiceLayer;<br>37 FeatureLayer fl = msl as ESRI.ArcGIS.Mobile.MobileServices.FeatureLayer;<br>38 if (fl.InScaleRange(mp.Scale))<br>39 {<br>40 if (PointInEnvelop(cornerpoint as Geometry,newEnvelope))<br>41 {<br>42 mp.FlashGeometry(new Pen(Color.Blue, 2.0F), new SolidBrush(Color.Red), 15, 500, 15, cornerpoint);<br>43 }<br>44 else<br>45 {<br>46 Coordinate tmpcd = cornerpoint.GetExtent().GetCenter();<br>47 Envelope tmpel = new Envelope(tmpcd, 250, 250);<br>48 mp.SetExtent(tmpel);<br>49 mp.FlashGeometry(new Pen(Color.Blue, 2.0F), new SolidBrush(Color.Red), 20, 500, 20, cornerpoint);<br>50 }<br>51 <br>52 }<br>53 else<br>54 {<br>55 Coordinate tmpcd = cornerpoint.GetExtent().GetCenter();<br>56 Envelope tmpel = new Envelope(tmpcd, 250, 250);<br>57 mp.SetExtent(tmpel);<br>58 mp.FlashGeometry(new Pen(Color.Blue, 2.0F), new SolidBrush(Color.Red), 20, 500, 20, cornerpoint);<br>59 <br>60 }<br>61 }<br>62 }<br><br> <br>
|
|
|