This sample demonstrates using an ESRI.ArcGIS.Client.Tasks GeometryService that takes an input polygon graphic and returns a densified polygon graphic. The original vertices to create the original polygon graphic are shown in red. The returned polygon shows the additional densified vertices in green.
To use the sample, create a polygon on the map. Red points indicate digitized polygon vertices. Click the Densify Polygon button to densify the polygon. Green points show all vertices (original and new) in the densified polygon.
<UserControlx:Class="ArcGISWPFSDK.Densify"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="12"/><esri:SimpleMarkerSymbolx:Key="NewMarkerSymbol"Size="8"><esri:SimpleMarkerSymbol.Color><RadialGradientBrush><GradientStopColor="LightGreen"Offset="0.578"/><GradientStopColor="#FF0E0D07"Offset="1"/></RadialGradientBrush></esri:SimpleMarkerSymbol.Color></esri:SimpleMarkerSymbol><esri:SimpleFillSymbolx:Key="DefaultFillSymbol"Fill="#88000000"BorderBrush="Black"/><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"><esri:ArcGISTiledMapServiceLayerID="StreetMapLayer"Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/><esri:GraphicsLayerID="PolygonGraphicsLayer"/><esri:GraphicsLayerID="VerticesGraphicsLayer"/></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,25,30"><TextBlockx:Name="ResponseTextBlock"Text="Create a polygon on the map. Red points indicate digitized polygon vertices. Click the Densify Polygon button to densify the polygon. Green points show all vertices (original and new) in the densified polygon."Width="550"TextAlignment="Left"TextWrapping="Wrap"Foreground="Black"/><StackPanelOrientation="Horizontal"HorizontalAlignment="Center"><ButtonContent="Densify Polygon"Margin="0,5,5,0"x:Name="DensifyButton"Click="DensifyButton_Click"IsEnabled="False"/></StackPanel></StackPanel></Grid></Grid></UserControl>
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Tasks;
namespace ArcGISWPFSDK
{
publicpartialclass Densify : UserControl
{
private Draw MyDrawObject;
public Densify()
{
InitializeComponent();
MyDrawObject = new Draw(MyMap)
{
DrawMode = DrawMode.Polygon,
IsEnabled = true,
FillSymbol = LayoutRoot.Resources["DefaultFillSymbol"] as ESRI.ArcGIS.Client.Symbols.FillSymbol
};
MyDrawObject.DrawComplete += MyDrawObject_DrawComplete;
MyDrawObject.DrawBegin += MyDrawObject_DrawBegin;
}
privatevoid MyDrawObject_DrawBegin(object sender, EventArgs args)
{
GraphicsLayer graphicsLayerPolygon = MyMap.Layers["PolygonGraphicsLayer"] as GraphicsLayer;
graphicsLayerPolygon.ClearGraphics();
GraphicsLayer graphicsLayerVertices = MyMap.Layers["VerticesGraphicsLayer"] as GraphicsLayer;
graphicsLayerVertices.ClearGraphics();
}
privatevoid MyDrawObject_DrawComplete(object sender, DrawEventArgs args)
{
ESRI.ArcGIS.Client.Geometry.Polygon polygon = args.Geometry as ESRI.ArcGIS.Client.Geometry.Polygon;
polygon.SpatialReference = MyMap.SpatialReference;
Graphic graphic = new Graphic()
{
Symbol = LayoutRoot.Resources["DefaultFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol,
Geometry = polygon
};
GraphicsLayer graphicsLayerPolygon = MyMap.Layers["PolygonGraphicsLayer"] as GraphicsLayer;
graphicsLayerPolygon.Graphics.Add(graphic);
GraphicsLayer graphicsLayerVertices = MyMap.Layers["VerticesGraphicsLayer"] as GraphicsLayer;
foreach (MapPoint point in polygon.Rings[0])
{
Graphic vertice = new Graphic()
{
Symbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol,
Geometry = point
};
graphicsLayerVertices.Graphics.Add(vertice);
}
DensifyButton.IsEnabled = true;
}
privatevoid DensifyButton_Click(object sender, RoutedEventArgs e)
{
DensifyButton.IsEnabled = false;
GraphicsLayer graphicsLayerPolygon = MyMap.Layers["PolygonGraphicsLayer"] as GraphicsLayer;
GeometryService geometryService =
new GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
geometryService.DensifyCompleted += GeometryService_DensifyCompleted;
geometryService.Failed += GeometryService_Failed;
DensifyParameters densityParameters = new DensifyParameters()
{
LengthUnit = LinearUnit.Meter,
Geodesic = true,
MaxSegmentLength = MyMap.Resolution * 10
};
geometryService.DensifyAsync(graphicsLayerPolygon.Graphics.ToList(), densityParameters);
}
void GeometryService_DensifyCompleted(object sender, GraphicsEventArgs e)
{
GraphicsLayer graphicsLayerVertices = MyMap.Layers["VerticesGraphicsLayer"] as GraphicsLayer;
foreach (Graphic g in e.Results)
{
ESRI.ArcGIS.Client.Geometry.Polygon p = g.Geometry as ESRI.ArcGIS.Client.Geometry.Polygon;
foreach (ESRI.ArcGIS.Client.Geometry.PointCollection pc in p.Rings)
{
foreach (MapPoint point in pc)
{
Graphic vertice = new Graphic()
{
Symbol = LayoutRoot.Resources["NewMarkerSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol,
Geometry = point
};
graphicsLayerVertices.Graphics.Add(vertice);
}
}
}
DensifyButton.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.Geometry
Imports ESRI.ArcGIS.Client.Tasks
Namespace ArcGISWPFSDK
PartialPublicClass Densify
Inherits UserControl
Private MyDrawObject As Draw
PublicSubNew()
InitializeComponent()
MyDrawObject = New Draw(MyMap) With { _
.DrawMode = DrawMode.Polygon, _
.IsEnabled = True, _
.FillSymbol = TryCast(LayoutRoot.Resources("DefaultFillSymbol"), ESRI.ArcGIS.Client.Symbols.FillSymbol) _
}
AddHandler MyDrawObject.DrawComplete, AddressOf MyDrawObject_DrawComplete
AddHandler MyDrawObject.DrawBegin, AddressOf MyDrawObject_DrawBegin
EndSubPrivateSub MyDrawObject_DrawBegin(sender AsObject, args As EventArgs)
Dim graphicsLayerPolygon As GraphicsLayer = TryCast(MyMap.Layers("PolygonGraphicsLayer"), GraphicsLayer)
graphicsLayerPolygon.ClearGraphics()
Dim graphicsLayerVertices As GraphicsLayer = TryCast(MyMap.Layers("VerticesGraphicsLayer"), GraphicsLayer)
graphicsLayerVertices.ClearGraphics()
EndSubPrivateSub MyDrawObject_DrawComplete(sender AsObject, args As DrawEventArgs)
Dim polygon As ESRI.ArcGIS.Client.Geometry.Polygon = TryCast(args.Geometry, ESRI.ArcGIS.Client.Geometry.Polygon)
polygon.SpatialReference = MyMap.SpatialReference
Dim graphic AsNew Graphic() With { _
.Symbol = TryCast(LayoutRoot.Resources("DefaultFillSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol), _
.Geometry = polygon _
}
Dim graphicsLayerPolygon As GraphicsLayer = TryCast(MyMap.Layers("PolygonGraphicsLayer"), GraphicsLayer)
graphicsLayerPolygon.Graphics.Add(graphic)
Dim graphicsLayerVertices As GraphicsLayer = TryCast(MyMap.Layers("VerticesGraphicsLayer"), GraphicsLayer)
ForEach point As MapPoint In polygon.Rings(0)
Dim vertice AsNew Graphic() With { _
.Symbol = TryCast(LayoutRoot.Resources("DefaultMarkerSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol), _
.Geometry = point _
}
graphicsLayerVertices.Graphics.Add(vertice)
Next
DensifyButton.IsEnabled = TrueEndSubPrivateSub DensifyButton_Click(sender AsObject, e As RoutedEventArgs)
DensifyButton.IsEnabled = FalseDim graphicsLayerPolygon As GraphicsLayer = TryCast(MyMap.Layers("PolygonGraphicsLayer"), GraphicsLayer)
Dim geometryService AsNew GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer")
AddHandler geometryService.DensifyCompleted, AddressOf GeometryService_DensifyCompleted
AddHandler geometryService.Failed, AddressOf GeometryService_Failed
Dim densityParameters AsNew DensifyParameters() With { _
.LengthUnit = LinearUnit.Meter, _
.Geodesic = True, _
.MaxSegmentLength = MyMap.Resolution * 10 _
}
geometryService.DensifyAsync(graphicsLayerPolygon.Graphics.ToList(), densityParameters)
EndSubPrivateSub GeometryService_DensifyCompleted(sender AsObject, e As GraphicsEventArgs)
Dim graphicsLayerVertices As GraphicsLayer = TryCast(MyMap.Layers("VerticesGraphicsLayer"), GraphicsLayer)
ForEach g As Graphic In e.Results
Dim p As ESRI.ArcGIS.Client.Geometry.Polygon = TryCast(g.Geometry, ESRI.ArcGIS.Client.Geometry.Polygon)
ForEach pc As ESRI.ArcGIS.Client.Geometry.PointCollection In p.Rings
ForEach point As MapPoint In pc
Dim vertice AsNew Graphic() With { _
.Symbol = TryCast(LayoutRoot.Resources("NewMarkerSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol), _
.Geometry = point _
}
graphicsLayerVertices.Graphics.Add(vertice)
NextNextNext
DensifyButton.IsEnabled = TrueEndSubPrivateSub GeometryService_Failed(sender AsObject, e As TaskFailedEventArgs)
MessageBox.Show("Geometry Service error: " & Convert.ToString(e.[Error]))
EndSubEndClassEndNamespace