Function by row pairs instead all-to-all (dist_google of stplanr in R)

96 Views Asked by At

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?

1

There are 1 best solutions below

1
On

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:

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

And I got what I was looking for. Just row pairs:

  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

Instead of this output obtained in the original way, which is 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