Click or drag to resize

ServiceAreaTask Class

Instances of this class represent tasks that can compute service areas.
Inheritance Hierarchy
SystemObject
  Esri.ArcGISRuntime.Tasks.NetworkAnalysisServiceAreaTask

Namespace:  Esri.ArcGISRuntime.Tasks.NetworkAnalysis
Assembly:  Esri.ArcGISRuntime (in Esri.ArcGISRuntime.dll) Version: 100.11.0
Syntax
public sealed class ServiceAreaTask : IApiKeyResource

The ServiceAreaTask type exposes the following members.

Properties
  NameDescription
Public propertyApiKey
Gets or sets the unique key generated from ArcGIS Developers website.
Public propertyCredential
Gets or sets the security credential used to access a remote task.
Public propertyServiceAreaTaskInfo
Gets service area task info.
Public propertyTransportationNetworkDataset
Gets the transportation network dataset.
Public propertyUri
Gets the service endpoint used to create task.
Top
Methods
  NameDescription
Public methodStatic memberCreateAsync(TransportationNetworkDataset)
Initializes a new instance of the ServiceAreaTask class from a transportation network dataset.
Public methodStatic memberCreateAsync(String, String)
Initializes a new instance of the ServiceAreaTask class from a local geodatabase.
Public methodStatic memberCode exampleCreateAsync(Uri, Credential)
Creates a service area task with URL.
Public methodStatic memberCreateAsync(Uri, String)
Creates a service area task with URL.
Public methodCode exampleCreateDefaultParametersAsync
Creates default parameters as defined by the service.
Public methodCode exampleSolveServiceAreaAsync
Solves a service area passing in a parameters object.
Top
Remarks
A Service Area task allows you to calculate areas that can be serviced (reached) from a given location. A service area is a region that encompasses all accessible streets, that is, streets that lie within a specified impedance. For instance, the 10-minute service area for a facility includes all the streets that can be reached within 10 minutes from that facility.
Examples

WPF

Example Name: FindServiceArea

Find the service area within a network from a given point.

Code example screen shot.

C#
// Copyright 2018 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 Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.Tasks.NetworkAnalysis;
using Esri.ArcGISRuntime.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace ArcGISRuntime.WPF.Samples.FindServiceArea
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        name: "Find service area",
        category: "Network analysis",
        description: "Find the service area within a network from a given point.",
        instructions: "In order to find any service areas at least one facility needs to be added to the map view.",
        tags: new[] { "Routing and Logistics", "barriers", "facilities", "impedance" })]
    public partial class FindServiceArea
    {
        // Uri for the service area around San Diego.
        private Uri _serviceAreaUri = new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/NetworkAnalysis/SanDiego/NAServer/ServiceArea");

        public FindServiceArea()
        {
            InitializeComponent();

            // Create the map, graphics overlay, and sketch editor.
            Initialize();
        }

        private void Initialize()
        {
            // Center the map on San Diego.
            Map streetsMap = new Map(BasemapStyle.ArcGISLightGray)
            {
                InitialViewpoint = new Viewpoint(32.73, -117.14, 30000)
            };
            MyMapView.Map = streetsMap;

            // Create graphics overlays for all of the elements of the map.
            MyMapView.GraphicsOverlays.Add(new GraphicsOverlay());

            // Add a new behavior for double taps on the MapView.
            MyMapView.GeoViewDoubleTapped += (s, e) =>
            {
                // If the sketch editor complete command is enabled, a sketch is in progress.
                if (MyMapView.SketchEditor.CompleteCommand.CanExecute(null))
                {
                    // Set the event as handled.
                    e.Handled = true;

                    // Finish the drawing.
                    MyMapView.SketchEditor.CompleteCommand.Execute(null);
                    DrawBarrierButton.Content = "Draw barrier";
                }
            };
        }

        private async void PlaceFacilityButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Let the user tap on the map view using the point sketch mode.
                SketchCreationMode creationMode = SketchCreationMode.Point;
                Geometry geometry = await MyMapView.SketchEditor.StartAsync(creationMode, false);

                // Symbology for a facility.
                PictureMarkerSymbol facilitySymbol = new PictureMarkerSymbol(new Uri("https://static.arcgis.com/images/Symbols/SafetyHealth/Hospital.png"))
                {
                    Height = 30,
                    Width = 30
                };

                // Create a graphic for the facility.
                Graphic facilityGraphic = new Graphic(geometry, new Dictionary<string, object>() { { "Type", "Facility" } }, facilitySymbol)
                {
                    ZIndex = 2
                };

                // Add the graphic to the graphics overlay.
                MyMapView.GraphicsOverlays[0].Graphics.Add(facilityGraphic);
            }
            catch (TaskCanceledException)
            {
                // Ignore this exception.
            }
            catch (Exception ex)
            {
                // Report exceptions.
                MessageBox.Show("Error drawing facility:\n" + ex.Message);
            }
        }

        private async void DrawBarrierButton_Click(object sender, RoutedEventArgs e)
        {
            // Finish the drawing if already started.
            if ((string) DrawBarrierButton.Content != "Draw barrier")
            {
                if (MyMapView.SketchEditor.CompleteCommand.CanExecute(null))
                    MyMapView.SketchEditor.CompleteCommand.Execute(null);
                DrawBarrierButton.Content = "Draw barrier";
                return;
            }
            try
            {
                // Update the button title.
                DrawBarrierButton.Content = "Finish barrier";

                // Let the user draw on the map view using the polyline sketch mode.
                SketchCreationMode creationMode = SketchCreationMode.Polyline;
                Geometry geometry = await MyMapView.SketchEditor.StartAsync(creationMode, false);

                // Symbol for the barriers.
                SimpleLineSymbol barrierSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, System.Drawing.Color.Black, 5.0f);

                // Create the graphic to be used for barriers.
                Graphic barrierGraphic = new Graphic(geometry, new Dictionary<string, object>() { { "Type", "Barrier" } }, barrierSymbol)
                {
                    ZIndex = 1
                };

                // Add a graphic from the polyline the user drew.
                MyMapView.GraphicsOverlays[0].Graphics.Add(barrierGraphic);
            }
            catch (TaskCanceledException)
            {
                // Ignore this exception.
            }
            catch (Exception ex)
            {
                // Report exceptions.
                MessageBox.Show("Error drawing barrier:\n" + ex.Message);
            }
        }

        private async void ShowServiceAreasButton_Click(object sender, RoutedEventArgs e)
        {
            // Finish any drawings.
            if (MyMapView.SketchEditor.CompleteCommand.CanExecute(null))
            {
                MyMapView.SketchEditor.CompleteCommand.Execute(null);
                DrawBarrierButton.Content = "Draw barrier";
            }
            // Use a local variable for the graphics overlay.
            GraphicCollection allGraphics = MyMapView.GraphicsOverlays[0].Graphics;

            // Get a list of the facilities from the graphics overlay.
            List<ServiceAreaFacility> serviceAreaFacilities = (from g in allGraphics
                                                               where (string)g.Attributes["Type"] == "Facility"
                                                               select new ServiceAreaFacility((MapPoint)g.Geometry)).ToList();

            // Check that there is at least 1 facility to find a service area for.
            if (!serviceAreaFacilities.Any())
            {
                MessageBox.Show("Must have at least one Facility!", "Sample error");
                return;
            }

            // Create the service area task and parameters based on the Uri.
            ServiceAreaTask serviceAreaTask = await ServiceAreaTask.CreateAsync(_serviceAreaUri);

            // Store the default parameters for the service area in an object.
            ServiceAreaParameters serviceAreaParameters = await serviceAreaTask.CreateDefaultParametersAsync();

            // Add impedance cutoffs for facilities (drive time minutes).
            serviceAreaParameters.DefaultImpedanceCutoffs.Add(2.0);
            serviceAreaParameters.DefaultImpedanceCutoffs.Add(5.0);

            // Set the level of detail for the polygons.
            serviceAreaParameters.PolygonDetail = ServiceAreaPolygonDetail.High;

            // Get a list of the barriers from the graphics overlay.
            List<PolylineBarrier> polylineBarriers = (from g in allGraphics
                                                      where (string)g.Attributes["Type"] == "Barrier"
                                                      select new PolylineBarrier((Polyline)g.Geometry)).ToList();

            // Add the barriers to the service area parameters.
            serviceAreaParameters.SetPolylineBarriers(polylineBarriers);

            // Update the parameters to include all of the placed facilities.
            serviceAreaParameters.SetFacilities(serviceAreaFacilities);

            // Clear existing graphics for service areas.
            foreach (Graphic g in allGraphics.ToList())
            {
                // Check if the graphic g is a service area.
                if ((string)g.Attributes["Type"] == "ServiceArea")
                {
                    allGraphics.Remove(g);
                }
            }

            try
            {
                // Solve for the service area of the facilities.
                ServiceAreaResult result = await serviceAreaTask.SolveServiceAreaAsync(serviceAreaParameters);

                // Loop over each facility.
                for (int i = 0; i < serviceAreaFacilities.Count; i++)
                {
                    // Create list of polygons from a service facility.
                    List<ServiceAreaPolygon> polygons = result.GetResultPolygons(i).ToList();

                    // Symbol for the outline of the service areas.
                    SimpleLineSymbol serviceOutline = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, System.Drawing.Color.DarkGray, 3.0f);

                    // Create a list of fill symbols for the polygons.
                    List<SimpleFillSymbol> fillSymbols = new List<SimpleFillSymbol>();
                    fillSymbols.Add(new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, System.Drawing.Color.FromArgb(70, 255, 0, 0), serviceOutline));
                    fillSymbols.Add(new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, System.Drawing.Color.FromArgb(70, 255, 165, 0), serviceOutline));

                    // Loop over every polygon in every facilities result.
                    for (int j = 0; j < polygons.Count; j++)
                    {
                        // Create the graphic for the service areas, alternating between fill symbols.
                        Graphic serviceGraphic = new Graphic(polygons[j].Geometry, new Dictionary<string, object>() { { "Type", "ServiceArea" } }, fillSymbols[j % 2])
                        {
                            ZIndex = 0
                        };

                        // Add graphic for service area. Alternate the color of each polygon.
                        allGraphics.Add(serviceGraphic);
                    }
                }
            }
            catch (Esri.ArcGISRuntime.Http.ArcGISWebException exception)
            {
                if (exception.Message.ToString().Equals("Unable to complete operation."))
                {
                    MessageBox.Show("Facility not within San Diego area!", "Sample error");
                }
                else
                {
                    MessageBox.Show("An ArcGIS web exception occurred. \n" + exception.Message.ToString(), "Sample error");
                }
            }
        }

        private void Reset_Click(object sender, RoutedEventArgs e)
        {
            // Clear all of the graphics.
            MyMapView.GraphicsOverlays[0].Graphics.Clear();
        }
    }
}
XAML
<UserControl x:Class="ArcGISRuntime.WPF.Samples.FindServiceArea.FindServiceArea"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013">
    <Grid>
        <esri:MapView x:Name="MyMapView" />
        <Border Style="{StaticResource BorderStyle}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Button Name="PlaceFacilityButton"
                        Grid.Row="0" Grid.Column="0"
                        Margin="5,5,5,5"
                        Content="Place facility"
                        Click="PlaceFacilityButton_Click" />
                <Button Name="DrawBarrierButton"
                        Grid.Row="0" Grid.Column="1"
                        Margin="5,5,5,5"
                        Content="Draw barrier"
                        Click="DrawBarrierButton_Click" />
                <Button Name="ShowServiceAreasButton"
                        Grid.Row="3"
                        Grid.ColumnSpan="2"
                        Margin="5,5,5,5"
                        Content="Show service areas"
                        Click="ShowServiceAreasButton_Click" />
                <Button Name="ResetButton"
                        Grid.Row="4"
                        Grid.ColumnSpan="2"
                        Margin="5,5,5,5"
                        Content="Reset"
                        Click="Reset_Click" />
            </Grid>
        </Border>
    </Grid>
</UserControl>
See Also
Additional Examples
Hyperlink to ExampleDescription
FindServiceAreaFind the service area within a network from a given point.
FindServiceAreasForMultipleFacilitiesFind the service areas of several facilities from a feature service.