Viewshed

Download Samples Repository

Description

This sample demonstrates use of the Geoprocessor to call a Viewshed geoprocessing service.

"Desktop" "Store" "Phone" Available for Desktop, Store, Phone

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.Viewshed"
             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 x:Name="layoutGrid">
        <Grid.Resources>
            <esri:SimpleMarkerSymbol x:Key="MarkerSymbol" Color="Black" Style="X" Size="8" />
            <esri:SimpleRenderer x:Key="PointRenderer" Symbol="{StaticResource MarkerSymbol}" />
            <esri:SimpleLineSymbol x:Key="OutlineSymbol" Color="Red" Width="2" Style="Solid" />
            <esri:SimpleFillSymbol x:Key="DefaultFillSymbol" Color="#44FF9999" Outline="{StaticResource OutlineSymbol}" />
			<esri:SimpleRenderer x:Key="ViewshedRenderer" Symbol="{StaticResource DefaultFillSymbol}" />
		</Grid.Resources>

        <esri:MapView x:Name="MyMapView">
			<esri:Map InitialViewpoint="-12004036,4652780,-11735714,4808810,3857">
				<esri:ArcGISTiledMapServiceLayer ID="TopoMapLayer"
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" />
            </esri:Map>
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="ViewshedOverlay" Renderer="{StaticResource ViewshedRenderer}"/>
				<esri:GraphicsOverlay ID="inputOverlay" Renderer="{StaticResource PointRenderer}"/>
			</esri:MapView.GraphicsOverlays>
		</esri:MapView>
		
		<Border x:Name="uiPanel" Background="White" BorderBrush="Black" BorderThickness="1"
			HorizontalAlignment="Right" VerticalAlignment="Top"
			Margin="30" Padding="20"
			Width="350">
			<Border.Effect>
				<DropShadowEffect/>
			</Border.Effect>
			<StackPanel>
				<TextBlock Text="Click 'Start' and then click on the map to see the calculated viewshed for the distance specified below."
                            TextWrapping="Wrap" FontSize="14" Margin="8,8,8,0" HorizontalAlignment="Left"/>

				<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="12,16,12,0">
					<TextBlock Text="Miles:" VerticalAlignment="Center" Margin="0,0,8,0" />
					<TextBox x:Name="txtMiles" Text="10" Width="35" TextAlignment="Right" />
				</StackPanel>

				<Button Content="Start" HorizontalAlignment="Stretch" Margin="24,16,24,0" Click="StartButton_Click"/>

				<StackPanel x:Name="progress" Visibility="Collapsed">
					<ProgressBar Margin="12,12,12,0" IsIndeterminate="True" />
					<TextBlock x:Name="txtStatus" Margin="12,12,12,0" FontSize="14"/>
				</StackPanel>
			</StackPanel>
		</Border>
    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Tasks.Geoprocessing;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// This sample demonstrates use of the Geoprocessor to call a Viewshed geoprocessing service.
    /// </summary>
    /// <title>Viewshed</title>
	/// <category>Tasks</category>
	/// <subcategory>Geoprocessing</subcategory>
	public partial class Viewshed : UserControl
    {
		private const string ViewshedServiceUrl =
		   "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Elevation/ESRI_Elevation_World/GPServer/Viewshed";

		private GraphicsOverlay _inputOverlay;
		private GraphicsOverlay _viewshedOverlay;
		private Geoprocessor _gpTask;

        /// <summary>Construct Viewshed sample control</summary>
        public Viewshed()
        {
            InitializeComponent();

			_inputOverlay = MyMapView.GraphicsOverlays["inputOverlay"];
			_viewshedOverlay = MyMapView.GraphicsOverlays["ViewshedOverlay"];

            _gpTask = new Geoprocessor(new Uri(ViewshedServiceUrl));
        }

        // Get the users click point on the map and fire off a GP Job to calculate the viewshed
        private async void StartButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                uiPanel.IsEnabled = false;
				_inputOverlay.Graphics.Clear();
				_viewshedOverlay.Graphics.Clear();

                //get the user's input point
                var inputPoint = await MyMapView.Editor.RequestPointAsync();

                progress.Visibility = Visibility.Visible;
				_inputOverlay.Graphics.Add(new Graphic() { Geometry = inputPoint });

                var parameter = new GPInputParameter() { OutSpatialReference = SpatialReferences.WebMercator };
				parameter.GPParameters.Add(new GPFeatureRecordSetLayer("Input_Observation_Point", inputPoint));
				parameter.GPParameters.Add(new GPLinearUnit("Viewshed_Distance", LinearUnits.Miles, Convert.ToDouble(txtMiles.Text)));

				txtStatus.Text = "Processing on server...";
				var result = await _gpTask.ExecuteAsync(parameter);
                if (result == null || result.OutParameters == null || !(result.OutParameters[0] is GPFeatureRecordSetLayer))
					throw new ApplicationException("No viewshed graphics returned for this start point.");

                txtStatus.Text = "Finished processing. Retrieving results...";
				var viewshedLayer = result.OutParameters[0] as GPFeatureRecordSetLayer;
				_viewshedOverlay.Graphics.AddRange(viewshedLayer.FeatureSet.Features.OfType<Graphic>());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Sample Error");
            }
            finally
            {
                uiPanel.IsEnabled = true;
                progress.Visibility = Visibility.Collapsed;
            }
        }
    }
}
Feedback on this topic?