1

I have a data.frame with 1500 latitude/longitude entries. I need to map these coordinates to their zip codes. Is there a way to do this besides entering them in one at a time to a geonames -type api? I would like a package in R that takes in a lat-lon combo and produces a zip code for every row of my data.frame.

leeor
  • 17,041
  • 6
  • 34
  • 60
gannawag
  • 225
  • 2
  • 12

1 Answers1

2

Using the Open Streetmap API, you could try

library(RCurl)
library(RJSONIO)

latlon2zip <- function(lat, lon) {
    url <- sprintf("http://nominatim.openstreetmap.org/reverse?format=json&lat=%f&lon=%f&zoom=18&addressdetails=1", lat, lon)
    res <- fromJSON(url)
    return(res[["address"]][["postcode"]])
}

latlon2zip(lat=52.5487429714954, lon=-1.81602098644987)

In order to use the latlon2zip function in transform, use Vectorize.

Karsten W.
  • 17,826
  • 11
  • 69
  • 103