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是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返回的结果):