GEOG 6180 Geoprocessing with Python
Directional Slope - A Python Tool For Path Design
Background & Introduction
Slope calculation is a fundamental operation in geospatial analysis, particularly for planning routes, trails, roads, and other infrastructure in mountainous terrain. Traditional slope calculations often focus on the maximum slope at a point, which is useful for many applications but doesn't account for directionality. For applications like trail design, understanding how steep a path will be when following a specific direction is crucial.
This project implements a directional slope tool (Figure 1) that calculates the slope along a specific path, considering both the terrain and the direction of travel. The result is a much more useful metric for path design than traditional slope calculations, as it accounts for the actual steepness that would be experienced when traversing the path.

Figure 1: Directional Slope Python Code Snippet
Methodology
The directional slope tool takes two inputs: a Digital Elevation Model (DEM) raster and a line shapefile. The output is a new line with directional slope calculated at each segment. The process involves:
- Reading the input DEM and line files
- Resampling the input line at a specified interval (default = 0.5 meters) to create points along the path
- At each point, calculating the slope using Horn's method (Horn, 1981)
- Calculating the bearing between consecutive points
- Using the slope and bearing to determine the directional slope at each segment
- Creating a new line shapefile with directional slope and bearing attributes
Horn's method was chosen for slope calculation as it is considered more accurate than other methods like the simple 4-neighbor approach, especially in variable terrain. This method is also the default in ESRI ArcGIS products.
The actual directional slope calculation uses the following formula (Neteler and Mitášová, 2008):
Where dx and dy are the east-west and north-south slopes from Horn's method, and bearing is the direction of travel. The result is a directional slope value where negative values indicate uphill travel and positive values indicate downhill travel.
Implementation
The tool was implemented in Python using GDAL/OGR for raster and vector processing, as well as NumPy and Shapely for mathematical operations and geometry handling. The key components include:
- get_horn function: Calculates slope at a point using Horn's method
- dir_slope function: Calculates directional slope between two points
- Line resampling: Divides the input line into segments at a specified interval
- Attribute handling: Transfers attributes from the input line to the output
Below is an example of the get_horn function, which calculates the slope at a specific point:
Results & Applications
The directional slope tool produces a new line shapefile with segments that contain both bearing and directional slope attributes. These results can be visualized in GIS software to identify steep sections, evaluate trail difficulty, and plan optimal routes.
When applied to real-world data, the directional slope tool provides valuable insights that traditional slope analysis cannot. For example, in mountainous terrain, a path that follows contour lines will show minimal directional slope despite being on steep hillsides. Conversely, a path that goes directly up or down a slope will show the maximum directional slope.
The negative values in the output indicate uphill travel while positive values indicate downhill travel, making it easy to interpret the results for path planning purposes.
The tool has several practical applications in trail and path design:
- Trail Design: Identifying sections that exceed recommended slope thresholds
- Route Planning: Finding optimally graded paths for hiking, biking, or accessibility
- Infrastructure Development: Planning roads, utility corridors, and other linear features in mountainous terrain
- Erosion Analysis: Predicting areas susceptible to erosion based on slope and direction
- Accessibility Analysis: Evaluating trail difficulty for different user groups
Future Development
The full standalone script can be found here.
Skills Used
- Python Programming
- Geospatial Libraries (GDAL/OGR, Shapely)
- Digital Elevation Model (DEM) Handling
- Terrain Analysis Algorithms
- Geospatial Data Formats