> Updated on February 2020 to reflect changes to the
elevation function and how it retrieves data from the web. The Google Elevation API was replaced by the GeoNames service.
Update 12/10/2015: I found and fixed an error in the formula for converting latlong coordinates to decimal degrees. My bad.
Update 14/9/2015: Scott Chamberlain of rOpenSci has added checks to the elevation function to warn when the input coordinates have impossible values, incomplete cases, and values at 0,0.
As part of a project on bat macroecology, I was given a spreadsheet of point occurrences for Stenodermatines. All the records had georeferenced location data in Degrees/Minutes/Seconds, but some did not include original elevation data. I wanted to fetch the elevation for the points with missing data – a simple enough task. At first, I considered doing it the way I remembered from my undergraduate projects, by plugging the coordinates for individual localities into third-party websites that locate them in an embedded Google Map and show the elevation (for example: mygeoposition.com).
Then I remembered that it’s not 2004 anymore. A quick search led me to the
rgbif package by the helpful folks from the rOpenSci project.
rgbif includes the
elevation function, which queries the relevant web resources (DEMs) to get the elevations for a data frame or list of points.
In this post, I go through some reproducible example code for getting elevation data using
Example code and data
When I received the point data, I was warned that records with no altitude used “9999” as the NA value. This was pretty obvious to spot and easy to put into the
na.strings argument when importing the data.
After loading the data, I used
dplyr to rename some columns, convert the coordinates into decimal degrees, and discard records that already have elevation data. Then I tried out the
elevation() function. Because I already had columns named ‘decimalLatitude’ and ‘decimalLongitude’, the only arguments needed were the name of the data frame and my GeoNames username (which is easy to obtain, see the help file for
The distinct localities and their elevations (with the default setting for the Elevation Model) look like this:
For now, I hope this post helps others to fetch elevations programatically. Let me know if there are any errors.