ArcGIS中的线性参考/动态分段技术(三):几个应用场景在ArcGIS Server中的实现

 现在来实现上面提到的3个功能。
1、对于某条公路上的一点进行Identify操作,要求返回该点在公路上的桩号值:
以Silverlight API为例。为了在服务器端使用ArcObjects,在Asp.net工程中添加一个名为LinearRef的Silverlight-Enabled WCF Service,在LinearRef.svc.cs文件中添加以下代码:
  1. namespace RoadDycSeg.Web.WCF
  2. {
  3.     [ServiceContract(Namespace = "")]
  4.     [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  5.     public class LinearRef
  6.     {
  7.         public IRouteLocator2 pRtLocator = null;
  8.         ESRI.ArcGIS.Server.IServerObjectManager pSOM = null;
  9.         public ESRI.ArcGIS.Server.IServerContext pServerContext = null;

  10.         public LinearRef()
  11.         {
  12.             ESRI.ArcGIS.ADF.Identity identity = new ESRI.ArcGIS.ADF.Identity("ArcGISWebServices", "yourpassword", "");
  13.             ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection agsconn = new ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection("localhost", identity);
  14.             agsconn.Connect();
  15.             pSOM = agsconn.ServerObjectManager;
  16.             pServerContext = pSOM.CreateServerContext("shaanxi", "MapServer");
  17.             ESRI.ArcGIS.Carto.IMapServer2 pMapServer = pServerContext.ServerObject as ESRI.ArcGIS.Carto.IMapServer2;
  18.             ESRI.ArcGIS.Carto.IMapServerObjects2 pMapServerObjects = pMapServer as ESRI.ArcGIS.Carto.IMapServerObjects2;
  19.             ESRI.ArcGIS.Carto.IMap pMap = pMapServerObjects.get_Map(pMapServer.DefaultMapName);
  20.             IFeatureClass pFC = (pMap.get_Layer(2) as ESRI.ArcGIS.Carto.IFeatureLayer).FeatureClass;

  21.             //create the RouteLocator
  22.             IDataset dS = (IDataset)pFC; // A polylineM feature class.
  23.             IName name = dS.FullName;
  24.             IRouteLocatorName rtLocatorName = pServerContext.CreateObject("esriLocation.RouteMeasureLocatorName") as IRouteLocatorName;
  25.             rtLocatorName.RouteFeatureClassName = name;
  26.             rtLocatorName.RouteIDFieldName = "道路编码";
  27.             rtLocatorName.RouteMeasureUnit = esriUnits.esriUnknownUnits;
  28.             name = (IName)rtLocatorName;
  29.             pRtLocator = (IRouteLocator2)name.Open();

  30.             pServerContext.ReleaseContext();
  31.         }

  32. // Add more operations here and mark them with [OperationContract]

  33.         [OperationContract]
  34.         /// <summary>
  35.         /// used by the client Identify operation and return the result
  36.         /// </summary>
  37.         /// <param name="mapX">the x coords of the mouseclick in map units</param>
  38.         /// <param name="mapY">the y coords of the mouseclick in map units</param>
  39.         /// <param name="resolution">the map distance of 1 pixel</param>
  40.         /// <returns>a identifyresult class defined in Classes.cs. If no result, mvalue=x=y=-1</returns>
  41.         public IdentifyResult Identify(double mapX, double mapY, double resolution)
  42.         {
  43.             pServerContext = pSOM.CreateServerContext("shaanxi", "MapServer");

  44.             IdentifyResult ir = new IdentifyResult("", -1, -1, -1);

  45.             IPoint pPoint = pServerContext.CreateObject("esriGeometry.Point") as IPoint;
  46.             pPoint.PutCoords(mapX, mapY);
  47.             IEnvelope pEnvelope = pPoint.Envelope;
  48.             pEnvelope.Expand(10 * resolution, 10 * resolution, false);

  49.             IEnumRouteIdentifyResult pEnumResult = pRtLocator.Identify(pEnvelope, "");
  50.             pEnumResult.Reset();
  51.             //only get the first result
  52.             if (pEnumResult.Count > 0)
  53.             {
  54.                 IRouteLocation pRL = null;
  55.                 IFeature pF = null;
  56.                 pEnumResult.Next(out pRL, out pF);
  57.                 IRouteMeasurePointLocation pRMPL = pRL as IRouteMeasurePointLocation;

  58.                 //retrieve the location geometry by calling LOCATE method
  59.                 IRouteLocation routeLocation = pServerContext.CreateObject("esriLocation.RouteMeasurePointLocation") as IRouteLocation;
  60.                 routeLocation.MeasureUnit = esriUnits.esriUnknownUnits;
  61.                 routeLocation.RouteID = pRL.RouteID;
  62.                 routeLocation.LateralOffset = 0;
  63.                 IRouteMeasurePointLocation rMPointLoc = (IRouteMeasurePointLocation)routeLocation;
  64.                 rMPointLoc.Measure = pRMPL.Measure;

  65.                 IGeometry geom;
  66.                 esriLocatingError locError;
  67.                 pRtLocator.Locate((IRouteLocation)rMPointLoc, out geom, out locError);

  68.                 ir.RouteID = pRL.RouteID.ToString();
  69.                 ir.MValue = pRMPL.Measure;
  70.                 ir.X = (geom as IPointCollection5).get_Point(0).X;
  71.                 ir.Y = (geom as IPointCollection5).get_Point(0).Y;
  72.             }

  73.             pServerContext.ReleaseContext();

  74.             return ir;
  75.         }
复制代码
其中,IdentifyResult是自定义的一个类,用来存储查询的结果:
  1. namespace RoadDycSeg.Web.WCF
  2. {
  3.     /// <summary>
  4.     /// the result of Identify operation
  5.     /// </summary>
  6.     [DataContract]
  7.     public class IdentifyResult
  8.     {
  9.         [DataMember]
  10.         public string RouteID {get;set;}
  11.         [DataMember]
  12.         public double MValue {get;set;}
  13.         [DataMember]
  14.         public double X {get;set;}
  15.         [DataMember]
  16.         public double Y {get;set;}

  17.         public IdentifyResult(string routeid, double mvalue, double x, double y)
  18.         {
  19.             RouteID = routeid;
  20.             MValue = mvalue;
  21.             X = x;
  22.             Y = y;
  23.         }
  24.     }
  25. }
复制代码
Identify方法需要传入3个参数,一个点的地理坐标以及当前地图的Resolution,后者用来生成一个10像素的缓冲区,方便用户的点击操作;其中利用IRouteLocator2.Identify方法获得M值,据此利用 IRouteLocator2.Locate方法获得精确落在公路上的点的Geometry,用以显示在客户端;如果用户点击的点距离公路较远,则IRouteLocator2.Identify结果为空,返回的IdentifyResult中M值为-1。
在Silverlight工程中,Add Service Reference,找到刚才的LinearRef服务,取名为LRService。
客户端中,新建一个COperation类,实现业务操作。
  1. public class COperation
  2. {
  3. private LRService.LinearRefClient LR = null;
  4.         private GraphicsLayer glayer = null;
  5.         private bool isBusy = false;//indicate whether there is an operation is performing
  6.         private Map Map1 = null;
  7.         public COperation(Map map)
  8.         {
  9.             Map1=map;
  10.             glayer = Map1.Layers["glayer"] as GraphicsLayer;
  11.             //initialize the LR object
  12.             LR = new RoadDycSeg.LRService.LinearRefClient();
  13.             LR.IdentifyCompleted += new EventHandler<RoadDycSeg.LRService.IdentifyCompletedEventArgs>(LR_IdentifyCompleted);
  14.         }
  15.         private void LR_IdentifyCompleted(object sender, LRService.IdentifyCompletedEventArgs e)
  16.         {
  17.             LRService.IdentifyResult ir = e.Result;
  18.             //add a point graphic to the map with attributes displaying by the maptip, maptip defined in mainpage.xaml
  19.             if (ir.X > 0)//the result exists
  20.             {
  21.                 Graphic g = new Graphic()
  22.                 {
  23.                     Geometry = new MapPoint(ir.X, ir.Y),
  24.                     Symbol = Application.Current.Resources["strobeSymbol"] as Symbol,
  25.                 };
  26.                 g.Attributes.Add("RouteID", ir.RouteID);
  27.                 g.Attributes.Add("桩号", ir.MValue);
  28.                 g.Attributes.Add("X坐标", ir.X);
  29.                 g.Attributes.Add("Y坐标", ir.Y);
  30.                 glayer.Graphics.Clear();
  31.                 glayer.Graphics.Add(g);
  32.             }
  33.             isBusy = false;
  34.         }
  35.         
  36.         private void Map1_Identify(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
  37.         {
  38.             if (!isBusy)
  39.             {
  40.                 LR.IdentifyAsync(e.MapPoint.X, e.MapPoint.Y, Map1.Resolution);
  41.                 isBusy = true;
  42.             }
  43.         }
复制代码
在需要使用Identify功能时,将Map控件的Click事件绑定到Map1_Identify即可。结果如图:

2、输入起始和终止桩号,要求将其间的路段显示在地图上:
要解决这个问题,关键是根据两个M值,求的其间一段公路的Geometry。利用的仍然是IRouteLocator2.Locate方法,其中的第一个参数routeLocation,传入的是用IRouteMeasureLineLocation接口定义的“线性位置”,具体可参考帮助文档。
仍然是在LinearRef.svc.cs文件的LinearRef类中,添加以下方法:
  1. [OperationContract(Name = "RetrieveRoutePortionGeometry")]
  2.         /// <summary>
  3.         /// used by the client,such as QueryPortion operation, to retrieve the geometry of the route portion
  4.         /// </summary>
  5.         /// <param name="routeid">the route id which the portion is part of</param>
  6.         /// <param name="fromMValue">fromMvalue</param>
  7.         /// <param name="toMValue">toMvalue</param>
  8.         /// <returns>a json string with specific format,such as [{"x":1111,"y":1111},{"x":2222,"y":2222},{"x":3333,"y":3333}.....]</returns>
  9.         public string RetrieveRoutePortionGeometry(string routeid, double fromMValue, double toMValue)
  10.         {
  11.             pServerContext = pSOM.CreateServerContext("shaanxi", "MapServer");
  12.             IRouteLocation routeLoc = pServerContext.CreateObject("esriLocation.RouteMeasureLineLocation") as IRouteLocation;
  13.             routeLoc.MeasureUnit = esriUnits.esriUnknownUnits;
  14.             routeLoc.RouteID = routeid;
  15.             routeLoc.LateralOffset = 0;
  16.             IRouteMeasureLineLocation rMLineLoc = (IRouteMeasureLineLocation)routeLoc;
  17.             rMLineLoc.FromMeasure = fromMValue;
  18.             rMLineLoc.ToMeasure = toMValue;

  19.             IGeometry geom;
  20.             esriLocatingError locError;
  21.             pRtLocator.Locate((IRouteLocation)rMLineLoc, out geom, out locError);

  22.             pServerContext.ReleaseContext();

  23.             //return the routeportion's vertices
  24.             StringBuilder sb = new StringBuilder("[");
  25.             IPolyline pLine = geom as IPolyline;
  26.             IPointCollection pPC = pLine as IPointCollection;
  27.             //json format such as:
  28.             //[{"x":1111,"y":1111},{"x":2222,"y":2222},{"x":3333,"y":3333}.....]
  29.             for (int i = 0; i < pPC.PointCount; i++)
  30.             {
  31.                 sb.Append("{\"x\":");
  32.                 sb.Append(pPC.get_Point(i).X.ToString());
  33.                 sb.Append(",\"y\":");
  34.                 sb.Append(pPC.get_Point(i).Y.ToString());
  35.                 sb.Append("},");
  36.             }
  37.             //remove the last ","
  38.             sb.Remove(sb.Length - 1, 1);
  39.             sb.Append("]");
  40.             return sb.ToString();
  41.         }
复制代码
由于在服务器端的AO操作,产生的结果是ESRI.ArcGIS.Geometry.IGeometry类型,而客户端需要的则是ESRI.ArcGIS.Client.Geometry.Geometry类型,所以需要对结果序列化/反序列化,这里采用JSON字符串处理。客户端处理结果的函数仍然放在COperation.cs文件中,如下:
  1. private void LR_RetrieveRoutePortionGeometryCompleted(object sender,LRService.RetrieveRoutePortionGeometryCompletedEventArgs e)
  2.         {
  3.             //create a esri.arcgis.client.geometry.polyline from the coords of points contained in the json string
  4.             string jsonpoints = e.Result;
  5.             JsonArray jsonarrays = (JsonArray)JsonArray.Load(new System.IO.StringReader(jsonpoints));
  6.             ESRI.ArcGIS.Client.Geometry.Polyline line = new ESRI.ArcGIS.Client.Geometry.Polyline()
  7.             {
  8.                 SpatialReference = Map1.SpatialReference,
  9.             };
  10.             ESRI.ArcGIS.Client.Geometry.PointCollection pc = new ESRI.ArcGIS.Client.Geometry.PointCollection();
  11.             foreach (JsonObject jo in jsonarrays)
  12.             {
  13.                 pc.Add(new ESRI.ArcGIS.Client.Geometry.MapPoint(jo["x"], jo["y"]));
  14.             }
  15.             line.Paths.Add(pc);

  16.             //add a graphic
  17.             Graphic g = new Graphic()
  18.             {
  19.                 Geometry = line,
  20.                 Symbol = Application.Current.Resources["LineSymbol"] as SimpleLineSymbol,
  21.             };
  22.             glayer.Graphics.Add(g);
  23. }
复制代码
Silverlight中提供了System.Json库,可以非常方便的对JSON字符串进行解析。结果如图:

3、类似GoogleMap的交通流量地图:
首先看一下Google的交通流量地图:

可以看出,之所以能产生不同颜色段,需要的数据有3个:该段的起始和终止M值,该段的车流量。假设我们获取的业务数据表(EventTable)如下:

联想到上面第二个应用场景,我们便想可以获取所有交通数据后,用循环的办法展示出交通流量地图。但这里有两个问题需要思考:1、即使使用IRouteLocator2.LocateRow方法,服务器端根据起止M值解析出所有的分段图形需要一次循环,客户端将这些图形显示出来还需要一次循环,这样是否合理?2、由于结果显示在客户端,不同用户发出请求时,都将重复上面两个循环,如何改进?
解决问题一:在第二节的Linear Referencing实现原理中,提到了RouteEventSource类,可以将它看作是把Route FeatureClass和EventTable组合在一起的结果,而该类继承自FeatureClass,可当作一个FeatureClass来用。在ArcMap中观察生成后(利用Make Route Event Layer工具)的RouteEventSource层:

不仅包含了EventTable中的事件,还有了我们想要的Shape字段(可取出Geometry)。其实Shape字段中的内容,是利用Dynamic Segmentation技术动态计算出来的。看其图层属性便知,它是在内存中动态生成的:

有了FeatureClass,我们便可以对整个图层进行一次渲染,避免服务器端和客户端的两次循环了。这里要注意,FeatureClass是动态生成的,图层便是动态添加,在地图服务的REST接口中无法暴露出来,也就不能利用客户端API的渲染技术了。
解决问题二:利用SOA的思想,将交通流量图做成一个Traffic服务,不同用户请求时动态添加该服务即可。想想这个服务应该具有的特性:不同时段的交通图,需要动态生成;如果交通图已经生成,则无需重新生成,显示即可。所以我们采取池化方式服务(pooled-service)的特性,刚好解决这一问题。
这个场景我们用Web ADF来实现(客户端API与前两个场景相同)。
首先准备两个Map Service:底图服务:shaanxi,交通流量图服务:ShaanxiTraffic。
后者在ArcMap中打开是这样的:

ShaanxiTraffic服务除了动态生成的交通流量图外,不需要在显示任何信息,所以发布时是空白的;因为动态生成的RouteEventSource将作为图层插入到这个地图服务中,所以Route FeatureClass必须也在这个mxd中才行。
Default.aspx页面中放置一个MapResourceManager控件、一个Map控件、一个CallbackButton按钮和一个GridView控件。先看一下结果:

点击按钮后:

看一下Default.aspx.cs中按钮的点击事件:
  1. protected void CallbackButton1_Clicked(object sender, EventArgs args)
  2.     {
  3.         //先判断Traffic服务中是否已经产生结果
  4.         if (hasTrafficResult())//Traffic服务中已经有结果
  5.         {
  6.             //将结果插入地图
  7.             InsertTrafficService();
  8.         }
  9.         else//服务中还没有结果
  10.         {
  11.             //先生成交通流量结果
  12.             MakeResult();
  13.             //将结果插入地图
  14.             InsertTrafficService();
  15.         }
  16.         //显示RouteEventSource的属性表
  17.         DisplayAttributeTable();
  18.     }
复制代码
思路都在代码中。依次来看几个函数:
  1. /// <summary>
  2.     /// 判断池化的Traffic服务中,获取的ServerContext是否已经产生结果
  3.     /// </summary>
  4.     /// <returns>true or false</returns>
  5.     private bool hasTrafficResult()
  6.     {
  7.         ESRI.ArcGIS.ADF.Identity identity = new ESRI.ArcGIS.ADF.Identity("ArcGISWebServices", "yourpassword", "");
  8.         ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection agsconn = new ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection("machinename", identity);
  9.         agsconn.Connect();
  10.         pSOM = agsconn.ServerObjectManager;
  11.         ESRI.ArcGIS.Server.IServerContext pServerContext = pSOM.CreateServerContext("ShaanxiTraffic", "MapServer");
  12.         ESRI.ArcGIS.Carto.IMapServer2 pMapServer = pServerContext.ServerObject as ESRI.ArcGIS.Carto.IMapServer2;
  13.         ESRI.ArcGIS.Carto.IMapServerObjects2 pMapServerObjects = pMapServer as ESRI.ArcGIS.Carto.IMapServerObjects2;
  14.         ESRI.ArcGIS.Carto.IMap pMap = pMapServerObjects.get_Map(pMapServer.DefaultMapName);
  15.         pServerContext.ReleaseContext();
  16.         return pMap.LayerCount == 1 ? false : true;
  17.     }   
复制代码
对于ShaanxiTraffic服务,在获得的Server Object中,查看当前Map的图层数(默认有一个隐藏的Road图层),若为1则还未产生结果。
下面是动态添加服务的函数,注意要设置背景透明,以便能够看到底图服务:
  1. /// <summary>
  2.     /// 动态添加服务
  3.     /// </summary>
  4.     private void InsertTrafficService()
  5.     {
  6.         if (MapResourceManager1.ResourceItems.Count==1)//若已插入交通流量服务,则不做动作
  7.         {
  8.             MapResourceItem mapResourceItem = new MapResourceItem();
  9.             GISResourceItemDefinition definition = new GISResourceItemDefinition();
  10.             mapResourceItem.Name = "Traffic";
  11.             definition.ResourceDefinition = "Layers@ShaanxiTraffic";
  12.             definition.DataSourceDefinition = "machinename";
  13.             definition.DataSourceType = "ArcGIS Server Local";
  14.             mapResourceItem.Parent = MapResourceManager1;
  15.             mapResourceItem.Definition = definition;
  16.             ESRI.ArcGIS.ADF.Web.DisplaySettings displaysettings = new ESRI.ArcGIS.ADF.Web.DisplaySettings();
  17.             displaysettings.Transparency = 0.0F;
  18.             displaysettings.Visible = true;
  19.             ESRI.ArcGIS.ADF.Web.ImageDescriptor imagedescriptor = new ESRI.ArcGIS.ADF.Web.ImageDescriptor();
  20.             imagedescriptor.TransparentBackground = true;
  21.             imagedescriptor.TransparentColor = System.Drawing.Color.White;
  22.             displaysettings.ImageDescriptor = imagedescriptor;
  23.             mapResourceItem.DisplaySettings = displaysettings;

  24.             MapResourceManager1.ResourceItems.Insert(0, mapResourceItem);
  25.             MapResourceManager1.CreateResource(mapResourceItem);

  26.             if (Map1.ImageBlendingMode == ImageBlendingMode.WebTier)
  27.             {
  28.                 Map1.Refresh();
  29.             }
  30.             else
  31.             {
  32.                 Map1.RefreshResource("Traffic");
  33.             }

  34.             CallbackButton1.CallbackResults.CopyFrom(Map1.CallbackResults);
  35.         }
  36.     }            
复制代码
MakeResult函数利用ESRI.ArcGIS.Location库来生成交通流量图。由于Traffic服务是池化服务,当第一个用户请求,生成交通流量图后,该实例(servercontext)会返回服务器上而不被销毁,所以下一个用户再次请求时可以直接显示。如果要实现实时的交通流量图,定时在服务器端重生servercontext,并读取当前时段的EventTable即可。
  1. /// <summary>
  2.     /// 生成交通流量的结果
  3.     /// </summary>
  4.     private void MakeResult()
  5.     {
  6.         ESRI.ArcGIS.Server.IServerContext pServerContextTraffic = pSOM.CreateServerContext("ShaanxiTraffic", "MapServer");
  7.         ESRI.ArcGIS.Carto.IMapServer2 pMapServer = pServerContextTraffic.ServerObject as ESRI.ArcGIS.Carto.IMapServer2;
  8.         ESRI.ArcGIS.Carto.IMapServerObjects2 pMapServerObjects = pMapServer as ESRI.ArcGIS.Carto.IMapServerObjects2;
  9.         ESRI.ArcGIS.Carto.IMap pMap = pMapServerObjects.get_Map(pMapServer.DefaultMapName);
  10.         //创建RouteLocator
  11.         IFeatureClass pFC = (GetLayerByName("Road",pMap) as IFeatureLayer).FeatureClass;
  12.         IDataset dS = (IDataset)pFC; // A polylineM feature class.
  13.         IName name = dS.FullName;
  14.         IRouteLocatorName rtLocatorName = pServerContextTraffic.CreateObject("esriLocation.RouteMeasureLocatorName") as IRouteLocatorName;
  15.     rtLocatorName.RouteFeatureClassName = name;
  16. rtLocatorName.RouteIDFieldName = "道路编码";
  17. rtLocatorName.RouteMeasureUnit = esriUnits.esriUnknownUnits;
  18.         name = (IName)rtLocatorName;
  19.         IRouteLocator2 pRtLocator = (IRouteLocator2)name.Open();

  20.         //创建RouteEventProperties,为RouteEventSource做准备
  21.         IRouteEventProperties2 rtProp = pServerContextTraffic.CreateObject("esriLocation.RouteMeasureLineProperties") as IRouteEventProperties2;
  22.         rtProp.AddErrorField = true;
  23.         rtProp.ErrorFieldName = "LOC_ERROR";
  24.         rtProp.EventMeasureUnit = esriUnits.esriUnknownUnits;
  25.         rtProp.EventRouteIDFieldName = "RouteID";
  26.         IRouteMeasureLineProperties rMLineProp = (IRouteMeasureLineProperties)rtProp;
  27.         rMLineProp.FromMeasureFieldName = "FromM";
  28.         rMLineProp.ToMeasureFieldName = "ToM";

  29.         //创建RouteEventSource
  30.         ITable eventTable = (pFC.FeatureDataset.Workspace as IFeatureWorkspace).OpenTable("lineEventsTable");
  31.         IDataset ds = (IDataset)eventTable;
  32.         IName name2 = ds.FullName;
  33.         IRouteEventSourceName rtEvtSrcName = pServerContextTraffic.CreateObject("esriLocation.RouteEventSourceName") as IRouteEventSourceName;
  34.         rtEvtSrcName.EventTableName = name2;
  35.         rtEvtSrcName.EventProperties = (IRouteEventProperties)rMLineProp;
  36.         rtEvtSrcName.RouteLocatorName = rtLocatorName;
  37.         name2 = (IName)rtEvtSrcName;
  38.         IRouteEventSource rtEvtSrc = (IRouteEventSource)name2.Open();
  39.         //转换到FeatureClass
  40.     pFC = rtEvtSrc as IFeatureClass;

  41. //将RouteEventSource添加到地图中,并进行唯一值渲染
  42.         IUniqueValueRenderer pUVR = pServerContextTraffic.CreateObject("esriCarto.UniqueValueRenderer") as IUniqueValueRenderer;
  43.         pUVR.FieldCount = 1;
  44.         pUVR.set_Field(0, "Vehicles");
  45.         pUVR.UseDefaultSymbol = false;
  46.         IFeatureCursor pFeatureCursor = pFC.Search(null, false);
  47.         IFeature pFeature = pFeatureCursor.NextFeature();
  48.         int ifiledindex = pFC.Fields.FindField("Vehicles");//EventTable中的车流量字段
  49.         while (pFeature != null)
  50.         {
  51.             ISimpleLineSymbol pSymbol = pServerContextTraffic.CreateObject("esriDisplay.SimpleLineSymbol") as ISimpleLineSymbol;
  52.             double value = double.Parse(pFeature.get_Value(ifiledindex).ToString());
  53.             if (value <= 20)
  54.                 pSymbol.Color = ESRI.ArcGIS.ADF.Converter.ToRGBColor(pServerContextTraffic, System.Drawing.Color.FromArgb(71, 196, 69)) as IColor;
  55.             else if (value > 20 && value <= 50)
  56.                 pSymbol.Color = ESRI.ArcGIS.ADF.Converter.ToRGBColor(pServerContextTraffic, System.Drawing.Color.FromArgb(255, 255, 0)) as IColor;
  57.             else if (value > 50 && value <= 100)
  58.                 pSymbol.Color = ESRI.ArcGIS.ADF.Converter.ToRGBColor(pServerContextTraffic, System.Drawing.Color.FromArgb(201, 59, 46)) as IColor;
  59.             else
  60.                 pSymbol.Color = ESRI.ArcGIS.ADF.Converter.ToRGBColor(pServerContextTraffic, System.Drawing.Color.FromArgb(250, 52, 17)) as IColor;
  61.             pSymbol.Width = 5;
  62.             pUVR.AddValue(pFeature.get_Value(ifiledindex).ToString(), "Vehicles", pSymbol as ISymbol);
  63.             pFeature = pFeatureCursor.NextFeature();
  64.         }
  65.         IFeatureLayer pFL = pServerContextTraffic.CreateObject("esriCarto.FeatureLayer") as IFeatureLayer;
  66.         pFL.Name = "RouteEventSource";
  67.         pFL.FeatureClass = pFC;
  68.         (pFL as IGeoFeatureLayer).Renderer = pUVR as IFeatureRenderer;
  69.         
  70.         pMap.AddLayer(pFL as ILayer);//to the 0 index
  71.         pMapServerObjects.RefreshServerObjects();
  72.         pServerContextTraffic.ReleaseContext();
  73.     }        
复制代码
一点注意,在使用IServerContext接口时要及时进行释放。
思考一下:如何通过Geoprocessing Service来实现“交通流量图”?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值