Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting connection timed out error while GeoCoding in R

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.

like image 268
IMK Avatar asked Feb 11 '19 05:02

IMK


People also ask

What causes HTTP connection timeout?

If the server takes so long to respond, a timeout error displays. This error is meant to prevent devices from waiting ceaselessly for the server to respond. The possible causes may be a server issue, outdated browser and cache, blacklisted sites, sporadic internet connection, faulty extensions, etc.

What is connection time out error?

“Err_Connection_Timed_Out” means that the connection has taken too long when calling a website. When a query is sent to the target server, and it does not respond within 30 seconds, then the browser terminates the communication attempt.

What is connection timeout?

Connection timeout is on the client's side, usually meaning that the client lost connection, or is unable to establish connection to a server for whatever reason (such as remote firewall is dropping the traffic or the server went down).


2 Answers

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

like image 56
IMK Avatar answered Oct 17 '22 01:10

IMK


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

like image 1
Tao Hu Avatar answered Oct 17 '22 03:10

Tao Hu