This sample demonstrates use of the Geoprocessor to call a ClipFeatures geoprocessing service. To use the sample, specify a distance, then click subsequent points on the map to draw a line. Double-click to finish the line. When the line is finished, county features within the specified distance of the line will be returned and drawn map, with their boundaries clipped to the buffer area.
In the code-behind, a Geoprocessor is used to pass the distance and line to the ClipFeatures service. When the results are returned, they are checked to see whether they contain any features. If they do not, then the operation generated too many features to be sent back to the client as graphics, and instead generated a result image. In this case, the result image is requested. Once either the graphics or the result image has been retrieved, they are added to the map.
<UserControlx:Class="ArcGISWPFSDK.ClipFeatures"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:SimpleLineSymbolx:Key="ClipLineSymbol"Color="Red"Width="2"/><esri:SimpleFillSymbolx:Key="ClipFeaturesFillSymbol"Fill="#770000FF"BorderBrush="#FF0000FF"BorderThickness="1"/><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"Background="White"Extent="-130,10,-70,60"><esri:ArcGISTiledMapServiceLayerID="StreetMapLayer"Url="http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/><esri:GraphicsLayerID="MyResultGraphicsLayer"><esri:GraphicsLayer.MapTip><GridBackground="LightYellow"><StackPanelOrientation="Horizontal"Margin="5"><TextBlockText="County name: "FontWeight="Bold"Foreground="Black"/><TextBlockText="{Binding [NAME]}"Foreground="Black"/></StackPanel><BorderBorderBrush="Black"BorderThickness="1"/></Grid></esri:GraphicsLayer.MapTip></esri:GraphicsLayer><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="#FFFFFFFF"Stroke="DarkGray"RadiusX="5"RadiusY="5"Margin="5,5,5,10"/><StackPanelOrientation="Vertical"Margin="10,10,10,10"HorizontalAlignment="Left"><TextBlockText="Clip polygon features - US counties"HorizontalAlignment="Center"FontWeight="Bold"Foreground="Black"Margin="5,5,5,5"/><TextBlockx:Name="InformationTextBlock"Text="Draw a line on the map over the United States. When finished the line will be buffered and the buffer will be used to clip US county polygons. Results are returned as a GP map image."Width="200"TextAlignment="Left"TextWrapping="Wrap"Foreground="Black"/><StackPanelOrientation="Horizontal"HorizontalAlignment="Center"Margin="5,5,5,10"><TextBlockText="Distance (in miles): "VerticalAlignment="Center"Foreground="Black"/><TextBoxx:Name="DistanceTextBox"Text="100"Width="35"TextAlignment="Right"Margin="0,0,5,0"/><Buttonx:Name="ClearButton"Content="Clear Results"Click="ClearButton_Click"/></StackPanel><TextBlockx:Name="ProcessingTextBlock"MaxWidth="150"Margin="5,5,5,5"Visibility="Collapsed"HorizontalAlignment="Center"Text="Processing."TextWrapping="Wrap"Foreground="Black"/></StackPanel></Grid></Grid></UserControl>
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
namespace ArcGISWPFSDK
{
publicpartialclass ClipFeatures : UserControl
{
private DispatcherTimer _processingTimer;
private Draw MyDrawObject;
public ClipFeatures()
{
InitializeComponent();
_processingTimer = new System.Windows.Threading.DispatcherTimer();
_processingTimer.Interval = new TimeSpan(0, 0, 0, 0, 800);
_processingTimer.Tick += ProcessingTimer_Tick;
MyDrawObject = new Draw(MyMap)
{
DrawMode = DrawMode.Polyline,
IsEnabled = true,
LineSymbol = LayoutRoot.Resources["ClipLineSymbol"] as ESRI.ArcGIS.Client.Symbols.LineSymbol
};
MyDrawObject.DrawComplete += MyDrawObject_DrawComplete;
}
privatevoid MyDrawObject_DrawComplete(object sender, DrawEventArgs args)
{
MyDrawObject.IsEnabled = false;
ProcessingTextBlock.Visibility = Visibility.Visible;
_processingTimer.Start();
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
Graphic graphic = new Graphic()
{
Symbol = LayoutRoot.Resources["ClipLineSymbol"] as ESRI.ArcGIS.Client.Symbols.LineSymbol,
Geometry = args.Geometry
};
graphicsLayer.Graphics.Add(graphic);
Geoprocessor geoprocessorTask = new Geoprocessor("http://serverapps.esri.com/ArcGIS/rest/services/" +
"SamplesNET/USA_Data_ClipTools/GPServer/ClipCounties");
geoprocessorTask.UpdateDelay = 5000;
geoprocessorTask.JobCompleted += GeoprocessorTask_JobCompleted;
List<GPParameter> parameters = new List<GPParameter>();
parameters.Add(new GPFeatureRecordSetLayer("Input_Features", args.Geometry));
parameters.Add(new GPLinearUnit("Linear_unit", esriUnits.esriMiles, Int32.Parse(DistanceTextBox.Text)));
geoprocessorTask.SubmitJobAsync(parameters);
}
privatevoid GeoprocessorTask_JobCompleted(object sender, JobInfoEventArgs e)
{
Geoprocessor geoprocessorTask = sender as Geoprocessor;
geoprocessorTask.GetResultDataCompleted += (s1, ev1) =>
{
GraphicsLayer graphicsLayer = MyMap.Layers["MyResultGraphicsLayer"] as GraphicsLayer;
if (ev1.Parameter is GPFeatureRecordSetLayer)
{
GPFeatureRecordSetLayer gpLayer = ev1.Parameter as GPFeatureRecordSetLayer;
if (gpLayer.FeatureSet.Features.Count == 0)
{
geoprocessorTask.GetResultImageLayerCompleted += (s2, ev2) =>
{
GPResultImageLayer gpImageLayer = ev2.GPResultImageLayer;
gpImageLayer.Opacity = 0.5;
MyMap.Layers.Add(gpImageLayer);
ProcessingTextBlock.Text = "Greater than 500 features returned. Results drawn using map service.";
_processingTimer.Stop();
};
geoprocessorTask.GetResultImageLayerAsync(e.JobInfo.JobId, "Clipped_Counties");
return;
}
foreach (Graphic graphic in gpLayer.FeatureSet.Features)
{
graphic.Symbol = LayoutRoot.Resources["ClipFeaturesFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
graphicsLayer.Graphics.Add(graphic);
}
}
ProcessingTextBlock.Visibility = Visibility.Collapsed;
_processingTimer.Stop();
};
geoprocessorTask.GetResultDataAsync(e.JobInfo.JobId, "Clipped_Counties");
}
privatevoid GeoprocessorTask_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show("Geoprocessor service failed: " + e.Error);
}
privatevoid ClearButton_Click(object sender, RoutedEventArgs e)
{
List<Layer> gpResultImageLayers = new List<Layer>();
foreach (Layer layer in MyMap.Layers)
if (layer is GraphicsLayer)
(layer as GraphicsLayer).ClearGraphics();
elseif (layer is GPResultImageLayer)
gpResultImageLayers.Add(layer);
for (int i = 0; i < gpResultImageLayers.Count; i++)
MyMap.Layers.Remove(gpResultImageLayers[i]);
MyDrawObject.IsEnabled = true;
ProcessingTextBlock.Text = "";
ProcessingTextBlock.Visibility = Visibility.Collapsed;
}
void ProcessingTimer_Tick(object sender, EventArgs e)
{
if (ProcessingTextBlock.Text.Length > 20 || !ProcessingTextBlock.Text.StartsWith("Processing"))
ProcessingTextBlock.Text = "Processing.";
else
ProcessingTextBlock.Text = ProcessingTextBlock.Text + ".";
}
}
}
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Threading
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Tasks
Namespace ArcGISWPFSDK
PartialPublicClass ClipFeatures
Inherits UserControl
Private _processingTimer As DispatcherTimer
Private MyDrawObject As Draw
PublicSubNew()
InitializeComponent()
_processingTimer = New System.Windows.Threading.DispatcherTimer()
_processingTimer.Interval = New TimeSpan(0, 0, 0, 0, 800)
AddHandler _processingTimer.Tick, AddressOf ProcessingTimer_Tick
MyDrawObject = New Draw(MyMap) With { _
.DrawMode = DrawMode.Polyline, _
.IsEnabled = True, _
.LineSymbol = TryCast(LayoutRoot.Resources("ClipLineSymbol"), ESRI.ArcGIS.Client.Symbols.LineSymbol) _
}
AddHandler MyDrawObject.DrawComplete, AddressOf MyDrawObject_DrawComplete
EndSubPrivateSub MyDrawObject_DrawComplete(sender AsObject, args As DrawEventArgs)
MyDrawObject.IsEnabled = False
ProcessingTextBlock.Visibility = Visibility.Visible
_processingTimer.Start()
Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
Dim graphic AsNew Graphic() With { _
.Symbol = TryCast(LayoutRoot.Resources("ClipLineSymbol"), ESRI.ArcGIS.Client.Symbols.LineSymbol), _
.Geometry = args.Geometry _
}
graphicsLayer.Graphics.Add(graphic)
Dim geoprocessorTask AsNew Geoprocessor("http://serverapps.esri.com/ArcGIS/rest/services/" & "SamplesNET/USA_Data_ClipTools/GPServer/ClipCounties")
geoprocessorTask.UpdateDelay = 5000
AddHandler geoprocessorTask.JobCompleted, AddressOf GeoprocessorTask_JobCompleted
Dim parameters AsNew List(Of GPParameter)()
parameters.Add(New GPFeatureRecordSetLayer("Input_Features", args.Geometry))
parameters.Add(New GPLinearUnit("Linear_unit", esriUnits.esriMiles, Int32.Parse(DistanceTextBox.Text)))
geoprocessorTask.SubmitJobAsync(parameters)
EndSubPrivateSub GeoprocessorTask_GetResultDataCompleted(ByVal sender AsObject, ByVal e As GPParameterEventArgs)
Dim geoprocessorTask As Geoprocessor = TryCast(sender, Geoprocessor)
Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyResultGraphicsLayer"), GraphicsLayer)
IfTypeOf e.Parameter Is GPFeatureRecordSetLayer ThenDim gpLayer As GPFeatureRecordSetLayer = TryCast(e.Parameter, GPFeatureRecordSetLayer)
If gpLayer.FeatureSet.Features.Count = 0 ThenAddHandler geoprocessorTask.GetResultImageLayerCompleted, AddressOf GeoprocessorTask_GetResultImageLayerCompleted
geoprocessorTask.GetResultImageLayerAsync(TryCast(e.UserState, String), "Clipped_Counties")
ReturnEndIfForEach graphic As Graphic In gpLayer.FeatureSet.Features
graphic.Symbol = TryCast(LayoutRoot.Resources("ClipFeaturesFillSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
graphicsLayer.Graphics.Add(graphic)
Next graphic
EndIf
ProcessingTextBlock.Visibility = Visibility.Collapsed
_processingTimer.Stop()
EndSubPrivateSub GeoprocessorTask_Failed(sender AsObject, e As TaskFailedEventArgs)
MessageBox.Show("Geoprocessor service failed: " & Convert.ToString(e.[Error]))
EndSubPrivateSub ClearButton_Click(sender AsObject, e As RoutedEventArgs)
Dim gpResultImageLayers AsNew List(Of Layer)()
ForEach layer As Layer In MyMap.Layers
IfTypeOf layer Is GraphicsLayer Then
TryCast(layer, GraphicsLayer).ClearGraphics()
ElseIfTypeOf layer Is GPResultImageLayer Then
gpResultImageLayers.Add(layer)
EndIfNextFor i AsInteger = 0 To gpResultImageLayers.Count - 1
MyMap.Layers.Remove(gpResultImageLayers(i))
Next
MyDrawObject.IsEnabled = True
ProcessingTextBlock.Text = ""
ProcessingTextBlock.Visibility = Visibility.Collapsed
EndSubPrivateSub ProcessingTimer_Tick(sender AsObject, e As EventArgs)
If ProcessingTextBlock.Text.Length > 20 OrElseNot ProcessingTextBlock.Text.StartsWith("Processing") Then
ProcessingTextBlock.Text = "Processing."Else
ProcessingTextBlock.Text = ProcessingTextBlock.Text & "."EndIfEndSubPrivateSub GeoprocessorTask_GetResultImageLayerCompleted(ByVal s2 AsObject, ByVal ev2 As GetResultImageLayerEventArgs)
Dim gpImageLayer As GPResultImageLayer = ev2.GPResultImageLayer
gpImageLayer.Opacity = 0.5
MyMap.Layers.Add(gpImageLayer)
ProcessingTextBlock.Text = "Greater than 500 features returned. Results drawn using map service."
_processingTimer.Stop()
EndSubPrivateSub GeoprocessorTask_JobCompleted(ByVal sender AsObject, ByVal e As JobInfoEventArgs)
Dim geoprocessorTask As Geoprocessor = TryCast(sender, Geoprocessor)
AddHandler geoprocessorTask.GetResultDataCompleted, AddressOf GeoprocessorTask_GetResultDataCompleted
Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyResultGraphicsLayer"), GraphicsLayer)
geoprocessorTask.GetResultDataAsync(e.JobInfo.JobId, "Clipped_Counties")
EndSubEndClassEndNamespace