convert the input sf to a different geometry

sf_cast(sf, to, close = TRUE, list_columns = NULL)

Arguments

sf

object to convert

to

the geometry to convert to.

close

logical indicating if polygons should be closed

list_columns

vector of column names or indexes. List columns are columns of data where there is a value corresponding to each coordinate in the geometry (sfc). List columns get cast with the geometries.

Examples


df <- data.frame(
 id1 = c(1,1,1,1,1,1,1,1,2,2,2,2)
 , id2 = c(1,1,1,1,2,2,2,2,1,1,1,1)
 , x = c(0,0,1,1,1,1,2,2,3,4,4,3)
 , y = c(0,1,1,0,1,2,2,1,3,3,4,4)
)

pt <- sf_point(obj = df, x = "x", y = "y", z = "id1")
mpt <- sf_multipoint(obj = df, x = "x", y = "y", multipoint_id = "id1")
ls <- sf_linestring(obj = df, x = "x", y = "y", linestring_id = "id1")
mls <- sf_multilinestring(obj = df, x = "x", y = "y", multilinestring_id = "id1")
p <- sf_polygon(
  obj = df
  , x = "x"
  , y = "y"
  , polygon_id = "id1"
  , linestring_id = "id2"
  , close = FALSE
  )
mp <- sf_multipolygon(
  obj = df
  , x = "x"
  , y = "y"
  , multipolygon_id = "id1"
  , linestring_id = "id2"
  , close = FALSE
  )

sf_cast( pt, "LINESTRING" )
#>    geometry
#> 1   0, 0, 1
#> 2   0, 1, 1
#> 3   1, 1, 1
#> 4   1, 0, 1
#> 5   1, 1, 1
#> 6   1, 2, 1
#> 7   2, 2, 1
#> 8   2, 1, 1
#> 9   3, 3, 2
#> 10  4, 3, 2
#> 11  4, 4, 2
#> 12  3, 4, 2
sf_cast( mpt, "POLYGON" )
#>   id1                                             geometry
#> 1   1 0, 0, 1, 1, 1, 1, 2, 2, 0, 0, 1, 1, 0, 1, 2, 2, 1, 0
#> 2   2                         3, 4, 4, 3, 3, 3, 3, 4, 4, 3
sf_cast( ls, "POINT" )
#>    id1 geometry
#> 1    1     0, 0
#> 2    1     0, 1
#> 3    1     1, 1
#> 4    1     1, 0
#> 5    1     1, 1
#> 6    1     1, 2
#> 7    1     2, 2
#> 8    1     2, 1
#> 9    2     3, 3
#> 10   2     4, 3
#> 11   2     4, 4
#> 12   2     3, 4
sf_cast( mls, "MULTIPOLYGON" )
#>   id1                                             geometry
#> 1   1 0, 0, 1, 1, 1, 1, 2, 2, 0, 0, 1, 1, 0, 1, 2, 2, 1, 0
#> 2   2                         3, 4, 4, 3, 3, 3, 3, 4, 4, 3
sf_cast( p, "POINT" )
#>    id1 geometry
#> 1    1     0, 0
#> 2    1     0, 1
#> 3    1     1, 1
#> 4    1     1, 0
#> 5    1     1, 1
#> 6    1     1, 2
#> 7    1     2, 2
#> 8    1     2, 1
#> 9    2     3, 3
#> 10   2     4, 3
#> 11   2     4, 4
#> 12   2     3, 4
sf_cast( mp, "LINESTRING" )
#>   id1               geometry
#> 1   1 0, 0, 1, 1, 0, 1, 1, 0
#> 2   1 1, 1, 2, 2, 1, 2, 2, 1
#> 3   2 3, 4, 4, 3, 3, 3, 4, 4


## List Columns

df <- data.frame(
 id1 = c(1,1,1,1,1,1,1,1,2,2,2,2)
 , id2 = c(1,1,1,1,2,2,2,2,1,1,1,1)
 , x = c(0,0,1,1,1,1,2,2,3,4,4,3)
 , y = c(0,1,1,0,1,2,2,1,3,3,4,4)
)

## Add a column where each value is an attribute of each coordinate
df$val <- letters[1:nrow(df)]

## Make a multipolygon, and specify `val` as a list_column
mp <- sf_multipolygon(
  obj = df
  , x = "x"
  , y = "y"
  , multipolygon_id = "id1"
  , linestring_id = "id2"
  , list_column = "val"
  , keep = TRUE
  , close = FALSE
  )

## The 'val' attributes follow the same structure as the geometry column
## So each 'val' corresponds to a single coordinate in the geometry
str( mp )
#> Classes ‘sf’ and 'data.frame':	2 obs. of  3 variables:
#>  $ id1     : num  1 2
#>  $ val     :List of 2
#>   ..$ :List of 1
#>   .. ..$ :List of 2
#>   .. .. ..$ : chr  "a" "b" "c" "d"
#>   .. .. ..$ : chr  "e" "f" "g" "h"
#>   ..$ :List of 1
#>   .. ..$ :List of 1
#>   .. .. ..$ : chr  "i" "j" "k" "l"
#>  $ geometry:List of 2
#>   ..$ :List of 1
#>   .. ..$ :List of 2
#>   .. .. ..$ : num [1:4, 1:2] 0 0 1 1 0 1 1 0
#>   .. .. ..$ : num [1:4, 1:2] 1 1 2 2 1 2 2 1
#>   .. ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
#>   ..$ :List of 1
#>   .. ..$ :List of 1
#>   .. .. ..$ : num [1:4, 1:2] 3 4 4 3 3 3 4 4
#>   .. ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
#>   ..- attr(*, "n_empty")= int 0
#>   ..- attr(*, "crs")=List of 2
#>   .. ..$ input: chr NA
#>   .. ..$ wkt  : chr NA
#>   .. ..- attr(*, "class")= chr "crs"
#>   ..- attr(*, "class")= chr [1:2] "sfc_MULTIPOLYGON" "sfc"
#>   ..- attr(*, "precision")= num 0
#>   ..- attr(*, "bbox")= 'bbox' Named num [1:4] 0 0 4 4
#>   .. ..- attr(*, "names")= chr [1:4] "xmin" "ymin" "xmax" "ymax"
#>  - attr(*, "sf_column")= chr "geometry"

## specifying `list_columns = "val"` when casting will retain the association
## between the 'val' attribute and each coordinate.
res <- sf_cast( mp, "LINESTRING", list_columns = "val" )

## The 'val' attribute still follows the same structure as the geometry column
str( res )
#> Classes ‘sf’ and 'data.frame':	3 obs. of  3 variables:
#>  $ id1     : num  1 1 2
#>  $ val     :List of 3
#>   ..$ : chr  "a" "b" "c" "d"
#>   ..$ : chr  "e" "f" "g" "h"
#>   ..$ : chr  "i" "j" "k" "l"
#>  $ geometry:List of 3
#>   ..$ : 'XY' num [1:4, 1:2] 0 0 1 1 0 1 1 0
#>   ..$ : 'XY' num [1:4, 1:2] 1 1 2 2 1 2 2 1
#>   ..$ : 'XY' num [1:4, 1:2] 3 4 4 3 3 3 4 4
#>   ..- attr(*, "n_empty")= int 0
#>   ..- attr(*, "crs")=List of 2
#>   .. ..$ input: chr NA
#>   .. ..$ wkt  : chr NA
#>   .. ..- attr(*, "class")= chr "crs"
#>   ..- attr(*, "class")= chr [1:2] "sfc_LINESTRING" "sfc"
#>   ..- attr(*, "precision")= num 0
#>   ..- attr(*, "bbox")= 'bbox' Named num [1:4] 0 0 4 4
#>   .. ..- attr(*, "names")= chr [1:4] "xmin" "ymin" "xmax" "ymax"
#>  - attr(*, "sf_column")= chr "geometry"