From 5b2b361f9f95b8444e3f533870f52f3c2c8b2dd3 Mon Sep 17 00:00:00 2001 From: Atum Scott Date: Wed, 15 Feb 2023 07:17:37 +0000 Subject: [PATCH] Add updates to speed-up binding rows, see https://github.com/r-spatial/sf/issues/798 --- R/get_routes.R | 3 ++- code/tests/bind_rows_sf.R | 46 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 code/tests/bind_rows_sf.R diff --git a/R/get_routes.R b/R/get_routes.R index bcd202ac..7d90878b 100644 --- a/R/get_routes.R +++ b/R/get_routes.R @@ -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) } diff --git a/code/tests/bind_rows_sf.R b/code/tests/bind_rows_sf.R new file mode 100644 index 00000000..66ce02d3 --- /dev/null +++ b/code/tests/bind_rows_sf.R @@ -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 +)