The purpose of this article is not to produce the best Topographic images (I’m not an artist); rather, it is to demonstrate how easy it can be to read and manipulate GeoTiff data.
All we need to kick us off is a GeoTiff file and a few packages that can be installed via pip. I am using elevation data that I downloaded from data.gov.au but you can BYO if you like.
The main package is the geotiff package:
pip install geotiff
pip install numpy
And finally, to produce images, let’s use Pillow
pip install Pillow
Reading a GeoTiff
Often, we want to read only a section of our GeoTiff file; either because we are interested in a certain area or our files are very large (sometimes multiple gigabytes). Thankfully there is a read_box method that takes a bounding box and returns a 2D array of all the points inside that box.
Manipulating the Data and making Images
The first thing to do is to view the data using Pillow. We will need to scale the values to a range from 0 to 255 (the range of values for the RGB system). Let’s save this as “greyscale_dem.png”.
That’s cool, but I’d like to see some colour. To do this, I made a function called colorize that changes the RGB values based on a set of rules; but you can make your own rules depending on how you’d like it to look. I then looped over each value in the array to produce the RGB values.
I’d also like to add some shading to the hills. This should be a function of the hill slope in the north-south direction. Lucky for us, numpy has a gradient method to do just that. The gradient method returns a tuple of size two with the gradient arrays in the y-direction and the x-direction.
And finally, we can combine the two by decreasing the RGB values based on the magnitude of the slope values (scales from 0 to 1):
Here’s the full script:
Typically, to achieve this with python, you would have to install GDAL onto your system. GDAL comes with a steep learning curve, for both installation and usage which I discuss in my article; Geospatial Python without GDAL. With the geotiff python package, I aim to reduce the hassle when reading and writing GeoTiff files. If you like this aim, please support me by heading over to the geotiff GitHub repo and giving it a star. The project still has a way to go before it’s stable and has all the features I’d like to implement, so the support would be very appreciated. Thanks!