Does anyone know if there is a tool in R to find the height above sea level of a location, given the latitude and longitude ?
-
1You want to find what the altitude of a given location on the Earth's surface is? – ChrisBD Jan 23 '12 at 15:18
5 Answers
Or you can use the package that looks up from geonames, and get the value from the srtm3 digital elevation model:
First get a geonames username by registering at geonames.org
. Then set it:
> options(geonamesUsername="myusernamehere")
then:
> require(geonames)
> GNsrtm3(54.481084,-3.220625)
srtm3 lng lat
1 797 -3.220625 54.48108
or the gtopo30 model:
> GNgtopo30(54.481084,-3.220625)
gtopo30 lng lat
1 520 -3.220625 54.48108
geonames is on CRAN so install.packages("geonames") will get it.
The difference between these two models is because they are only approximations based on satellite data. Don't go expecting to pinpoint mountains from this.

- 92,590
- 12
- 140
- 224
-
1
-
I get the message that I need to pass my username. I found http://www.geonames.org/export/web-services.html#srtm3 but it doesn't say how to do this through the GNsrtm3 function (and it doesn't seem the function can take username as an argument) – David LeBauer Mar 17 '15 at 22:31
-
1@DavidLeBauer: add this command before running the function: options(geonamesUsername="YourUserName") – Metariat Aug 15 '17 at 11:44
-
@Spacedman Is this function obsolete now? It says. `Error in url(url, open = "r") : cannot open the connection to 'http://api.geonames.org/srtm3JSON?lat=NA&lng=NA&` It doesn't work even on dummy data..same error – Rspacer Jul 04 '21 at 01:50
-
@Biotechgeek still works for me, but since 2012 you need a username for the API - edit done! – Spacedman Jul 04 '21 at 16:20
Update: Earthtools no longer exists, so this answer is obsolete. I recommend @Spacedman's answer instead.
As DWin said, there are two parts to this: find a good source of data with a web service, then parse it in R. This answer uses the earthtools.org
service.
library(RCurl)
library(XML)
latitude <- 52.4822
longitude <- -1.8946
url <- paste(
"http://www.earthtools.org/height",
latitude,
longitude,
sep = "/"
)
page <- getURL(url)
ans <- xmlTreeParse(page, useInternalNodes = TRUE)
heightNode <- xpathApply(ans, "//meters")[[1]]
(height <- as.numeric(xmlValue(heightNode)))

- 118,240
- 47
- 247
- 360
-
What you have looked pretty interesting and so I tried it on a lat and long I knew of a town [link](http://irapl.altervista.org/geografia/usa/index.php?recn=14032). I used `latitude <- 41.25` & `longitude <- -77.3` yielding 293 not 597 as expected. Am I using this incorrectly? – Tyler Rinker Jan 23 '12 at 17:15
-
The problem is most likely related to the quality of the data. Heights above sea level are given at 3 arc second (approximately 90m) resolution according to the linked Earth Tools page. In 90m, you can have a lot of hill, so don't expect perfect accuracy. – Richie Cotton Jan 23 '12 at 17:39
-
2Also, 41.25 degrees is not the same as 41deg, 25sec since there are only 60 seconds in a degree. Using the correct location, the service returns 568m. – Richie Cotton Jan 23 '12 at 17:46
-
Thanks. This is not my area of research but the R solution looked pretty interesting. Ignorantly I thought 41 degrees 25' (minutes) was just a decimal. Wrong! Thanks for the information. For others like me (ignorant) the conversion is simple. Multiply the minutes by 60 and that's your decimal. I gathered this from [link](http://geography.about.com/library/howto/htdegrees.htm) Thanks for sharing. – Tyler Rinker Jan 23 '12 at 19:43
You can access elevation data through Google Maps Elevation API. And in R you can use this through my googleway
package
To use Google Maps API you need an API key
library(googleway)
api_key <- "your_api_key"
df_locations <- data.frame(lat = c(54.481084), lon = c(-3.220625))
google_elevation(df_locations = df_locations, key = api_key)
# $results
# elevation location.lat location.lng resolution
# 1 813.9291 54.48108 -3.220625 610.8129
#
# $status
# [1] "OK"

- 25,502
- 4
- 67
- 139
There are R packages such as RCurl that allow web queries. There are also web resources, Further specfics will require .... well, ... more specifics.
http://gisdata.usgs.net/xmlwebservices2/elevation_service.asmx?op=getElevation

- 258,963
- 21
- 364
- 487
You can also use package rgbif which uses geonames internally. I like this option because you can provide a data frame as input, as well as other input formats. Again, you need to provide your GeoNames user name.
library(rgbif)
coords <- data.frame(decimalLatitude = 54.481084,
decimalLongitude = -3.220625)
elevation(coords, username = "myusernamehere")
If you provide a data frame as input, it must contain the coordinates columns named as decimalLatitude and decimalLongitude, which relates to the DarwinCore standards.
You can choose among different options of models using the argument 'elevation_model'.

- 412
- 4
- 15