【问题标题】:Reverse GeoCoding connection issues反向地理编码连接问题
【发布时间】:2018-04-07 08:34:06
【问题描述】:

我正在尝试对大型数据集进行反向地理编码。我正在使用RJSONIO 包并使用 Google 地图 API 来获取数据集中给定纬度的位置。在成功显示位置信息 100 或 150 次后,它正在显示:

Warning message - "In readLines(con) : cannot open: HTTP status was '0 (null)'"

和:

Error : "Error in fromJSON(paste(readLines(con), collapse = "")) :
  error in evaluating the argument 'content' in selecting a method for function 'fromJSON': Error in readLines(con) : cannot open the connection"

 location<-function(latlng){
 latlngStr <-  gsub(' ','%20', paste(latlng, collapse=","))
 library("RJSONIO") #Load Library
 #Open Connection
 connectStr <- paste('http://maps.google.com/maps/api/geocode/json?sensor=false&latlng=',latlngStr, sep="")
 con <- url(connectStr)
 data.json <- fromJSON(paste(readLines(con), collapse=""))
 close(con)
 data.json <- unlist(data.json)
 if(data.json["status"]=="OK")
   address <- data.json["results.formatted_address"]
   print (address)
 } 

可能的原因是什么以及如何解决问题?

我使用的是 R 版本 3.2.1 和 Ubuntu 14.10。

【问题讨论】:

  • 在给定时间段内可以执行的查询数量是否有任何限制?
  • 对于那个问题,我已经设置了一个等待函数,在每次查询后调用并暂停执行 5 秒,仍然存在同样的问题。
  • 你应该使用ggmap::revgeocode(它调用谷歌的api),谷歌会限制你的速率。还有一些包可以使用 HERE 地图地理/revgeo 查找blog.corynissen.com/2014/10/… 和地理编码包有GNfindNearestAddress,所以有很多选项可供选择。而且,您现在拥有nominatim。适当的批量地理编码会以一种或另一种方式花费您(无论是 API、AWS 服务器场或法律费用/罚款/损害的前期费用)。

标签: r reverse-geocoding


【解决方案1】:

您很可能会达到使用限制,但总会有限制: https://developers.google.com/maps/documentation/geocoding/usage-limits

除此之外,您可以(正确地)使用 API 响应的用途也有一些限制,特别是在存储这些响应方面: https://developers.google.com/maps/documentation/geocoding/policies https://developers.google.com/maps/terms#10-license-restrictions (具体见 10.5.d)

【讨论】:

    【解决方案2】:

    我在使用 ggmap 函数访问 google API 时遇到了类似的错误。如果您处于速率限制之下,则服务器可能只是无法响应(就像任何服务器有时都可以做的那样,您更有可能遇到您提取的数据越多)。

    如果您的代码中没有任何错误处理来处理服务器不响应时的处理方式,那么您的脚本将中断并显示错误消息。简单的解决方案是通过重新 ping API 几次来构建一些错误处理,如果它返回错误(例如,如果它不起作用,请再试一次)。

    这是在我的脚本中修复它的原因:

    attempt = 1 #start attempt counter
    
    while(attempt != 20) #repeat the API request for up to 20 times
    {
      #use try to test for whether or not your API function returns error
      dat.test <- try(PASTE YOUR FUNCTION THAT IS CALLING THE API INTO HERE) 
    
      if(is(dat.test, 'try-error')) #check if try returned error when pinging the API
      {
        #do these things if an error is returned
        #if there is an error, after completing these items the while loop will continue to the next attempt to reach the API 
        Sys.sleep(1) #wait 1 second
        warning("reattempting google api fetch...") #warn the user
        attempt <- attempt + 1 #update the attempt counter
      } else break #exit the while loop if no error returned (API returned data successfully))
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-31
      相关资源
      最近更新 更多