This sample demonstrates using the ArcGIS Runtime SDK for WPF to perform a geometry operation that takes an input polyline graphic and makes a redline polygon graphic output.
To use the sample, connect the dots to create a polyline which defines a boundary for a new polygon. Double click the final dot (#3) to auto complete the polygon boundary.
In the code-behind, a LocalGeometryService
is started. The url is passed into a GeometryService,
which is used to takes the input polyline to attempt to make a closed polygon and if successful the output redline polygon graphic is returned.
<UserControlx:Class="ArcGISWPFSDK.LocalAutoComplete"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><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><esri:SimpleMarkerSymbolx:Key="BlackMarkerSymbol"Color="Black"Size="10"/><esri:SimpleLineSymbolx:Key="RedLineSymbol"Color="Red"Width="4"Style="Solid"/><esri:SimpleFillSymbolx:Key="BlueFillSymbol"Fill="#660000FF"BorderBrush="Blue"BorderThickness="2"/><esri:SimpleFillSymbolx:Key="RedFillSymbol"Fill="#66FF0000"BorderBrush="Red"BorderThickness="2"/></Grid.Resources><esri:Mapx:Name="MyMap"Extent="-15000000,2000000,-7000000,8000000"Background="#FFE3E3E3"IsEnabled="False"><esri:ArcGISLocalTiledLayerID="BaseMap"Path="..\\Data\\TPKs\\Topographic.tpk"/><esri:GraphicsLayerID="ParcelsGraphicsLayer"x:Name="_parcelsGraphicsLayer"/><esri:GraphicsLayerID="CompletedGraphicsLayer"x:Name="_completedGraphicsLayer"/><esri:GraphicsLayerID="ConnectDotsGraphicsLayer"><esri:GraphicSymbol="{StaticResource BlackMarkerSymbol}"><esri:MapPointX="-12944384"Y="6804466"/><esri:Graphic.MapTip><TextBlockText="Click"FontSize="12"Foreground="White"><TextBlock.Effect><DropShadowEffectOpacity="1"ShadowDepth="2"/></TextBlock.Effect></TextBlock></esri:Graphic.MapTip></esri:Graphic><esri:Graphic><esri:Graphic.Symbol><esri:TextSymbolFontFamily="Arial"FontSize="16"Foreground="Black"Text="2"OffsetX="-5"OffsetY="15"/></esri:Graphic.Symbol><esri:MapPointX="-12944384"Y="6804466"/></esri:Graphic><esri:GraphicSymbol="{StaticResource BlackMarkerSymbol}"><esri:MapPointX="-12834399"Y="6207201"/><esri:Graphic.MapTip><TextBlockText="Click"FontSize="12"Foreground="White"><TextBlock.Effect><DropShadowEffectOpacity="1"ShadowDepth="2"/></TextBlock.Effect></TextBlock></esri:Graphic.MapTip></esri:Graphic><esri:Graphic><esri:Graphic.Symbol><esri:TextSymbolFontFamily="Arial"FontSize="16"Foreground="Black"Text="1"OffsetX="-5"OffsetY="15"/></esri:Graphic.Symbol><esri:MapPointX="-12834399"Y="6207201"/></esri:Graphic><esri:GraphicSymbol="{StaticResource BlackMarkerSymbol}"><esri:MapPointX="-11690605"Y="6154733"/><esri:Graphic.MapTip><TextBlockText="Double click"FontSize="12"Foreground="White"><TextBlock.Effect><DropShadowEffectOpacity="1"ShadowDepth="2"/></TextBlock.Effect></TextBlock></esri:Graphic.MapTip></esri:Graphic><esri:Graphic><esri:Graphic.Symbol><esri:TextSymbolFontFamily="Arial"FontSize="16"Foreground="Black"Text="3"OffsetX="-5"OffsetY="15"/></esri:Graphic.Symbol><esri:MapPointX="-11690605"Y="6154733"/></esri:Graphic></esri:GraphicsLayer></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"/><TextBlockx:Name="ResponseTextBlock"Foreground="Black"Text="Connect the dots to create a polyline which defines a boundary for a new polygon. Double click the final dot (#3) to auto complete the polygon boundary."Width="200"TextAlignment="Left"Margin="30,20,20,30"TextWrapping="Wrap"/></Grid></Grid></UserControl>
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;
namespace ArcGISWPFSDK
{
publicpartialclass LocalAutoComplete : UserControl
{
private Draw _drawObject;
GeometryService _geometryTask;
QueryTask _queryTask;
public LocalAutoComplete()
{
InitializeComponent();
MyMap.Layers.LayersInitialized += (s,e)=>
{
LocalMapService.GetServiceAsync("..\\Data\\MPKS\\USCitiesStates.mpk", (localMapService) =>
{
_queryTask = new QueryTask();
_queryTask.Url = localMapService.UrlMapService + "/2";
Query query = new Query();
query.Geometry = MyMap.Extent;
query.ReturnGeometry = true;
query.Where = "\"STATE_NAME\" = 'Montana'";
_queryTask.ExecuteCompleted += queryTask_ExecuteCompleted;
_queryTask.Failed += queryTask_Failed;
_queryTask.ExecuteAsync(query);
});
_drawObject = new Draw(MyMap)
{
DrawMode = DrawMode.Polyline,
IsEnabled = true,
LineSymbol = LayoutRoot.Resources["RedLineSymbol"] as ESRI.ArcGIS.Client.Symbols.LineSymbol
};
_drawObject.DrawComplete += DrawObject_DrawComplete;
MyMap.MinimumResolution = double.Epsilon;
};
LocalGeometryService.GetServiceAsync(localGeometryService =>
{
_geometryTask = new GeometryService();
_geometryTask.Url = localGeometryService.UrlGeometryService;
_geometryTask.AutoCompleteCompleted += GeometryService_AutoCompleteCompleted;
_geometryTask.Failed += GeometryService_Failed;
});
}
void queryTask_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show("Query error: " + e.Error);
}
void queryTask_ExecuteCompleted(object sender, QueryEventArgs e)
{
foreach (Graphic g in e.FeatureSet.Features)
{
g.Symbol = LayoutRoot.Resources["BlueFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
_parcelsGraphicsLayer.Graphics.Add(g);
}
MyMap.IsEnabled = true;
}
privatevoid DrawObject_DrawComplete(object sender, DrawEventArgs args)
{
ESRI.ArcGIS.Client.Geometry.Polyline polyline = args.Geometry as ESRI.ArcGIS.Client.Geometry.Polyline;
Graphic polylineGraphic = new Graphic()
{
Geometry = polyline
};
List<Graphic> polylineList = new List<Graphic>();
polylineList.Add(polylineGraphic);
GraphicsLayer graphicsLayer = MyMap.Layers["ParcelsGraphicsLayer"] as GraphicsLayer;
List<Graphic> polygonList = new List<Graphic>();
foreach (Graphic g in graphicsLayer.Graphics)
{
g.Geometry.SpatialReference = MyMap.SpatialReference;
polygonList.Add(g);
}
_geometryTask.AutoCompleteAsync(polygonList, polylineList);
}
void GeometryService_AutoCompleteCompleted(object sender, GraphicsEventArgs e)
{
_completedGraphicsLayer.ClearGraphics();
foreach (Graphic g in e.Results)
{
g.Symbol = LayoutRoot.Resources["RedFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
_completedGraphicsLayer.Graphics.Add(g);
}
if (e.Results.Count > 0)
(MyMap.Layers["ConnectDotsGraphicsLayer"] as GraphicsLayer).ClearGraphics();
}
privatevoid GeometryService_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show("Geometry Service error: " + e.Error);
}
}
}
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Tasks
Imports ESRI.ArcGIS.Client.Local
Namespace ArcGISWPFSDK
PartialPublicClass LocalAutoComplete
Inherits UserControl
Private MyDrawObject As Draw
Private outputGraphicsLayer As GraphicsLayer
Private _geometryService As GeometryService
Private _queryTask As QueryTask
Private _mapService As LocalMapService
PublicSubNew()
InitializeComponent()
LocalMapService.GetServiceAsync("..\Data\MPKS\USCitiesStates.mpk", Function(localMapService__1)
_mapService = localMapService__1
_queryTask = New QueryTask()
_queryTask.Url = _mapService.UrlMapService & "/2"Dim query AsNew Query()
query.Geometry = MyMap.Extent
query.ReturnGeometry = True
query.Where = """STATE_NAME"" = 'Montana'"AddHandler _queryTask.ExecuteCompleted, AddressOf queryTask_ExecuteCompleted
AddHandler _queryTask.Failed, AddressOf queryTask_Failed
_queryTask.ExecuteAsync(query)
EndFunction)
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.AutoCompleteCompleted, AddressOf GeometryService_AutoCompleteCompleted
AddHandler _geometryService.Failed, AddressOf GeometryService_Failed
MyMap.IsEnabled = True
MyMap.MinimumResolution = Double.Epsilon
MyDrawObject = New Draw(MyMap) With { _
.DrawMode = DrawMode.Polyline, _
.IsEnabled = True, _
.LineSymbol = TryCast(LayoutRoot.Resources("RedLineSymbol"), ESRI.ArcGIS.Client.Symbols.LineSymbol) _
}
AddHandler MyDrawObject.DrawComplete, AddressOf MyDrawObject_DrawComplete
outputGraphicsLayer = TryCast(MyMap.Layers("CompletedGraphicsLayer"), GraphicsLayer)
EndFunction
localGeometryService.StartAsync()
EndSubPrivateSub queryTask_Failed(sender AsObject, e As TaskFailedEventArgs)
MessageBox.Show("Query error: " & Convert.ToString(e.[Error]))
EndSubPrivateSub queryTask_ExecuteCompleted(sender AsObject, e As QueryEventArgs)
Dim parcelGraphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("ParcelsGraphicsLayer"), GraphicsLayer)
ForEach g As Graphic In e.FeatureSet.Features
g.Symbol = TryCast(LayoutRoot.Resources("BlueFillSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
parcelGraphicsLayer.Graphics.Add(g)
NextEndSubPrivateSub MyDrawObject_DrawComplete(sender AsObject, args As DrawEventArgs)
Dim polyline As ESRI.ArcGIS.Client.Geometry.Polyline = TryCast(args.Geometry, ESRI.ArcGIS.Client.Geometry.Polyline)
Dim polylineGraphic AsNew Graphic() With { _
.Geometry = polyline _
}
Dim polylineList AsNew List(Of Graphic)()
polylineList.Add(polylineGraphic)
Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("ParcelsGraphicsLayer"), GraphicsLayer)
Dim polygonList AsNew List(Of Graphic)()
ForEach g As Graphic In graphicsLayer.Graphics
g.Geometry.SpatialReference = MyMap.SpatialReference
polygonList.Add(g)
Next
_geometryService.AutoCompleteAsync(polygonList, polylineList)
EndSubPrivateSub GeometryService_AutoCompleteCompleted(sender AsObject, e As GraphicsEventArgs)
outputGraphicsLayer.ClearGraphics()
ForEach g As Graphic In e.Results
g.Symbol = TryCast(LayoutRoot.Resources("RedFillSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
outputGraphicsLayer.Graphics.Add(g)
NextIf e.Results.Count > 0 Then
TryCast(MyMap.Layers("ConnectDotsGraphicsLayer"), GraphicsLayer).ClearGraphics()
EndIfEndSubPrivateSub GeometryService_Failed(sender AsObject, e As TaskFailedEventArgs)
MessageBox.Show("Geometry Service error: " & Convert.ToString(e.[Error]))
EndSubEndClassEndNamespace