convert the input sfc
to a different geometry
sfc_cast(sfc, to, close = TRUE)
geometry object to convert to a different geometry
the geometry to convert to.
logical indicating if polygons should be closed
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 <- sfc_point(obj = df, x = "x", y = "y", z = "id1")
mpt <- sfc_multipoint(obj = df, x = "x", y = "y", multipoint_id = "id1")
ls <- sfc_linestring(obj = df, x = "x", y = "y", linestring_id = "id1")
mls <- sfc_multilinestring(obj = df, x = "x", y = "y", multilinestring_id = "id1")
p <- sfc_polygon(
obj = df
, x = "x"
, y = "y"
, polygon_id = "id1"
, linestring_id = "id2"
, close = FALSE
)
mp <- sfc_multipolygon(
obj = df
, x = "x"
, y = "y"
, multipolygon_id = "id1"
, linestring_id = "id2"
, close = FALSE
)
sfc_cast( pt, "LINESTRING" )
#> [[1]]
#> [,1] [,2] [,3]
#> [1,] 0 0 1
#> attr(,"class")
#> [1] "XYZ" "LINESTRING" "sfg"
#>
#> [[2]]
#> [,1] [,2] [,3]
#> [1,] 0 1 1
#> attr(,"class")
#> [1] "XYZ" "LINESTRING" "sfg"
#>
#> [[3]]
#> [,1] [,2] [,3]
#> [1,] 1 1 1
#> attr(,"class")
#> [1] "XYZ" "LINESTRING" "sfg"
#>
#> [[4]]
#> [,1] [,2] [,3]
#> [1,] 1 0 1
#> attr(,"class")
#> [1] "XYZ" "LINESTRING" "sfg"
#>
#> [[5]]
#> [,1] [,2] [,3]
#> [1,] 1 1 1
#> attr(,"class")
#> [1] "XYZ" "LINESTRING" "sfg"
#>
#> [[6]]
#> [,1] [,2] [,3]
#> [1,] 1 2 1
#> attr(,"class")
#> [1] "XYZ" "LINESTRING" "sfg"
#>
#> [[7]]
#> [,1] [,2] [,3]
#> [1,] 2 2 1
#> attr(,"class")
#> [1] "XYZ" "LINESTRING" "sfg"
#>
#> [[8]]
#> [,1] [,2] [,3]
#> [1,] 2 1 1
#> attr(,"class")
#> [1] "XYZ" "LINESTRING" "sfg"
#>
#> [[9]]
#> [,1] [,2] [,3]
#> [1,] 3 3 2
#> attr(,"class")
#> [1] "XYZ" "LINESTRING" "sfg"
#>
#> [[10]]
#> [,1] [,2] [,3]
#> [1,] 4 3 2
#> attr(,"class")
#> [1] "XYZ" "LINESTRING" "sfg"
#>
#> [[11]]
#> [,1] [,2] [,3]
#> [1,] 4 4 2
#> attr(,"class")
#> [1] "XYZ" "LINESTRING" "sfg"
#>
#> [[12]]
#> [,1] [,2] [,3]
#> [1,] 3 4 2
#> attr(,"class")
#> [1] "XYZ" "LINESTRING" "sfg"
#>
#> attr(,"n_empty")
#> [1] 0
#> attr(,"crs")
#> $input
#> [1] NA
#>
#> $wkt
#> [1] NA
#>
#> attr(,"class")
#> [1] "crs"
#> attr(,"class")
#> [1] "sfc_LINESTRING" "sfc"
#> attr(,"precision")
#> [1] 0
#> attr(,"bbox")
#> xmin ymin xmax ymax
#> 0 0 4 4
#> attr(,"class")
#> [1] "bbox"
#> attr(,"z_range")
#> zmin zmax
#> 1 2
#> attr(,"class")
#> [1] "z_range"
sfc_cast( mpt, "POLYGON" )
#> [[1]]
#> [[1]]
#> [,1] [,2]
#> [1,] 0 0
#> [2,] 0 1
#> [3,] 1 1
#> [4,] 1 0
#> [5,] 1 1
#> [6,] 1 2
#> [7,] 2 2
#> [8,] 2 1
#> [9,] 0 0
#>
#> attr(,"class")
#> [1] "XY" "POLYGON" "sfg"
#>
#> [[2]]
#> [[1]]
#> [,1] [,2]
#> [1,] 3 3
#> [2,] 4 3
#> [3,] 4 4
#> [4,] 3 4
#> [5,] 3 3
#>
#> attr(,"class")
#> [1] "XY" "POLYGON" "sfg"
#>
#> attr(,"n_empty")
#> [1] 0
#> attr(,"crs")
#> $input
#> [1] NA
#>
#> $wkt
#> [1] NA
#>
#> attr(,"class")
#> [1] "crs"
#> attr(,"class")
#> [1] "sfc_POLYGON" "sfc"
#> attr(,"precision")
#> [1] 0
#> attr(,"bbox")
#> xmin ymin xmax ymax
#> 0 0 4 4
#> attr(,"class")
#> [1] "bbox"
sfc_cast( ls, "POINT" )
#> [[1]]
#> [,1] [,2]
#> [1,] 0 0
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[2]]
#> [,1] [,2]
#> [1,] 0 1
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[3]]
#> [,1] [,2]
#> [1,] 1 1
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[4]]
#> [,1] [,2]
#> [1,] 1 0
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[5]]
#> [,1] [,2]
#> [1,] 1 1
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[6]]
#> [,1] [,2]
#> [1,] 1 2
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[7]]
#> [,1] [,2]
#> [1,] 2 2
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[8]]
#> [,1] [,2]
#> [1,] 2 1
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[9]]
#> [,1] [,2]
#> [1,] 3 3
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[10]]
#> [,1] [,2]
#> [1,] 4 3
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[11]]
#> [,1] [,2]
#> [1,] 4 4
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[12]]
#> [,1] [,2]
#> [1,] 3 4
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> attr(,"n_empty")
#> [1] 0
#> attr(,"crs")
#> $input
#> [1] NA
#>
#> $wkt
#> [1] NA
#>
#> attr(,"class")
#> [1] "crs"
#> attr(,"class")
#> [1] "sfc_POINT" "sfc"
#> attr(,"precision")
#> [1] 0
#> attr(,"bbox")
#> xmin ymin xmax ymax
#> 0 0 4 4
#> attr(,"class")
#> [1] "bbox"
sfc_cast( mls, "MULTIPOLYGON" )
#> [[1]]
#> [[1]]
#> [[1]]
#> [,1] [,2]
#> [1,] 0 0
#> [2,] 0 1
#> [3,] 1 1
#> [4,] 1 0
#> [5,] 1 1
#> [6,] 1 2
#> [7,] 2 2
#> [8,] 2 1
#> [9,] 0 0
#>
#> attr(,"class")
#> [1] "XY" "MULTILINESTRING" "sfg"
#>
#> attr(,"class")
#> [1] "XY" "MULTIPOLYGON" "sfg"
#>
#> [[2]]
#> [[1]]
#> [[1]]
#> [,1] [,2]
#> [1,] 3 3
#> [2,] 4 3
#> [3,] 4 4
#> [4,] 3 4
#> [5,] 3 3
#>
#> attr(,"class")
#> [1] "XY" "MULTILINESTRING" "sfg"
#>
#> attr(,"class")
#> [1] "XY" "MULTIPOLYGON" "sfg"
#>
#> attr(,"n_empty")
#> [1] 0
#> attr(,"crs")
#> $input
#> [1] NA
#>
#> $wkt
#> [1] NA
#>
#> attr(,"class")
#> [1] "crs"
#> attr(,"class")
#> [1] "sfc_MULTIPOLYGON" "sfc"
#> attr(,"precision")
#> [1] 0
#> attr(,"bbox")
#> xmin ymin xmax ymax
#> 0 0 4 4
#> attr(,"class")
#> [1] "bbox"
sfc_cast( p, "POINT" )
#> [[1]]
#> [,1] [,2]
#> [1,] 0 0
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[2]]
#> [,1] [,2]
#> [1,] 0 1
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[3]]
#> [,1] [,2]
#> [1,] 1 1
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[4]]
#> [,1] [,2]
#> [1,] 1 0
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[5]]
#> [,1] [,2]
#> [1,] 1 1
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[6]]
#> [,1] [,2]
#> [1,] 1 2
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[7]]
#> [,1] [,2]
#> [1,] 2 2
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[8]]
#> [,1] [,2]
#> [1,] 2 1
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[9]]
#> [,1] [,2]
#> [1,] 3 3
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[10]]
#> [,1] [,2]
#> [1,] 4 3
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[11]]
#> [,1] [,2]
#> [1,] 4 4
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> [[12]]
#> [,1] [,2]
#> [1,] 3 4
#> attr(,"class")
#> [1] "XY" "POINT" "sfg"
#>
#> attr(,"n_empty")
#> [1] 0
#> attr(,"crs")
#> $input
#> [1] NA
#>
#> $wkt
#> [1] NA
#>
#> attr(,"class")
#> [1] "crs"
#> attr(,"class")
#> [1] "sfc_POINT" "sfc"
#> attr(,"precision")
#> [1] 0
#> attr(,"bbox")
#> xmin ymin xmax ymax
#> 0 0 4 4
#> attr(,"class")
#> [1] "bbox"
sfc_cast( mp, "LINESTRING" )
#> [[1]]
#> [,1] [,2]
#> [1,] 0 0
#> [2,] 0 1
#> [3,] 1 1
#> [4,] 1 0
#> attr(,"class")
#> [1] "XY" "LINESTRING" "sfg"
#>
#> [[2]]
#> [,1] [,2]
#> [1,] 1 1
#> [2,] 1 2
#> [3,] 2 2
#> [4,] 2 1
#> attr(,"class")
#> [1] "XY" "LINESTRING" "sfg"
#>
#> [[3]]
#> [,1] [,2]
#> [1,] 3 3
#> [2,] 4 3
#> [3,] 4 4
#> [4,] 3 4
#> attr(,"class")
#> [1] "XY" "LINESTRING" "sfg"
#>
#> attr(,"n_empty")
#> [1] 0
#> attr(,"crs")
#> $input
#> [1] NA
#>
#> $wkt
#> [1] NA
#>
#> attr(,"class")
#> [1] "crs"
#> attr(,"class")
#> [1] "sfc_LINESTRING" "sfc"
#> attr(,"precision")
#> [1] 0
#> attr(,"bbox")
#> xmin ymin xmax ymax
#> 0 0 4 4
#> attr(,"class")
#> [1] "bbox"