Shaded relief
The shaded relief technique is a method for representing the topography which is prettier and intuitive.
It’s possible to get a simple shaded relief representation using only DEM data. I based all the calculations on this example made with python.
You can find the whole code here.
- The way to color the different height values is the same as in the drawing raster data section, but clipping the image:
- Note that the canvasShaded is pasted twice. The first time without clipping and adding a white rectangle to make the background smoother. The second one, with the clipping, to show a stronger effect on the colored zone
Calculating the shaded relief
To calculate the shaded relief, a hidden canvas is created with this data:
- Changing these parameters, the relief will change its aspect
- azimuth is the angle where the sun is, calculated from the north. Note that in the northern hemisphere, this angle is impossible, but it’s more natural.
- angleAltitude is the position of the sun from the horizon. Increasing the angle will make the image to have less contrast
- Here is where the data is really calculated
- The loop iterates all the pixels, as always when reading rasters, but the stored value is calculated
- The slope and aspect need the gradients. Those are calculated with the central difference method, since is the one that gave me the same results as the Numpy gradient function
- Since the borders don’t have both side pixels, a check has to be done and apply the correct case
- The central difference, in this case, is a simple average of the left and right differences
- The gradient is calculated in the x and y directions
- The slope is simply the arctan of the module of the gradient in both directions
- The aspect is the direction of the slope. It’s important to note which component and sign goes first.
- Once the slope and aspect are calculated, the shaded relief data is directly calculated
- Once the shaded relief data is known, the image of the shaded relief is calculated taking the closest pixel for each image pixel
- Note that the alpha value is the inverse of the calculated hillshade value, as explained here. Using this, the shades will be much sharper, and the flat zones won’t add noise to the colored image