I'm working with OD data with stplanr package. My interest in this package is to compute route distances and times specially for walking and public transport modes. I have some OD points coordinates resulted from the od2odf() used on a desire lines object. How can I perform dist_google() of stplanr package just for row pairs of points, not all-to-all as the function do?
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")
Each pair of points are row wise combined, fx and fy variables representing "from" points and tx and ty the "to" points.
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
If I call the function, it calculate distance and route time for all combinations of pairs:
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])
But I want this result instead the all-to-all combinations:
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: I need do this for thousand cases, but the API is limited to 100 results per call.
Can someone help me?
Sharing my solution! This was a simple task, but I'm just learning to work with loops and functions. After a while reading a little, I did this using this
for
looping code:And I got what I was looking for. Just row pairs:
Instead of this output obtained in the original way, which is all-origins-to-all-destinations: