简体   繁体   English

R中的反向地理编码循环

[英]Loop for Reverse Geocoding in R

I am trying to reverse geocode a large data-set (around 100k). 我试图反转地理编码一个大型数据集(大约100k)。 I used the revgeocode function from the package ggmap . 我使用了包ggmaprevgeocode函数。 I got the result for 1 entry 我得到了一个条目的结果

48 Grand View Terrace, San Francisco, 
CA 94114, USA            
48 Grand View Terrace Eureka Valley San Francisco        
San Francisco County                  California United States
postal_code postal_code_suffix

, but I need to automate the process and use it for the entire data-set. ,但我需要自动化该过程并将其用于整个数据集。

I tried 我试过了

r <- lapply(revgeocode(location = (c(z$lon),c(z$lat)),
             output = "more",
            messaging = FALSE, sensor = FALSE, override_limit = FALSE,
            client = "", signature = ""))

and got the errors for unexpected ',' in each step. 并在每一步中得到意外','的错误。

I tried to write the following loop too 我也尝试编写以下循环

r <- for(i in 1:10){
  revgeocode(location = ("z$lon", "z$lat"),output = "more", messaging =      FALSE, sensor = FALSE, override_limit = FALSE,client = "", signature = "")}

and got similar errors 并得到类似的错误

Please provide some material or helpful links that will help me to write the loop for reverse geocoding. 请提供一些材料或有用的链接,帮助我编写反向地理编码循环。 How to verify the authenticity of the data? 如何验证数据的真实性?

Based on this answer , you could create new variables in your data.frame 根据此答案 ,您可以在data.frame中创建新变量

We use mapply() to process your coordinates and return the results in a list res . 我们使用mapply()来处理您的坐标并将结果返回到列表res

res <- mapply(FUN = function(lon, lat) { 
  revgeocode(c(lon, lat), output = "more") 
  }, 
  df$lon, df$lat
  )

Then, we use rbindlist() from data.table to convert the list into a data.frame (with fill = TRUE since not all elements of res have the same lenghts ie some results do not return a street_number and a postal_code ) and cbind() it to the original data 然后,我们使用rbindlist()data.table到列表转换成data.frame (与fill = TRUE ,因为不是所有的要素res具有相同的lenghts,即一些结果不返回street_numberpostal_code )和cbind()它到原始数据

cbind(df, data.table::rbindlist(res, fill = TRUE))

Update 更新

Following up on your comment, if you want to process more than 2500 queries , you could subscribe to Google Maps APIs Premium Plan to unlock higher quotas. 跟进您的评论,如果您要处理超过2500个查询 ,您可以订阅Google Maps API Premium Plan来解锁更高的配额。 Then you can pass on your credentials to revgeocode() using the signature and client parameter. 然后,您可以使用signatureclient参数将凭据传递给revgeocode()

As per mentionned in the documentation : 正如文档中提到的那样:

Upon purchasing your Google Maps APIs Premium Plan license, you will receive a welcome email from Google that contains your client ID and your private cryptographic key. 购买Google Maps API Premium Plan许可后,您将收到Google发来的欢迎电子邮件,其中包含您的客户ID和私人加密密钥。

Your client ID is used to access the special features of Google Maps APIs Premium Plan. 您的客户ID用于访问Google Maps API Premium Plan的特殊功能。 All client IDs begin with a gme- prefix. 所有客户端ID都以gme-前缀开头。 Pass your client ID as the value of the client parameter. 将您的客户端ID作为客户端参数的值传递。 A unique digital signature is generated using your private cryptographic key. 使用您的私人加密密钥生成唯一的数字签名。 Pass this signature as the value of the signature parameter. 将此签名作为签名参数的值传递。

You can see how it works under the hood by examining the revgeocode() source and see how the URL is constructed: 通过检查revgeocode() 并查看URL的构造方式,您可以了解它是如何工作的:

sensor4url <- paste("&sensor=", sensor, sep = "")
client4url <- paste("&client=", client, sep = "")
signature4url <- paste("&signature=", signature, sep = "")
url_string <- paste("http://maps.googleapis.com/maps/api/geocode/json?latlng=", 
        loc4url, sensor4url, sep = "")
    if (userType == "business") {
        url_string <- paste(url_string, client4url, signature4url, 
            sep = "")
    }

Data 数据

df <- structure(list(lat = c(32.31, 32.19, 34.75, 35.09, 35.35, 34.74 ), lon = 
c(119.827, 119.637, 119.381, 119.364, 119.534, 119.421 )), .Names = 
c("lat", "lon"), row.names = c(21L, 32L, 37L, 48L, 50L, 89L), class = "data.frame") 

I have had a similar problem for integrating the API key. 我在集成API密钥方面遇到了类似的问题。 Basically it is a matter of integrating the API key in the URL that R calls. 基本上,这是在R调用的URL中集成API密钥的问题。 If this doesn't help you, you need to change the core code (look it up on Github) to allow for an argument calling a key. 如果对您没有帮助,您需要更改核心代码(在Github上查找)以允许调用密钥的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM