Zip Codes from Lat/Lon (batch query) [duplicate]

纵然是瞬间 提交于 2021-01-05 13:14:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!