This sample demonstrates adding 3 or more graphic points on the map to call an ESRI.ArcGIS.Client.Tasks GeometryService that will return and display a convex hull graphic. A convex hull is the smallest polygon that completely encases a set (i.e. locus) of points.
To use the sample, add 3 or more points on the map and click the Generate Convex Hull button. A polygon containing all the points entered will be returned.
<UserControlx:Class="ArcGISWPFSDK.ConvexHull"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"><Grid.Resources><esri:SimpleMarkerSymbolx:Key="DefaultMarkerSymbol"Color="Red"Size="6"/><esri:SimpleFillSymbolx:Key="DefaultFillSymbol"Fill="#88FFFF00"BorderBrush="Yellow"/><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"Extent="-100,25,-60,50"><esri:ArcGISTiledMapServiceLayerID="StreetMapLayer"Url="http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_Imagery_World_2D/MapServer"/><esri:GraphicsLayerID="InputGraphicsLayer"/><esri:GraphicsLayerID="ConvexHullGraphicsLayer"/></esri:Map><GridHorizontalAlignment="Right"VerticalAlignment="Top"Margin="0,10,10,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="10,10,10,15"/><StackPanelOrientation="Vertical"Margin="30,20,20,30"><TextBlockx:Name="ResponseTextBlock"Text="Add 3 or more points on the map and click the Generate Convex Hull button. A polygon containing all the points entered will be returned. "Width="400"TextAlignment="Left"TextWrapping="Wrap"Foreground="Black"/><StackPanelOrientation="Horizontal"HorizontalAlignment="Center"><ButtonContent="Generate Convex Hull"Margin="0,5,5,0"x:Name="ConvexButton"Click="ConvexButton_Click"IsEnabled="False"/></StackPanel></StackPanel></Grid></Grid></UserControl>
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
namespace ArcGISWPFSDK
{
publicpartialclass ConvexHull : UserControl
{
private Draw MyDrawObject;
private GraphicsLayer outputGraphicsLayer;
private GraphicsLayer inputGraphicsLayer;
public ConvexHull()
{
InitializeComponent();
MyDrawObject = new Draw(MyMap)
{
DrawMode = DrawMode.Point,
IsEnabled = true
};
MyDrawObject.DrawComplete += MyDrawObject_DrawComplete;
outputGraphicsLayer = MyMap.Layers["ConvexHullGraphicsLayer"] as GraphicsLayer;
inputGraphicsLayer = MyMap.Layers["InputGraphicsLayer"] as GraphicsLayer;
}
privatevoid MyDrawObject_DrawComplete(object sender, DrawEventArgs args)
{
outputGraphicsLayer.ClearGraphics();
ESRI.ArcGIS.Client.Geometry.MapPoint point = args.Geometry as ESRI.ArcGIS.Client.Geometry.MapPoint;
point.SpatialReference = MyMap.SpatialReference;
Graphic graphic = new Graphic()
{
Symbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol,
Geometry = point
};
inputGraphicsLayer.Graphics.Add(graphic);
if (inputGraphicsLayer.Graphics.Count >= 3)
ConvexButton.IsEnabled = true;
}
privatevoid ConvexButton_Click(object sender, RoutedEventArgs e)
{
ConvexButton.IsEnabled = false;
outputGraphicsLayer.ClearGraphics();
GeometryService geometryService =
new GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
geometryService.ConvexHullCompleted += GeometryService_ConvexHullCompleted;
geometryService.Failed += GeometryService_Failed;
geometryService.ConvexHullAsync(inputGraphicsLayer.ToList());
}
void GeometryService_ConvexHullCompleted(object sender, GeometryEventArgs e)
{
Graphic graphic = new Graphic()
{
Symbol = LayoutRoot.Resources["DefaultFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol,
Geometry = e.Result
};
outputGraphicsLayer.Graphics.Add(graphic);
ConvexButton.IsEnabled = true;
}
privatevoid GeometryService_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show("Geometry Service error: " + e.Error);
}
}
}
Imports System.Linq
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Tasks
Namespace ArcGISWPFSDK
PartialPublicClass ConvexHull
Inherits UserControl
Private MyDrawObject As Draw
Private outputGraphicsLayer As GraphicsLayer
Private inputGraphicsLayer As GraphicsLayer
PublicSubNew()
InitializeComponent()
MyDrawObject = New Draw(MyMap) With { _
.DrawMode = DrawMode.Point, _
.IsEnabled = True _
}
AddHandler MyDrawObject.DrawComplete, AddressOf MyDrawObject_DrawComplete
outputGraphicsLayer = TryCast(MyMap.Layers("ConvexHullGraphicsLayer"), GraphicsLayer)
inputGraphicsLayer = TryCast(MyMap.Layers("InputGraphicsLayer"), GraphicsLayer)
EndSubPrivateSub MyDrawObject_DrawComplete(sender AsObject, args As DrawEventArgs)
outputGraphicsLayer.ClearGraphics()
Dim point As ESRI.ArcGIS.Client.Geometry.MapPoint = TryCast(args.Geometry, ESRI.ArcGIS.Client.Geometry.MapPoint)
point.SpatialReference = MyMap.SpatialReference
Dim graphic AsNew Graphic() With { _
.Symbol = TryCast(LayoutRoot.Resources("DefaultMarkerSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol), _
.Geometry = point _
}
inputGraphicsLayer.Graphics.Add(graphic)
If inputGraphicsLayer.Graphics.Count >= 3 Then
ConvexButton.IsEnabled = TrueEndIfEndSubPrivateSub ConvexButton_Click(sender AsObject, e As RoutedEventArgs)
ConvexButton.IsEnabled = False
outputGraphicsLayer.ClearGraphics()
Dim geometryService AsNew GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer")
AddHandler geometryService.ConvexHullCompleted, AddressOf GeometryService_ConvexHullCompleted
AddHandler geometryService.Failed, AddressOf GeometryService_Failed
geometryService.ConvexHullAsync(inputGraphicsLayer.ToList())
EndSubPrivateSub GeometryService_ConvexHullCompleted(sender AsObject, e As GeometryEventArgs)
Dim graphic AsNew Graphic() With { _
.Symbol = TryCast(LayoutRoot.Resources("DefaultFillSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol), _
.Geometry = e.Result _
}
outputGraphicsLayer.Graphics.Add(graphic)
ConvexButton.IsEnabled = TrueEndSubPrivateSub GeometryService_Failed(sender AsObject, e As TaskFailedEventArgs)
MessageBox.Show("Geometry Service error: " & Convert.ToString(e.[Error]))
EndSubEndClassEndNamespace