【问题标题】:Drawing driving routes in R在 R 中绘制行车路线
【发布时间】:2020-05-03 15:46:29
【问题描述】:

我正在关注这个问题中提供的答案:Drawing journey path using leaflet in R

并希望复制相同的想法:绘制显示城市之间行驶路线的地图。

作为参考,当我从 SymbolixAu 的答案中复制并粘贴代码时……效果很好! (简单的地图,即 googe_map,而不是“闪亮”的代码)。

换句话说,我认为我的 api 密钥设置得很好。但是由于某种原因,当我尝试在我的位置数据上使用相同的代码时,它不起作用。这是我的代码:

df_locations<-structure(list(origin = c("WARWICK", "EAST PROVIDENCE", "WARREN", 
                      "CENTERDALE", "CENTRAL FALLS", "DAVISVILLE", "NORTH PROVIDENCE", 
                      "EAST PROVIDENCE", "PROVIDENCE", "CHEPACHET"), destination = c("CENTERDALE", "EAST PROVIDENCE", "BRISTOL", "JOHNSTON", "CRANSTON", "WARWICK","NORTH PROVIDENCE", "EAST PROVIDENCE", "WARREN", "CHEPACHET")), class = "data.frame", row.names = c(NA, -10L))


## loop over each pair of locations, and extract the polyline from the result
lst_directions <- apply(df_locations, 1, function(x){
res <- google_directions(
key = api_key
, origin = x[['origin']]
, destination = x[['destination']]
)

df_result <- data.frame(
origin = x[['origin']]
, destination = x[['destination']]
, route = res$routes$overview_polyline$points
)
return(df_result)
})

## convert the results to a data.frame
df_directions <- do.call(rbind, lst_directions)

## plot the map
google_map(key = map_key ) %>%
add_polylines(data = df_directions, polyline = "route")

当我进入“应用”循环时,我收到错误“data.frame 中的错误(origin = x[[“origin”]],destination = x[[“destination”]],: 参数暗示不同的行数:1, 0"

当我重新运行完全相同的代码时,只需使用示例数据框:

df_locations <- data.frame(
origin = c("Melbourne, Australia", "Sydney, Australia")
, destination = c("Sydney, Australia", "Brisbane, Australia")
, stringsAsFactors = F
)

完美运行。有任何想法吗?我想知道是不是因为我的位置只有城市名称(没有州或国家/地区),所以我将示例数据调整为仅显示“墨尔本”或“悉尼”,仍然有效。

【问题讨论】:

  • 函数:google_directions() 可能没有为您的部分或全部位置返回有效响应。
  • @Dave2e 虽然这是完全可能的,但当我之前让“地理编码”功能找到其中一列的纬度和经度坐标时,它工作得很好。他们会有那么不同吗?我怎么能检查?
  • 该错误表明您指向data.frame 的向量之一的长度为零。 google_directions() 的输出“res”似乎是一个可能的来源。作为快速调试,我会尝试将print(res) 添加到循环中并尝试重现。如果存在错误值,可能是网络超时或函数输入错误。
  • @Dave2e 所以奇怪的是:我刚刚尝试了你的 print(res) 代码。首先,我用样本数据来看看我应该期待什么,效果很好。然后,我尝试在拥有数百行的“真实”数据上使用它,这就像从消防水管中喝水一样,大量信息出现在屏幕上,而我在抛出的类似信息页面中找不到问题在我得到同样的错误之前,我(看起来像很多好的谷歌方向),所以我用上面的示例数据尝试了 print(res),在显示任何方向之前,错误就被抛出了。即它的行为不同。
  • 有趣的是它的行为不同。还有一个可能的原因,一些 Web API 会限制允许的调用次数(每天或每秒)。使用Sys.sleep( ) 插入一点延迟可能会有所帮助。在 print 周围添加 if 语句,仅在 res 为 NA 时打印信息。

标签: r googleway


【解决方案1】:

对于它的价值,我只是运行你的代码没有任何问题。因此,@Dave2e 怀疑您的 API 调用可能有问题,无论是超时还是谷歌由于某种原因没有返回结果。因此,您必须继续调试/打印结果以查看是否是这种情况。

df_locations<-structure(list(origin = c("WARWICK", "EAST PROVIDENCE", "WARREN", 
                                        "CENTERDALE", "CENTRAL FALLS", "DAVISVILLE", "NORTH PROVIDENCE", 
                                        "EAST PROVIDENCE", "PROVIDENCE", "CHEPACHET"), destination = c("CENTERDALE", "EAST PROVIDENCE", "BRISTOL", "JOHNSTON", "CRANSTON", "WARWICK","NORTH PROVIDENCE", "EAST PROVIDENCE", "WARREN", "CHEPACHET")), class = "data.frame", row.names = c(NA, -10L))

library(googleway)

set_key("MYKEY")


## loop over each pair of locations, and extract the polyline from the result
lst_directions <- apply(df_locations, 1, function(x){
  res <- google_directions(
    origin = x[['origin']]
    , destination = x[['destination']]
  )

  df_result <- data.frame(
    origin = x[['origin']]
    , destination = x[['destination']]
    , route = res$routes$overview_polyline$points
  )
  return(df_result)
})

df_directions <- do.call(rbind, lst_directions)

## plot the map
google_map() %>%
  add_polylines(data = df_directions, polyline = "route")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 1970-01-01
    相关资源
    最近更新 更多