In this post I will show you how to make an RGB composite with shaded relief using rayshader, elevatr, maptiles,sf, terra and magick packages. First load the libraries we are going to use.
Then, read the roi polygon file and use it to obtain the RGB tiles and DEM data.
Then, mask the images using the roi’s polygon and crop the dem to the extent of the RGB.
Afterward, transform the RGB into an array and the dem into a matrix.
Make a hillshade using the dem (as matrix). Transform it to rast again, set its extent and mask with the roi’s polygon.
Then export the two images in a single png, setting some transparency in the second image so the hillshade can be appreciated under the RGB composite. 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 will see that the colors of the image are somewhat pale. Thus, you can use magick to increase the saturation of the colors, increase the contrast and write the image into another png.
The result:
RGB shaded relief map of Mexico.
If you are familiar with the surrounding of Morelia, Michoacán, Mexico, you will immediatly recognize Patzcuaro and Cuitzeo lakes, as well as some hills, such as the Quinceo.