I have an sf data frame with the number of trips from one bike station to another. The geometry column contains a direct route (defined by osm) between the two sides.
I want to build a map with streets colored by the gradient of the number of trips that went on.
My problem is that I have a number of trips along routes, not streets.
I use st_interception()to extract the same part from two routes and st_difference()to extract the differences.
For two lines with 10 and 15 rides here I want.
library('sf')
library('ggplot2')
route1 <- st_linestring(rbind(c(0, 0), c(1, 1), c(2, 2), c(3, 3)))
route2 <- st_linestring(rbind(c(1, 0), c(1, 1), c(2, 2), c(3, 0)))
route1 <- st_sf(id = 1, trips = 10, geometry = st_sfc(route1))
route2 <- st_sf(id = 2, trips = 15, geometry = st_sfc(route2))
ggplot(data = rbind(route1, route2)) + geom_sf(mapping = aes(color = trips)) +
theme(panel.grid.major = element_line(colour = 'transparent'))
route <- mergeRoutes(route1, route2, init = TRUE)
ggplot(data = route) + geom_sf(mapping = aes(color = trips)) +
theme(panel.grid.major = element_line(colour = 'transparent'))
I wrote the mergeRoute function, it gives what I want for two routes, but cannot be extended to many, many routes.
mergeRoutes <- function(route1, route2, init = FALSE)
{
intersection <- st_intersection(route1$geometry, route2$geometry)
if(length(intersection) != 0 &
!'sfc_POINT' %in% class(intersection) &
!'sfc_MULTIPOINT' %in% class(intersection)) {
intersection <- st_collection_extract(x = intersection, type = "LINESTRING")
count <- route1$count + route2$count
intersection <- data.frame(id = route1$id, count = count, geometry = intersection)
route1_dif <- st_difference(route1$geometry, route2$geometry)
route2_dif <- st_difference(route2$geometry, route1$geometry)
if(length(route1_dif) != 0) {
route1 <- data.frame(id = route1$id,
count = route1$count,
geometry = route1_dif)
} else {
route1 <- NULL
}
if(length(route2_dif) != 0) {
route2 <- data.frame(id = route2$id,
count = route2$count,
geometry = route2_dif)
} else {
route2 <- NULL
}
result <- rbind(intersection, route1, route2)
return(result)
} else if (init) {
result <- rbind(route1, route2)
} else {
result <- route2
}
return(result)
}
, -, , , . , lapply() for, mac (16 RAM, 2.5 ghz), - 15 .
attemp 2000 ( ).
segment_routes <- mergeRoutes(route1 = directions %>% slice(1),
route2 = directions %>% slice(2),
init = TRUE)
for(i in 3:nrow(directions)) {
new_route <- directions %>% slice(i)
new_routes <- lapply(X = seq(nrow(segment_routes)),
FUN = function(j) mergeRoutes(route1 = segment_routes %>% slice(j),
route2 = new_route))
new_routes <- do.call(rbind, new_routes)
new_routes <- st_sf(new_routes,
geometry = new_routes$geometry,
crs = st_crs(directions))
segment_routes <- rbind(segment_routes, new_routes)
}
, st_intersection(), , , , , , 2 , .
sf , sp / data.table .
.
:
R version 3.4.3 (2017-11-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.3
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_2.2.1.9000 sf_0.6-0
loaded via a namespace (and not attached):
[1] Rcpp_0.12.15 class_7.3-14 withr_2.1.1.9000 plyr_1.8.4
[5] grid_3.4.3 gtable_0.2.0 DBI_0.7 magrittr_1.5
[9] e1071_1.6-8 units_0.5-1 scales_0.5.0.9000 pillar_1.2.1
[13] rlang_0.2.0 lazyeval_0.2.1 tools_3.4.3 udunits2_0.13
[17] munsell_0.4.3 yaml_2.1.17 compiler_3.4.3 colorspace_1.3-2
[21] classInt_0.1-24 tibble_1.4.2