This sample demonstrates use of the Geoprocessor to call a ViewShed
geoprocessing service. To use the sample, specify a distance and
click a point on the map. Once the viewshed is calculated, it will
be shown on the map.
In the code-behind, a Geoprocessor is used to
pass the distance and click point to the ViewShed service. When the
results are returned, they are added to a GraphicsLayer and shown
in the map.
<UserControlx:Class="ArcGISWPFSDK.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/client/2009"><Gridx:Name="LayoutRoot"Background="White"><Grid.Resources><esri:PictureMarkerSymbolx:Key="StartMarkerSymbol"OffsetX="8"OffsetY="8"Source="/Assets/Images/i_about.png"/><esri:SimpleFillSymbolx:Key="DefaultFillSymbol"Fill="#99FF9999"BorderBrush="#FFFF9999"BorderThickness="2"/><LinearGradientBrushx:Key="PanelGradient"EndPoint="0.5,1"StartPoint="0.5,0"><LinearGradientBrush.RelativeTransform><TransformGroup><ScaleTransformCenterY="0.5"CenterX="0.5"/><SkewTransformCenterY="0.5"CenterX="0.5"/><RotateTransformAngle="176"CenterY="0.5"CenterX="0.5"/><TranslateTransform/></TransformGroup></LinearGradientBrush.RelativeTransform><GradientStopColor="#FF145787"Offset="0.16"/><GradientStopColor="#FF3D7FAC"Offset="0.502"/><GradientStopColor="#FF88C5EF"Offset="0.984"/></LinearGradientBrush></Grid.Resources><esri:Mapx:Name="MyMap"MouseClick="MyMap_MouseClick"Cursor="Hand"Extent="76.35,35.65,76.65,35.95"><esri:ArcGISTiledMapServiceLayerID="TopoMapLayer"Url="http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/><esri:GraphicsLayerID="MyGraphicsLayer"/></esri:Map><GridHorizontalAlignment="Right"VerticalAlignment="Top"Margin="0,15,15,0"><RectangleFill="{StaticResource PanelGradient}"Stroke="Gray"RadiusX="10"RadiusY="10"Margin="0,0,0,5"><Rectangle.Effect><DropShadowEffect/></Rectangle.Effect></Rectangle><RectangleFill="#DDFFFFFF"Stroke="DarkGray"RadiusX="5"RadiusY="5"Margin="10,10,10,15"/><StackPanelOrientation="Vertical"Margin="20,20,20,20"HorizontalAlignment="Left"><TextBlockText="Calculate Viewshed"HorizontalAlignment="Center"FontWeight="Bold"Foreground="Black"/><TextBlockx:Name="InformationTextBlock"Text="Click on map to see the calculated viewshed. Set distance below."Width="220"TextAlignment="Left"TextWrapping="Wrap"Foreground="Black"/><StackPanelOrientation="Horizontal"HorizontalAlignment="Center"Margin="5,5,5,10"><TextBlockText="Miles: "VerticalAlignment="Center"Foreground="Black"/><TextBoxx:Name="MilesTextBox"Text="10"Width="35"TextAlignment="Right"Margin="0,0,5,0"/></StackPanel></StackPanel></Grid></Grid></UserControl>
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Symbols;
namespace ArcGISWPFSDK
{
publicpartialclass ViewShed : UserControl
{
Geoprocessor _geoprocessorTask;
public ViewShed()
{
InitializeComponent();
_geoprocessorTask = new Geoprocessor("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
"Elevation/ESRI_Elevation_World/GPServer/Viewshed");
_geoprocessorTask.ExecuteCompleted += GeoprocessorTask_ExecuteCompleted;
_geoprocessorTask.Failed += GeoprocessorTask_Failed;
}
privatevoid MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
{
_geoprocessorTask.CancelAsync();
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.ClearGraphics();
MapPoint mapPoint = e.MapPoint;
mapPoint.SpatialReference = new SpatialReference(4326);
Graphic graphic = new Graphic()
{
Symbol = LayoutRoot.Resources["StartMarkerSymbol"] as Symbol,
Geometry = mapPoint
};
graphicsLayer.Graphics.Add(graphic);
MyMap.Cursor = System.Windows.Input.Cursors.Wait;
List<GPParameter> parameters = new List<GPParameter>();
parameters.Add(new GPFeatureRecordSetLayer("Input_Observation_Point", mapPoint));
parameters.Add(new GPLinearUnit("Viewshed_Distance", esriUnits.esriMiles, Convert.ToDouble(MilesTextBox.Text)));
_geoprocessorTask.OutputSpatialReference = new SpatialReference(4326);
_geoprocessorTask.ExecuteAsync(parameters);
}
privatevoid GeoprocessorTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.GPExecuteCompleteEventArgs args)
{
MyMap.Cursor = System.Windows.Input.Cursors.Hand;
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
foreach (GPParameter gpParameter in args.Results.OutParameters)
{
if (gpParameter is GPFeatureRecordSetLayer)
{
GPFeatureRecordSetLayer layer = gpParameter as GPFeatureRecordSetLayer;
foreach (Graphic graphic in layer.FeatureSet.Features)
{
graphic.Symbol = LayoutRoot.Resources["DefaultFillSymbol"] as Symbol;
graphicsLayer.Graphics.Add(graphic);
}
}
}
}
privatevoid GeoprocessorTask_Failed(object sender, TaskFailedEventArgs e)
{
MyMap.Cursor = System.Windows.Input.Cursors.Hand;
MessageBox.Show("Geoprocessor service failed: " + e.Error);
}
}
}
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Geometry
Imports ESRI.ArcGIS.Client.Tasks
Imports ESRI.ArcGIS.Client.Symbols
Namespace ArcGISWPFSDK
PartialPublicClass ViewShed
Inherits UserControl
Private _geoprocessorTask As Geoprocessor
PublicSubNew()
InitializeComponent()
_geoprocessorTask = New Geoprocessor("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" & "Elevation/ESRI_Elevation_World/GPServer/Viewshed")
AddHandler _geoprocessorTask.ExecuteCompleted, AddressOf GeoprocessorTask_ExecuteCompleted
AddHandler _geoprocessorTask.Failed, AddressOf GeoprocessorTask_Failed
EndSubPrivateSub MyMap_MouseClick(sender AsObject, e As ESRI.ArcGIS.Client.Map.MouseEventArgs)
_geoprocessorTask.CancelAsync()
Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
graphicsLayer.ClearGraphics()
Dim mapPoint As MapPoint = e.MapPoint
mapPoint.SpatialReference = New SpatialReference(4326)
Dim graphic AsNew Graphic() With { _
.Symbol = TryCast(LayoutRoot.Resources("StartMarkerSymbol"), Symbol), _
.Geometry = mapPoint _
}
graphicsLayer.Graphics.Add(graphic)
MyMap.Cursor = System.Windows.Input.Cursors.Wait
Dim parameters AsNew List(Of GPParameter)()
parameters.Add(New GPFeatureRecordSetLayer("Input_Observation_Point", mapPoint))
parameters.Add(New GPLinearUnit("Viewshed_Distance", esriUnits.esriMiles, Convert.ToDouble(MilesTextBox.Text)))
_geoprocessorTask.OutputSpatialReference = New SpatialReference(4326)
_geoprocessorTask.ExecuteAsync(parameters)
EndSubPrivateSub GeoprocessorTask_ExecuteCompleted(sender AsObject, args As ESRI.ArcGIS.Client.Tasks.GPExecuteCompleteEventArgs)
MyMap.Cursor = System.Windows.Input.Cursors.Hand
Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
ForEach gpParameter As GPParameter In args.Results.OutParameters
IfTypeOf gpParameter Is GPFeatureRecordSetLayer ThenDim layer As GPFeatureRecordSetLayer = TryCast(gpParameter, GPFeatureRecordSetLayer)
ForEach graphic As Graphic In layer.FeatureSet.Features
graphic.Symbol = TryCast(LayoutRoot.Resources("DefaultFillSymbol"), Symbol)
graphicsLayer.Graphics.Add(graphic)
NextEndIfNextEndSubPrivateSub GeoprocessorTask_Failed(sender AsObject, e As TaskFailedEventArgs)
MyMap.Cursor = System.Windows.Input.Cursors.Hand
MessageBox.Show("Geoprocessor service failed: " & Convert.ToString(e.[Error]))
EndSubEndClassEndNamespace