dcooley / sfheaders

Build sf objects from R and Rcpp
https://dcooley.github.io/sfheaders/
Other
74 stars 5 forks source link

sf_cast - list columns are replicated when casting #90

Closed dcooley closed 3 years ago

dcooley commented 3 years ago
library(sf)

nc <- sf::st_read( system.file("./shape/nc.shp", package = "sf"))

## Get a multipolygon with multiple polygons
df <- sfheaders::sf_to_df(
  sf = nc[4, ]
  , fill = TRUE
)

df$s <- sample( 1:nrow(df), replace = F )
df$z <- sample( 5:10000, nrow(df), replace = T)
# df$z <- ( df$y ^ 2 ) * 10
# df$z <- 1:nrow(df)
# df$s <- df$z

head( df )

sf <- sfheaders::sf_multipolygon(
  obj = df
  , x = "x"
  , y = "y"
  , z = "z"
  , list_columns = "s"
  , multipolygon_id = "multipolygon_id"
  , polygon_id = "polygon_id"
  , linestring_id = "linestring_id"
  , keep = T
)

l <- list(
  fill_colour = "sfg_id"
)

sf_poly <- sfheaders::sf_cast( sf, "POLYGON" )

## the 's' column is copied, rather than subset
sf_poly$s