Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return only coordinates for points in sfc_to_df()? #101

Open
kadyb opened this issue Oct 1, 2023 · 0 comments
Open

Return only coordinates for points in sfc_to_df()? #101

kadyb opened this issue Oct 1, 2023 · 0 comments

Comments

@kadyb
Copy link

kadyb commented Oct 1, 2023

I wonder if it would be resonable to only return X and Y coordinates for points when converting geometry to data frame like sf::st_coordinates() does?

library("sf")
library("sfheaders")

n = 100000
df = data.frame(x = rnorm(n), y = rnorm(n))
pts = st_as_sfc(st_as_sf(df, coords = c("x", "y")))

x = sf::st_coordinates(pts)
str(x)
#> num [1:100000, 1:2] -0.4177 -0.0679 0.6598 0.756 0.1694 ...
#> - attr(*, "dimnames")=List of 2
#>  ..$ : NULL
#>  ..$ : chr [1:2] "X" "Y"
format(object.size(x), units = "auto")
#> [1] "1.5 Mb"

y = sfheaders::sfc_to_df(pts)
str(y)
#> 'data.frame':	100000 obs. of  4 variables:
#>  $ sfg_id  : int  1 2 3 4 5 6 7 8 9 10 ...
#>  $ point_id: int  1 2 3 4 5 6 7 8 9 10 ...
#>  $ x       : num  -0.4177 -0.0679 0.6598 0.756 0.1694 ...
#>  $ y       : num  -0.71246 -0.00779 -2.32569 -0.14325 2.83237 ...
format(object.size(y), units = "auto")
#> [1] "2.3 Mb"

t = bench::mark(
  check = FALSE,
  sf = st_coordinates(pts),
  sfheaders = sfc_to_df(pts),
)
t[, 1:5]
#>   expression      min   median `itr/sec` mem_alloc
#> 1 sf           1.72ms   1.78ms     532.     3.05MB
#> 2 sfheaders   28.15ms  29.07ms      34.1     4.2MB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant