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) {
require(RJSONIO)
locstring <- paste(do.call(paste, list(locs[, 2], locs[, 1], sep=',')),
collapse='|')
u <- sprintf('http://maps.googleapis.com/maps/api/elevation/json?locations=%s&sensor=false',
locstring)
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)
return(out)
}
m <- matrix(c(146.9442, 146.4622, -36.0736, -36.0491), nc=2)
googEl(m)
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.