ARCGIS 10.1 for Server SOE介绍及开发实例(5)

1.6
由于这个SOE是Rest方式的,所以我们可以在Flex,Silverlight这样的客户端使用,我们只需要构造这个SOE的rest地址,然后通过WebClient对象调用(这个是Silverlight中的,Flex中可能不一样),并获取结果,详细的例子可以参照Esri官网提供的例子:                         http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SOEElevationLatLonJsonObject
因为在上面的这个例子中,我将几何对象转成json格式的,但是在客户端使用的时候没有将json格式转成几何对象的API,有两种解决办法,我们在客户端获取这个json格式的字符串然后解析,自己构造这个几何对象,第二种方式,我们可以借用FeatureSet.FromJson()方法,这就要求我们在SOE中返回的是一个FeatureSet的描述,我们在代码中稍微修改下即可,如下:
private byte[] pLineOperHandler(NameValueCollection boundVariables,
                                                                                  JsonObject operationInput,
                                                                                               string outputFormat,
                                                                                       string requestProperties,
                                                                            out string responseProperties)
       {
             responseProperties = "";
             if (serverObjectHelper.ServerObject is IMapServer)
                {
                      mapServer = (IMapServer3)serverObjectHelper.ServerObject;
                }
             // IMapServerObjects pMSO = serverObjectHelper.ServerObject as IMapServerObjects;
             IMapServerDataAccess pMapServerDataAcc = null;
             IFeatureClass pFeatureClass = null;
             if (serverObjectHelper.ServerObject is IMapServerDataAccess)
                {
                     pMapServerDataAcc = serverObjectHelper.ServerObject as IMapServerDataAccess;
                     pFeatureClass = pMapServerDataAcc.GetDataSource(mapServer.DefaultMapName, 0) as IFeatureClass;
                }
              string _pPKName;
              operationInput.TryGetString("RouteFieldName", out _pPKName);
              if (_pPKName==null)
              throw new ArgumentNullException("StationMeasure");
              long? _pID;
             operationInput.TryGetAsLong("RouteID", out _pID);
             if (_pID == null)
             throw new ArgumentNullException("RouteID");
            double ? _pFrom;
            operationInput.TryGetAsDouble("Stationfrom", out _pFrom);
            if (_pFrom == null)
            throw new ArgumentNullException("Stationfrom");
            double? _pTo;
            operationInput.TryGetAsDouble("Stationto", out _pTo);
            if (_pTo == null)
            throw new ArgumentNullException("Stationto");
            IPolyline pointColl = FindRoutByMeasure(pFeatureClass, _pPKName.Substring(1, _pPKName.Length - 2), _pID.Value, _pFrom.Value, _pTo.Value);
           JsonObject featureJson = new JsonObject();
           //构造FeatureSet
           JsonObject pRest = new JsonObject();
           pRest.AddString("displayFieldName","restlt");
           pRest.AddString("geometryType", "esriGeometryPolyline");
           JsonObject pSpatial = new JsonObject();
           //空间参考
           pSpatial.AddString("wkid" ,pointColl.SpatialReference.FactoryCode.ToString());
           pRest.AddJsonObject("spatialReference", pSpatial);
           //属性
           JsonObject pAtt = new JsonObject();
           pAtt.AddString("ID", "1");
          //要素
          featureJson.AddJsonObject("attributes", pAtt);
          featureJson.AddJsonObject("geometry", Conversion.ToJsonObject(pointColl as IGeometry));
          //要素集合
          JsonObject[] pFea = new JsonObject[1];
          pFea[0] = featureJson;
          pRest.AddArray("features", pFea);
          return Encoding.UTF8.GetBytes(pRest.ToJson());
    }
我们建立一个Silverlight工程,在Silverlight中调用这个SOE,核心代码如下:
     string SOEurl="http://192.168.110.150:6080/arcgis/rest/services/Routes/MapServer/exts/RestLinear/pLineOperationRouteFieldName=%22ROUTE1%22&RouteID=20000013&Stationfrom=0&Stationto=25&f=pjson";
            WebClient webClient = new WebClient();
            FeatureSet pFeature = new FeatureSet();
            webClient.OpenReadCompleted += (s, a) =>
                {
                    JsonValue jsonResponse = JsonObject.Load(a.Result);
                     FeatureSet pFeatureSet = FeatureSet.FromJson(jsonResponse.ToString());
                    if (pFeatureSet.Features != null)
                        {
                             SimpleLineSymbol symbol = new SimpleLineSymbol();
                             symbol.Color = new SolidColorBrush(Colors.Red);
                             GraphicsLayer gLayer = new GraphicsLayer();
                             foreach (Graphic g in pFeatureSet.Features)
                                            {
                                                    g.Symbol = symbol;
                                                    gLayer.Graphics.Add(g);
                                             }
                           myMap.Layers.Add(gLayer);
                          }
                  };
              webClient.OpenReadAsync(new Uri(SOEurl));
            

      运行结果如下图(红色部分为SOE返回的结果):


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值