0

我正在使用 stplanr 包处理 OD 数据。我对这个包的兴趣是计算步行和公共交通模式的路线距离和时间。我有一些 OD 点坐标是由欲望线对象上使用的 od2odf() 产生的。如何仅为行对点执行stplanr包的 dist_google() ,而不是像函数那样全对全?

dline_coords <-
  structure(list(code_o = c("355030843000199", "355030892000149"
), code_d = c("2787458", "2027720"), fx = c(-46.75786949, -46.59211679
), fy = c(-23.68324013, -23.49609762), tx = c(-46.7574929661601, 
-46.5905849090996), ty = c(-23.6920856941273, -23.4999753327844
)), .Names = c("code_o", "code_d", "fx", "fy", "tx", "ty"), row.names = c(55L, 
130L), class = "data.frame")

每对点都是按行组合的,fxfy变量表示“从”点,txty表示“到”点。

print(dline_coords)
             code_o  code_d        fx        fy        tx        ty
55  355030843000199 2787458 -46.75787 -23.68324 -46.75749 -23.69209
130 355030892000149 2027720 -46.59212 -23.49610 -46.59058 -23.49998

如果我调用该函数,它会计算所有对组合的距离和路线时间:

library(stplanr)
distances <- dist_google(from=dline_coords[1:2,3:4], to=dline_coords[1:2,5:6], mode="walking")
dim(distances)
[1] 4   6

# if line index is not explicit, it returns an error
# distances <- dist_google(from=dline_coords[,3:4], to=dline_coords[,5:6])

但我想要这个结果而不是全部组合:

distances <-
  rbind(dist_google(from=dline_coords[1,3:4], to=dline_coords[1,5:6]),
        dist_google(from=dline_coords[2,3:4], to=dline_coords[2,5:6]))
print(distances)

Obs:我需要为一千个案例执行此操作,但 API 限制为每次调用 100 个结果。

有人能帮我吗?

4

1 回答 1

0

分享我的解决方案!这是一项简单的任务,但我只是在学习使用循环和函数。读了一会儿之后,我使用这个for循环代码做到了这一点:

distances <- data.frame()
for (line in 1:nrow(dline_coords)) {
  origin <- dline_coords[line 3:4]
  destiny  <- dline_coords[line 5:6]
  distances <- rbind(distances, dist_google(from=origin to=destiny mode='walking'))
}

我得到了我想要的东西。只是行对:

  from_addresses                                                                to_addresses                                                             distances duration currency fare
1 R. Magister Leoninus, 33-171 - Jardim Santa Margarida, São Paulo - SP, Brazil Rua Ignácio Limas, 10 - Jardim Angela, São Paulo - SP, 04920-050, Brazil      1378     1160       NA   NA
2 R. Guilherme Baer, 211 - Vila Medeiros, São Paulo - SP, Brazil                R. André da Fonseca, 71 - Vila Maria, São Paulo - SP, 02135-010, Brazil        675      572       NA   NA

而不是以原始方式获得的输出,即 all-origins-to-all-destinations:

  from_addresses                                                                to_addresses                                                             distances duration currency fare
1 R. Magister Leoninus, 33-171 - Jardim Santa Margarida, São Paulo - SP, Brazil Rua Ignácio Limas, 10 - Jardim Angela, São Paulo - SP, 04920-050, Brazil      1378     1160       NA   NA
2 R. Magister Leoninus, 33-171 - Jardim Santa Margarida, São Paulo - SP, Brazil R. André da Fonseca, 71 - Vila Maria, São Paulo - SP, 02135-010, Brazil      29523    22764       NA   NA
3 R. Guilherme Baer, 204-210 - Vila Medeiros, São Paulo - SP, Brazil            Rua Ignácio Limas, 10 - Jardim Angela, São Paulo - SP, 04920-050, Brazil     30808    23783       NA   NA
4 R. Guilherme Baer, 204-210 - Vila Medeiros, São Paulo - SP, Brazil            R. André da Fonseca, 71 - Vila Maria, São Paulo - SP, 02135-010, Brazil        675      572       NA   NA
于 2018-02-10T16:35:18.800 回答