i've been working with googleway packages to retreive information using place_type key, but i've been stuck with the 60 results limit restriction.
I'm trying a slightly different approach to deal qith this restriction: divide an conquer. In my work, all the spatial analysis is performed using QGIS and one idea pops up. create a buffer area around certain coordinates; lets say a radius of 1km. Then using that buffered area, i apply a hex-bin tessellation to get a set of centroid coordinates that can be used to complete the whole area (1km radius) using small chunks of 60 results (let's say 100mts radius query using googleway packages). Graphically the intuition is as shown on the image attached.
In order to retrieve the 60 results i'm using the excelent solution provided here and everthings is fine when i perform singular searchs. Now i'm trying to use recursively a set of coordinates by just adding a for-loop at the very begining of the code but i doesn't work. I'm not much a programmer (as a matter of fact, i'm a sociologist) and i dont really know what i'm doing wrong. Could someone point me in the right direction please?
Thanks in advance and best regards from Guatemala
here my plain text coordinates file:
coords
"14.5446147628533, -90.84266666418"
"14.5538523714673, -90.84266666418"
And here my code:
###Preamble packages##
library(tidyverse)
library(googleway)
### load coordinates, plain text
dfCoords <- read.csv("~/coords.txt", sep="")
##Added For-loop begin##
for (i in dfCoords$coords) {
#### Original script begin ###
place_type <- "store"
key <- c("API Key")
radius <- 100
location <- i
format_res <- function(res) {
setNames(
cbind(
googleway::access_result(res, "coordinates"),
googleway::access_result(res, "place_name")
)
, c("lat", "long", "name")
)
}
do_search <- function(place_type, key, location, radius, page_token = NULL) {
google_places(
place_type = place_type,
location = location,
key = key,
radius = radius,
page_token = page_token
)
}
full_search <- function(place_type, key, location, radius) {
counter <- 0
page_token <- NULL ## can start on NULL because it means we're doing the first query
is_another_page <- TRUE
while( is_another_page ) {
res <- do_search(place_type, key, location, radius, page_token)
if( res$status == "OK" ) { ## check a valid result was returned
if( counter == 0 ) {
df <- format_res( res )
} else {
df <- rbind(df, format_res( res ) )
}
counter <- counter + 1
}
page_token <- res[["next_page_token"]]
is_another_page <- !is.null( page_token )
Sys.sleep(3) ## Sleep the function before the next call because there's a time limit
}
return(df)
}
df <- full_search(place_type, key, location, radius)
##Original script ends
}
##Added for loop end
str( df )
You only need to loop over the locations and call the functions from inside the loop (otherwise you are creating & defining the functions in each iteration)
I've added the
place_idto the results informat_res()so you get the unique place ids. You'll need this when processing the results because even though you specify aradius, google will still give you results outside this value.You need to assign the results of each iteration of the loop to an object. I've created a list
lst_resultsfor thisThe two example coordinates you've given don't produce any results, so I've added some error handling to account for
ZERO_RESULTSreturned from google. And I've added a third coordinate pair to show you it working.Here's the full updated code