This sample demonstrates using the ArcGIS Runtime SDK for WPF to perform a geometry operation to measure the area and perimeters of specified polygons. To use the sample, click subsequent points on the map to draw a polygon. Double-click to complete the polygon. Once the polygon is completed, its area and perimeter will be calculated and displayed in the upper right corner of the application.
In the code-behind, a LocalGeometryService
is started. The url is passed into a GeometryService,
which is used to first project the polygon into a spatial reference that uses measured units, and then to calculate the area and perimeter of the polygon.
<UserControlx:Class="ArcGISWPFSDK.LocalAreasAndLengths"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="Transparent"><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:SimpleFillSymbolx:Key="DefaultFillSymbol"Fill="#7F0000FF"BorderBrush="Blue"BorderThickness="4"/></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="MyGraphicsLayer"/></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"Text="Create a polygon by clicking on map. The area and perimeter are displayed here."Width="200"TextAlignment="Left"Margin="30,20,20,30"TextWrapping="Wrap"Foreground="Black"/></Grid></Grid></UserControl>
using System;
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 LocalAreasAndLengths : UserControl
{
private Draw _drawObject;
GeometryService _geometryTask;
public LocalAreasAndLengths()
{
InitializeComponent();
LocalGeometryService.GetServiceAsync(localGeometryService =>
{
_geometryTask = new GeometryService();
_geometryTask.Url = localGeometryService.UrlGeometryService;
_geometryTask.AreasAndLengthsCompleted += GeometryService_AreasAndLengthsCompleted;
_geometryTask.Failed += GeometryService_Failed;
MyMap.IsEnabled = true;
});
MyMap.Layers.LayersInitialized += (s, e) =>
{
_drawObject = new Draw(MyMap)
{
DrawMode = DrawMode.Polygon,
IsEnabled = true,
FillSymbol = LayoutRoot.Resources["DefaultFillSymbol"] as ESRI.ArcGIS.Client.Symbols.FillSymbol
};
_drawObject.DrawComplete += DrawObject_DrawComplete;
_drawObject.DrawBegin += DrawObject_DrawBegin;
};
}
privatevoid DrawObject_DrawBegin(object sender, EventArgs args)
{
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.ClearGraphics();
}
privatevoid DrawObject_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.FillSymbol,
Geometry = polygon,
};
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.Graphics.Add(graphic);
List<Graphic> graphicList = new List<Graphic>();
graphicList.Add(graphic);
_geometryTask.AreasAndLengthsAsync(graphicList);
}
privatevoid GeometryService_AreasAndLengthsCompleted(object sender, AreasAndLengthsEventArgs args)
{
double miles = args.Results.Lengths[0] * 0.0006213700922;
double sqmi = Math.Abs(args.Results.Areas[0]) * 0.0000003861003;
ResponseTextBlock.Text = String.Format("Polygon area: {0} sq. miles\nPolygon perimeter: {1} miles.", Math.Round(sqmi, 3), Math.Round(miles, 3));
}
privatevoid GeometryService_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show("Geometry Service error: " + e.Error);
}
}
}
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Geometry
Imports ESRI.ArcGIS.Client.Tasks
Imports ESRI.ArcGIS.Client.Local
Namespace ArcGISWPFSDK
PartialPublicClass LocalAreasAndLengths
Inherits UserControl
Private MyDrawObject As Draw
Private geometryService As GeometryService
PublicSubNew()
InitializeComponent()
Dim localGeometryService AsNew LocalGeometryService()
AddHandler localGeometryService.StartCompleted, AddressOf localGeometryService_StartCompleted
localGeometryService.StartAsync()
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 graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
graphicsLayer.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.FillSymbol), _
.Geometry = polygon _
}
Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
graphicsLayer.Graphics.Add(graphic)
Dim graphicList AsNew List(Of Graphic)()
graphicList.Add(graphic)
geometryService.AreasAndLengthsAsync(graphicList)
EndSubPrivateSub GeometryService_AreasAndLengthsCompleted(sender AsObject, args As AreasAndLengthsEventArgs)
Dim miles AsDouble = args.Results.Lengths(0) * 0.0006213700922
Dim sqmi AsDouble = Math.Abs(args.Results.Areas(0)) * 0.0000003861003
ResponseTextBlock.Text = [String].Format("Polygon area: {0} sq. miles" & vbLf & "Polygon perimeter: {1} miles.", Math.Round(sqmi, 3), Math.Round(miles, 3))
EndSubPrivateSub 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.AreasAndLengthsCompleted, AddressOf GeometryService_AreasAndLengthsCompleted
AddHandler geometryService.Failed, AddressOf GeometryService_Failed
MyMap.IsEnabled = TrueEndSubEndClassEndNamespace