问题
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.
回答1:
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
.
来源:https://stackoverflow.com/questions/32960548/zip-codes-from-lat-lon-batch-query