ipeaGIT/gtfs2gps

adjust_speed approach

Closed this issue · 2 comments

The function adjust_speed is currently being applied to gtfs2gps::gtfs2gps()
output files, but there are certain values (speed == 1.000000e-12 [km/h])
that should not be updated, as they indicate a situation of a
stopped vehicle.

See reprex

library(data.table)
library(magrittr)
gtfs_for <- gtfs2gps::read_gtfs(system.file("extdata/poa.zip"
                                            , package = "gtfs2gps")) %>%
  gtfs2gps::filter_single_trip() %>% 
  gtfs2gps::filter_by_shape_id("R10-2") %>% 
  suppressMessages()

We can clearly see the patterns
of low speeds

gps_for <-  gtfs_for %>% 
  gtfs2gps::gtfs2gps() %>% 
  suppressMessages()
gps_for[1:2]
#>    shape_id     trip_id route_type id timestamp shape_pt_lon shape_pt_lat
#> 1:    R10-2 R10-2@1#645          3  1  06:45:00     -51.2278    -30.03247
#> 2:    R10-2 R10-2@1#645          3  2  06:45:00     -51.2278    -30.03247
#>    stop_id stop_sequence        speed  dist cumdist cumtime trip_number
#> 1:    5410             1 1e-12 [km/h] 0 [m]   0 [m]   0 [s]           1
#> 2:    5410             1 1e-12 [km/h] 0 [m]   0 [m]   0 [s]           1

gps_for[(.N-3):.N]
#>    shape_id     trip_id route_type  id timestamp shape_pt_lon shape_pt_lat
#> 1:    R10-2 R10-2@1#645          3 562  07:34:55    -51.14570    -30.15031
#> 2:    R10-2 R10-2@1#645          3 563  07:35:00    -51.14610    -30.15014
#> 3:    R10-2 R10-2@1#645          3 564  07:35:00    -51.14610    -30.15014
#> 4:    R10-2 R10-2@1#645          3 565      <NA>    -51.14692    -30.14979
#>    stop_id stop_sequence               speed         dist      cumdist
#> 1:    <NA>            NA 3.200199e+01 [km/h] 82.11437 [m] 26625.00 [m]
#> 2:     433            40 3.200199e+01 [km/h] 43.32548 [m] 26668.33 [m]
#> 3:     433            40 1.000000e-12 [km/h]  0.00000 [m] 26668.33 [m]
#> 4:    <NA>            NA           NA [km/h] 87.23608 [m] 26755.56 [m]
#>         cumtime trip_number
#> 1: 2995.126 [s]           1
#> 2: 3000.000 [s]           1
#> 3: 3000.000 [s]           1
#> 4:       NA [s]           1

When we apply adjust_speed, the speed == 0 turns into mean values,
which is wrong by principle.

 gps_for_fix <-  gps_for %>% 
   gtfs2gps::adjust_speed() %>% 
   suppressMessages()
 gps_for_fix[1:2]
#>    shape_id     trip_id route_type id timestamp shape_pt_lon shape_pt_lat
#> 1:    R10-2 R10-2@1#645          3  1  06:45:00     -51.2278    -30.03247
#> 2:    R10-2 R10-2@1#645          3  2  06:45:00     -51.2278    -30.03247
#>    stop_id stop_sequence           speed  dist cumdist cumtime trip_number
#> 1:    5410             1 32.00199 [km/h] 0 [m]   0 [m]   0 [s]           1
#> 2:    5410             1 32.00199 [km/h] 0 [m]   0 [m]   0 [s]           1
#>     time
#> 1: 0 [s]
#> 2: 0 [s]
 
 
 gps_for_fix[(.N-3):.N]
#>    shape_id     trip_id route_type  id timestamp shape_pt_lon shape_pt_lat
#> 1:    R10-2 R10-2@1#645          3 562  07:34:55    -51.14570    -30.15031
#> 2:    R10-2 R10-2@1#645          3 563  07:35:00    -51.14610    -30.15014
#> 3:    R10-2 R10-2@1#645          3 564  07:35:00    -51.14610    -30.15014
#> 4:    R10-2 R10-2@1#645          3 565  07:35:10    -51.14692    -30.14979
#>    stop_id stop_sequence           speed         dist      cumdist      cumtime
#> 1:    <NA>            NA 32.00199 [km/h] 82.11437 [m] 26625.00 [m] 2995.126 [s]
#> 2:     433            40 32.00199 [km/h] 43.32548 [m] 26668.33 [m] 3000.000 [s]
#> 3:     433            40 32.00199 [km/h]  0.00000 [m] 26668.33 [m] 3000.000 [s]
#> 4:    <NA>            NA 32.00199 [km/h] 87.23608 [m] 26755.56 [m] 3009.813 [s]
#>    trip_number         time
#> 1:           1 9.237291 [s]
#> 2:           1 4.873812 [s]
#> 3:           1 0.000000 [s]
#> 4:           1 9.813447 [s]

My suggestion is to update speeds in adjust_speed() based on
gps_as_sflinestring() output

Created on 2022-05-11 by the reprex package (v2.0.1)

I decided against updating gps_as_sflinestring() speeds, but to edit adjust_speed() without considering the bus stops to update speed. See difference in image.

PS: This is the trip_id == "U804-T01V01B01-I" of Fortaleza. The speeds of U804-T01V01B01-I trip are being update according to all shape_ids, and not based on the average speed of the individual shape_id.

red dots represents the vehicle stops | blue dots represents the modified speeds.

image

other examples
image
image
image