r-spatial / sf

Simple Features for R
https://r-spatial.github.io/sf/
Other
1.33k stars 294 forks source link

Feature Request: as.data.frame for sf_layers-object #2412

Closed ratnanil closed 3 months ago

ratnanil commented 3 months ago

I would like to be able to coerce the output of st_layers to a data.frame. Currently, I do the following, which seems wrong.

suppressPackageStartupMessages(library(sf))

as.data.frame.sf_layers <- \(x) sf:::print.sf_layers(x) |> data.frame()

layers <- st_layers(system.file("sqlite/test3.sqlite",package="sf")) 

layers_df <- as.data.frame(layers) # I dont know how to supress print-ing here
#> Driver: SQLite 
#> Available layers:
#>   layer_name geometry_type features fields              crs_name
#> 1      Towns         Point     8101      5 WGS 84 / UTM zone 32N
#> 2   HighWays   Line String       10      2 WGS 84 / UTM zone 32N
#> 3    Regions Multi Polygon       10      2 WGS 84 / UTM zone 32N

class(layers_df)
#> [1] "data.frame"

Would it not make sense to have something similar implemented in sf?

edzer commented 3 months ago

I think we already have that; did you check the dev version of sf? See https://github.com/r-spatial/sf/blob/main/NEWS.md

ratnanil commented 3 months ago

Ah beautiful, thanks!