Examples for 'sf::st_cast'


Cast geometry to another type: either simplify, or cast explicitly

Aliases: st_cast st_cast.MULTIPOLYGON st_cast.MULTILINESTRING st_cast.MULTIPOINT st_cast.POLYGON st_cast.LINESTRING st_cast.POINT st_cast.GEOMETRYCOLLECTION st_cast.CIRCULARSTRING st_cast.MULTISURFACE st_cast.COMPOUNDCURVE st_cast.MULTICURVE st_cast.CURVE st_cast.sfc st_cast.sf st_cast.sfc_CIRCULARSTRING

Keywords:

### ** Examples

# example(st_read)
nc = st_read(system.file("shape/nc.shp", package="sf"))
Reading layer `nc' from data source 
  `/usr/local/R/4.2/site-library/sf/shape/nc.shp' using driver `ESRI Shapefile'
Simple feature collection with 100 features and 14 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
Geodetic CRS:  NAD27
mpl <- st_geometry(nc)[[4]]
#st_cast(x) ## error 'argument "to" is missing, with no default'
cast_all <- function(xg) {
  lapply(c("MULTIPOLYGON", "MULTILINESTRING", "MULTIPOINT", "POLYGON", "LINESTRING", "POINT"),
      function(x) st_cast(xg, x))
}
st_sfc(cast_all(mpl))
Warning in st_cast.MULTIPOLYGON(xg, x): polygon from first part only
Warning in st_cast.MULTIPOLYGON(xg, x): line from first ring only
Warning in st_cast.MULTIPOLYGON(xg, x): point from first coordinate only
Geometry set for 6 features 
Geometry type: GEOMETRY
Dimension:     XY
Bounding box:  xmin: -76.33025 ymin: 36.07282 xmax: -75.77316 ymax: 36.55716
CRS:           NA
First 5 geometries:
MULTIPOLYGON (((-76.00897 36.3196, -76.01735 36...
MULTILINESTRING ((-76.00897 36.3196, -76.01735 ...
MULTIPOINT ((-76.00897 36.3196), (-76.01735 36....
POLYGON ((-76.00897 36.3196, -76.01735 36.33773...
LINESTRING (-76.00897 36.3196, -76.01735 36.337...
## no closing coordinates should remain for multipoint
any(duplicated(unclass(st_cast(mpl, "MULTIPOINT"))))  ## should be FALSE
[1] FALSE
## number of duplicated coordinates in the linestrings should equal the number of polygon rings 
## (... in this case, won't always be true)
sum(duplicated(do.call(rbind, unclass(st_cast(mpl, "MULTILINESTRING"))))
     ) == sum(unlist(lapply(mpl, length)))  ## should be TRUE
[1] TRUE
p1 <- structure(c(0, 1, 3, 2, 1, 0, 0, 0, 2, 4, 4, 0), .Dim = c(6L, 2L))
p2 <- structure(c(1, 1, 2, 1, 1, 2, 2, 1), .Dim = c(4L, 2L))
st_polygon(list(p1, p2))
POLYGON ((0 0, 1 0, 3 2, 2 4, 1 4, 0 0), (1 1, 1 2, 2 2, 1 1))
mls <- st_cast(st_geometry(nc)[[4]], "MULTILINESTRING")
st_sfc(cast_all(mls))
Warning in st_cast.MULTILINESTRING(xg, x): keeping first linestring only
Warning in st_cast.MULTILINESTRING(xg, x): keeping first coordinate only
Geometry set for 6 features 
Geometry type: GEOMETRY
Dimension:     XY
Bounding box:  xmin: -76.33025 ymin: 36.07282 xmax: -75.77316 ymax: 36.55716
CRS:           NA
First 5 geometries:
MULTIPOLYGON (((-76.00897 36.3196, -76.01735 36...
MULTILINESTRING ((-76.00897 36.3196, -76.01735 ...
MULTIPOINT ((-76.00897 36.3196), (-76.01735 36....
POLYGON ((-76.00897 36.3196, -76.01735 36.33773...
LINESTRING (-76.00897 36.3196, -76.01735 36.337...
mpt <- st_cast(st_geometry(nc)[[4]], "MULTIPOINT")
st_sfc(cast_all(mpt))
Warning in st_cast.MULTIPOINT(xg, x): point from first coordinate only
Geometry set for 6 features 
Geometry type: GEOMETRY
Dimension:     XY
Bounding box:  xmin: -76.33025 ymin: 36.07282 xmax: -75.77316 ymax: 36.55716
CRS:           NA
First 5 geometries:
MULTIPOLYGON (((-76.00897 36.3196, -76.01735 36...
MULTILINESTRING ((-76.00897 36.3196, -76.01735 ...
MULTIPOINT ((-76.00897 36.3196), (-76.01735 36....
POLYGON ((-76.00897 36.3196, -76.01735 36.33773...
LINESTRING (-76.00897 36.3196, -76.01735 36.337...
pl <- st_cast(st_geometry(nc)[[4]], "POLYGON")
Warning in st_cast.MULTIPOLYGON(st_geometry(nc)[[4]], "POLYGON"): polygon from
first part only
st_sfc(cast_all(pl))
Warning in st_cast.POLYGON(xg, x): point from first coordinate only
Geometry set for 6 features 
Geometry type: GEOMETRY
Dimension:     XY
Bounding box:  xmin: -76.33025 ymin: 36.07282 xmax: -75.79885 ymax: 36.55716
CRS:           NA
First 5 geometries:
MULTIPOLYGON (((-76.00897 36.3196, -76.01735 36...
MULTILINESTRING ((-76.00897 36.3196, -76.01735 ...
MULTIPOINT ((-76.00897 36.3196), (-76.01735 36....
POLYGON ((-76.00897 36.3196, -76.01735 36.33773...
LINESTRING (-76.00897 36.3196, -76.01735 36.337...
ls <- st_cast(st_geometry(nc)[[4]], "LINESTRING")
Warning in st_cast.MULTIPOLYGON(st_geometry(nc)[[4]], "LINESTRING"): line from
first ring only
st_sfc(cast_all(ls))
Warning in st_cast.LINESTRING(xg, x): point from first coordinate only
Geometry set for 6 features 
Geometry type: GEOMETRY
Dimension:     XY
Bounding box:  xmin: -76.33025 ymin: 36.07282 xmax: -75.79885 ymax: 36.55716
CRS:           NA
First 5 geometries:
MULTIPOLYGON (((-76.00897 36.3196, -76.01735 36...
MULTILINESTRING ((-76.00897 36.3196, -76.01735 ...
MULTIPOINT ((-76.00897 36.3196), (-76.01735 36....
POLYGON ((-76.00897 36.3196, -76.01735 36.33773...
LINESTRING (-76.00897 36.3196, -76.01735 36.337...
pt <- st_cast(st_geometry(nc)[[4]], "POINT")
Warning in st_cast.MULTIPOLYGON(st_geometry(nc)[[4]], "POINT"): point from
first coordinate only
## st_sfc(cast_all(pt))  ## Error: cannot create MULTIPOLYGON from POINT 
st_sfc(lapply(c("POINT", "MULTIPOINT"), function(x) st_cast(pt, x)))
Geometry set for 2 features 
Geometry type: GEOMETRY
Dimension:     XY
Bounding box:  xmin: -76.00897 ymin: 36.3196 xmax: -76.00897 ymax: 36.3196
CRS:           NA
POINT (-76.00897 36.3196)
MULTIPOINT ((-76.00897 36.3196))
s = st_multipoint(rbind(c(1,0)))
st_cast(s, "POINT")
Warning in st_cast.MULTIPOINT(s, "POINT"): point from first coordinate only
POINT (1 0)
# https://github.com/r-spatial/sf/issues/1930:
pt1 <- st_point(c(0,1))
pt23 <- st_multipoint(matrix(c(1,2,3,4), ncol = 2, byrow = TRUE))
d <- st_sf(geom = st_sfc(pt1, pt23))
st_cast(d, "POINT") # will not convert the entire MULTIPOINT, and warns
Warning in st_cast.MULTIPOINT(X[[i]], ...): point from first coordinate only
Simple feature collection with 2 features and 0 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 0 ymin: 1 xmax: 1 ymax: 2
CRS:           NA
         geom
1 POINT (0 1)
2 POINT (1 2)
st_cast(d, "MULTIPOINT") %>% st_cast("POINT")
Simple feature collection with 3 features and 0 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 0 ymin: 1 xmax: 3 ymax: 4
CRS:           NA
         geom
1 POINT (0 1)
2 POINT (1 2)
3 POINT (3 4)

[Package sf version 1.0-19 Index]