private async Task CalculateViewshed(MapPoint location)
{
// This function will define a new geoprocessing task that performs a custom viewshed analysis based upon a
// user click on the map and then display the results back as a polygon fill graphics overlay. If there
// is a problem with the execution of the geoprocessing task an error message will be displayed
// Create new geoprocessing task using the url defined in the member variables section
var myViewshedTask = new GeoprocessingTask(new Uri(_viewshedUrl));
// Create a new feature collection table based upon point geometries using the current map view spatial reference
var myInputFeatures = new FeatureCollectionTable(new List<Field>(), GeometryType.Point, MyMapView.SpatialReference);
// Create a new feature from the feature collection table. It will not have a coordinate location (x,y) yet
Feature myInputFeature = myInputFeatures.CreateFeature();
// Assign a physical location to the new point feature based upon where the user clicked in the map view
myInputFeature.Geometry = location;
// Add the new feature with (x,y) location to the feature collection table
await myInputFeatures.AddFeatureAsync(myInputFeature);
// Create the parameters that are passed to the used geoprocessing task
GeoprocessingParameters myViewshedParameters =
new GeoprocessingParameters(GeoprocessingExecutionType.SynchronousExecute);
// Request the output features to use the same SpatialReference as the map view
myViewshedParameters.OutputSpatialReference = MyMapView.SpatialReference;
// Add an input location to the geoprocessing parameters
myViewshedParameters.Inputs.Add("Input_Observation_Point", new GeoprocessingFeatures(myInputFeatures));
// Create the job that handles the communication between the application and the geoprocessing task
var myViewshedJob = myViewshedTask.CreateJob(myViewshedParameters);
try
{
// Execute analysis and wait for the results
GeoprocessingResult myAnalysisResult = await myViewshedJob.GetResultAsync();
// Get the results from the outputs
GeoprocessingFeatures myViewshedResultFeatures = myAnalysisResult.Outputs["Viewshed_Result"] as GeoprocessingFeatures;
// Add all the results as a graphics to the map
IFeatureSet myViewshedAreas = myViewshedResultFeatures.Features;
foreach (var myFeature in myViewshedAreas)
{
_resultOverlay.Graphics.Add(new Graphic(myFeature.Geometry));
}
}
catch (Exception ex)
{
// Display an error message if there is a problem
if (myViewshedJob.Status == JobStatus.Failed && myViewshedJob.Error != null)
MessageBox.Show("Executing geoprocessing failed. " + myViewshedJob.Error.Message, "Geoprocessing error");
else
MessageBox.Show("An error occurred. " + ex.ToString(), "Sample error");
}
finally
{
// Indicate that the geoprocessing is not running
SetBusy(false);
}
}