gpt4 book ai didi

r - Google Maps Platform 最多 60 个结果限制的解决方法

转载 作者:行者123 更新时间:2023-12-05 05:39:07 25 4
gpt4 key购买 nike

我一直在使用 googleway 包来使用 place_type 键检索信息,但我一直坚持使用 60 results limit restriction .

我正在尝试一种稍微不同的方法来解决这个限制:分而治之。在我的工作中,所有的空间分析都是使用 QGIS 进行的,并且突然出现了一个想法。在某些坐标周围创建缓冲区;假设半径为 1 公里。然后使用该缓冲区域,我应用 hex-bin tessellation 来获取一组质心坐标,这些坐标可用于使用 60 个结果的小块来完成整个区域(1km 半径)(假设使用 googleway 包进行 100mts 半径查询)。从图形上看,直觉如所附图片所示。

为了检索 60 个结果,我使用了提供的出色解决方案 here当我执行单一搜索时,一切都很好。现在我试图通过在代码的最开头添加一个 for 循环来递归地使用一组坐标,但我不起作用。我不是一个程序员(事实上,我是一个社会学家)而且我真的不知道我做错了什么。有人能给我指出正确的方向吗?

提前致谢,危地马拉致以最诚挚的问候

这里是我的纯文本坐标文件:

coords
"14.5446147628533, -90.84266666418"
"14.5538523714673, -90.84266666418"

这里是我的代码:

###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 )

intuition of the workaround

最佳答案

  1. 您只需遍历位置并从循环内部调用函数(否则您将在每次迭代中创建和定义函数)

  2. 我已将 place_id 添加到 format_res() 的结果中,因此您可以获得唯一的地点 ID。在处理结果时您将需要它,因为即使您指定了 radius,google 仍会为您提供超出此值的结果。

  3. 您需要将循环的每次迭代的结果分配给一个对象。我为此创建了一个列表 lst_results

  4. 您提供的两个示例坐标不会产生任何结果,因此我添加了一些错误处理以解决从 google 返回的 ZERO_RESULTS。我添加了第三个坐标对来向您展示它的工作原理。

这是完整的更新代码


library(googleway)

format_res <- function(res) {
setNames(
cbind(
googleway::access_result(res, "coordinates"),
googleway::access_result(res, "place_name"),
googleway::access_result(res, "place") ## store the unique place_id as well
)
, c("lat", "long", "name", "place_id")
)
}

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

## initialise a data.frame to store the results
df <- data.frame(
lat = vector("numeric", 0L)
, long = vector("numeric", 0L)
, name = vector("character", 0L)
, place_id = vector("character", 0L)
)

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
} else {
## print a message for not-OK results
print(paste0(res[["status"]], " for ", paste0(location, collapse = ", ") ))
}

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)
}

## I've added a 3rd example that actually has results
dfCoords <- data.frame(
coords = c("14.5446147628533, -90.84266666418" ,"14.5538523714673, -90.84266666418", "-37.816660, 144.967092")
)

key <- secret::get_secret("GOOGLE")
place_type <- "store"
radius <- 100

## create a list to store the results
lst_results <- vector("list", length = nrow(dfCoords))
## Using a list will be more efficient that `rbind`-ing a data.frame in each iteration

## loop through the indexes of the coordinates
## this wy we can assign the results to the correct index of the list
for (i in 1:nrow(dfCoords) ) {

location <- dfCoords[i, "coords"]

## the coordiantes must be a numeric vector
location <- as.numeric(strsplit(location, ",")[[1]])


lst_results[[ i ]] <- full_search(
place_type = place_type
, key = key
, location = location
, radius = radius
)
}

lapply(lst_results, head)

# [[1]]
# [1] lat long name
# <0 rows> (or 0-length row.names)
#
# [[2]]
# [1] lat long name
# <0 rows> (or 0-length row.names)
#
# [[3]]
# lat long name place_id
# 1 -37.81681 144.9665 StayCentral Flinders Lane Melbourne ChIJmy5Y5YxD1moRwnnrXIAiejM
# 2 -37.81601 144.9665 EB Games / ZiNG Pop Culture - Swanston Street ChIJz6n71LVC1moR-wgn04JtBjk
# 3 -37.81666 144.9668 Tiffany Pollard Jewellery ChIJ45afhLZC1moRnyg_JBIEf2o
# 4 -37.81666 144.9668 dead & buried ChIJx_udg7ZC1moR2Kw-kXTvRIw
# 5 -37.81670 144.9667 Citizen Watch Repair ChIJtW1Cx8lC1moRxJsUpo14NAY
# 6 -37.81671 144.9669 Paris in Melbourne ChIJR_J5hLZC1moRxZ7EIUb5ZQw

关于r - Google Maps Platform 最多 60 个结果限制的解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72776905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com