[R-sig-Geo] lat/lon to timezone

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Thu Sep 25 16:52:42 CEST 2008


2008/9/25 John Callahan <john.callahan at udel.edu>:
> If there's not a function within R for this, maybe there's a way to use the
> web services from Geonames.org and parse the results.  Maybe this could be
> done within R, or in a separate script called by R.  I haven't used R enough
> to know if this is possible.
>
> http://ws.geonames.org/timezone?lat=47.01&lng=10.2&style=full

  Oh well, in that case it is trivial...

getTZ <- function(lat,long){
  url=paste("http://ws.geonames.org/timezone?lat=",lat,"&lng=",long,"&style=full",sep="")
  u=url(url,open="r")
  text=readLines(u)
  tz = text[grep("timezoneId",text)]
  tz = sub("<timezoneId>","",tz)
  tz = sub("</timezoneId>","",tz)
  return(tz)
}

 note how I avoid parsing XML....

Barry




More information about the R-sig-Geo mailing list