Getting connection timed out error while GeoCoding in R
Asked Answered
G

2

7

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.

Garnet answered 11/2, 2019 at 5:9 Comment(0)
G
4

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

Garnet answered 11/2, 2019 at 15:20 Comment(0)
F
1

I got the same problems as you, and I resolved it with the following code :

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

If it returns a status like 200,problem has been resolved.

Fluvial answered 11/5, 2019 at 6:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.