Create an elevation profile using a geoprocessing package executed with Local Server.
Use case
Applications that include Local Server are valuable in offline workflows that require advanced spatial analysis or data manipulation. This sample uses a geoprocessing package (.gpkx
) created in ArcGIS Pro involving a custom geoprocessing model that includes the Interpolate Shape (3D Analyst) geoprocessing tool. The geoprocessing package is executed with ArcGIS Local Server.
You can generate elevation profiles to carry out topographical analysis of valley profiles, or visualize a hiking, cycling, or road trip over varied topography.
How to use the sample
The sample loads at the full extent of the raster dataset. Click the "Draw Polyline" button and sketch a polyline along where you'd like the elevation profile to be calculated (the polyline can be any shape). Click the "Save" button to save the sketch and draw the polyline. Click "Generate Elevation Profile" to interpolate the sketched polyline onto the raster surface in 3D. Once ready, the view will automatically zoom onto the newly drawn elevation profile. Click "Clear Results" to reset the sample.
How it works
- Create a
Raster
from a raster dataset, and apply a series ofRasterFunction
s to mask any data at or below sea level. - Start the Local Server instance with
LocalServer.Instance.StartAsync()
. - Start a
LocalGeoprocessingService
and create aGeoprocessingTask
.- Instantiate
LocalGeoprocessingService(Url, ServiceType)
to create a local geoprocessing service. - Invoke
LocalGeoprocessingService.StartAsync()
to start the service asynchronously. - Instantiate
GeoprocessingTask.CreateAsync(LocalGeoprocessingService.Url + "/CreateElevationProfileModel")
to create a geoprocessing task that uses the elevation profile tool.
- Instantiate
- Create an instance of
GeoprocessingParameters
and get its list of inputs withGeoprocessingParameters.Inputs
. - Add
GeoprocessingFeatures
with aFeatureCollectionTable
pointing to a polyline geometry, andGeoprocessingString
with a path to the raster data on disk to the list of inputs. - Create and start a
GeoprocessingJob
using the input parameters.- Create a geoprocessing job with
GeoprocessingTask.CreateJob(GeoprocessingParameters)
. - Start the job with
GeoprocessingJob.Start()
.
- Create a geoprocessing job with
- Add generated elevation profile as a
FeatureLayer
to the scene.- Get the url from the local geoprocessing service using
LocalGeoprocessingService.Url
. - Get the server job id of the geoprocessing job using
GeoprocessingJob.ServerJobId
. - Replace
GPServer
from the url withMapServer/jobs/jobId
, to get generate elevation profile data. - Create a
ServiceGeodatabase
from the derived url and create aFeatureLayer
from the firstFeatureTable
. - Set the surface placement mode and add a renderer to the feature layer, then add the new layer to the scene's list of operational layers.
- Get the url from the local geoprocessing service using
Relevant API
- GeoprocessingFeatures
- GeoprocessingJob
- GeoprocessingParameter
- GeoprocessingParameters
- GeoprocessingTask
- LocalGeoprocessingService
- LocalGeoprocessingService.ServiceType
- LocalServer
- LocalServerStatus
- Raster
- RasterFunction
About the data
This sample loads with a 10m resolution digital terrain elevation model of the Island of Arran, Scotland (data Copyright Scottish Government and Sepa 2014).
Three raster functions (json format) are applied to the raster data to mask out data at or below sea level.
The geoprocessing task is started with a gpkx
. This Create elevation profile geoprocessing package was authored in ArcGIS Pro using ModelBuilder, and the Interpolate Shape (3D Analyst) tool.
Additional information
Local Server can be downloaded for Windows and Linux platforms from your ArcGIS Developers dashboard. Local Server is not supported on macOS.
The Package Result tool in ArcGIS Pro is used to author ArcGIS Maps SDK for Native Apps compatible geoprocessing packages (.gpkx files). For more information on running powerful offline geoprocessing tasks to provide advanced spatial analysis to your applications, see ArcGIS Local Server SDK.
The results of the geoprocessing tasks executed with Local Server can be accessed with code, persisted, and shared, for example as a feature collection portal item. This contrasts with the Scene visibility analyses, viewshed and line of sight, which are calculated dynamically at render-time and are displayed only in analysis overlays.
Tags
elevation profile, geoprocessing, interpolate shape, local server, offline, parameters, processing, raster, raster function, scene, service, terrain
Sample Code
// Copyright 2022 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.
using ArcGIS.Samples.Managers;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.LocalServices;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Rasters;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.Tasks;
using Esri.ArcGISRuntime.Tasks.Geoprocessing;
using Esri.ArcGISRuntime.UI;
using Esri.ArcGISRuntime.UI.Controls;
using Microsoft.UI.Xaml;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
namespace ArcGIS.WinUI.Samples.LocalServerGenerateElevationProfile
{
[ArcGIS.Samples.Shared.Attributes.Sample(
name: "Generate elevation profile with Local Server",
category: "Local Server",
description: "Create an elevation profile using a geoprocessing package executed with Local Server.",
instructions: "The sample loads at the full extent of the raster dataset. Click the \"Draw Polyline\" button and sketch a polyline along where you'd like the elevation profile to be calculated (the polyline can be any shape). Click the \"Save\" button to save the sketch and draw the polyline. Click \"Generate Elevation Profile\" to interpolate the sketched polyline onto the raster surface in 3D. Once ready, the view will automatically zoom onto the newly drawn elevation profile. Click \"Clear Results\" to reset the sample.",
tags: new[] { "elevation profile", "geoprocessing", "interpolate shape", "local server", "offline", "parameters", "processing", "raster", "raster function", "scene", "service", "terrain" })]
[ArcGIS.Samples.Shared.Attributes.OfflineData("db9cd9beedce4e0987c33c198c8dfb45", "259f420250a444b4944a277eec2c4e42", "831cbdc61b1c4cd3bfedd1af91d09d36")]
public partial class LocalServerGenerateElevationProfile
{
// Hold references to the paths for the files used in this sample.
private readonly string _rasterPath = DataManager.GetDataFolder("db9cd9beedce4e0987c33c198c8dfb45", "Arran_10m_raster.tif");
private readonly string _geoprocessingPackagePath = DataManager.GetDataFolder("831cbdc61b1c4cd3bfedd1af91d09d36", "create_elevation_profile_model.gpkx");
private readonly string _aboveSeaLevelJsonPath = DataManager.GetDataFolder("259f420250a444b4944a277eec2c4e42", "raster_functions", "above_sea_level_raster_calculation.json");
private readonly string _restoreElevationJsonPath = DataManager.GetDataFolder("259f420250a444b4944a277eec2c4e42", "raster_functions", "restore_elevation_raster_calculation.json");
private readonly string _maskJsonPath = DataManager.GetDataFolder("259f420250a444b4944a277eec2c4e42", "raster_functions", "mask.json");
// Hold references for the objects used in event handlers.
private FeatureCollection _featureCollection;
private FeatureLayer _elevationProfileFeatureLayer;
private Polyline _polyline;
private GeoprocessingTask _gpTask;
private GeoprocessingJob _gpJob;
private Raster _arranRaster;
private LocalGeoprocessingService _localGPService;
private GraphicsOverlay _polylineGraphicsOverlay;
private GraphicsOverlay _pointsGraphicsOverlay;
private Viewpoint _rasterExtentViewPoint;
private RasterLayer _rasterLayer;
private SpatialReference _rasterLayerSpatialReference;
private PointCollection _pointCollection;
private bool _isDrawing;
public LocalServerGenerateElevationProfile()
{
InitializeComponent();
_ = Initialize();
}
private async Task Initialize()
{
// Try to start Local Server.
try
{
// LocalServer must not be running when setting the data path.
if (LocalServer.Instance.Status == LocalServerStatus.Started)
{
await LocalServer.Instance.StopAsync();
}
// Set the local data path - must be done before starting. On most systems, this will be C:\EsriSamples\AppData.
// This path should be kept short to avoid Windows path length limitations.
string tempDataPathRoot = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.Windows)).FullName;
string tempDataPath = Path.Combine(tempDataPathRoot, "EsriSamples", "AppData");
Directory.CreateDirectory(tempDataPath); // CreateDirectory won't overwrite if it already exists.
LocalServer.Instance.AppDataPath = tempDataPath;
// Start the local server instance.
await LocalServer.Instance.StartAsync();
// Initialize and start a local geoprocessing service instance using our local geoprocessing package.
_localGPService = new LocalGeoprocessingService(_geoprocessingPackagePath, GeoprocessingServiceType.AsynchronousSubmitWithMapServiceResult);
await _localGPService.StartAsync();
// Initialize a geoprocessing task using the local geoprocessing service.
_gpTask = await GeoprocessingTask.CreateAsync(new Uri(_localGPService.Url + "/CreateElevationProfileModel"));
}
catch (Exception ex)
{
TypeInfo localServerTypeInfo = typeof(LocalMapService).GetTypeInfo();
FileVersionInfo localServerVersion = FileVersionInfo.GetVersionInfo(localServerTypeInfo.Assembly.Location);
await new MessageDialog2($"Please ensure that local server {localServerVersion.FileVersion} is installed prior to using the sample. The download link is in the description. Message: {ex.Message}", "Local Server failed to start").ShowAsync();
return;
}
// Create a scene with a topographic basemap style.
MySceneView.Scene = new Scene(BasemapStyle.ArcGISHillshadeDark);
try
{
// Create a new raster from a local file and display it on the scene.
_arranRaster = new Raster(_rasterPath);
await DisplayRaster(_arranRaster);
}
catch (Exception ex)
{
await new MessageDialog2(ex.Message, "Error").ShowAsync();
}
// Set up a new feature collection.
_featureCollection = new FeatureCollection();
// Create a graphics overlay for displaying the sketched polyline and add it to the scene view's list of graphics overlays.
_polylineGraphicsOverlay = new GraphicsOverlay();
SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Dash, System.Drawing.Color.Black, 3);
_polylineGraphicsOverlay.Renderer = new SimpleRenderer(lineSymbol);
MySceneView.GraphicsOverlays.Add(_polylineGraphicsOverlay);
// Update the UI.
MyInstructionsTitleTextBlock.Visibility = Visibility.Visible;
MyInstructionsContentTextBlock.Text = "Draw a polyline on the scene with the 'Draw Polyline' button.";
MyDrawPolylineButton.IsEnabled = true;
MyButtonPanel.Visibility = Visibility.Visible;
}
private async Task DisplayRaster(Raster raster)
{
try
{
// Get the masked raster after applying the masking function.
Raster maskedRaster = ApplyMaskingRasterFunction(raster);
// Create a raster layer from the masked raster.
_rasterLayer = new RasterLayer(maskedRaster);
// Set a hillshade renderer on the raster layer.
_rasterLayer.Renderer = new HillshadeRenderer(30, 210, 1, SlopeType.None, 1, 1, 8);
// Load the raster layer.
await _rasterLayer.LoadAsync();
// Add the raster layer to the scene.
MySceneView.Scene.OperationalLayers.Add(_rasterLayer);
// Create a viewpoint for the raster layers full extent.
_rasterExtentViewPoint = new Viewpoint(_rasterLayer.FullExtent);
// Update the scene viewpoint based on the raster layer.
await MySceneView.SetViewpointAsync(_rasterExtentViewPoint);
}
catch (Exception e)
{
await new MessageDialog2(e.Message, "Error").ShowAsync();
}
}
private Raster ApplyMaskingRasterFunction(Raster originalRaster)
{
// Initiate raster for output.
Raster maskedRaster;
// Raster function to get pixels above 0m (above sea level).
string aboveSeaLevelRasterFunctionJson = File.ReadAllText(_aboveSeaLevelJsonPath);
RasterFunction aboveSeaLevelRasterFunction = RasterFunction.FromJson(aboveSeaLevelRasterFunctionJson);
// Get the raster arguments and the names of raster variables.
RasterFunctionArguments aboveSeaLevelRasterArguments = aboveSeaLevelRasterFunction.Arguments;
IReadOnlyList<string> aboveSeaLevelRasterNames = aboveSeaLevelRasterArguments.GetRasterNames();
// Apply the raster function to the original raster.
aboveSeaLevelRasterArguments.SetRaster(aboveSeaLevelRasterNames[0], originalRaster);
// Apply the raster function to the input raster, this gets a raster composed of 1s and 0s, 1 represents data above sea level.
Raster aboveSeaLevelRaster = new Raster(aboveSeaLevelRasterFunction);
// Raster function to restore elevation profiles post above sea level calculations.
string restoreElevationRasterFunctionJson = File.ReadAllText(_restoreElevationJsonPath);
RasterFunction restoreElevationRasterFunction = RasterFunction.FromJson(restoreElevationRasterFunctionJson);
// Get the raster arguments and the names of raster variables.
RasterFunctionArguments restoreElevationRasterArguments = restoreElevationRasterFunction.Arguments;
IReadOnlyList<string> restoreElevationRasterNames = restoreElevationRasterArguments.GetRasterNames();
// Apply the raster function to the original raster and the above sea level raster.
restoreElevationRasterArguments.SetRaster(restoreElevationRasterNames[0], originalRaster);
restoreElevationRasterArguments.SetRaster(restoreElevationRasterNames[1], aboveSeaLevelRaster);
// Create a new raster with elevation values restored above 0.
Raster restoredElevationRaster = new Raster(restoreElevationRasterFunction);
// Raster function to mask out values below sea level (pixels with value of 0).
string maskRasterFunctionJson = File.ReadAllText(_maskJsonPath);
RasterFunction maskRasterFunction = RasterFunction.FromJson(maskRasterFunctionJson);
// Get the raster arguments and the names of raster variables.
RasterFunctionArguments maskRasterArguments = maskRasterFunction.Arguments;
IReadOnlyList<string> maskRasterNames = maskRasterArguments.GetRasterNames();
// Apply the raster function to the restored elevation raster.
maskRasterArguments.SetRaster(maskRasterNames[0], restoredElevationRaster);
// Create a new raster with values equal to 0 masked out.
maskedRaster = new Raster(maskRasterFunction);
return maskedRaster;
}
private void GenerateElevationProfileButton_Click(object sender, RoutedEventArgs e)
{
_ = GenerateElevationProfileTask();
}
private async Task GenerateElevationProfileTask()
{
try
{
// Update the UI.
MyGenerateElevationProfileButton.IsEnabled = false;
MyProgressBar.Visibility = Visibility.Visible;
// Create the feature collection table from the sketched polyline.
await CreateFeatureCollectionTableWithPolylineFeature();
// Create default parameters and get their inputs.
GeoprocessingParameters defaultParams = await _gpTask.CreateDefaultParametersAsync();
IDictionary<string, GeoprocessingParameter> inputParameters = defaultParams.Inputs;
// Ensure the feature collection is loaded before attempting to access the feature table it contains.
await _featureCollection.LoadAsync();
// Set the input polyline parameter geoprocessing feature, pointing to polyline.
inputParameters["Input_Polyline"] = new GeoprocessingFeatures(_featureCollection.Tables[0]);
// Set the input raster path geoprocessing string, pointing to raster file.
inputParameters["Input_Raster"] = new GeoprocessingString(_arranRaster.Path);
// Create geoprocessing job from the geoprocessing parameters to show elevation profile on the scene.
_gpJob = _gpTask.CreateJob(defaultParams);
// Add the event handlers to the geoprocessing job.
_gpJob.StatusChanged += GpJob_StatusChanged;
_gpJob.ProgressChanged += GpJob_ProgressChanged;
// Update UI.
MyProgressBar.Value = 0;
MyProgressBar.Visibility = Visibility.Visible;
MyProgressBarLabel.Visibility = Visibility.Visible;
MyCancelJobButton.Visibility = Visibility.Visible;
MyGenerateElevationProfileButton.Visibility = Visibility.Collapsed;
MyDrawPolylineButton.Visibility = Visibility.Collapsed;
MyClearResultsButton.Visibility = Visibility.Collapsed;
_gpJob.Start();
}
catch (Exception e)
{
await new MessageDialog2(e.Message, "Error").ShowAsync();
}
}
private void GpJob_ProgressChanged(object sender, EventArgs e)
{
DispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal, () =>
{
// Update the UI.
MyProgressBar.Value = _gpJob.Progress;
MyProgressBarLabel.Text = $"{MyProgressBar.Value}%";
});
}
private void GpJob_StatusChanged(object sender, JobStatus e)
{
if (e == JobStatus.Succeeded)
{
_ = GeoprocessingJobSucceededTask();
}
}
private async Task GeoprocessingJobSucceededTask()
{
try
{
// Convert geoprocessor server url to that of a map server and get the job id.
Uri serviceUrl = _localGPService.Url;
string mapServerUrl = serviceUrl.ToString().Replace("GPServer", "MapServer/jobs/" + _gpJob.ServerJobId);
// Create a service geodatabase from the map server url.
var serviceGeodatabase = new ServiceGeodatabase(new Uri(mapServerUrl));
// Load the service geodatabase.
await serviceGeodatabase.LoadAsync();
// Get the feature table from the service geodatabase.
FeatureTable featureTable = serviceGeodatabase.GetTable(0);
// Instantiate the elevation profile feature layer from the feature table.
_elevationProfileFeatureLayer = new FeatureLayer(featureTable);
// Load the elevation profile feature layer.
await _elevationProfileFeatureLayer.LoadAsync();
// Set the surface placement and renderer for the elevation profile feature layer.
_elevationProfileFeatureLayer.SceneProperties.SurfacePlacement = SurfacePlacement.Absolute;
_elevationProfileFeatureLayer.Renderer = new SimpleRenderer(new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, System.Drawing.Color.White, 3));
DispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal, () =>
{
// Add the elevation profile feature layer to the scene.
MySceneView.Scene.OperationalLayers.Add(_elevationProfileFeatureLayer);
// Update the UI.
MyInstructionsContentTextBlock.Text = "Elevation Profile drawn.";
MyGenerateElevationProfileButton.Visibility = Visibility.Visible;
MyDrawPolylineButton.Visibility = Visibility.Visible;
MyClearResultsButton.Visibility = Visibility.Visible;
MyClearResultsButton.IsEnabled = true;
MyCancelJobButton.Visibility = Visibility.Collapsed;
MyProgressBar.Visibility = Visibility.Collapsed;
MyProgressBarLabel.Visibility = Visibility.Collapsed;
});
// Set the viewpoint based on the position of the elevation profile.
await MySceneView.SetViewpointCameraAsync(CreateCameraFacingElevationProfile(), new TimeSpan(0, 0, 0, 2, 0));
}
catch (Exception e)
{
await new MessageDialog2(e.Message, "Error").ShowAsync();
}
finally
{
// Remove the event handlers.
_gpJob.ProgressChanged -= GpJob_ProgressChanged;
_gpJob.StatusChanged -= GpJob_StatusChanged;
}
}
private Camera CreateCameraFacingElevationProfile()
{
// Get the polyline's end point coordinates.
MapPoint endPoint = _polyline.Parts[0].EndPoint;
double endPointX = endPoint.X;
double endPointY = endPoint.Y;
// Get the polyline's center point coordinates.
MapPoint centerPoint = _polyline.Extent.GetCenter();
double centerX = centerPoint.X;
double centerY = centerPoint.Y;
// Calculate the position of a point perpendicular to the centre of the polyline.
double lengthX = endPointX - centerX;
double lengthY = endPointY - centerY;
var cameraPositionPoint = new MapPoint(centerX + lengthY * 2, centerY - lengthX * 2, 1200,
SpatialReferences.WebMercator);
// Calculate the heading for the camera position so that it points perpendicularly towards the elevation profile.
double theta;
double cameraHeadingPerpToProfile;
// Account for switching opposite and adjacent depending on angle direction from drawn line.
if (lengthY < 0)
{
// Account for a downwards angle.
theta = (180 / Math.PI) * (Math.Atan((centerX - endPointX) / (centerY - endPointY)));
cameraHeadingPerpToProfile = theta + 90;
}
else
{
// Account for an upwards angle.
theta = (180 / Math.PI) * (Math.Atan((centerY - endPointY) / (centerX - endPointX)));
// Determine if theta is positive or negative, then account accordingly for calculating the angle back from north
// and then rotate that value by + or - 90 to get the angle perpendicular to the drawn line.
double angleFromNorth = (90 - theta);
// If theta is positive, rotate angle anticlockwise by 90 degrees, else, clockwise by 90 degrees.
if (theta > 0)
{
cameraHeadingPerpToProfile = angleFromNorth - 90;
}
else
{
cameraHeadingPerpToProfile = angleFromNorth + 90;
}
}
// Create a new camera from the calculated camera position point and camera angle perpendicular to profile.
return new Camera(cameraPositionPoint, cameraHeadingPerpToProfile, 80, 0);
}
private async Task CreateFeatureCollectionTableWithPolylineFeature()
{
// Create name field for polyline.
List<Field> polylineField = new List<Field>();
polylineField.Add(Field.CreateString("Name", "Name of feature", 20));
// Create a feature collection table.
FeatureCollectionTable featureCollectionTable = new FeatureCollectionTable(polylineField, GeometryType.Polyline, SpatialReferences.WebMercator, true, false);
// Load the feature collection table.
await featureCollectionTable.LoadAsync();
// Add the feature collection table to the feature collection used to generate the elevation profile.
_featureCollection.Tables.Add(featureCollectionTable);
// Create an attributes dictionary for the feature.
var attributes = new Dictionary<string, object>();
attributes["ElevationSection"] = polylineField[0].Name;
// Create a polyline feature and add it to the feature collection table.
Feature addedFeature = featureCollectionTable.CreateFeature(attributes, _polyline);
await featureCollectionTable.AddFeatureAsync(addedFeature);
}
private void DrawPolylineButton_Click(object sender, RoutedEventArgs e)
{
_isDrawing = true;
// Update the UI.
MyInstructionsContentTextBlock.Text = "Click on the map to draw a polyline path, click the 'Save' button to save it.";
MyGenerateElevationProfileButton.Visibility = Visibility.Collapsed;
MyClearResultsButton.Visibility = Visibility.Collapsed;
MyDrawPolylineButton.Visibility = Visibility.Collapsed;
MyDrawPolylineButton.IsEnabled = false;
MySaveButton.Visibility = Visibility.Visible;
// Create a temporary graphics overlay to display a point collection on the map.
_pointsGraphicsOverlay = new GraphicsOverlay();
MySceneView.GraphicsOverlays.Add(_pointsGraphicsOverlay);
// Create a cross markery symbol for the tapped points.
var simpleMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Cross, System.Drawing.Color.Black, 10);
_pointsGraphicsOverlay.Renderer = new SimpleRenderer(simpleMarkerSymbol);
// Create a point collection with the same spatial reference as the raster layer.
_rasterLayerSpatialReference = _rasterLayer.SpatialReference;
_pointCollection = new PointCollection(_rasterLayerSpatialReference);
MySceneView.GeoViewTapped += MySceneView_GeoViewTapped;
}
private void MySceneView_GeoViewTapped(object sender, GeoViewInputEventArgs e)
{
if (_isDrawing)
{
_ = GeoViewTappedTask(e);
}
}
private async Task GeoViewTappedTask(GeoViewInputEventArgs e)
{
try
{
// Get the location from the tapped screen position.
MapPoint point = await MySceneView.ScreenToLocationAsync(e.Position);
// In order to project the tapped point, the point must have a valid spatial reference.
if (point.SpatialReference == null)
{
await new MessageDialog2("Clicked point must contain a valid spatial reference.", "Error").ShowAsync();
return;
}
// Project the point to the spatial reference of the raster layer.
MapPoint projectedPoint = (MapPoint)point.Project(_rasterLayerSpatialReference);
// Check that the user has clicked within the extent of the raster.
if (projectedPoint.Intersects(_rasterLayer.FullExtent))
{
// Add the projected point to the collection of projected points and the graphics overlay displaying tapped points.
_pointCollection.Add(projectedPoint);
_pointsGraphicsOverlay.Graphics.Add(new Graphic(projectedPoint));
}
else
{
await new MessageDialog2("Clicked point must be within raster layer extent.", "Error").ShowAsync();
}
}
catch (Exception ex)
{
await new MessageDialog2(ex.Message, "Error").ShowAsync();
}
}
private void ClearResultsButton_Click(object sender, RoutedEventArgs e)
{
_ = ClearResultsTask();
}
private async Task ClearResultsTask()
{
try
{
// Remove all graphics.
_polylineGraphicsOverlay.Graphics.Clear();
// Clear the temporary graphics overlay.
_pointsGraphicsOverlay.Graphics.Clear();
// If the elevation profile feature layer has been added to the scene, remove it.
if (MySceneView.Scene.OperationalLayers.Count > 1)
{
// Remove last elevation profile feature layer from the scene.
MySceneView.Scene.OperationalLayers.Remove(_elevationProfileFeatureLayer);
}
// If more than one graphics overlay is present, remove the temporary points overlay.
if (MySceneView.GraphicsOverlays.Count > 1)
{
MySceneView.GraphicsOverlays.Remove(_pointsGraphicsOverlay);
}
// Update the UI.
MyGenerateElevationProfileButton.IsEnabled = false;
MyDrawPolylineButton.IsEnabled = true;
MyClearResultsButton.IsEnabled = false;
MyInstructionsContentTextBlock.Visibility = Visibility.Visible;
MyInstructionsContentTextBlock.Text = "Draw a polyline on the scene with the 'Draw Polyline' button.";
// Set the viewpoint to the initial raster layer extent view.
await MySceneView.SetViewpointAsync(_rasterExtentViewPoint);
// Clear the elevation polyline data from the feature collection.
_featureCollection.Tables.Clear();
}
catch (Exception e)
{
await new MessageDialog2(e.Message, "Error").ShowAsync();
}
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
_ = SaveTask();
}
private async Task SaveTask()
{
if (_pointsGraphicsOverlay.Graphics.Count > 1)
{
_isDrawing = false;
// Remove the temporarily created points from the map.
_pointsGraphicsOverlay.Graphics.Clear();
// Create a polyline from the clicked points on the scene and add it as a graphic to the graphics overlay.
_polyline = new Polyline(_pointCollection);
Graphic graphic = new Graphic(_polyline);
_polylineGraphicsOverlay.Graphics.Add(graphic);
// Update the UI.
MyGenerateElevationProfileButton.Visibility = Visibility.Visible;
MyGenerateElevationProfileButton.IsEnabled = true;
MyDrawPolylineButton.Visibility = Visibility.Visible;
MyDrawPolylineButton.IsEnabled = false;
MyClearResultsButton.Visibility = Visibility.Visible;
MyClearResultsButton.IsEnabled = true;
MyInstructionsContentTextBlock.Text = "Generate an elevation profile along the polyline using the 'Generate Elevation Profile' button.";
MySaveButton.Visibility = Visibility.Collapsed;
// Clear the polyline points.
_pointCollection.Clear();
MySceneView.GeoViewTapped -= MySceneView_GeoViewTapped;
}
else
{
await new MessageDialog2("More than one point required to draw a polyline.", "Warning").ShowAsync();
}
}
private void CancelJobButton_Click(object sender, RoutedEventArgs e)
{
if (_gpJob != null)
{
_ = _gpJob.CancelAsync();
// Remove the event handlers.
_gpJob.ProgressChanged -= GpJob_ProgressChanged;
_gpJob.StatusChanged -= GpJob_StatusChanged;
// Update the UI.
MyCancelJobButton.Visibility = Visibility.Collapsed;
MyProgressBar.Visibility = Visibility.Collapsed;
MyProgressBarLabel.Visibility = Visibility.Collapsed;
MyGenerateElevationProfileButton.Visibility = Visibility.Visible;
MyGenerateElevationProfileButton.IsEnabled = true;
MyDrawPolylineButton.Visibility = Visibility.Visible;
MyClearResultsButton.Visibility = Visibility.Visible;
}
}
}
}