This sample demonstrates use of a GeometryService to measure area and perimeter. 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 GeometryService 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.AreasAndLengths"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"><esri:ArcGISTiledMapServiceLayerID="StreetMapLayer"Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/><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.Geometry;
using ESRI.ArcGIS.Client.Tasks;
namespace ArcGISWPFSDK
{
publicpartialclass AreasAndLengths : UserControl
{
private Draw MyDrawObject;
public AreasAndLengths()
{
InitializeComponent();
ESRI.ArcGIS.Client.Geometry.Envelope initialExtent =
new ESRI.ArcGIS.Client.Geometry.Envelope(
ESRI.ArcGIS.Client.Bing.Transform.GeographicToWebMercator(
new ESRI.ArcGIS.Client.Geometry.MapPoint(-130, 20)),
ESRI.ArcGIS.Client.Bing.Transform.GeographicToWebMercator(
new ESRI.ArcGIS.Client.Geometry.MapPoint(-65, 55)));
initialExtent.SpatialReference = new SpatialReference(102100);
MyMap.Extent = initialExtent;
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 graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.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.FillSymbol,
Geometry = polygon,
};
GeometryService geometryService =
new GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
geometryService.AreasAndLengthsCompleted += GeometryService_AreasAndLengthsCompleted;
geometryService.Failed += GeometryService_Failed;
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.Graphics.Add(graphic);
List<Graphic> graphicList = new List<Graphic>();
graphicList.Add(graphic);
geometryService.AreasAndLengthsAsync(graphicList);
// GeometryService.AreasAndLengths returns distances and areas in the units of the spatial reference.// The units in the map view's projection is decimal degrees. // Use the Project method to convert graphic points to a projection that uses a measured unit (e.g. meters).// If the map units are in measured units, the call to Project is unnecessary. // Important: Use a projection appropriate for your area of interest.
}
privatevoid GeometryService_AreasAndLengthsCompleted(object sender, AreasAndLengthsEventArgs args)
{
//TODO: Geometry service not returning correct values, area unit seems invalid, may need to use data in //planar coordinate space// convert results from meters into miles and sq meters into sq miles for our displaydouble 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
Namespace ArcGISWPFSDK
PartialPublicClass AreasAndLengths
Inherits UserControl
Private MyDrawObject As Draw
PublicSubNew()
InitializeComponent()
Dim initialExtent AsNew ESRI.ArcGIS.Client.Geometry.Envelope(ESRI.ArcGIS.Client.Bing.Transform.GeographicToWebMercator(New ESRI.ArcGIS.Client.Geometry.MapPoint(-130, 20)), ESRI.ArcGIS.Client.Bing.Transform.GeographicToWebMercator(New ESRI.ArcGIS.Client.Geometry.MapPoint(-65, 55)))
initialExtent.SpatialReference = New SpatialReference(102100)
MyMap.Extent = initialExtent
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 geometryService AsNew GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer")
AddHandler geometryService.AreasAndLengthsCompleted, AddressOf GeometryService_AreasAndLengthsCompleted
AddHandler geometryService.Failed, AddressOf GeometryService_Failed
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)
' GeometryService.AreasAndLengths returns distances and areas in the units of the spatial reference.' The units in the map view's projection is decimal degrees. ' Use the Project method to convert graphic points to a projection that uses a measured unit (e.g. meters).' If the map units are in measured units, the call to Project is unnecessary. ' Important: Use a projection appropriate for your area of interest.EndSubPrivateSub GeometryService_AreasAndLengthsCompleted(sender AsObject, args As AreasAndLengthsEventArgs)
'TODO: Geometry service not returning correct values, area unit seems invalid, may need to use data in 'planar coordinate space' convert results from meters into miles and sq meters into sq miles for our displayDim 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]))
EndSubEndClassEndNamespace