/// <summary> /// Handler operation Identify Route /// </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[] IdentifyRouteOperHandler(NameValueCollection boundVariables, JsonObject operationInput, string outputFormat, string requestProperties, out string responseProperties) { responseProperties = "{\"Content-Type\" : \"application/json\"}"; string methodName = MethodBase.GetCurrentMethod().Name; int routeLayerID = Convert.ToInt32(boundVariables["RouteLayersID"], CultureInfo.InvariantCulture); esriUnits routeMeasureUnit = DSUtility.GetMeasureUnit(operationInput, MeasureUnit.routeMeasureUnit); IFeatureClass featureClass = this.GetRouteFeatureClass(routeLayerID); string routeIDFieldNameValue = DSUtility.GetRouteIDFieldName(operationInput, featureClass.OIDFieldName); IRouteLocator2 routeLocator = DSUtility.GetRouteLocator(featureClass, routeIDFieldNameValue, routeMeasureUnit); double?tolerance; bool found = operationInput.TryGetAsDouble("tolerance", out tolerance); if (!found || !tolerance.HasValue) { tolerance = 0.0; } JsonObject jsonLocation; if (!operationInput.TryGetJsonObject("location", out jsonLocation)) { throw new ArgumentException("Invalid location", methodName); } IPoint location = Conversion.ToGeometry(jsonLocation, esriGeometryType.esriGeometryPoint) as IPoint; if (location == null) { throw new ArgumentException("Invalid location", methodName); } IEnvelope envelope = location.Envelope; envelope.Expand(tolerance.Value, tolerance.Value, false); IRouteMeasurePointLocation routeMeasurePointLocation = new RouteMeasurePointLocationClass(); IRouteLocation routeLocation; IFeature feature; JsonObject result = new JsonObject(); List <JsonObject> measures = new List <JsonObject>(); IEnumRouteIdentifyResult enumResult = routeLocator.Identify(envelope, string.Empty); for (int i = 1; i <= enumResult.Count; i++) { enumResult.Next(out routeLocation, out feature); routeMeasurePointLocation = (IRouteMeasurePointLocation)routeLocation; JsonObject measure = new JsonObject(); measure.AddString("routeID", routeLocation.RouteID.ToString()); measure.AddDouble("measure", routeMeasurePointLocation.Measure); measures.Add(measure); } result.AddArray("location", measures.ToArray()); return(result.JsonByte()); }
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 Identify Route Ex /// </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[] IdentifyRouteExOperHandler(NameValueCollection boundVariables, JsonObject operationInput, string outputFormat, string requestProperties, out string responseProperties) { responseProperties = "{\"Content-Type\" : \"application/json\"}"; string methodName = MethodBase.GetCurrentMethod().Name; int routeLayerID = Convert.ToInt32(boundVariables["RouteLayersID"], CultureInfo.InvariantCulture); esriUnits routeMeasureUnit = DSUtility.GetMeasureUnit(operationInput, MeasureUnit.routeMeasureUnit); esriSegmentExtension segmentExtension = DSUtility.GetSegmentExtension(operationInput); IFeatureClass featureClass = this.GetRouteFeatureClass(routeLayerID); JsonObject jsonLocation; if (!operationInput.TryGetJsonObject("location", out jsonLocation)) { throw new ArgumentException("Invalid location", methodName); } IPoint location = Conversion.ToGeometry(jsonLocation, esriGeometryType.esriGeometryPoint) as IPoint; if (location == null) { throw new ArgumentException("Invalid location", methodName); } 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; bool found = operationInput.TryGetObject("routeID", out routeID); if (!found) { throw new DynamicSegmentationException("routeID not valid"); } double?tolerance; found = operationInput.TryGetAsDouble("tolerance", out tolerance); if (!found || !tolerance.HasValue) { tolerance = 0.0; } IField field = fields.get_Field(indexField); this.CheckRouteID(routeID, field); IRouteLocator2 routeLocator = DSUtility.GetRouteLocator(featureClass, routeIDFieldNameValue, routeMeasureUnit); IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.SubFields = routeLocator.RouteIDFieldName; queryFilter.AddField(routeLocator.RouteFeatureClass.ShapeFieldName); string where = string.Format("{0} = {1}{2}{1}", routeLocator.RouteIDFieldNameDelimited, routeLocator.RouteIDIsString ? "'" : string.Empty, routeID); queryFilter.WhereClause = where; IPoint locationNearest = null; IFeatureCursor featureCursor = null; try { featureCursor = routeLocator.RouteFeatureClass.Search(queryFilter, true); IFeature featureRoute = featureCursor.NextFeature(); if (featureRoute == null) { throw new DynamicSegmentationException(string.Format(CultureInfo.InvariantCulture, "Feature with value {0} not found!", routeID)); } IProximityOperator proximityOperator = featureRoute.ShapeCopy as IProximityOperator; locationNearest = proximityOperator.ReturnNearestPoint(location, segmentExtension); } catch { throw; } finally { if (featureCursor != null) { Marshal.ReleaseComObject(featureCursor); } } IEnvelope envelope = locationNearest.Envelope; envelope.Expand(tolerance.Value, tolerance.Value, false); IRouteMeasurePointLocation routeMeasurePointLocation = new RouteMeasurePointLocationClass(); IRouteLocation routeLocation; IFeature feature; JsonObject result = new JsonObject(); List <JsonObject> measures = new List <JsonObject>(); IEnumRouteIdentifyResult enumResult = routeLocator.Identify(envelope, where); for (int i = 1; i <= enumResult.Count; i++) { enumResult.Next(out routeLocation, out feature); routeMeasurePointLocation = (IRouteMeasurePointLocation)routeLocation; JsonObject measure = new JsonObject(); measure.AddString("routeID", routeLocation.RouteID.ToString()); measure.AddDouble("measure", routeMeasurePointLocation.Measure); measure.AddJsonObject("location", Conversion.ToJsonObject(locationNearest, true)); measures.Add(measure); } result.AddArray("location", measures.ToArray()); return(result.JsonByte()); }
/// <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()); }