Failure to geocode using the ggmap and Google Geocoding API in R; behind company firewall

笑着哭i 提交于 2019-12-13 06:20:41

问题


I am attempting to geocode addresses in R using the geocode() function in the ggmap package. I have done this on my personal computer relatively easily and want to attempt this on my work computer. Generally, I am supposed to register with Google, library the ggmap package, read-in my security key, and then I can use the geocode() function. But I get errors. See below:

# Library package
library(ggmap)

# Set key file
gmAPI     <- "key_file.txt"

# Read-in Google API key
gmAPIKey  <- readLines(gmAPI)[1]

# Register key
register_google(key = gmAPIKey)

# Geocode Waco, TX
geocode("waco, texas", output = "latlona")

Instead of generating geocoded output, I receive:

Source : https://maps.googleapis.com/maps/api/geocode/json?address=waco,+texas&key=xxx.txt
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Failed to connect to maps.googleapis.com port 443: Timed out

or sometimes:

Source : https://maps.googleapis.com/maps/api/geocode/json?address=waco,+texas&key=xxx.txt
Error in curl::curl_fetch_memory(url, handle = handle) : 
  Failed to connect to url port ###: Connection refused

Note: I replaced the actual url/port posted in the error message with url port ### as I imagine this is specific to my computer.

I have a feeling this has to do with my work network. Similar questions have set some configuration using the httr package, but those solutions have not worked for me. It's possible I am entering the wrong information. Any help?


回答1:


I have had a similar issue at my work, which I managed to solve adding one line of code with a function from the httr library indeed.

I did:

library(httr)
set_config(use_proxy(url="http://proxy.mycompanyname.com", port=****))

Just insert the proxy through which a computer in your company's network connects to the internet and the port that needs to be opened. Commonly used web proxy server ports are 3128, 8080, 6588 and 80.

Hope this helps!




回答2:


After trying each of the solutions here, and none of them working, I figured out the problem through trial and error. Ultimately, my proxy and port were wrong. In my example, I followed instructions in the link to find my proxy by IE -> Tools -> Internet Options -> Connections tab -> LAN Settings. However, the proxy was somewhat different from what my computer was using. Thus a fool-proof method was to use the curl package and to use the ie_get_proxy_for_url() function to do so programmatically. When I used the output of the ie_get_proxy_for_url() function, @Lennyy's solution worked (and is thus credited). See code:

library(curl)
library(ggmap)
library(httr)

# Get proxy and port
proxyPort <- ie_get_proxy_for_url()

# Split the string to feed the proxy and port arguments
proxyURL  <- strsplit(proxyPort, ":")[[1]][1]
portUsed  <- as.integer(strsplit(proxyPort, ":")[[1]][2])

# Set configuration
set_config(use_proxy(url=proxyURL, port = portUsed), override = TRUE)

# Geocode Waco, TX
geocode("waco, texas", output = "latlona")

# Output commented below:
# A tibble: 1 x 3
#    lon   lat address      
#  <dbl> <dbl> <chr>        
# 1 -97.1  31.5 waco, tx, usa


来源:https://stackoverflow.com/questions/55603757/failure-to-geocode-using-the-ggmap-and-google-geocoding-api-in-r-behind-company

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