geometries
geometries copied to clipboard
coordinates
General function to extract any dimensions, and any nesting of coordinates
TODO v0.1
- [x] R-facing function
- [x] return data.frame with column names
- [ ] Vignette entry
- [x] Tests
TODO v0.2
- [ ] fully templated
- [ ] can I get the dimensions of each individual geometry at the start, rather than in each iteration?
- [ ] optimise where all dimensions & nests are the same (to avoid extra
dimensions()
calls?)
This is designed to be a general coordinate-flattener, where it can handle any level of nesting of matrices and lists.
l <- list(
list(
matrix(1:12, ncol = 2)
, matrix(1:4, ncol = 2)
)
, 1:5
, 1:2
, matrix(1:9, ncol = 3)
)
gm_coordinates( l )
id id1 id2 c1 c2 c3 c4 c5
1 0 1 1 1 7 NA NA NA
2 0 1 1 2 8 NA NA NA
3 0 1 1 3 9 NA NA NA
4 0 1 1 4 10 NA NA NA
5 0 1 1 5 11 NA NA NA
6 0 1 1 6 12 NA NA NA
7 0 2 2 1 3 NA NA NA
8 0 2 2 2 4 NA NA NA
9 1 NA NA 1 2 3 4 5
10 2 NA NA 1 2 NA NA NA
11 3 NA 1 1 4 7 NA NA
12 3 NA 1 2 5 8 NA NA
13 3 NA 1 3 6 9 NA NA
But, it's still slower because of this generalisation (I believe)
library(sf)
library(sfheaders)
# remotes::install_github("paleolimbot/wk")
library(wk)
nc_sf <- sf::read_sf(system.file("shape/nc.shp", package="sf"))
nc_sfc <- sf::st_geometry(nc_sf)
nc_wkb <- sf::st_as_binary(nc_sfc)
wk_wkb = wk::wkb_coords(rep(nc_wkb, 10))
sfheaders = sfheaders::sfc_to_df(rep(nc_sfc, 10))
sf = sf::st_coordinates(rep(nc_sfc, 10))
wb <- rep(nc_wkb, 10)
sfc <- rep(nc_sfc, 10)
microbenchmark::microbenchmark(
wk_wkb = { df_wk <- wk::wkb_coords(wb) },
sfheaders = { df_sf <- sfheaders::sfc_to_df(sfc) },
geometries = { df_gm <- geometries:::rcpp_coordinates( sfc ) }
)
# Unit: milliseconds
# expr min lq mean median uq max neval
# wk_wkb 1.434450 1.526489 1.820761 1.575800 1.748121 9.416171 100
# sfheaders 4.661102 5.189502 8.213676 5.586032 11.717638 56.477026 100
# geometries 4.045773 4.294310 5.387846 4.456686 4.746964 12.798332 100