Area

Download Samples Repository

Description

Demonstrates how to calculate the area and perimeter of a polygon using the GeometryEngine.

"Desktop" "Store" "Phone" Available for Desktop, Store, Phone

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.AreaSample"
             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/runtime/2013">
    <Grid x:Name="LayoutRoot">
        <Grid.Resources>
			<esri:SimpleFillSymbol x:Key="DefaultFillSymbol" Color="#7F0000FF">
				<esri:SimpleFillSymbol.Outline>
					<esri:SimpleLineSymbol Color="Blue" Width="2"/>
				</esri:SimpleFillSymbol.Outline>
			</esri:SimpleFillSymbol>

			<Style x:Key="TextBasicStyle" TargetType="TextBlock">
                <Setter Property="FontSize" Value="14"/>
            </Style>
            <Style x:Key="TextHeadingStyle" TargetType="TextBlock" BasedOn="{StaticResource TextBasicStyle}">
                <Setter Property="Margin" Value="0,10,0,0"/>
            </Style>
            <Style x:Key="TextValueStyle" TargetType="TextBlock" BasedOn="{StaticResource TextBasicStyle}">
                <Setter Property="Margin" Value="10,0,0,0"/>
            </Style>
        </Grid.Resources>

        <esri:MapView x:Name="MyMapView">
			<esri:Map InitialViewpoint="-130,20,-65,55,4326">
				<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
			</esri:Map>
			<esri:MapView.GraphicsOverlays>
				<esri:GraphicsOverlay ID="AreaOverlay" />
			</esri:MapView.GraphicsOverlays>
		</esri:MapView>

		<Border Background="White" BorderBrush="Black" BorderThickness="1"
				 HorizontalAlignment="Right" VerticalAlignment="Top"
				 Margin="30" Padding="20">
			<Border.Effect>
				<DropShadowEffect />
			</Border.Effect>
			<Grid>
				<Grid.RowDefinitions>
					<RowDefinition Height="auto"/>
					<RowDefinition Height="auto"/>
				</Grid.RowDefinitions>

				<StackPanel x:Name="Instructions">
					<TextBlock x:Name="ResponseTextBlock" Style="{StaticResource TextBasicStyle}"
                                   Text="Create a polygon by clicking on the map. Double-click to end. The area and perimeter are displayed here." 
                                   Width="250" TextWrapping="Wrap"/>
					<Button x:Name="CancelCurrent" Content="Cancel" HorizontalAlignment="Right" Click="CancelCurrent_Click" Margin="0,6,0,0"/>
				</StackPanel>

				<StackPanel Grid.Row="1" x:Name="Results" Visibility="Collapsed">
					<TextBlock Text="Results" FontSize="20"/>
					<StackPanel>
						<TextBlock Text="Planar" Style="{StaticResource TextBasicStyle}"/>
						<StackPanel Orientation="Horizontal">
							<TextBlock Text="Area :" Style="{StaticResource TextValueStyle}"/>
							<TextBlock x:Name="ResultsAreaPlanar" Style="{StaticResource TextValueStyle}" />
						</StackPanel>
						<StackPanel Orientation="Horizontal">
							<TextBlock Text="Perimeter :" Style="{StaticResource TextValueStyle}"/>
							<TextBlock x:Name="ResultsPerimeterPlanar" Style="{StaticResource TextValueStyle}" />
						</StackPanel>
						<TextBlock Text="Geodesic" FontSize="14" Margin="0,10,0,0"/>
						<StackPanel Orientation="Horizontal">
							<TextBlock Text="Area :" Style="{StaticResource TextValueStyle}"/>
							<TextBlock x:Name="ResultsAreaGeodesic" Style="{StaticResource TextValueStyle}" />
						</StackPanel>
						<StackPanel Orientation="Horizontal">
							<TextBlock Text="Perimeter :" Style="{StaticResource TextValueStyle}"/>
							<TextBlock x:Name="ResultsPerimeterGeodesic" Style="{StaticResource TextValueStyle}" />
						</StackPanel>
					</StackPanel>
					<Button Content="Restart" x:Name="RestartButton" Click="RestartButton_Click" Margin="8,8,8,0"/>
				</StackPanel>
			</Grid>
		</Border>
	</Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Layers;
using Esri.ArcGISRuntime.Symbology;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// Demonstrates how to calculate the area and perimeter of a polygon using the GeometryEngine.
    /// </summary>
    /// <title>Area</title>
	/// <category>Geometry</category>
	public partial class AreaSample : UserControl
    {
        private const double toMilesConversion = 0.0006213700922;
        private const double toSqMilesConversion = 0.0000003861003;

		private GraphicsOverlay _graphicsOverlay;

        /// <summary>Construct Area sample control</summary>
        public AreaSample()
        {
            InitializeComponent();
			MyMapView.SpatialReferenceChanged += MyMapView_SpatialReferenceChanged;
			_graphicsOverlay = MyMapView.GraphicsOverlays["AreaOverlay"];
        }

		private async void MyMapView_SpatialReferenceChanged(object sender, EventArgs e)
		{
			MyMapView.SpatialReferenceChanged -= MyMapView_SpatialReferenceChanged;
			await DoCalculateAreaAndLengthAsync();
		}

        private async Task DoCalculateAreaAndLengthAsync()
        {
            try
            {
                // Wait for user to draw
                var geom = await MyMapView.Editor.RequestShapeAsync(DrawShape.Polygon);

                // show geometry on map
				_graphicsOverlay.Graphics.Clear();

                var graphic = new Graphic 
				{ 
					Geometry = geom, 
					Symbol = LayoutRoot.Resources["DefaultFillSymbol"] as Symbol 
				};
				_graphicsOverlay.Graphics.Add(graphic);

                // Calculate results
                var areaPlanar = GeometryEngine.Area(geom);
                ResultsAreaPlanar.Text = string.Format("{0} sq. miles", (areaPlanar * toSqMilesConversion).ToString("n3"));

                var perimPlanar = GeometryEngine.Length(geom);
                ResultsPerimeterPlanar.Text = string.Format("{0} miles", (perimPlanar * toMilesConversion).ToString("n3"));

                var areaGeodesic = GeometryEngine.GeodesicArea(geom);
                ResultsAreaGeodesic.Text = string.Format("{0} sq. miles", (areaGeodesic * toSqMilesConversion).ToString("n3"));

                var perimGeodesic = GeometryEngine.GeodesicLength(geom);
                ResultsPerimeterGeodesic.Text = string.Format("{0} miles", (perimGeodesic * toMilesConversion).ToString("n3"));

                Instructions.Visibility = Visibility.Collapsed;
                Results.Visibility = Visibility.Visible;
            }
            catch (TaskCanceledException)
            {
                MessageBox.Show("Current sketch has been canceled.", "Task Canceled!");
            }
        }

        private async void CancelCurrent_Click(object sender, RoutedEventArgs e)
        {
            MyMapView.Editor.Cancel.Execute(null);
            ResetUI();
			await DoCalculateAreaAndLengthAsync();
        }

        private async void RestartButton_Click(object sender, RoutedEventArgs e)
        {
            ResetUI();
			await DoCalculateAreaAndLengthAsync();
        }

        private void ResetUI()
        {
			_graphicsOverlay.Graphics.Clear();
            Instructions.Visibility = Visibility.Visible;
            Results.Visibility = Visibility.Collapsed;
        }
    }
}
Feedback on this topic?