9 Estimating Time Deltas Between Drivers Over Small Sections
We can now generate a list of delta values describing the time difference between the drivers in completing each section.
Note that if the telemetry samples are widely separated, this estimates might be quite poor, particularly if they are based on linear interpolation of times between points.
= evans_1km_segments - fourmaux_1km_segments
dummy_split_deltas head(dummy_split_deltas, 3)
## [1] 2.2213305 -1.2957991 -0.1980497
If we cast the original route to a trajr
directory, we can easily split the route into separate sections based on the cumulative distance of steps along the route.
library(trajr)
<- TrajFromCoords(as.data.frame(st_coordinates(stage_route_utm)))
trj $distance = Mod(trj$displacement)
trj
# Note that the trj object natively also provides a displacement time
$cum_dist = cumsum(trj$distance) trj
We can then plot the route with the time delta highlighted:
= ggplot()+ coord_fixed()
g
# Create a trace for each segment
for (i in 1:length(segments)){
=(i-1)*segment_length
start= start+segment_length
end
# Filter out the part of the trajectory we want to plot
= trj$cum_dist >= start & trj$cum_dist <= end
segment_filter = trj[segment_filter,]
route_segment
# Set colour based on delta
= dummy_split_deltas[i]
cif (is.na(c)) c=0
$c = c
route_segment
# Add segment to plot
= g+geom_path(data=route_segment, aes(x=x, y=y, color=c))
g
}
# Render plot
+scale_colour_gradient( low = "red", high = "green", breaks=c(0)) g
# TO DO
TO DO - can we split on the route using split points a distance along the route?
# not required?
= function(route_utm, dist, units='m') {
getlocation = rgeos::gInterpolate(as(route_utm, "Spatial"),
sample_point_sp_utm # Distance along route in meters
dist, normalized = FALSE)
# We can convert back from an sp to an sf object:
= st_as_sf(sample_point_sp_utm)
sample_point_sf_utm
# And also convert back to a latlong reference system
= sample_point_sf_utm %>% st_transform(crs = st_crs(latlon_crs))
sample_point_sf
sample_point_sf
}
getlocation(stage_route_utm, c(1500, 2000))
## Warning in proj4string(spgeom): CRS object has comment, which is lost in output; in tests, see
## https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html
## Simple feature collection with 2 features and 0 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: 25.11525 ymin: 61.81992 xmax: 25.11939 ymax: 61.82377
## Geodetic CRS: WGS 84
## geometry
## 1 POINT (25.11525 61.82377)
## 2 POINT (25.11939 61.81992)
# Not required?
= getlocation(stage_route_utm, segments)
df_dummy_split_points = df_dummy_split_points %>%
df_dummy_split_coords st_coordinates() %>%
as.data.frame() %>%
rename(lon=X, lat=Y)
$delta = dummy_split_deltas
df_dummy_split_coords
head(df_dummy_split_coords, 3)
## lon lat delta
## 1 25.11383 61.83364 2.2213305
## 2 25.12013 61.82727 -1.2957991
## 3 25.11939 61.81992 -0.1980497
We can also split the co-ordinates out from the geometry using a recipe such as:
#https://stackoverflow.com/questions/54734771/sf-write-lat-long-from-geometry-into-separate-column-and-keep-id-column
#df %>%
# mutate(lat = unlist(map(geometry,1)),
# long = unlist(map(geometry,2)))