ArcGIS API for Silverlight - Library Reference
Length(Polygon) Method
See Also  Example Send comments on this topic
ESRI.ArcGIS.Client.Geometry Namespace > Geodesic Class > Length Method : Length(Polygon) Method

polygon
The polygon to calculate the length for.
Calculates the Geodesic length of the perimeter of a polygon in meters using WGS84-ellipsoid great circles between each vertex.

Syntax

Visual Basic (Declaration) 
Public Overloads Shared Function Length( _
   ByVal polygon As Polygon _
) As Double
C# 
public static double Length( 
   Polygon polygon
)

Remarks

The term Geodesic means calculating the shortest distance between two points on a sphere. Using Geodesic algorithms to calculate distances provides a highly accurate way to obtain length and area of measurements of geographic features. The Geodesic algorithm uses the concept of a great circle to obtain the shortest route between two points along the Earth’s surface. The length and area measurements obtained via the Geodesic functions are superior to those of the Euclidian functions, as the Euclidian functions are based upon a projection (i.e. a flat 2D representation of a 3D surface) which introduces distortion.

The Geodesic.Length(Polygon) function is Shared/Static which means that you do not use the new keyword to instantiate it as an object. The output return value from the Geodesic.Length(Polygon) Method is length in meters.

The Polyline and Polygon Geometries that are used as parameters in the Geodesic functions require that the SpatialReference be in Geographic Coordinate System based upon latitude and longitude coordinates. The SpatialReference is a combination of an ellipsoid, datum, and a coordinate system used to display geographic data of the three dimensional Earth on a two dimensional surface (such as a piece of paper of computer monitor). A good article for describing how a spatial reference works can be found here. If the SpatialReference of the geometries in the layer are in a Projected Coordinate System then it will be required to converted those geometries into a Geographic Coordinate System via Methods like: Projection.WebMercator.ToGeographic or Tasks.GeometryService.ProjectAsync(IEnumerable<Graphic>,SpatialReference).

Parameters

polygon
The polygon to calculate the length for.

Return Value

Length in meters

Example

How to use:

Click on the Button to calcuate the Geodesic.Length of the perimiter of the state of Alaska. The FeatureLayer that has the Polygon geometries are in the SpatialReference.WKID = 102100 (3857) which is a Projected Coordinate System. This means that in order to calculate the perimeter Length accurately using the Geodesic.Length Shared/static Method, it is required to first convert the Polygon Geometry of Alaska into SpatialReference.WKID = 4326 in the Geographic Coordinate System.

The XAML code in this example is used in conjunction with the code-behind (C# or VB.NET) to demonstrate the functionality.

The following screen shot corresponds to the code example in this page.

Calculating the perimeter Geodesic.Length of the Polgon feature for the state of Alaksa. Before the area calculation can occur, first the feature must be converted from WKID=102100 to WKID=4326.

XAMLCopy Code
<Grid x:Name="LayoutRoot">
            
  <!-- Add a Map control. -->
  <esri:Map Background="White" HorizontalAlignment="Left" Margin="12,107,0,0" Name="Map1" 
          VerticalAlignment="Top" Height="318" Width="483"  WrapAround="True"
            Extent="-22198191,5927190,-11909781,12700926">
      
    <!-- Add a background ArcGISTiledImageServiceLayer. -->
    <esri:ArcGISTiledMapServiceLayer ID="BaseMap" 
                 Url="http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer" />
            
    <!-- 
    Add a FeatureLayer of the United States polygons. Set a Where clause to only return the state of Alaska. The 
    SpatialReference.WKID = 102100 (3857) which is a Projected Coordinate System. This means that in order to calculate 
    the perimeter Length accurately using the Geodesic.Length Shared/static Method, it is required to first convert the
    Polygon Geometry of Alaska into SpatialReference.WKID = 4326 in the Geographic Coordinate System.
    -->
    <esri:FeatureLayer ID="USStates" Where="ST_ABBREV = 'AK'" OutFields="*"
          Url="http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_1990-2000_Population_Change/MapServer/4"/>
    
  </esri:Map>
  
  <!-- Add a button to calculate the Geodesic.Length-->
  <sdk:Label Height="17" HorizontalAlignment="Left" Margin="258,82,0,0" Name="Label_Length" 
             VerticalAlignment="Top" Width="99" Content="Length (meters): "/>
  <TextBox Height="23" HorizontalAlignment="Left" Margin="355,82,0,0" Name="TextBox_Length" 
           VerticalAlignment="Top" Width="140" />
  <Button Content="Get the Geodesic.Length" Height="23" HorizontalAlignment="Left" Margin="12,82,0,0" 
          Name="Button1" VerticalAlignment="Top" Width="222" Click="Button1_Click"/>
  
  <!-- Provide the instructions on how to use the sample code. -->
  <TextBlock Height="67" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="670" 
           TextWrapping="Wrap" Margin="2,9,0,0" 
           Text="Click on the Button to calcuate the Geodesic.Length of the perimiter of the state of Alaska.
           The FeatureLayer that has the Polygon geometries are in the SpatialReference.WKID = 102100 (3857) 
           which is a Projected Coordinate System. This means that in order to calculate the perimeter Length 
           accurately using the Geodesic.Length Shared/static Method, it is required to first convert the 
           Polygon Geometry of Alaska into SpatialReference.WKID = 4326 in the Geographic Coordinate System." />
            
</Grid>
C#Copy Code
private void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
{
  // Get the FeatureLayer that was defined in XAML. All of the features in the FeatureLayer are based upon
  // the SpatialReference.WKID = 102100 (3857) Projected Coordinate System.
  ESRI.ArcGIS.Client.FeatureLayer theFeatureLayer = (ESRI.ArcGIS.Client.FeatureLayer)(Map1.Layers["USStates"]);
  
  // Get the GraphicCollection from the FeatureLayer.
  ESRI.ArcGIS.Client.GraphicCollection theGraphicCollection = theFeatureLayer.Graphics;
  
  // Loop through all of the Graphics. We should only have one, Alaksa, since there was a restrictive
  // Where clause placed on the FeatureLayer defined in XAML.
  foreach (ESRI.ArcGIS.Client.Graphic aGraphic in theGraphicCollection)
  {
    // Get the Geometry of the Graphic. 
    ESRI.ArcGIS.Client.Geometry.Geometry theGeometry = aGraphic.Geometry;
    
    // Define a new WebMercator utility object.
    ESRI.ArcGIS.Client.Projection.WebMercator myWebMercator = new ESRI.ArcGIS.Client.Projection.WebMercator();
    
    // Define a new Geometry object that will have the SpatialReference.WKID = 4326 Geographic Coordinate System. 
    // The new Geometry object is obtained using the WebMercator.ToGeographic utility helper Method.
    ESRI.ArcGIS.Client.Geometry.Geometry WGS84GeographicGeometry = myWebMercator.ToGeographic(theGeometry);
    
    // Only process Polygon type of geometries.
    if (WGS84GeographicGeometry is ESRI.ArcGIS.Client.Geometry.Polygon)
    {
      // Cast the generic Geometry to the specific Polygon type.
      ESRI.ArcGIS.Client.Geometry.Polygon thePolygon = (ESRI.ArcGIS.Client.Geometry.Polygon)WGS84GeographicGeometry;
      
      // Obtain the Geodesic.Length from the Polygon.
      double theLength = ESRI.ArcGIS.Client.Geometry.Geodesic.Length(thePolygon);
     
      // Display the results to the user.
      TextBox_Length.Text = theLength.ToString();
    }
  }
}
VB.NETCopy Code
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
  
  ' Get the FeatureLayer that was defined in XAML. All of the features in the FeatureLayer are based upon
  ' the SpatialReference.WKID = 102100 (3857) Projected Coordinate System.
  Dim theFeatureLayer As ESRI.ArcGIS.Client.FeatureLayer = CType(Map1.Layers("USStates"), ESRI.ArcGIS.Client.FeatureLayer)
  
  ' Get the GraphicCollection from the FeatureLayer.
  Dim theGraphicCollection As ESRI.ArcGIS.Client.GraphicCollection = theFeatureLayer.Graphics
  
  ' Loop through all of the Graphics. We should only have one, Alaksa, since there was a restrictive
  ' Where clause placed on the FeatureLayer defined in XAML.
  For Each aGraphic As ESRI.ArcGIS.Client.Graphic In theGraphicCollection
    
    ' Get the Geometry of the Graphic. 
    Dim theGeometry As ESRI.ArcGIS.Client.Geometry.Geometry = aGraphic.Geometry
    
    ' Define a new WebMercator utility object.
    Dim myWebMercator As New ESRI.ArcGIS.Client.Projection.WebMercator
    
    ' Define a new Geometry object that will have the SpatialReference.WKID = 4326 Geographic Coordinate System. 
    ' The new Geometry object is obtained using the WebMercator.ToGeographic utility helper Method.
    Dim WGS84GeographicGeometry As ESRI.ArcGIS.Client.Geometry.Geometry = myWebMercator.ToGeographic(theGeometry)
    
    ' Only process Polygon type of geometries.
    If TypeOf WGS84GeographicGeometry Is ESRI.ArcGIS.Client.Geometry.Polygon Then
      
      ' Cast the generic Geometry to the specific Polygon type.
      Dim thePolygon As ESRI.ArcGIS.Client.Geometry.Polygon = CType(WGS84GeographicGeometry, ESRI.ArcGIS.Client.Geometry.Polygon)
      
      ' Obtain the Geodesic.Length from the Polygon.
      Dim theLength As Double = ESRI.ArcGIS.Client.Geometry.Geodesic.Length(thePolygon)
      
      ' Display the results to the user.
      TextBox_Length.Text = theLength.ToString
      
    End If
    
  Next
  
End Sub

Requirements

Target Platforms: Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7

See Also

© ESRI, Inc. All Rights Reserved.