/// <summary> /// Handles the incoming rest requests /// </summary> /// <param name="boundVariables"> The bound variables. </param> /// <param name="operationInput"> The operation input. </param> /// <param name="outputFormat"> The output format. </param> /// <param name="requestProperties"> The request properties. </param> /// <param name="responseProperties"> The response properties. </param> /// <returns> </returns> /// <exception cref="System.ArgumentNullException"></exception> public static byte[] Handler(NameValueCollection boundVariables, JsonObject operationInput, string outputFormat, string requestProperties, out string responseProperties) { responseProperties = null; string layerName; object[] objectid; var found = operationInput.TryGetString("layerName", out layerName); if (!found || string.IsNullOrEmpty(layerName)) { throw new ArgumentNullException("layerName"); } found = operationInput.TryGetArray("objectid", out objectid); if (!found || objectid == null || objectid.Length < 1) { throw new ArgumentNullException("objectid"); } var searchArgs = new EnvelopeArgs(layerName, objectid.Cast <int>().ToArray()); var connector = SdeConnectorFactory.Create(layerName); if (connector == null) { return(Json(new { Message = "Database does not exist for {0}".With(layerName) })); } var workspace = connector.Connect(); var featureWorkSpace = workspace as IFeatureWorkspace; if (featureWorkSpace == null) { return(Json(new { Message = "Error connecting to SDE." })); } var response = CommandExecutor.ExecuteCommand(new GetEnvelopeCommand(searchArgs, featureWorkSpace)); if (response == null) { return(Json(new { Message = "No features found in {0} with the Id of {1}.".With( searchArgs.LayerName, string.Join(",", searchArgs.ObjectIds)) })); } return(Json(response)); }
/// <summary> /// Handles the incoming rest requests /// </summary> /// <param name="boundVariables"> The bound variables. </param> /// <param name="operationInput"> The operation input. </param> /// <param name="outputFormat"> The output format. </param> /// <param name="requestProperties"> The request properties. </param> /// <param name="responseProperties"> The response properties. </param> /// <returns> </returns> /// <exception cref="System.ArgumentNullException"></exception> public static byte[] Handler(NameValueCollection boundVariables, JsonObject operationInput, string outputFormat, string requestProperties, out string responseProperties) { responseProperties = null; var errors = new ErrorContainer(400); var wkid = 26912; //pull out all the variables var featureClass = operationInput.GetStringValue("featureClass"); var returnValues = operationInput.GetStringValue("returnValues"); var predicate = operationInput.GetStringValue("predicate", nullable: true); var geometryJson = operationInput.GetStringValue("geometry", nullable: true); var wkidInput = operationInput.GetNumberValue("wkid", nullable: true); var bufferInput = operationInput.GetNumberValue("buffer", nullable: true); if (wkidInput > 0) { wkid = Convert.ToInt32(wkidInput); } var isSafeSqlCommand = new IsSafeSqlCommand(new[] { featureClass, returnValues, predicate }); if (!CommandExecutor.ExecuteCommand(isSafeSqlCommand)) { errors.Add("Input appears to be unsafe. That is all I will tell you."); } GeometryContainer container = null; ISpatialReference newSpatialRefefence = null; ISpatialReferenceFactory srFactory = null; //reproject to our data's spatial reference if (wkid != 26912) { srFactory = new SpatialReferenceEnvironmentClass(); var isProjected = true; try { newSpatialRefefence = srFactory.CreateProjectedCoordinateSystem(wkid); } catch (ArgumentException) { isProjected = false; } if (!isProjected) { newSpatialRefefence = srFactory.CreateGeographicCoordinateSystem(wkid); } } //input has a geometry - deal with it if (!string.IsNullOrEmpty(geometryJson)) { var extractGeometryCommand = new ExtractGeometryCommand(geometryJson, wkid); container = CommandExecutor.ExecuteCommand(extractGeometryCommand); if (container == null) { var message = "Geometry coordinates appear to be invalid."; if (!string.IsNullOrEmpty(extractGeometryCommand.ErrorMessage)) { message += " Maybe this information can help: {0}".With(extractGeometryCommand.ErrorMessage); } errors.Add(message); } //input is in different projection - reproject it if (wkid != 26912) { if (srFactory == null) { srFactory = new SpatialReferenceEnvironmentClass(); } var toUtm = srFactory.CreateProjectedCoordinateSystem(26912); if (container != null) { if ((container.Coordinates == null || container.Coordinates.Count < 1) && container.Geometry != null) { container.Geometry.Project(toUtm); } else { foreach (var points in container.Coordinates) { var point = new PointClass { X = points[0], Y = points[1], SpatialReference = newSpatialRefefence }; point.Project(toUtm); if (point.IsEmpty) { errors.Add("Input geometry is empty. Check your x and y values."); return(Json(errors)); } container.Geometry = point; points[0] = point.X; points[1] = point.Y; } } } } //buffer point - set container type to polygon if (bufferInput > 0 && container != null) { container.Geometry = CommandExecutor.ExecuteCommand(new BufferGeometryCommand(container, bufferInput)); container.Type = "POLYGON"; } } var sdeConnector = SdeConnectorFactory.Create(featureClass); if (sdeConnector == null) { errors.Add("{0} was not found in our database. ".With(featureClass) + "A valid example would be SGID10.BOUNDARIES.Counties."); } if (errors.HasErrors) { return(Json(errors)); } // ReSharper disable PossibleNullReferenceException because of returning errors if null var workspace = sdeConnector.Connect(); // ReSharper restore PossibleNullReferenceException if (workspace is not IFeatureWorkspace featureWorkspace) { errors.Add("Error connecting to SDE."); return(Json(errors)); } var values = returnValues.Split(',').Select(x => x.Trim()).ToArray(); var commandToExecute = SpatialCommandFactory.Get(container, featureWorkspace, featureClass, values, predicate, newSpatialRefefence); var result = CommandExecutor.ExecuteCommand(commandToExecute); if (!string.IsNullOrEmpty(commandToExecute.ErrorMessage)) { errors.Add(commandToExecute.ErrorMessage); return(Json(errors)); } return(Json(result)); }
/// <summary> /// Handles the incoming rest requests /// </summary> /// <param name="boundVariables"> The bound variables. </param> /// <param name="operationInput"> The operation input. </param> /// <param name="outputFormat"> The output format. </param> /// <param name="requestProperties"> The request properties. </param> /// <param name="responseProperties"> The response properties. </param> /// <returns> </returns> /// <exception cref="System.ArgumentNullException"></exception> public static byte[] Handler(NameValueCollection boundVariables, JsonObject operationInput, string outputFormat, string requestProperties, out string responseProperties) { responseProperties = null; var errors = new ErrorModel(400); string layerName, relationshipClassName; double?utmx, utmy; object[] attributeListObj, relationshipAttributeListObj; var found = operationInput.TryGetString("layerName", out layerName); if (!found || string.IsNullOrEmpty(layerName)) { throw new ArgumentNullException("layerName"); } found = operationInput.TryGetString("relationshipClassName", out relationshipClassName); if (!found || string.IsNullOrEmpty(relationshipClassName)) { throw new ArgumentNullException("relationshipClassName"); } found = operationInput.TryGetAsDouble("utmx", out utmx); if (!found || !utmx.HasValue) { throw new ArgumentNullException("utmx"); } found = operationInput.TryGetAsDouble("utmy", out utmy); if (!found || !utmy.HasValue) { throw new ArgumentNullException("utmy"); } found = operationInput.TryGetArray("attributeList", out attributeListObj); if (!found || attributeListObj == null || attributeListObj.Length < 1) { throw new ArgumentNullException("attributeList"); } found = operationInput.TryGetArray("relationshipAttributeListObj", out relationshipAttributeListObj); if (!found || relationshipAttributeListObj == null || relationshipAttributeListObj.Length < 1) { throw new ArgumentNullException("relationshipAttributeListObj"); } var attributeList = attributeListObj.Cast <string>().ToArray(); var relatedAttributeList = relationshipAttributeListObj.Cast <string>().ToArray(); var searchArgs = new PointInPolyArgsWithRelation(layerName, utmx.Value, utmy.Value, attributeList, relatedAttributeList, relationshipClassName); var connector = SdeConnectorFactory.Create(layerName); if (connector == null) { return(Json(new { Message = "Database does not exist for {0}".With(layerName) })); } var workspace = connector.Connect(); var featureWorkSpace = workspace as IFeatureWorkspace; if (featureWorkSpace == null) { errors.Message = "Error connecting to SDE."; return(Json(errors)); } var response = CommandExecutor.ExecuteCommand(new PointInPolygoinQueryCommand(searchArgs, featureWorkSpace)); if (response == null) { errors.Message = "No features found in {2} at the location {0}, {1}.".With( searchArgs.Point.X, searchArgs.Point.Y, searchArgs.LayerName); return(Json(errors)); } return(Json(response)); }
/// <summary> /// Handles the incoming rest requests /// </summary> /// <param name="boundVariables"> The bound variables. </param> /// <param name="operationInput"> The operation input. </param> /// <param name="outputFormat"> The output format. </param> /// <param name="requestProperties"> The request properties. </param> /// <param name="responseProperties"> The response properties. </param> /// <returns> </returns> /// <exception cref="System.ArgumentNullException"></exception> public static byte[] Handler(NameValueCollection boundVariables, JsonObject operationInput, string outputFormat, string requestProperties, out string responseProperties) { responseProperties = null; var errors = new ErrorModel(400); string layerName; double?utmx, utmy; var found = operationInput.TryGetString("layerName", out layerName); if (!found || string.IsNullOrEmpty(layerName)) { throw new ArgumentNullException("layerName"); } found = operationInput.TryGetAsDouble("utmx", out utmx); if (!found || !utmx.HasValue) { throw new ArgumentNullException("utmx"); } found = operationInput.TryGetAsDouble("utmy", out utmy); if (!found || !utmy.HasValue) { throw new ArgumentNullException("utmy"); } var connector = SdeConnectorFactory.Create(layerName); if (connector == null) { return(Json(new { Message = "Database does not exist for {0}".With(layerName) })); } var workspace = connector.Connect(); var featureWorkSpace = workspace as IFeatureWorkspace; if (featureWorkSpace == null) { errors.Message = "Error connecting to SDE."; return(Json(errors)); } var rasterArgs = new RasterArgs(layerName, utmx, utmy); var response = CommandExecutor.ExecuteCommand(new GetValueFromRasterCommand(rasterArgs, featureWorkSpace as IRasterWorkspaceEx)); if (response == null) { errors.Message = "No features found in {2} at the location {0}, {1}.".With( rasterArgs.X, rasterArgs.Y, rasterArgs.LayerName); return(Json(errors)); } return(Json(response)); }