0

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?

Bruno Pinheiro
  • 964
  • 8
  • 20
  • You could just use the distance formula, or since it looks like those are lat/lons, a version accounting for curvature like `geosphere::distHaversine(dline_coords[c('fx', 'fy')], dline_coords[c('tx', 'ty')])`. By default, results are in meters. – alistaire Jan 15 '18 at 15:12
  • Sorry, i forgot to say that my interest is to compute public transport routes times and distances, not only euclidean distance. Because this I'm using stplanr functions, which are preety good to transport analysis. I will edit to make it more clear. However, it's a great and simple way to perform distance calculations. Thanks! – Bruno Pinheiro Jan 15 '18 at 15:24
  • I posted an answer sharing my solution @alistaire Thanks for your help! – Bruno Pinheiro Feb 10 '18 at 16:35

1 Answers1

0

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
Bruno Pinheiro
  • 964
  • 8
  • 20
  • `do.call(rbind, lapply(...))` or `purrr::map_df` are nice alternatives to the `for` loop here. Better, iterate over the actual values instead of an index, e.g. with `Map` (the multivariate `lapply`) or `purrr::pmap_df`. – alistaire Feb 10 '18 at 17:14