AIM: I am trying to retrieve a series of maps using the get_map
function from ggmaps
.
I know the following works when I use latitude and longitude:
houses_maps <- lapply(latlon,
function(x)
get_map(location = x,
zoom = 20,
maptype = "satellite",
source = "google"))
PROBLEM: It doesn't finish the loop when I use addresses instead of latitude and longitude. This is likely due to it not finding one of the addresses, for example with "tomet, 6-10, 25720 Bellver de Cerdanya, Lleida, Spain"
I get this error:
Error in data.frame(ll.lat = ll[1], ll.lon = ll[2], ur.lat = ur[1], ur.lon = ur[2]) :
arguments imply differing number of rows: 0, 1
In addition: Warning message:
geocode failed with status ZERO_RESULTS, location = "tomet, 6-10, 25720 Bellver de Cerdanya, Lleida, Spain"
Called from: data.frame(ll.lat = ll[1], ll.lon = ll[2], ur.lat = ur[1], ur.lon = ur[2])
QUESTION: How can I get it to ignore the addresses it couldn't find and leave them NA and continue searching for the rest instead of stoping. I have 2,000 addresses and it's likely it wont find a few.
Since I neither have example data (please always provide data in your question) and
nor know many details of the get_map
function I am demonstrating
just the basic idea here:
# simplified example data
latlon = c("address 1", "address 2", "address 3")
# mock the function
get_map <- function(location, ...) {
if (location == "address 2") stop(paste("geocode failed with status ZERO_RESULTS, location =", location))
return(location)
}
houses_maps <- lapply(latlon,
function(x)
tryCatch(get_map(location = x,
zoom = 20,
maptype = "satellite",
source = "google"),
error = function(e) {
print(e)
return(NA)
}))
# <simpleError in get_map(location = x, zoom = 20, maptype = "satellite",
# source = "google"): geocode failed with status ZERO_RESULTS,
# location = address 2>
houses_maps
# [[1]]
# [1] "address 1"
#
# [[2]]
# [1] NA
#
# [[3]]
# [1] "address 3"