Drive Times Feature Action

This Operations Dashboard for ArcGIS sample demonstrates how to implement a custom feature action that calculates a drive time polygon around a specific feature and shows the resulting polygon in the map widget. The calculation is done by calling a web service. Note that the service only works on features in the US.
Operations Dashboard for ArcGIS samples are supported only in Visual Studio 2012. A live preview is not available.
To run this sample, open the solution in Visual Studio 2012, set the Start Action and Start Options debug options in the Project Properties as described in the Testing add-ins help topic, and then build and run the project.
Download Sample ApplicationThis sample is implemented in C# and Visual Basic code, and does not include a Xaml file.
// Copyright 2013 ESRI // // All rights reserved under the copyright laws of the United States // and applicable international laws, treaties, and conventions. // // You may freely redistribute and use this sample code, with or // without modification, provided you include the original copyright // notice and use restrictions. // // See the use restrictions http://help.arcgis.com/en/sdk/10.0/usageRestrictions.htm. // using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.ComponentModel.Composition; using System.Windows.Media; using ESRI.ArcGIS.OperationsDashboard; using client = ESRI.ArcGIS.Client; namespace DriveTimesFeatureActionCS { [Export("ESRI.ArcGIS.OperationsDashboard.FeatureAction")] [ExportMetadata("DisplayName", "DriveTimes Feature Action C#")] [ExportMetadata("Caption", "Show Drive Times C#")] [ExportMetadata("Description", "Custom drive-times feature action sample C#")] [ExportMetadata("ImagePath", "/DriveTimesFeatureActionCS;component/Images/CarRedFront.png")] public class DriveTimesFeatureAction : IFeatureAction { string _serviceUrl = "http://sampleserver1a.arcgisonline.com/ArcGIS/rest/services/Network/ESRI_DriveTime_US/GPServer/CreateDriveTimePolygons"; string _driveTimeString = "10"; client.Tasks.Geoprocessor _geoprocessorTask = null; // Define a separate graphics layer to contain viewshed graphics, and a symbol to draw them. client.GraphicsLayer _graphics = null; client.Symbols.SimpleFillSymbol _sym = new client.Symbols.SimpleFillSymbol() { Fill = new SolidColorBrush(Color.FromArgb(128, 255, 0, 0)) }; public DriveTimesFeatureAction() { // Set up a GP task to point to the drive times service on SampleServer1 _geoprocessorTask = new client.Tasks.Geoprocessor(_serviceUrl); _geoprocessorTask.ExecuteCompleted += ((sender, args) => { // When the task completes, look for GPFeatureRecordSetLayers in the output, and iterate any features // in the layer, adding them to the map as graphics. if ((args.Results != null) && (args.Results.OutParameters != null) && (args.Results.OutParameters.Count > 0)) { // Expecting a single output parameter. client.Tasks.GPFeatureRecordSetLayer layer = args.Results.OutParameters[0] as client.Tasks.GPFeatureRecordSetLayer; if (layer != null) { // Expecting a single feature for the single input, but code here adds a graphic for all features returned, // may be usesful for example if the Drive Times input parameter is changed. foreach (client.Graphic graphic in layer.FeatureSet.Features) { graphic.Symbol = _sym; _graphics.Graphics.Add(graphic); } } } }); // If the task fails, inform the user. _geoprocessorTask.Failed += ((s, e) => { System.Windows.MessageBox.Show("Geoprocessor service failed: " + e.Error.Message); }); } #region IFeatureAction public bool CanConfigure { get { return false; } } public bool Configure(System.Windows.Window owner) { // Not implemented, as CanConfigure returned true. throw new NotImplementedException(); } public bool CanExecute(ESRI.ArcGIS.OperationsDashboard.DataSource dataSource, client.Graphic feature) { // Check if the data source and feature can be used with this feature action. // Find out if the data source is ultimately from a map widget, and also that the input feature is a point. MapWidget mapW = MapWidget.FindMapWidget(dataSource); return ((mapW != null) && (feature.Geometry is client.Geometry.MapPoint)); } public void Execute(ESRI.ArcGIS.OperationsDashboard.DataSource dataSource, client.Graphic feature) { // Cancel any preexisting execution. _geoprocessorTask.CancelAsync(); // Remove any graphics added previously. if (_graphics != null) _graphics.ClearGraphics(); // Get the map widget associated with the data source passed to the action. MapWidget mapW = MapWidget.FindMapWidget(dataSource); if (mapW == null) return; client.Map map = mapW.Map; // Only work with data sources ultimately based on a feature layer in a map widget. if (map != null) { // Ensure the output spatial reference is set appropriately, _geoprocessorTask.OutputSpatialReference = feature.Geometry.SpatialReference; // Create separate layer for drive times graphics. if (_graphics == null) { _graphics = new ESRI.ArcGIS.Client.GraphicsLayer(); _graphics.ID = "TempDriveTimeGraphics"; // Add the new layer to the accelerated display layers collection. client.AcceleratedDisplayLayers aclyrs = map.Layers.FirstOrDefault(lyr => lyr is client.AcceleratedDisplayLayers) as client.AcceleratedDisplayLayers; aclyrs.ChildLayers.Add(_graphics); } // Create the input geometry for the drive times service, based on the feature passed to the feature action. client.Geometry.MapPoint inputPt = feature.Geometry as client.Geometry.MapPoint; if (inputPt.SpatialReference == null) inputPt.SpatialReference = feature.Geometry.SpatialReference;// Ensure spatial reference is set. // Create the parameters and pass them to the drive times service and begin to execute a call to the service. List<client.Tasks.GPParameter> parameters = new List<client.Tasks.GPParameter>(); parameters.Add(new client.Tasks.GPFeatureRecordSetLayer("Input_Location", inputPt)); parameters.Add(new client.Tasks.GPString("Drive_Times", _driveTimeString)); _geoprocessorTask.ExecuteAsync(parameters); } } #endregion } }
' Copyright 2013 ESRI ' ' All rights reserved under the copyright laws of the United States ' and applicable international laws, treaties, and conventions. ' ' You may freely redistribute and use this sample code, with or ' without modification, provided you include the original copyright ' notice and use restrictions. ' ' See the use restrictions http://help.arcgis.com/en/sdk/10.0/usageRestrictions.htm. ' Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Text Imports System.Threading Imports System.Threading.Tasks Imports System.Windows Imports System.ComponentModel.Composition Imports System.Runtime.Serialization Imports System.Windows.Media Imports ESRI.ArcGIS.OperationsDashboard Imports client = ESRI.ArcGIS.Client ''' <summary> ''' A FeatureAction is an extension to ArcGIS Operations Dashboard which can be shown when a user right-clicks on ''' a feature in a widget. ''' </summary> <Export("ESRI.ArcGIS.OperationsDashboard.FeatureAction"), ExportMetadata("DisplayName", "DriveTimes Feature Action VB.Net"), ExportMetadata("Caption", "Show Drive Times VB.Net"), ExportMetadata("Description", "Custom drive-times feature action sample VB.Net"), ExportMetadata("ImagePath", "/DriveTimesFeatureActionVB;component/Images/CarRedFront.png")> Public Class DriveTimesFeatureAction Implements IFeatureAction Dim _serviceUrl As String = "http://sampleserver1a.arcgisonline.com/ArcGIS/rest/services/Network/ESRI_DriveTime_US/GPServer/CreateDriveTimePolygons" Dim _driveTimeString As String = "10" Dim _geoprocessorTask As client.Tasks.Geoprocessor = Nothing ' Define a separate graphics layer to contain viewshed graphics, and a symbol to draw them. Dim _graphics As client.GraphicsLayer = Nothing Dim _sym As client.Symbols.SimpleFillSymbol = New client.Symbols.SimpleFillSymbol() With { .Fill = New SolidColorBrush(Color.FromArgb(128, 255, 0, 0)) } Public Sub New() ' Set up a GP task to point to the drive times service on SampleServer1 _geoprocessorTask = New client.Tasks.Geoprocessor(_serviceUrl) AddHandler _geoprocessorTask.ExecuteCompleted, AddressOf gp_ExecuteCompleted AddHandler _geoprocessorTask.Failed, AddressOf gp_Failed End Sub Public ReadOnly Property CanConfigure As Boolean Implements IFeatureAction.CanConfigure Get Return False End Get End Property Public Function Configure(owner As Window) As Boolean Implements IFeatureAction.Configure ' Not implemented as CanConfigure returned false. Throw New NotImplementedException() End Function Public Function CanExecute(dataSource As DataSource, feature As client.Graphic) As Boolean Implements IFeatureAction.CanExecute ' Check if the data source and feature can be used with this feature action. ' Find out if the data source is ultimately from a map widget, and also that the input feature is a point. Dim mapW As MapWidget = MapWidget.FindMapWidget(dataSource) Return ((Not mapW Is Nothing) AndAlso (TypeOf feature.Geometry Is client.Geometry.MapPoint)) End Function Public Sub Execute(dataSource As DataSource, feature As client.Graphic) Implements IFeatureAction.Execute ' Cancel any preexisting execution. _geoprocessorTask.CancelAsync() ' Remove any graphics added previously. If (Not _graphics Is Nothing) Then _graphics.ClearGraphics() End If ' Get the map widget associated with the data source passed to the action. Dim mapW As MapWidget = MapWidget.FindMapWidget(dataSource) If (mapW Is Nothing) Then Return Dim map As client.Map = mapW.Map ' Only work with data sources ultimately based on a feature layer in a map widget. If (Not map Is Nothing) Then ' Ensure the output spatial reference is set appropriately, _geoprocessorTask.OutputSpatialReference = feature.Geometry.SpatialReference ' Create separate layer for drive times graphics. If (_graphics Is Nothing) Then _graphics = New ESRI.ArcGIS.Client.GraphicsLayer() _graphics.ID = "TempDriveTimeGraphics" ' Add the new layer to the accelerated display layers collection. Dim aclyrs As client.AcceleratedDisplayLayers = CType(map.Layers.FirstOrDefault(Function(lyr) TypeOf lyr Is client.AcceleratedDisplayLayers), client.AcceleratedDisplayLayers) aclyrs.ChildLayers.Add(_graphics) End If ' Create the input geometry for the drive times service, based on the feature passed to the feature action. Dim inputPt As client.Geometry.MapPoint = CType(feature.Geometry, client.Geometry.MapPoint) If (inputPt.SpatialReference Is Nothing) Then inputPt.SpatialReference = feature.Geometry.SpatialReference ' Ensure spatial reference is set. End If ' Create the parameters and pass them to the drive times service and begin to execute a call to the service. Dim parameters As List(Of client.Tasks.GPParameter) = New List(Of client.Tasks.GPParameter)() parameters.Add(New client.Tasks.GPFeatureRecordSetLayer("Input_Location", inputPt)) parameters.Add(New client.Tasks.GPString("Drive_Times", _driveTimeString)) _geoprocessorTask.ExecuteAsync(parameters) End If End Sub Private Sub gp_ExecuteCompleted(sender As Object, e As client.Tasks.GPExecuteCompleteEventArgs) ' When the task completes, look for GPFeatureRecordSetLayers in the output, and iterate any features ' in the layer, adding them to the map as graphics. If ((Not e.Results Is Nothing) AndAlso (Not e.Results.OutParameters Is Nothing) AndAlso (e.Results.OutParameters.Count > 0)) Then ' Expecting a single output parameter. Dim layer As client.Tasks.GPFeatureRecordSetLayer = CType(e.Results.OutParameters(0), client.Tasks.GPFeatureRecordSetLayer) If (Not layer Is Nothing) Then ' Expecting a single feature for the single input, but code here adds a graphic for all features returned, ' may be usesful for example if the Drive Times input parameter is changed. Dim graphic As client.Graphic For Each graphic In layer.FeatureSet.Features graphic.Symbol = _sym _graphics.Graphics.Add(graphic) Next graphic End If End If End Sub Private Sub gp_Failed(sender As Object, e As client.Tasks.TaskFailedEventArgs) ' If the task fails, inform the user. System.Windows.MessageBox.Show("Geoprocessor service failed: " & e.Error.Message) End Sub End Class
Copyright © 1995-2014 Esri. All rights reserved.
5/16/2014