This sample demonstrates using the ArcGIS Runtime SDK for WPF to perform a geometry operation that takes an input polygon that cuts across another existing polygon graphic to return the different between the two polygons.
To use the sample, create a polygon on the map that intersects the existing graphic on the map (red). When complete the difference between the polygon and the original graphics will be displayed (yellow). Click the Reset button to remove the results.
<UserControlx:Class="ArcGISWPFSDK.LocalDifference"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:SimpleFillSymbolx:Key="DefaultFillSymbol"Fill="#88FF0000"BorderBrush="Red"/><esri:SimpleLineSymbolx:Key="DefaultLineSymbol"Color="Red"Width="2"/><esri:SimpleMarkerSymbolx:Key="DefaultMarkerSymbol"Color="Red"Size="8"/><esri:SimpleFillSymbolx:Key="DrawFillSymbol"Fill="#8800FFFF"BorderBrush="Cyan"/><esri:SimpleFillSymbolx:Key="DifferenceFillSymbol"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:GraphicsLayer.Graphics><esri:GraphicSymbol="{StaticResource DefaultFillSymbol}"><esri:Polygon><esri:Polygon.SpatialReference><esri:SpatialReferenceWKID="102100"/></esri:Polygon.SpatialReference><esri:Polygon.Rings><esri:PointCollection><esri:MapPointX="-13411542"Y="8242944"/><esri:MapPointX="-12399574"Y="8242944"/><esri:MapPointX="-12399574"Y="4242944"/><esri:MapPointX="-13411542"Y="4242944"/></esri:PointCollection></esri:Polygon.Rings></esri:Polygon></esri:Graphic><esri:GraphicSymbol="{StaticResource DefaultFillSymbol}"><esri:Polygon><esri:Polygon.SpatialReference><esri:SpatialReferenceWKID="102100"/></esri:Polygon.SpatialReference><esri:Polygon.Rings><esri:PointCollection><esri:MapPointX="-11511542"Y="4442944"/><esri:MapPointX="-10399574"Y="4442944"/><esri:MapPointX="-10399574"Y="2542944"/><esri:MapPointX="-11511542"Y="2542944"/></esri:PointCollection></esri:Polygon.Rings></esri:Polygon></esri:Graphic><esri:GraphicSymbol="{StaticResource DefaultFillSymbol}"><esri:Polygon><esri:Polygon.SpatialReference><esri:SpatialReferenceWKID="102100"/></esri:Polygon.SpatialReference><esri:Polygon.Rings><esri:PointCollection><esri:MapPointX="-13611542"Y="4442944"/><esri:MapPointX="-10399574"Y="4442944"/><esri:MapPointX="-10399574"Y="3542944"/><esri:MapPointX="-13611542"Y="3542944"/></esri:PointCollection></esri:Polygon.Rings></esri:Polygon></esri:Graphic></esri:GraphicsLayer.Graphics></esri:GraphicsLayer><esri:GraphicsLayerID="OutputGraphicsLayer"x:Name="_outputGraphicsLayer"/></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="Create a polygon on the map that intersects the existing graphic on the map (red). When complete the difference between the polygon and the original graphics will be displayed (yellow). Click the Reset button to remove the results."Width="400"TextAlignment="Left"TextWrapping="Wrap"Foreground="Black"/><ButtonContent="Reset"Margin="0,5,5,0"x:Name="ResetButton"Click="ResetButton_Click"IsEnabled="False"Width="150"/></StackPanel></Grid></Grid></UserControl>
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Local;
namespace ArcGISWPFSDK
{
publicpartialclass LocalDifference : UserControl
{
private Draw _drawObject;
GeometryService _geometryTask;
public LocalDifference()
{
InitializeComponent();
LocalGeometryService.GetServiceAsync(localGeometryService =>
{
_geometryTask = new GeometryService();
_geometryTask.Url = localGeometryService.UrlGeometryService;
_geometryTask.DifferenceCompleted += GeometryService_DifferenceCompleted;
_geometryTask.SimplifyCompleted += GeometryService_SimplifyCompleted;
_geometryTask.Failed += GeometryService_Failed;
});
MyMap.Layers.LayersInitialized += (s, e) =>
{
_drawObject = new Draw(MyMap)
{
DrawMode = DrawMode.Polygon,
IsEnabled = true,
FillSymbol = LayoutRoot.Resources["DrawFillSymbol"] as ESRI.ArcGIS.Client.Symbols.FillSymbol
};
_drawObject.DrawComplete += DrawObject_DrawComplete;
};
}
privatevoid DrawObject_DrawComplete(object sender, DrawEventArgs args)
{
_drawObject.IsEnabled = false;
ESRI.ArcGIS.Client.Geometry.Polygon polygon = args.Geometry as ESRI.ArcGIS.Client.Geometry.Polygon;
polygon.SpatialReference = MyMap.SpatialReference;
_geometryTask.SimplifyAsync(new List<Graphic>() { new Graphic() { Geometry = polygon } });
}
void GeometryService_SimplifyCompleted(object sender, GraphicsEventArgs e)
{
_geometryTask.DifferenceAsync(_inputGraphicsLayer.Graphics.ToList(), e.Results[0].Geometry);
}
void GeometryService_DifferenceCompleted(object sender, GraphicsEventArgs e)
{
_outputGraphicsLayer = MyMap.Layers["OutputGraphicsLayer"] as GraphicsLayer;
_outputGraphicsLayer.ClearGraphics();
foreach (Graphic g in e.Results)
{
if (g.Geometry is Polygon)
g.Symbol = LayoutRoot.Resources["DifferenceFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
_outputGraphicsLayer.Graphics.Add(g);
}
_drawObject.IsEnabled = true;
ResetButton.IsEnabled = true;
}
privatevoid GeometryService_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show("Geometry Service error: " + e.Error);
_drawObject.IsEnabled = true;
}
privatevoid ResetButton_Click(object sender, RoutedEventArgs e)
{
GraphicsLayer outputGraphicsLayer = MyMap.Layers["OutputGraphicsLayer"] as GraphicsLayer;
outputGraphicsLayer.ClearGraphics();
ResetButton.IsEnabled = false;
}
}
}
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Tasks
Imports ESRI.ArcGIS.Client.Geometry
Imports ESRI.ArcGIS.Client.Local
Namespace ArcGISWPFSDK
PartialPublicClass LocalDifference
Inherits UserControl
Private MyDrawObject As Draw
Private geometryService As GeometryService
Private inputGraphicsLayer As GraphicsLayer
Private outputGraphicsLayer As GraphicsLayer
PublicSubNew()
InitializeComponent()
Dim localGeometryService AsNew LocalGeometryService()
AddHandler localGeometryService.StartCompleted, Function(sender, eventargs)
Dim lgs As LocalGeometryService = TryCast(sender, LocalGeometryService)
geometryService = New GeometryService()
geometryService.Url = lgs.UrlGeometryService
AddHandler geometryService.DifferenceCompleted, AddressOf GeometryService_DifferenceCompleted
AddHandler geometryService.SimplifyCompleted, AddressOf GeometryService_SimplifyCompleted
AddHandler geometryService.Failed, AddressOf GeometryService_Failed
inputGraphicsLayer = TryCast(MyMap.Layers("InputGraphicsLayer"), GraphicsLayer)
MyDrawObject = New Draw(MyMap) With { _
.DrawMode = DrawMode.Polygon, _
.IsEnabled = True, _
.FillSymbol = TryCast(LayoutRoot.Resources("DrawFillSymbol"), ESRI.ArcGIS.Client.Symbols.FillSymbol) _
}
AddHandler MyDrawObject.DrawComplete, AddressOf MyDrawObject_DrawComplete
EndFunction
localGeometryService.StartAsync()
EndSubPrivateSub MyDrawObject_DrawComplete(sender AsObject, args As DrawEventArgs)
MyDrawObject.IsEnabled = FalseDim polygon As ESRI.ArcGIS.Client.Geometry.Polygon = TryCast(args.Geometry, ESRI.ArcGIS.Client.Geometry.Polygon)
polygon.SpatialReference = MyMap.SpatialReference
geometryService.SimplifyAsync(New List(Of Graphic)() From { _
New Graphic() With { _
.Geometry = polygon _
} _
})
EndSubPrivateSub GeometryService_SimplifyCompleted(sender AsObject, e As GraphicsEventArgs)
geometryService.DifferenceAsync(inputGraphicsLayer.Graphics.ToList(), e.Results(0).Geometry)
EndSubPrivateSub GeometryService_DifferenceCompleted(sender AsObject, e As GraphicsEventArgs)
outputGraphicsLayer = TryCast(MyMap.Layers("OutputGraphicsLayer"), GraphicsLayer)
outputGraphicsLayer.ClearGraphics()
ForEach g As Graphic In e.Results
IfTypeOf g.Geometry Is Polygon Then
g.Symbol = TryCast(LayoutRoot.Resources("DifferenceFillSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
EndIf
outputGraphicsLayer.Graphics.Add(g)
Next
MyDrawObject.IsEnabled = True
ResetButton.IsEnabled = TrueEndSubPrivateSub GeometryService_Failed(sender AsObject, e As TaskFailedEventArgs)
MessageBox.Show("Geometry Service error: " & Convert.ToString(e.[Error]))
MyDrawObject.IsEnabled = TrueEndSubPrivateSub ResetButton_Click(sender AsObject, e As RoutedEventArgs)
Dim outputGraphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("OutputGraphicsLayer"), GraphicsLayer)
outputGraphicsLayer.ClearGraphics()
ResetButton.IsEnabled = FalseEndSubEndClassEndNamespace