Using Arcade to Calculate the Slope of a Line

478
0
03-12-2024 05:53 AM
TimothyHales
Esri Notable Contributor
3 0 478

Calculating the slope of a line across a surface is simple using the Add Surface Information tool in ArcGIS Pro. This tool overlaps the line feature with a surface, such as a LAS or raster dataset, to extract the elevation values and calculate the slope value.

But what do you do when you don't have a surface layer for the calculation?

How to calculate slope

Let’s first break down the slope calculation to understand your specific data needs better. The slope is calculated by dividing the elevation change by the horizontal distance covered. This is sometimes called “rise over run.” The output value of this calculation can be multiplied by 100 to get a percentage value.

TimothyHales_1-1710255803114.png

When a surface layer is unavailable to calculate elevation values, you must use either attribute values or Z-values from the line feature. The elevation change can be calculated by subtracting the starting elevation from the ending elevation, and the distance value can be accessed from the shape length.

Once you have determined your available values, you can use Arcade to calculate the slope value.

Use case for calculating slope using z-values

Imagine you are using a mobile device to perform landscape surveying. One of the data points that you collect is property drainage. This is done by collecting two points to form a drainage line. Usually, the data is collected and then accessed by an editor who calculates the slope of the line manually and inputs it to an attribute field.

After learning a little about ArcGIS Arcade, you find that you can use an attribute rule and Arcade to calculate the slope during data collection. Calculation rules can execute the slope expression immediately after collecting the feature. This is calculated based on the line's start and end vertex z-values. The return value is then written to the slope field in the attribute table.

Here is an example of an expression that uses z-values to calculate slope:

 

 

// If the geometry is empty or has a length of zero, return the original slope value.
if (IsEmpty(Geometry($feature)) || Length(Geometry($feature)) == 0) 
{
 return $feature.SLOPE; 
}

// Get the line path.
var paths = Geometry($feature)['paths'];

// Get the first and last vertex, and select the lowest of the two.
var down_elevation = min(paths[0][0]['z'], paths[-1][-1]['z'])

// Get the first and last vertex, and select the highest of the two.
var up_elevation = max(paths[0][0]['z'], paths[-1][-1]['z'])

// Calculate slope using the three variables.
return ABS((up_elevation - down_elevation)/Length(Geometry($feature))) 

 

 

Note: When using this expression, update the fields to match your data.

Use case for calculating slope using fields

Imagine you have received a pipeline feature class and have been asked to add a new field and calculate the slope for each feature. The line features are not z-enabled and do not have elevation values for each vertex. After reviewing the attribute table, you find two fields containing elevation values for each pipeline's start and end.

You decide to calculate the slope field using an Arcade expression. The expression uses the values from the UPELEV and DOWNELEV attribute fields to calculate the elevation change of the line. The distance of each pipeline is calculated using a geometry function. These three values are the inputs to the slope calculation and the return value is calculated to the SLOPE field.  

Here is an example of an expression that used field values to calculate slope:

 

 

// If either field not populated, return the original slope value.
if (IsEmpty($feature.UPELEV) || IsEmpty($feature.DOWNELEV))
{
 return $feature.SLOPE; 
}

// If the geometry is empty or has a length of zero, return the original slope value.
if (IsEmpty(Geometry($feature)) || Length(Geometry($feature)) == 0) 
{
 return $feature.SLOPE; 
}

// Calculate slope using the two fields and geometry function.
return ABS(($feature.UPELEV - $feature.DOWNELEV)/Length(Geometry($feature))); 

 

 

Note: When using this expression, update the fields to match your data.

ArcGIS Arcade is an expression language used to format and manipulate data in ArcGIS. These use cases for calculating slope in an attribute rule and field calculation are just two Arcade examples. These expressions can be used in other applications to return slope values, such as labeling, pop-ups, and forms. You can reuse and modify these sample expressions to meet the needs of your specific project. 

For further training on the Arcade expression language, check out Esri's Get Started with ArcGIS Arcade: https://go.esri.com/arcade-course 

About the Author
Timothy Hales is a Senior Education Specialist for Esri Training Services developing instructor-led and web courses. Before developing training content for Training Services, he was the Enterprise Community Manager for Esri Community (GeoNet).