In QGIS, I have a Digital Terrain Model. Using Slope (Menu Raster > Analysis > Slope..., in fact using GDAL Slope algorithm), I get an output raster, either in percents or degrees, the same principle as described here for ArcGIS. So far, so clear.

The question I have is (I guess) not so software specific, but more a general, conceptual question: how is the slope of a cell calculated, based on a raster DEM?

I can't make any sense of the values of my slope raster. I tried to manually calculate the slope value and I'm not sure how it is calculated: I suppose that eight neighboring cells are used for calculation. So the slope is calculated separately for each of these 8 neighbours? And then some kind of mean value is calculated?

Based on the data I got, I was not able to come even close to the slope raster's values by calculating this value manually. See screenshot for example data with a point grid, labeled with the elevation and slope values (0.5*0.5 m, corresponding to resolution and CRS of elevation and slope rasters). So how can you calcaluate the slope of 41.1% the cell indicated in yellow from the 8 neighbours (point 2)? Horizontal distance is 0.5, slope is in percent:

  1. Elevation current cell: 504.58
  2. Elevation 8 neighbours, red values (clockwise, from upper left): 504.75, 504.64, 504.51, 504.35, 504.15, 504.33, 504.3, 504.56
  3. Slope current cell, yellow value: 41.1

enter image description here


Based on the graphic linked by @FelixIP (see below), I manually calculated the angles (slope) of each cell with this forumula (pseudocode): degrees (arctan ((elevation(a)-elevation(e))/distance)) = slope-angle, where distance is 0.5 for cells b,f,h, and d (straight line from centroid to centroid) and 0.707=sqrt(0.5^2+0.5^2) for a,c,i,g (diagonal distance from centroid to centroid).

The resulting angles in degree looks like. So the question, more specifically, is: how do these values relate to the slope in degrees calculated for cell e as 41.1 degrees? None of these values seems to be close to the result:

Manually calculated slope angle from each cell to cell e:

a   13.51828463
b   6.842773413
c   -5.653572474
f   -24.70243023
i   -31.30427926
h   -26.56505118
g   -21.60256512
d   -2.290610043

enter image description here


1 Answer 1


Using ArcGIS documentation and cell naming (see equations at the bottom of help page):

enter image description here

and your data. This script:

a,b,c,f,i,h,g,d,e = 4.75,4.64,4.51,4.35,4.15,4.33,4.3,4.56,4.58

import math
dzBydx = ((c + 2*f + i)*4/4 - (a + 2*d + g)*4/4) / (8 * 0.5)
dzBydy = ((g + 2*h + i)*4/4 - (a + 2*b + c)*4/4) / (8 * 0.5)
rise_run = math.hypot(dzBydx, dzBydy)
print 'Planar method {:6.1f}'.format(rise_run*100)

import numpy as np
X = [-0.5,0,0.5,0.5,0.5,0,-0.5,-0.5,0.0]
Y = [0.5,0.5,0.5,0.0,-0.5,-0.5,-0.5,0.0,0.0]
mX = zip(X,Y)
dzBydx,dzBydy = np.linalg.lstsq(mX, [a,b,c,f,i,h,g,d,e])[0]
rise_run = math.hypot(dzBydx, dzBydy)
print 'Geodesic method {:6.1f}'.format(rise_run*100)


enter image description here

UPDATE: GIS does NOT compute 8 slopes. It approximates terrain around cell in the middle by plane using least square technique:

enter image description here

So it replaces your actual 'terrain':

enter image description here

by plane:

enter image description here

enter image description here

Plane equation:

Z = AX + BY + C

in you case:

enter image description here

due to:

enter image description here

Your slope equals 42.4 percent, not degrees.

  • Thanks a lot for this script. Not so familiar with Python, I can only partially follow the steps. Like dzBydx=12.8175, dzBydy=-0.3575, rise_run=12.82248465. Not sure how to proceed further. But in fact I am not so much interested in the formula, but more to understand conceptually what kind of value it calculates. If you have two points, it's easy to understand what "slope" represent. A slope from 1 point to 8 neighbours is more difficult to understand.
    – Babel
    Commented Apr 30, 2023 at 9:35
  • Equations taken from ArcGis help for planar. Ideas of both methods to approximate z variation by surface, that is plane in this case. 1st method ignores cell in the middle. Second method is much easier to understand.
    – FelixIP
    Commented Apr 30, 2023 at 9:55
  • I still don't understand how you come from anlges for each of the neighboring cells to the final slope value, see updated question.
    – Babel
    Commented Apr 30, 2023 at 10:39
  • If found this link quiet clear. Hope it helps gsp.humboldt.edu/olm/Lessons/GIS/09%20TerrainAnalsis/…
    – katagena
    Commented Apr 30, 2023 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.