Adam Adam - 1 year ago 163
R Question

Extracting elevation from website for lat/lon points in Australia, using R

G'day Everyone,

I am trying to get some elevation data for about 700 points I have. I thought I might use the code provided to the same question (Conversion for latitude/longitude to altitude in R), unfortunately I get errors when using the geonames package, and the website the best answer provides does not have Australian elevation data available (Errors provided below FYI).

I found another website which provides very accurate elevation data for Australia, but I have no idea how I might extract the information from the webpage . I think it is using the google elevation API, but again I have no idea how to access that.

When I put 'lat, lon' coordinates into the 'search for location' box it gives the elevation data below the map. However, I can't seem to find this in the source page. The website is

some example lon lat values that work:

-36.0736, 146.9442

-36.0491, 146.4622

I am wondering if anyone can help me query this site from R, and extract the elevation data? Or does it seem like to much of a hassle? I realise there is a batch function (up to 100 locations) on the website, but it would be cool to be able to do this from R.

Thanks everyone, sorry if this is extremely obvious.



When using geonames:

elevation <- GNgtopo30(adult$lat, adult$lon)
Error in getJson("gtopo30JSON", list(lat = lat, lng = lng)) :
error code 10 from server: Please add a username to each call in order for geonames to be able to identify the calling application and count the credits usage.
In addition: Warning message:
In readLines(u) :
incomplete final line found on ' lat=-36.0736&lng=146.9442'

When using query code:

url <- paste("", adult$lat, adult$lon, sep = '/')
page <- getURL(url)
ans <- xmlTreeParse(page, useInternalNodes = TRUE)
Space required after the Public Identifier
SystemLiteral " or ' expected
SYSTEM or PUBLIC, the URI is missing
Extra content at the end of the document
Error: 1: Space required after the Public Identifier
2: SystemLiteral " or ' expected
3: SYSTEM or PUBLIC, the URI is missing
4: Extra content at the end of the document

Answer Source

There's an Elevation API provided by Google, which returns either a JSON or XML response. Here's an example using a JSON response, parsed by fromJSON in the RJSONIO package.

googEl <- function(locs)  {
  locstring <- paste(, list(locs[, 2], locs[, 1], sep=',')),
  u <- sprintf('',
  res <- fromJSON(u)
  out <- t(sapply(res[[1]], function(x) {
    c(x[['location']]['lat'], x[['location']]['lng'], 
      x['elevation'], x['resolution']) 
  rownames(out) <- rownames(locs)

m <- matrix(c(146.9442, 146.4622, -36.0736, -36.0491), nc=2)


      lat     lng      elevation resolution
[1,] -36.0736 146.9442 163       152.7032  
[2,] -36.0491 146.4622 171.7301  152.7032  

The googEl function expects a matrix or data.frame of coordinates, with longitude in the first column and latitude in the second.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download