HOW TO

Calculate the longest side of a polygon using Arcade in Portal for ArcGIS and ArcGIS Online

Last Published: August 5, 2024

Summary

ArcGIS Arcade expressions can be used to calculate the longest side of a polygon. This can be useful in various applications, such as dividing buildings into sections, designing office layouts, or constructing car parking facilities.

This article demonstrates how to calculate the longest side of a polygon using ArcGIS Arcade in Portal for ArcGIS and ArcGIS Online.

Procedure

  1. Log in to the ArcGIS Enterprise portal or ArcGIS Online and click Content > My Content.
  2. Click the hosted feature layer containing the polygons to open the item details page.
  3. Add a new field and click the field header to calculate the field to determine the longest length of the polygons using Arcade. In this example, the Longest Length field is selected. Refer to Portal for ArcGIS: Calculate values for a field or ArcGIS Online: Calculate values for a field from the item page for instructions.
Selecting the Longest Length field in the attribute table
  1. In the Expression box, specify the following Arcade expression:
    1. Create a function to calculate the geodetic distance between two points.
function calculateDistanceGeodetic(point1, point2) {
    var distance = DistanceGeodetic(point1, point2, 'meters');
    return distance;
}
  1. Get the geometry of the polygon.
var geometry = Geometry($feature);
  1. Initialize the maximum distance variable.
var maxDistance = 0;
  1. Get the array of rings (each ring is an array of points).
var rings = geometry.rings;
  1. Iterate through each ring in the polygon (to handle multipart polygons).
for (var i = 0; i < Count(rings); i++) {
    var points = rings[i];
  1. Iterate through each point in the ring.
    for (var j = 0; j < Count(points) - 1; j++) {
        var point1 = Point(points[j]);
        var point2 = Point(points[j + 1]);
  1. Calculate the geodetic distance between the two points.
        var distance = calculateDistanceGeodetic(point1, point2);
  1. Update the maximum distance if the current distance is greater.
        if (distance > maxDistance) {
            maxDistance = distance;
        }
    }
  1. Handle the distance between the last point and the first point to close the polygon.
    var lastPoint = Point(points[Count(points) - 1]);
    var firstPoint = Point(points[0]);
    var closingDistance = calculateDistanceGeodetic(lastPoint, firstPoint);

    if (closingDistance > maxDistance) {
        maxDistance = closingDistance;
    }
}
  1. Return the length of the longest side of the polygon.
return maxDistance;

The code block below shows the example of the full working script.

function calculateDistanceGeodetic(point1, point2) {
    var distance = DistanceGeodetic(point1, point2, 'meters');
    return distance;
}

var geometry = Geometry($feature);

var maxDistance = 0;

var rings = geometry.rings;

for (var i = 0; i < Count(rings); i++) {
    var points = rings[i];

    for (var j = 0; j < Count(points) - 1; j++) {
        var point1 = Point(points[j]);
        var point2 = Point(points[j + 1]);

        var distance = calculateDistanceGeodetic(point1, point2);

        if (distance > maxDistance) {
            maxDistance = distance;
        }
    }

    var lastPoint = Point(points[Count(points) - 1]);
    var firstPoint = Point(points[0]);
    var closingDistance = calculateDistanceGeodetic(lastPoint, firstPoint);

    if (closingDistance > maxDistance) {
        maxDistance = closingDistance;
    }
}

return maxDistance;
  1. Click OK.

The image below shows the result of calculating the longest side of a polygon using ArcGIS Arcade in the attribute table.

The attribute table with the longest length of each polygon

Article ID: 000033145

Software:
  • ArcGIS Online
  • Portal for ArcGIS
  • ArcGIS Enterprise 11 1
  • ArcGIS Enterprise 11 3
  • ArcGIS Enterprise 11 2

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options