This sample demonstrates using the ArcGIS Runtime SDK for WPF to perform a geometry operation that takes graphic polyline with numerous vertices and returns a generalized polyline with less vertices. Additionally a SliderControl appears after the GeometryService completes that allows adjusting the opacity of the generalized polyline graphics layer for ease of viewing.
To use the sample, click the Generalize Line button to generalize the line feature. Original line and vertices are red. Generalized line a vertices are cyan. Use the slider to change the opacity of the generalized line. Zoom in on the graphics to see in better detail the difference between the original polyline and the generalized line.
<UserControlx:Class="ArcGISWPFSDK.LocalGeneralize"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:SimpleMarkerSymbolx:Key="NewMarkerSymbol"Color="#FF00FFFF"Size="6"/><esri:SimpleLineSymbolx:Key="DefaultLineSymbol"Color="#AAFF0000"Width="2"/><esri:SimpleLineSymbolx:Key="NewLineSymbol"Color="#AA00FFFF"Width="2"/><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="-14000000,4000000,-12000000,6000000"Background="#FFE3E3E3"MinimumResolution="500"><esri:ArcGISLocalTiledLayerID="Shaded Relief"Path="..\\Data\\TPKs\\Topographic.tpk"/><esri:GraphicsLayerID="OriginalLineGraphicsLayer"/><esri:GraphicsLayerID="GeneralizedLineGraphicsLayer"/></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="Click the Generalize Line button to generalize the line feature. Original line and vertices are red. Generalized line a vertices are cyan. Use the slider to change the opacity of the generalized line."Width="400"TextAlignment="Left"TextWrapping="Wrap"Foreground="Black"/><StackPanelOrientation="Horizontal"HorizontalAlignment="Center"><ButtonContent="Generalize Line"Margin="0,5,5,0"x:Name="GeneralizeButton"Click="GeneralizeButton_Click"IsEnabled="False"/></StackPanel><StackPanelx:Name="SliderStackPanel"Margin="5,5,5,5"Orientation="Horizontal"HorizontalAlignment="Center"Visibility="Collapsed"><TextBlockFontSize="9"Foreground="Black"Text="0"/><Sliderx:Name="GeneralizeLayersOpacity"Minimum="0"Maximum="1"SmallChange="0.01"LargeChange="0.1"Value="0.75"Orientation="Horizontal"ValueChanged="GeneralizeLayerOpacity_ValueChanged"Width="120"/><TextBlockFontSize="9"Foreground="Black"Text="100"/></StackPanel></StackPanel></Grid></Grid></UserControl>
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Local;
namespace ArcGISWPFSDK
{
publicpartialclass LocalGeneralize : UserControl
{
GeometryService _geometryTask;
public LocalGeneralize()
{
InitializeComponent();
MyMap.Layers.LayersInitialized += (s, e) =>
{
LocalMapService.GetServiceAsync("..\\Data\\MPKS\\USCitiesStates.mpk", (localMapService) =>
{
QueryTask _queryTask = new QueryTask();
_queryTask.Url = localMapService.UrlMapService + "/1";
Query query = new Query();
query.Geometry = MyMap.Extent;
query.Where = "OBJECTID = 480";
query.OutSpatialReference = MyMap.SpatialReference;
query.ReturnGeometry = true;
_queryTask.ExecuteCompleted += queryTask_ExecuteCompleted;
_queryTask.Failed += queryTask_Failed;
_queryTask.ExecuteAsync(query, query.OutSpatialReference);
});
};
LocalGeometryService.GetServiceAsync(lgs =>
{
_geometryTask = new GeometryService();
_geometryTask.Url = lgs.UrlGeometryService;
_geometryTask.GeneralizeCompleted += GeometryService_GeneralizeCompleted;
_geometryTask.Failed += GeometryService_Failed;
});
}
void queryTask_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show("Query error: " + e.Error);
}
void queryTask_ExecuteCompleted(object sender, QueryEventArgs e)
{
GraphicsLayer originalGraphicsLayer = MyMap.Layers["OriginalLineGraphicsLayer"] as GraphicsLayer;
foreach (Graphic g in e.FeatureSet.Features)
{
g.Symbol = LayoutRoot.Resources["DefaultLineSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
g.Geometry.SpatialReference = e.UserState as SpatialReference;
originalGraphicsLayer.Graphics.Add(g);
foreach (ESRI.ArcGIS.Client.Geometry.PointCollection pc in (g.Geometry as Polyline).Paths)
{
foreach (MapPoint point in pc)
{
Graphic vertice = new Graphic()
{
Symbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol,
Geometry = point
};
originalGraphicsLayer.Graphics.Add(vertice);
}
}
}
GeneralizeButton.IsEnabled = true;
}
privatevoid GeneralizeButton_Click(object sender, RoutedEventArgs e)
{
GeneralizeButton.IsEnabled = false;
SliderStackPanel.Visibility = Visibility.Collapsed;
GraphicsLayer originalGraphicsLayer = MyMap.Layers["OriginalLineGraphicsLayer"] as GraphicsLayer;
GeneralizeParameters generalizeParameters = new GeneralizeParameters()
{
DeviationUnit= LinearUnit.StatuteMile,
MaxDeviation = 10
};
_geometryTask.GeneralizeAsync(new List<Graphic>() { originalGraphicsLayer.Graphics[0] }, generalizeParameters);
}
void GeometryService_GeneralizeCompleted(object sender, GraphicsEventArgs e)
{
GraphicsLayer generalizedGraphicsLayer = MyMap.Layers["GeneralizedLineGraphicsLayer"] as GraphicsLayer;
generalizedGraphicsLayer.ClearGraphics();
foreach (Graphic g in e.Results)
{
g.Symbol = LayoutRoot.Resources["NewLineSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
generalizedGraphicsLayer.Graphics.Add(g);
ESRI.ArcGIS.Client.Geometry.Polyline p = g.Geometry as ESRI.ArcGIS.Client.Geometry.Polyline;
foreach (ESRI.ArcGIS.Client.Geometry.PointCollection pc in p.Paths)
{
foreach (MapPoint point in pc)
{
Graphic vertice = new Graphic()
{
Symbol = LayoutRoot.Resources["NewMarkerSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol,
Geometry = point
};
generalizedGraphicsLayer.Graphics.Add(vertice);
}
}
}
generalizedGraphicsLayer.Opacity = 0.75;
SliderStackPanel.Visibility = Visibility.Visible;
GeneralizeButton.IsEnabled = true;
}
privatevoid GeometryService_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show("Geometry Service error: " + e.Error);
}
privatevoid GeneralizeLayerOpacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (MyMap != null)
MyMap.Layers["GeneralizedLineGraphicsLayer"].Opacity = e.NewValue;
}
}
}
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports ESRI.ArcGIS.Client.Tasks
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Geometry
Imports ESRI.ArcGIS.Client.Local
Namespace ArcGISWPFSDK
PartialPublicClass LocalGeneralize
Inherits UserControl
Private originalGraphicsLayer As GraphicsLayer
Private _geometryService As GeometryService
Private _queryTask As QueryTask
Private _mapService As LocalMapService
PublicSubNew()
InitializeComponent()
LocalMapService.GetServiceAsync("..\Data\MPKS\USCitiesStatesEdit.mpk", Function(localMapService__1)
_mapService = localMapService__1
_queryTask = New QueryTask()
_queryTask.Url = _mapService.UrlMapService & "/1"Dim query AsNew Query()
query.Geometry = MyMap.Extent
query.Where = "OBJECTID = 480"
query.OutSpatialReference = MyMap.SpatialReference
query.ReturnGeometry = TrueAddHandler _queryTask.ExecuteCompleted, AddressOf queryTask_ExecuteCompleted
AddHandler _queryTask.Failed, AddressOf queryTask_Failed
_queryTask.ExecuteAsync(query, query.OutSpatialReference)
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
MyMap.IsEnabled = TrueAddHandler _geometryService.GeneralizeCompleted, AddressOf GeometryService_GeneralizeCompleted
AddHandler _geometryService.Failed, AddressOf GeometryService_Failed
EndFunction
localGeometryService.StartAsync()
originalGraphicsLayer = TryCast(MyMap.Layers("OriginalLineGraphicsLayer"), GraphicsLayer)
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 originalGraphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("OriginalLineGraphicsLayer"), GraphicsLayer)
ForEach g As Graphic In e.FeatureSet.Features
g.Symbol = TryCast(LayoutRoot.Resources("DefaultLineSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
g.Geometry.SpatialReference = TryCast(e.UserState, SpatialReference)
originalGraphicsLayer.Graphics.Add(g)
ForEach pc As ESRI.ArcGIS.Client.Geometry.PointCollection In TryCast(g.Geometry, Polyline).Paths
ForEach point As MapPoint In pc
Dim vertice AsNew Graphic() With { _
.Symbol = TryCast(LayoutRoot.Resources("DefaultMarkerSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol), _
.Geometry = point _
}
originalGraphicsLayer.Graphics.Add(vertice)
NextNextNext
GeneralizeButton.IsEnabled = TrueEndSubPrivateSub GeneralizeButton_Click(sender AsObject, e As RoutedEventArgs)
GeneralizeButton.IsEnabled = False
SliderStackPanel.Visibility = Visibility.Collapsed
Dim originalGraphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("OriginalLineGraphicsLayer"), GraphicsLayer)
Dim generalizeParameters AsNew GeneralizeParameters() With { _
.DeviationUnit = LinearUnit.SurveyMile, _
.MaxDeviation = 0.000001 _
}
_geometryService.GeneralizeAsync(New List(Of Graphic)() From { _
originalGraphicsLayer.Graphics(0) _
}, generalizeParameters)
EndSubPrivateSub GeometryService_GeneralizeCompleted(sender AsObject, e As GraphicsEventArgs)
Dim generalizedGraphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("GeneralizedLineGraphicsLayer"), GraphicsLayer)
generalizedGraphicsLayer.ClearGraphics()
ForEach g As Graphic In e.Results
g.Symbol = TryCast(LayoutRoot.Resources("NewLineSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
generalizedGraphicsLayer.Graphics.Add(g)
Dim p As ESRI.ArcGIS.Client.Geometry.Polyline = TryCast(g.Geometry, ESRI.ArcGIS.Client.Geometry.Polyline)
ForEach pc As ESRI.ArcGIS.Client.Geometry.PointCollection In p.Paths
ForEach point As MapPoint In pc
Dim vertice AsNew Graphic() With { _
.Symbol = TryCast(LayoutRoot.Resources("NewMarkerSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol), _
.Geometry = point _
}
generalizedGraphicsLayer.Graphics.Add(vertice)
NextNextNext
generalizedGraphicsLayer.Opacity = 0.75
SliderStackPanel.Visibility = Visibility.Visible
GeneralizeButton.IsEnabled = TrueEndSubPrivateSub GeometryService_Failed(sender AsObject, e As TaskFailedEventArgs)
MessageBox.Show("Geometry Service error: " & Convert.ToString(e.[Error]))
EndSubPrivateSub GeneralizeLayerOpacity_ValueChanged(sender AsObject, e As RoutedPropertyChangedEventArgs(Of Double))
If MyMap IsNotNothingThen
MyMap.Layers("GeneralizedLineGraphicsLayer").Opacity = e.NewValue
EndIfEndSubEndClassEndNamespace