HOW TO

Calculate the average geographic angle of polylines using Arcade in Portal for ArcGIS and ArcGIS Online

Last Published: July 16, 2024

Summary

In the attribute table of the ArcGIS Enterprise portal and ArcGIS Online, the average geographic angle of polylines can be calculated using ArcGIS Arcade. Finding the average angle of a line determines the overall trend of a line feature, and can be useful in fields such as construction, engineering and cartography. This article provides the workflow.

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 polylines to open the item details page.
  3. Add a new field and click the field header to calculate the average geographic angle of the lines using Arcade. In this example, the NewField2 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 further instructions.
The NewField2 is selected
  1. In the Expression box, specify the Arcade expressions as described below.
    1. To initialize the variables:
var paths = Geometry($feature).paths;
var totalAngle = 0;
var segmentCount = 0;
    1. To iterate through each path in the polyline:
for (var p in paths) {
    var path = paths[p];
    var count_p = Count(path);
    1. To iterate through each segment in the path:
    for (var q = 1; q < count_p; q++) {
        var angle_p = Angle(path[q], path[q - 1]);
    1. To adjust the angle to a geographic context (0-180 degrees):
        var geographicAngle = (450 - angle_p) % 180;
    1. To verify if the angle is positive and within 0-180 degrees:
        if (geographicAngle < 0) {
            geographicAngle += 180;
        }
    1. To provide the total sum of the angles:
        totalAngle += geographicAngle;
        segmentCount++;
    }
}
    1. To calculate the average angle:
var averageAngle = 0;
if (segmentCount > 0) {
    averageAngle = totalAngle / segmentCount;
}

return averageAngle;

The code block below shows the full working script.

var paths = Geometry($feature).paths;
var totalAngle = 0;
var segmentCount = 0;

for (var p in paths) {
    var path = paths[p];
    var count_p = Count(path);

    for (var q = 1; q < count_p; q++) {
        var angle_p = Angle(path[q], path[q - 1]);

        var geographicAngle = (450 - angle_p) % 180;

        if (geographicAngle < 0) {
            geographicAngle += 180;
        }

        totalAngle += geographicAngle;
        segmentCount++;
    }
}

var averageAngle = 0;
if (segmentCount > 0) {
    averageAngle = totalAngle / segmentCount;
}

return averageAngle;
  1. Click OK.

The image below shows the average geographic angle of polylines calculated in the attribute table.

The average geographic angle of polylines are calculated in the attribute table

Article ID: 000033093

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