private byte[] FindRouteLocationsHandler(NameValueCollection boundVariables, JsonObject operationInput, string outputFormat, string requestProperties, out string responseProperties) { responseProperties = null; if (!operationInput.TryGetAsLong("layer", out long?layerId)) { throw new ArgumentException("Layer ID not provided"); } bool hasLocations = operationInput.TryGetArray("locations", out object[] locationsArray); if (!hasLocations) { throw new ArgumentException($"Expected \"locations\" to be a JSON array: {operationInput.ToJson()}", "locations"); } var locations = locationsArray.Cast <JsonObject>().ToRouteLocations(); IRouteLocator2 routeLocator = serverObjectHelper.GetRouteLocator(layerId.GetValueOrDefault(0), routeIdFieldName); var located = locations.Select(loc => { routeLocator.Locate(loc, out IGeometry result, out esriLocatingError locatingError); return(new LocationResult { RouteLocation = loc, Geometry = result, LocatingError = locatingError }); }); var output = new JsonObject(); output.AddArray("results", located.Select(l => l.ToJsonObject()).ToArray()); return(Encoding.UTF8.GetBytes(output.ToJson())); //byte[] bytes; //var serializer = JsonSerializer.CreateDefault(); //using (var memoryStream = new MemoryStream()) //using (var textWriter = new StreamWriter(memoryStream, Encoding.UTF8)) //{ // serializer.Serialize(textWriter, located); // bytes = memoryStream.GetBuffer(); //} //return bytes; }
/// <summary> /// Handler operation Line Location /// </summary> /// <param name="boundVariables">list of variables bound</param> /// <param name="operationInput">input of operation</param> /// <param name="outputFormat">format of output</param> /// <param name="requestProperties">list of request properties</param> /// <param name="responseProperties">list of response properties </param> /// <returns>response in byte</returns> private byte[] LineLocationOperHandler(NameValueCollection boundVariables, JsonObject operationInput, string outputFormat, string requestProperties, out string responseProperties) { responseProperties = "{\"Content-Type\" : \"application/json\"}"; int routeLayerID = Convert.ToInt32(boundVariables["RouteLayersID"], CultureInfo.InvariantCulture); esriUnits routeMeasureUnit = DSUtility.GetMeasureUnit(operationInput, MeasureUnit.routeMeasureUnit); esriUnits routeLocationMeasureUnit = DSUtility.GetMeasureUnit(operationInput, MeasureUnit.routeLocationMeasureUnit); double?fromMeasure; operationInput.TryGetAsDouble("fromMeasure", out fromMeasure); double?toMeasure; operationInput.TryGetAsDouble("toMeasure", out toMeasure); if (!fromMeasure.HasValue && !toMeasure.HasValue) { throw new DynamicSegmentationException("Set at least fromMeasure or toMeasure!"); } double?lateralOffset; bool found = operationInput.TryGetAsDouble("lateralOffset", out lateralOffset); if (!found || !lateralOffset.HasValue) { lateralOffset = 0; } IFeatureClass featureClass = this.GetRouteFeatureClass(routeLayerID); string routeIDFieldNameValue = DSUtility.GetRouteIDFieldName(operationInput); IFields fields = featureClass.Fields; int indexField = fields.FindField(routeIDFieldNameValue); if (indexField == -1) { throw new DynamicSegmentationException(string.Format(CultureInfo.InvariantCulture, "routeIDFieldName {0} not found!", routeIDFieldNameValue)); } object routeID; found = operationInput.TryGetObject("routeID", out routeID); if (!found) { throw new DynamicSegmentationException("routeID not valid"); } IField field = fields.get_Field(indexField); this.CheckRouteID(routeID, field); IRouteLocator2 routeLocator = DSUtility.GetRouteLocator(featureClass, routeIDFieldNameValue, routeMeasureUnit); IRouteLocation routeLocation = new RouteMeasureLineLocationClass(); routeLocation.RouteID = routeID; routeLocation.MeasureUnit = routeLocationMeasureUnit; routeLocation.LateralOffset = lateralOffset.Value; IRouteMeasureLineLocation routeMeasureLineLocation = (IRouteMeasureLineLocation)routeLocation; if (fromMeasure.HasValue) { routeMeasureLineLocation.FromMeasure = fromMeasure.Value; } if (toMeasure.HasValue) { routeMeasureLineLocation.ToMeasure = toMeasure.Value; } IGeometry geometry; esriLocatingError locatingError; routeLocator.Locate(routeLocation, out geometry, out locatingError); int errorId = (int)locatingError; JsonObject result = null; if (errorId != 0) { string errorDescription = string.Format(CultureInfo.InvariantCulture, "{0} ({1})", Enum.GetName(typeof(esriLocatingError), locatingError), errorId); IObjectJson jsonObjectError = new ObjectError(errorDescription); result = jsonObjectError.ToJsonObject(); } if ((geometry != null) && (!geometry.IsEmpty)) { if (result == null) { result = new JsonObject(); } result.AddJsonObject("geometries", Conversion.ToJsonObject(geometry)); } return(result.JsonByte()); }