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.
Asked
Active
Viewed 1,478 times
1
-
Check this also : http://stackoverflow.com/questions/13316185/r-convert-zipcode-or-lat-long-to-county – Sony Mathew Oct 06 '15 at 02:12
1 Answers
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