Getting connection timed out error while GeoCoding in R

限于喜欢 提交于 2020-04-11 05:00:50

问题


I have to geocode few addresses in R but getting a "Timeout was reached: Connection timed out after 10000 milliseconds" error. I am behind office firewall so tried to use proxy as well but still getting the same error.

This works when I use source as "dsk" but it doesn't geocode most of the addresses hence want to use "google" as source.

Below is the piece of code that I used.

library(ggmap)
library(curl)

register_google(key = "Have_Entered_My_API_Key_Here")

#Used below code to use proxy...(saw it as a solution in stackoverflow only for working behind firewall..maybe I'm not doing it the correct way?)
library(httr)
set_config(use_proxy(url="10.3.100.207",port=8080))

origAddress <- read.csv("Data_for_Geocoding.csv",header = TRUE,sep = ",",stringsAsFactors = FALSE)

for(i in 1:nrow(origAddress))
{

  result <- geocode(origAddress$Add_to_GeoCode[i], output = "latlona", source = "google",sensor = TRUE)
  origAddress$LONGITUDE[i] <- as.numeric(result[1])
  origAddress$LATITUDE[i] <- as.numeric(result[2])
  # origAddress$ <- as.character(result[3])
}

I get the below error when I run this code.
"Error in curl::curl_fetch_memory(url, handle = handle) : Timeout was reached: Connection timed out after 10000 milliseconds"

I have thousands of addresses that I need to geocode so will really appreciate if someone can help here.


回答1:


After spending almost entire day on this I'm happy that I was able to unravel the issue :) hence posting the answer.

If you are getting a connection timed out error as I've listed above, the first thing that you should check is if you are behind a firewall (if you are working on it in office, most probably firewall is blocking you from accessing google api. At home you can simply turn off the firewall). Apparently when you are behind a firewall the below piece of code is what you need to geocode or even access google apis.

library(httr)
set_config(
use_proxy(url="Proxy_Add_Here", port=8080, username="username_here",password="password_here")
)

Make sure to add this code before your geocoding code.

Note: Please note that the Google Maps API is not a free service. There is a free allowance of 40,000 calls to the geocoding API per month (although maximum requests per day are capped at 2500), and beyond that calls are $0.005 each.

PS: Follow below steps, if you're not sure about your proxy add....
Open Internet Explorer-->Tools-->Internet Options-->Connections-->LAN Settings
Username and Password are your windows credentials only




回答2:


I got the same problems with you,and I reslovedit by following code :

library(httr)
set_config(
  use_proxy(url="127.0.0.1", port=1080)
)
httr::GET("www.google.com")  # if it returns status like 200 ,problem has been resloved

if it returns status like 200 ,problem has been resloved



来源:https://stackoverflow.com/questions/54624278/getting-connection-timed-out-error-while-geocoding-in-r

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