HOW TO

Display the total length of line features intersecting a polygon in a pop-up using ArcGIS Arcade in Map Viewer

Last Published: September 11, 2024

Summary

In Map Viewer for ArcGIS Online and ArcGIS Enterprise, calculating the length of line features that intersect with polygon features provides real-time, spatially aware information directly within a map. This calculation helps identify the area covered by the lines, which is useful when avoiding extra segments on the map or performing spatial analysis.

In this article, an Arcade expression is used to calculate the total length of lines that intersect with a polygon feature in Map Viewer.

Procedure

  1. Open the web map in Map Viewer.
  2. In the Layers pane, select the layer. In this example, a polygon layer named 'City' is used.
  3. On the Settings (light) toolbar, click Pop-ups. Ensure pop-ups are enabled by toggling Enable pop-ups on.
  4. In the Pop-ups pane, create an Arcade expression to calculate the total length of lines intersecting the polygon feature. Refer to ArcGIS Online: Use expressions or Portal for ArcGIS: Use expressions for instructions.
  5. In the editor window, configure the expression below.
    1. Specify the layer to be added using the FeatureSetByName function. Replace <LayerName> with the name of the line feature class. In this example, 'Roads' is used.
var lineLayer = FeatureSetByName($map, "<LayerName>", ["*"]);
var totalLength = 0;
  1. Loop through each line to add the length of the lines that intersect the current feature to the total length.
for (var line in lineLayer) {
    if (Intersects($feature, line)) {
            totalLength += Length(line);
    }
}
  1. Round the total length of line values to two decimal places.
Round(totalLength, 2)

The code below shows the full working script.

var lineLayer = FeatureSetByName($map, "Roads", ["*"]);
var totalLength = 0;
for (var line in lineLayer) {
    if (Intersects($feature, line)) {
            totalLength += Length(line);
    }
}
Round(totalLength, 2)
  1. Click Done.
The Arcade expression in the Map Viewer editor window.
  1. In the Pop-ups pane, click Fields list and click Select fields.
  2. In the Select fields pane, under Expressions, click the expression created in Step 5 to display in the pop-up.

The Map Viewer pop-up displays the total length of line features intersecting with a polygon feature.

The Map Viewer pop-up displaying total length of lines within the polygon

Article ID: 000033444

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