This sample demonstrates using the ArcGIS Runtime SDK for WPF to perform a geometry operation to 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.LocalConvexHull"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="-15000000,2000000,-7000000,8000000"Background="#FFE3E3E3"><esri:ArcGISLocalTiledLayerID="BaseMap"Path="..\\Data\\TPKs\\Topographic.tpk"/><esri:GraphicsLayerID="InputGraphicsLayer"x:Name="_inputGraphicsLayer"/><esri:GraphicsLayerID="ConvexHullGraphicsLayer"x:Name="_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;
using ESRI.ArcGIS.Client.Local;
namespace ArcGISWPFSDK
{
publicpartialclass LocalConvexHull : UserControl
{
private Draw _drawObject;
GeometryService _geometryTask;
public LocalConvexHull()
{
InitializeComponent();
LocalGeometryService.GetServiceAsync(lgs =>
{
_geometryTask = new GeometryService();
_geometryTask.Url = lgs.UrlGeometryService;
_geometryTask.ConvexHullCompleted += GeometryService_ConvexHullCompleted;
_geometryTask.Failed += GeometryService_Failed;
_drawObject = new Draw(MyMap)
{
DrawMode = DrawMode.Point,
IsEnabled = true
};
});
MyMap.Layers.LayersInitialized += (s, e) =>
{
_drawObject.DrawComplete += DrawObject_DrawComplete;
};
}
privatevoid DrawObject_DrawComplete(object sender, DrawEventArgs args)
{
_convexHullGraphicsLayer.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;
_convexHullGraphicsLayer.ClearGraphics();
_geometryTask.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
};
_convexHullGraphicsLayer.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
Imports ESRI.ArcGIS.Client.Local
Namespace ArcGISWPFSDK
PartialPublicClass LocalConvexHull
Inherits UserControl
Private MyDrawObject As Draw
Private outputGraphicsLayer As GraphicsLayer
Private inputGraphicsLayer As GraphicsLayer
Private geometryService As GeometryService
PublicSubNew()
InitializeComponent()
Dim localGeometryService AsNew LocalGeometryService()
AddHandler localGeometryService.StartCompleted, AddressOf localGeometryService_StartCompleted
localGeometryService.StartAsync()
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()
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]))
EndSubPrivateSub localGeometryService_StartCompleted(sender AsObject, e As ComponentModel.AsyncCompletedEventArgs)
Dim lgs As LocalGeometryService = TryCast(sender, LocalGeometryService)
geometryService = New GeometryService()
geometryService.Url = lgs.UrlGeometryService
AddHandler geometryService.ConvexHullCompleted, AddressOf GeometryService_ConvexHullCompleted
AddHandler geometryService.Failed, AddressOf GeometryService_Failed
MyDrawObject = New Draw(MyMap) With { _
.DrawMode = DrawMode.Point, _
.IsEnabled = True _
}
AddHandler MyDrawObject.DrawComplete, AddressOf MyDrawObject_DrawComplete
EndSubEndClassEndNamespace