This is a follow up of the series of experiments I have been working with rayshader. In this post, I will focus on making a shaded relief map using different colors to represent different altitudes. Also, in this post I will show you how to visualize the shaded relief map with a given projection and add some labels to the final map.
Define some variables: name of the polygon to save the files, the name used to add a label at the end, the CRS to project the visualization of the map, and some variables to render the shaded relief and final labels. In this case, I had to use PROJ notation to define the projection to which I wanted the map to be projected to; although, nowadays this notation is discouraged in favor of WKT2 notation or EPSG or ESRI codes.
Then, read the roi polygon file and use it to obtain the DEM data.
Then, extract the bounding box coordinates of the polygon to add them in the add as a notation in the final image. Do some adjustments such as round to two decimals and add N and W letters.
Then, mask the images using the roi’s polygon and crop the dem to the extent of the same polygon.
Afterward, transform the dem into a matrix.
Define color palette for the topography colors, using hexadecimal codes.
Then create the hillshade map under the topographic color representation and add shadows. I added some transparency to the height shade layer (resulting from height_shade) so it can be better combined with the hillshaded image (resulting from sphere_shade).
Then convert the array obtained in the previous step to spatRast again and project it.
Then export the image into a png. In this case, you need to create a folder named “Plots” outside R in your working directory or use dir.create("Plots") inside R, so you can export the file in the exact same location as in the example. Other alternative, might be to delete the folder part (i.e., “Plots/”) and just export it directly in the working directory.
Once you obtain the png, you can make some enhancements using the magick package to crop the image, increase the saturation of the colors, increase the contrast, among other adjustments.
Finally, using the same package you can make some annotations, add some borders to the image and write the final image into another png.
The result:
Shaded relief map of Mexico.
In the final map, the tallest peaks can be appreciated in white, such as the Pico de Orizaba (Citlaltépetl), Iztaccihuátl, Nevado de Toluca, Popocatépetl, Cofre de Perote, among others. As a final annotation I was planning to add the altitude range of the map, but the resulting range from the DEM is not very precise, so I decided not to include it (DEM highest point was 5139 m, while highest point should be around 5600 m).