Thank you to GEB reader ‘DJ’ for suggesting this idea in the comments of our post on simulating lakes. It turns out that the algorithm for drawing contours given a regular array of altitude data is actually very simple. We used the algorithm known as ‘Marching Squares’ as described on Wikipedia. For this first attempt we are not going to bother with some of the extras, such as smoothing the final result, or dealing with saddle points.
The main difficulty is getting hold of altitude data. Google offers the Google Maps Elevation API, but it has significant restrictions, including usage limits and most important, rules about what you may do with the data, especially this line:
The Google Maps Elevation API may only be used in conjunction with displaying results on a Google map. It is prohibited to use Google Maps Elevation API data without displaying a Google map.”
If we understand that correctly as well as other restrictions regarding not storing the results, then we are not allowed to use the API to create KML files for use in Google Earth.
We had a look around for other elevation API’s but most of them have similar restrictions, i.e., you can only use them with the providers maps. There are a few that might be less restrictive, such as the ones provided by the USGS and Mapzen, but we will need to investigate them further to double check the permissions as well as how to access the data. There may be others out there. If any of our readers knows of a no-restrictions elevation API, please let us know in the comments.
For now, we have decided to run some tests with the Google Maps API and try to comply with the restrictions.
To use it, start by drawing a polygon in Google Earth, outlining the approximate area you are interested in. Save the polygon as a KML file and select it below. Choose an altitude (in metres) at which you want to draw a contour. Click the ‘Draw contour’ button.
It takes about a minute because of restrictions imposed by the Elevation API on the number of queries you are allowed to make in a given time. For this reason we have used a fairly coarse grid of 100 x 100 points.
KML polygon of the area of interest:
The final result should be a black contour shown on the Google Map above. As an example, we tried the lake from our post on simulating lakes:
A contour outlining the proposed Batoka Gorge lake