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 LocalGeoprocessingService
is started asynchronously with the path to a Geoprocessing Package ( .gpk), which can be authored in ArcGIS for Desktop. The Url of the LocalGeoprocessingService and the Geoprocessing tool name is passed into the Geoprocessor, which is used to pass the distance and
line to the ClipFeatures service. When the results are returned they are added to
the map as graphics.
<UserControlx:Class="ArcGISWPFSDK.LocalClipFeatures"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><BooleanToVisibilityConverterx:Key="BooleanToVisibilityConverter"/><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="_map"Extent="-15000000,2000000,-7000000,8000000"Background="#FFE3E3E3"MinimumResolution="2445.98490512499"><esri:ArcGISLocalTiledLayerID="BaseMap"Path="..\\Data\\TPKs\\Topographic.tpk"/><esri:ArcGISLocalDynamicMapServiceLayerPath="..\\Data\\MPKS\\USCitiesStates.mpk"/><esri:GraphicsLayerx:Name="_resultGraphicsLayer"><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:GraphicsLayerx:Name="_inputGraphicsLayer"/></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."Width="200"TextAlignment="Left"TextWrapping="Wrap"Foreground="Black"/><StackPanelOrientation="Horizontal"HorizontalAlignment="Center"Margin="5,5,5,10"><TextBlockText="Distance (in km): "VerticalAlignment="Center"Foreground="Black"/><TextBoxx:Name="DistanceTextBox"Text="100"Width="35"TextAlignment="Right"Margin="0,0,5,0"/></StackPanel><TextBlockx:Name="ProcessingTextBlock"MaxWidth="150"Margin="5,5,5,5"Visibility="Collapsed"HorizontalAlignment="Center"Text="Processing."TextWrapping="Wrap"Foreground="Black"/></StackPanel></Grid><ProgressBarx:Name="MyProgressBar"IsIndeterminate="True"VerticalAlignment="Bottom"Width="200"Height="20"Margin="10"Visibility="{Binding Path=IsBusy, Converter={StaticResource BooleanToVisibilityConverter}}"></ProgressBar></Grid></UserControl>
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Local;
using System.ComponentModel;
namespace ArcGISWPFSDK
{
publicpartialclass LocalClipFeatures : UserControl, INotifyPropertyChanged
{
publicevent PropertyChangedEventHandler PropertyChanged;
privatebool _isBusy = true;
publicbool IsBusy
{
get
{
return _isBusy;
}
set
{
_isBusy = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("IsBusy"));
}
}
}
private Draw _drawObject;
Geoprocessor _gpTask;
public LocalClipFeatures()
{
InitializeComponent();
LocalGeoprocessingService.GetServiceAsync(@"..\\Data\\GPKs\\ClipFeatures\ClipFeatures.gpk",
GPServiceType.Execute, (gpService) =>
{
if (gpService.Error != null)
{
MessageBox.Show(gpService.Error.Message);
}
_gpTask = new Geoprocessor(gpService.UrlGeoprocessingService + "/" + "ClipFeatures");
DataContext = this;
IsBusy = false;
_drawObject = new Draw(_map)
{
DrawMode = DrawMode.Polyline,
IsEnabled = true,
LineSymbol = LayoutRoot.Resources["ClipLineSymbol"] as ESRI.ArcGIS.Client.Symbols.LineSymbol
};
_drawObject.DrawComplete += _drawObject_DrawComplete;
});
}
privatevoid _drawObject_DrawComplete(object sender, DrawEventArgs args)
{
IsBusy = true;
_drawObject.IsEnabled = false;
_inputGraphicsLayer.Graphics.Clear();
Graphic graphic = new Graphic()
{
Symbol = LayoutRoot.Resources["ClipLineSymbol"] as ESRI.ArcGIS.Client.Symbols.LineSymbol,
Geometry = args.Geometry
};
_inputGraphicsLayer.Graphics.Add(graphic);
List<GPParameter> parameters = new List<GPParameter>();
parameters.Add(new GPFeatureRecordSetLayer("Input", args.Geometry));
parameters.Add(new GPLinearUnit("Linear_Unit", esriUnits.esriKilometers, Int32.Parse(DistanceTextBox.Text)));
_gpTask.ExecuteCompleted += (s, e) =>
{
GPExecuteResults results = e.Results;
_resultGraphicsLayer.Graphics.Clear();
GPFeatureRecordSetLayer rs = results.OutParameters[0] as GPFeatureRecordSetLayer;
foreach (Graphic graphics in rs.FeatureSet.Features)
{
graphics.Symbol = LayoutRoot.Resources["ClipFeaturesFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
_resultGraphicsLayer.Graphics.Add(graphics);
}
IsBusy = false;
_drawObject.IsEnabled = true;
};
_gpTask.Failed += (s2, e2) =>
{
MessageBox.Show(e2.Error.Message);
IsBusy = false;
};
_gpTask.ExecuteAsync(parameters);
}
}
}
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Threading
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Tasks
Imports ESRI.ArcGIS.Client.Local
Imports System.ComponentModel
Namespace ArcGISWPFSDK
PartialPublicClass LocalClipFeatures
Inherits UserControl
Implements INotifyPropertyChanged
PublicEvent PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private _isBusy AsBoolean = TruePublicProperty IsBusy() AsBooleanGetReturn _isBusy
EndGetSet(value AsBoolean)
_isBusy = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("IsBusy"))
EndSetEndPropertyPrivate _drawObject As Draw
Private _gpTask As Geoprocessor
PublicSubNew()
InitializeComponent()
LocalGeoprocessingService.GetServiceAsync(
"..\\Data\\GPKs\\ClipFeatures\ClipFeatures.gpk",
GPServiceType.Execute,
Function(gpService)
If gpService.[Error] IsNotNothingThen
MessageBox.Show(gpService.[Error].Message)
EndIf
_gpTask = New Geoprocessor(gpService.UrlGeoprocessingService & "/" & "ClipFeatures")
DataContext = Me
IsBusy = False
_drawObject = New Draw(_map) With { _
.DrawMode = DrawMode.Polyline, _
.IsEnabled = True, _
.LineSymbol = TryCast(LayoutRoot.Resources("ClipLineSymbol"), ESRI.ArcGIS.Client.Symbols.LineSymbol) _
}
AddHandler _drawObject.DrawComplete, AddressOf _drawObject_DrawComplete
EndFunction)
EndSubPrivateSub _drawObject_DrawComplete(sender AsObject, args As DrawEventArgs)
IsBusy = True
_drawObject.IsEnabled = False
_inputGraphicsLayer.Graphics.Clear()
Dim graphic AsNew Graphic() With { _
.Symbol = TryCast(LayoutRoot.Resources("ClipLineSymbol"), ESRI.ArcGIS.Client.Symbols.LineSymbol), _
.Geometry = args.Geometry _
}
_inputGraphicsLayer.Graphics.Add(graphic)
Dim parameters AsNew List(Of GPParameter)()
parameters.Add(New GPFeatureRecordSetLayer("Input", args.Geometry))
parameters.Add(New GPLinearUnit("Linear_Unit", esriUnits.esriKilometers, Int32.Parse(DistanceTextBox.Text)))
AddHandler _gpTask.ExecuteCompleted, AddressOf gpTask_ExecuteCompleted
AddHandler _gpTask.Failed, AddressOf gpTask_Failed
_gpTask.ExecuteAsync(parameters)
EndSubPrivateSub gpTask_ExecuteCompleted(ByVal s AsObject, ByVal e1 As GPExecuteCompleteEventArgs)
_resultGraphicsLayer.Graphics.Clear()
Dim results As GPExecuteResults = e1.Results
Dim rs As GPFeatureRecordSetLayer = TryCast(results.OutParameters(0), GPFeatureRecordSetLayer)
ForEach graphics As Graphic In rs.FeatureSet.Features
graphics.Symbol = TryCast(LayoutRoot.Resources("ClipFeaturesFillSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
_resultGraphicsLayer.Graphics.Add(graphics)
Next
_drawObject.IsEnabled = True
IsBusy = FalseEndSubPrivateSub gpTask_Failed(ByVal s AsObject, ByVal e As TaskFailedEventArgs)
MessageBox.Show("Geoprocessor failed. Error: " & e.Error.Message)
IsBusy = FalseEndSubEndClassEndNamespace