[英]RDSTK: Reverse geocode lat/lon to city (using coordinates2politics)
I'm working on a Shiny app for R and I'm trying to use the RDSTK package to reverse geocode a list of lat/lon pairs and get the CITY from the json results and save it to a list. 我正在研究R的Shiny应用程序,我正在尝试使用RDSTK软件包来反转地理编码lat / lon对的列表,并从json结果中获取CITY并将其保存到列表中。 The workflow is:
工作流程是:
I'm having a lot of trouble understanding how to take the JSON output, convert it to data frame, then cbind it back to the original data frame. 我在理解如何获取JSON输出,将其转换为数据帧,然后将其重新绑定到原始数据框时遇到了很多麻烦。 Any help would be much appreciated!
任何帮助将非常感激! See below code for reference:
见下面的代码供参考:
Data frame: 数据框:
df <- data.frame(lat=c(34.048381, 37.757836, 40.729855, 42.356391),
lon=c(-118.266164, -122.441033, -73.987921, -71.062307))
I was able to extract the city from the returned JSON list, but I can't for the life of me, figure out how to do it multiple times for a larger list of lat/lon pairs. 我能够从返回的JSON列表中提取城市,但我不能为我的生活,找出如何多次为更大的纬度/经度对列表做这件事。 Searching through stackoverflow mainly results in dstk outside of R.
搜索stackoverflow主要导致在R之外的dstk。
My ideal output would be: 我理想的输出是:
lat lon city
34.048381 -118.266164 Los Angeles
37.757836 -122.441033 San Francisco
40.729855 -73.987921 New York
42.356391 -71.062307 Boston
I've also tried this example: R: How to GeoCode a simple address using Data Science Toolbox though I can't seem to re-engineer it for coordinates2politics. 我也试过这个例子: R:如何使用Data Science Toolbox GeoCode一个简单的地址,虽然我似乎无法为coordinate2politics重新设计它。
Any input? 有什么输入?
FWIW, here's one simple alternative using the Google API: FWIW,这是使用Google API的一个简单替代方案:
library(ggmap)
res <- lapply(with(df, paste(lat, lon, sep = ",")), geocode, output = "more")
transform(df, city = sapply(res, "[[", "locality"))
# lat lon city
# 1 34.04838 -118.26616 los angeles
# 2 37.75784 -122.44103 san francisco
# 3 40.72986 -73.98792 new york
# 4 42.35639 -71.06231 boston
Sound cool. 听起来很酷。 I've had some trouble with RDSTK lately... I'm assuming the stock server is no longer working for you, as the author's blog describes .
我最近在RDSTK遇到了一些麻烦...我假设股票服务器不再适合你,正如作者的博客描述的那样 。 Too bad.
太糟糕了。
Here are two workarounds. 这是两个解决方法。 You might be able to take the original lat/lon pairs, using the city places file from tigerfile, and use %over% in the sp package and then pull the name from the returned shape.
您可以使用tigerfile中的city places文件获取原始lat / lon对,并在sp包中使用%over%,然后从返回的形状中提取名称。 That should be faster than repeated calls to an API.
这应该比重复调用API更快。
However, I've got the same need for an open geocoder in R, and there are a few options. 但是,我对R中的开放式地理编码器有同样的需求,并且有一些选择。 Check out ggmap, referenced in LukeA's answer - can use DSTK (now defunct) and is a simple interface to the google API for just a few calls.
查看在LukeA的答案中引用的ggmap - 可以使用DSTK(现已不存在),并且只是几个调用的google API的简单界面。 Also see this fantstic post describing how to use the census bureau's geocoder API .
另请参阅这篇精彩的帖子,描述如何使用人口普查局的地理编码器API 。 Write a little wrapper function to handle the JSON and you're good to go.
写一个小的包装函数来处理JSON,你很高兴。 That code worked for me as of 1/1/2016.
截至2016年1月1日,该代码对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.