将revgeocode应用于经纬度坐标列表 [英] Applying revgeocode to a list of longitude-latitude coordinates

查看:157
本文介绍了将revgeocode应用于经纬度坐标列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用ggmap库中的revgeodcode函数来获取Longitude Latitude坐标(长)列表的Zip代码。



我的问题与解答;数据与此处相同:使用revgeocode功能在FOR循环中。帮助需要,但接受的答案不适用于我。



我的数据(.csv):

  ID,经度,纬度
311175,41.298437,-72.929179
292058,41.936943,-87.669838
12979,37.580956,-77.471439

我遵循相同的步骤:

  data<  -  read.csv(file.choose())
dset< - as.data.frame(data [,2:3])
location = dset
locaddr < - lapply(seq(nrow(location)),function(i){
revgeocode(location [i,],
output = c(address),
消息= FALSE,
sensor = FALSE,
override_limit = FALSE)
})

...并得到错误信息:错误:is.numeric(位置)&&长度(位置)== 2不为真
具体来说,is.numeric(location )是FALSE,这看起来很奇怪,因为我可以乘以2和ge t希望的答案。



任何帮助将不胜感激。

这里有很多错误。



首先,你的经纬度相反。

其次, revgeocode(...)在指定的数据集中的所有位置都在南极洲。预计包含经度和纬度的数字矢量长度为2。您正在传递一个 data.frame 对象(这是错误的原因),并且按照(1)它的顺序是错误的。



第三, revgeocode(...)使用google maps api,这限制了您每天查询2500次。所以如果你真的有一个大型的数据集,那就太好了。



这段代码适用于你的样本:

  data<  -  read.csv(text =ID,Longitude,Latitude 
311175,41.298437,-72.929179
292058,41.936943,-87.669838 $ b $ (1:nrow(data),$ b(











$ $ b函数(i)revgeocode(as.numeric(data [i,3:2]))))
data < - cbind(data,result)
data
#ID Longitude纬度结果
#1 311175 41.29844 -72.92918 16 Church Street South,New Haven,CT 06519,USA
#2 292058 41.93694 -87.66984 1632 West Nelson Street,Chicago,IL 60657,USA
# 3 12979 37.58096 -77.47144 2077-2199美国弗吉尼亚州里士满市Seddon Way

  library(stringr) 
data $ zipcode< - substr(str_extract(data $ result,[0-9] {5},。+),2,6)
data [, - 4]
#ID经度纬度邮编
#1 311175 41.29844 -72.92918 06519
#2 292058 41.93694 -87.66984 60657
#3 12979 37.58096 -77.47144 23230


I'm trying to get the Zip codes of a (long) list of Longitude Latitude coordinates by using the revgeodcode function in the ggmap library.

My question & data are the same as here: Using revgeocode function in a FOR loop. Help required but the accepted answer does not work for me.

My data (.csv):

ID,      Longitude,      Latitude
311175,  41.298437,      -72.929179
292058,  41.936943,      -87.669838
12979,   37.580956,      -77.471439

I follow the same steps:

data <- read.csv(file.choose())
dset <- as.data.frame(data[,2:3])
location = dset
locaddr <- lapply(seq(nrow(location)), function(i){
               revgeocode(location[i,],
               output = c("address"),
               messaging = FALSE,
               sensor = FALSE,
               override_limit = FALSE)
               })

... and get the error message: "Error: is.numeric(location) && length(location) == 2 is not TRUE" Specifically, is.numeric(location) is FALSE, which seems strange because I can multiply by 2 and get the expected answer.

Any help would be appreciated.

解决方案

There are lots of things wrong here.

First, you have latitude and longitude reversed. All the locations in your dataset, as specified, are in Antarctica.

Second, revgeocode(...) expects a numeric vector of length 2 containing the longitude and latitude in that order. You are passing a data.frame object (this is the reason for the error), and as per (1) it's in the wrong order.

Third, revgeocode(...) uses the google maps api, which limits you to 2500 queries a day. So if you really do have a large dataset, good luck with that.

This code works with your sample:

data <- read.csv(text="ID,      Longitude,      Latitude
311175,  41.298437,      -72.929179
292058,  41.936943,      -87.669838
12979,   37.580956,      -77.471439")

library(ggmap)
result <- do.call(rbind,
                  lapply(1:nrow(data),
                         function(i)revgeocode(as.numeric(data[i,3:2]))))
data <- cbind(data,result)
data
#       ID Longitude  Latitude                                           result
# 1 311175  41.29844 -72.92918 16 Church Street South, New Haven, CT 06519, USA
# 2 292058  41.93694 -87.66984  1632 West Nelson Street, Chicago, IL 60657, USA
# 3  12979  37.58096 -77.47144    2077-2199 Seddon Way, Richmond, VA 23230, USA

This extracts the zipcodes:

library(stringr)
data$zipcode <- substr(str_extract(data$result," [0-9]{5}, .+"),2,6)
data[,-4]
#       ID Longitude  Latitude zipcode
# 1 311175  41.29844 -72.92918   06519
# 2 292058  41.93694 -87.66984   60657
# 3  12979  37.58096 -77.47144   23230

这篇关于将revgeocode应用于经纬度坐标列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆