Skip to content

Commit

Permalink
Add updates to speed-up binding rows, see r-spatial/sf#798
Browse files Browse the repository at this point in the history
  • Loading branch information
atumscott committed Feb 15, 2023
1 parent 786a0d2 commit 5b2b361
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion R/get_routes.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ batch_routes = function(od, fun, nrow_batch = 100, plan = "fastest", purpose, ..
}
}
message("Combining results")
result = do.call(rbind, results)
result = sf::st_as_sf(data.table::rbindlist(results))
bbox = sfheaders::sf_bbox()
return(result)
}
46 changes: 46 additions & 0 deletions code/tests/bind_rows_sf.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@


# Ok, so the data.table approach is something else (as expected I guess):
library(sf)
library(tidyverse)
nc <- sf::read_sf(system.file("shape/nc.shp", package = "sf"))
set.seed(1234)
nc_list_10 <- nc %>%
dplyr::sample_n(size = 10, replace = TRUE) %>%
mutate(sample_id = paste0("sample_", row_number())) %>%
split(.$sample_id)

bench::mark(max_iterations = 2, check = FALSE,
rbind = do.call(what = rbind, args = nc_list_10),
bind_rows = do.call(what = bind_rows, args = nc_list_10),
rbindlist = sf::st_as_sf(data.table::rbindlist(nc_list_10)) # incorrect bb
)

waldo::compare(sf::st_as_sf(data.table::rbindlist(nc_list_10)), do.call(what = rbind, args = nc_list_10))

microbenchmark(
mapedit:::combine_list_of_sf(nc_list_1k),
do.call(what = sf:::rbind.sf,
args = nc_list_1k),
purrr::reduce(.x = nc_list_1k,
.f = sf:::rbind.sf),
sf::st_as_sf(data.table::rbindlist(nc_list_1k)),
times = 25L
)
mapedit:::combine_list_of_sf(nc_list_1k)
do.call(what = sf:::rbind.sf, args = nc_list_1k)
purrr::reduce(.x = nc_list_1k, .f = sf:::rbind.sf)
sf::st_as_sf(data.table::rbindlist(nc_list_1k))

set.seed(1234)
nc_list_10k <- nc %>%
dplyr::sample_n(size = 10000, replace = TRUE) %>%
mutate(sample_id = paste0("sample_", row_number())) %>%
split(.$sample_id)
microbenchmark(
mapedit:::combine_list_of_sf(nc_list_10k),
do.call(what = sf:::rbind.sf,
args = nc_list_10k),
sf::st_as_sf(data.table::rbindlist(nc_list_10k)),
times = 25L
)

0 comments on commit 5b2b361

Please sign in to comment.