Skip to content

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

Description

@kadyb

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions