[R-sig-Geo] Geonames elevation help

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Tue Nov 25 17:40:11 CET 2008


2008/11/25 Michael Denslow <mwdenslow at yahoo.com>:

> I am hoping that someone can help me with the GNsrtm3 function in the Geonames package. I am trying to get elevation values for more than one lat,long stored in a data frame. I am not sure if this function can process more than one request since it is a web query. But since R seems to be able to do anything, I figured I was missing something.
>
> I am an R novice so any help would be most appreciated. Here is a shortened example of what I am trying to do.
>
> library(geonames)
>
> long <- c(-81.66,-82.66)
> lat <- c(36.21,37.21)
> df <- data.frame(lat,long)
> GNsrtm3(df$lat,df$long)
>
> # the result below is only for the first set of coordinates
>  srtm3    lng   lat
> 1   990 -81.66 36.21
>
> I also tried using apply, but it also only seemed to work for the first set of coordinates.
>
> Thanks in advance,
> Michael
>
>

 Maybe you were using apply wrong.

 With your df, you can do:

 > df$srtm3=apply(df,1,function(l){GNsrtm3(l[1],l[2])$srtm3})
 > df
     lat   long srtm3
 1 36.21 -81.66   990
 2 37.21 -82.66   500

 apply() passes the row of the dataframe as a vector to the function,
so I just get the first and last element and call GNsrtm3, and strip
off just the result.

 Oh, make sure I've got the lat-long the right way round.

Barry




More information about the R-sig-Geo mailing list