st_cast: Cast geometry to another type: either simplify, or cast...

Description Usage Arguments Details Value Examples

View source: R/cast_sfc.R

Description

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

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
## S3 method for class 'MULTIPOLYGON'
st_cast(x, to, ...)

## S3 method for class 'MULTILINESTRING'
st_cast(x, to, ...)

## S3 method for class 'MULTIPOINT'
st_cast(x, to, ...)

## S3 method for class 'POLYGON'
st_cast(x, to, ...)

## S3 method for class 'LINESTRING'
st_cast(x, to, ...)

## S3 method for class 'POINT'
st_cast(x, to, ...)

## S3 method for class 'GEOMETRYCOLLECTION'
st_cast(x, to, ...)

## S3 method for class 'CIRCULARSTRING'
st_cast(x, to, ...)

## S3 method for class 'MULTISURFACE'
st_cast(x, to, ...)

## S3 method for class 'COMPOUNDCURVE'
st_cast(x, to, ...)

## S3 method for class 'MULTICURVE'
st_cast(x, to, ...)

## S3 method for class 'CURVE'
st_cast(x, to, ...)

st_cast(x, to, ...)

## S3 method for class 'sfc'
st_cast(x, to, ..., ids = seq_along(x), group_or_split = TRUE)

## S3 method for class 'sf'
st_cast(x, to, ..., warn = TRUE, do_split = TRUE)

## S3 method for class 'sfc_CIRCULARSTRING'
st_cast(x, to, ...)

Arguments

x

object of class sfg, sfc or sf

to

character; target type, if missing, simplification is tried; when x is of type sfg (i.e., a single geometry) then to needs to be specified.

...

ignored

ids

integer vector, denoting how geometries should be grouped (default: no grouping)

group_or_split

logical; if TRUE, group or split geometries; if FALSE, carry out a 1-1 per-geometry conversion.

warn

logical; if TRUE, warn if attributes are assigned to sub-geometries

do_split

logical; if TRUE, allow splitting of geometries in sub-geometries

Details

When converting a GEOMETRYCOLLECTION to COMPOUNDCURVE, MULTISURFACE or CURVEPOLYGON, the user is responsible for the validity of the resulting object: no checks are being carried out by the software.

the st_cast method for sf objects can only split geometries, e.g. cast MULTIPOINT into multiple POINT features. In case of splitting, attributes are repeated and a warning is issued when non-constant attributes are assigned to sub-geometries. To merge feature geometries and attribute values, use aggregate or summarise.

Value

object of class to if successful, or unmodified object if unsuccessful. If information gets lost while type casting, a warning is raised.

In case to is missing, st_cast.sfc will coerce combinations of "POINT" and "MULTIPOINT", "LINESTRING" and "MULTILINESTRING", "POLYGON" and "MULTIPOLYGON" into their "MULTI..." form, or in case all geometries are "GEOMETRYCOLLECTION" will return a list of all the contents of the "GEOMETRYCOLLECTION" objects, or else do nothing. In case to is specified, if to is "GEOMETRY", geometries are not converted, else, st_cast will try to coerce all elements into to; ids may be specified to group e.g. "POINT" objects into a "MULTIPOINT", if not specified no grouping takes place. If e.g. a "sfc_MULTIPOINT" is cast to a "sfc_POINT", the objects are split, so no information gets lost, unless group_or_split is FALSE.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# example(st_read)
nc = st_read(system.file("shape/nc.shp", package="sf"))
mpl <- nc$geometry[[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))
## no closing coordinates should remain for multipoint
any(duplicated(unclass(st_cast(mpl, "MULTIPOINT"))))  ## should be 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

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))
mls <- st_cast(nc$geometry[[4]], "MULTILINESTRING")
st_sfc(cast_all(mls))
mpt <- st_cast(nc$geometry[[4]], "MULTIPOINT")
st_sfc(cast_all(mpt))
pl <- st_cast(nc$geometry[[4]], "POLYGON")
st_sfc(cast_all(pl))
ls <- st_cast(nc$geometry[[4]], "LINESTRING")
st_sfc(cast_all(ls))
pt <- st_cast(nc$geometry[[4]], "POINT")
## st_sfc(cast_all(pt))  ## Error: cannot create MULTIPOLYGON from POINT 
st_sfc(lapply(c("POINT", "MULTIPOINT"), function(x) st_cast(pt, x)))
s = st_multipoint(rbind(c(1,0)))
st_cast(s, "POINT")

Example output

Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
Reading layer `nc' from data source `/usr/lib/R/site-library/sf/shape/nc.shp' using driver `ESRI Shapefile'
Simple feature collection with 100 features and 14 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
geographic CRS: NAD27
Geometry set for 6 features 
geometry type:  GEOMETRY
dimension:      XY
bbox:           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...
Warning messages:
1: In st_cast.MULTIPOLYGON(xg, x) : polygon from first part only
2: In st_cast.MULTIPOLYGON(xg, x) : line from first ring only
3: In st_cast.MULTIPOLYGON(xg, x) : point from first coordinate only
[1] FALSE
[1] TRUE
POLYGON ((0 0, 1 0, 3 2, 2 4, 1 4, 0 0), (1 1, 1 2, 2 2, 1 1))
Geometry set for 6 features 
geometry type:  GEOMETRY
dimension:      XY
bbox:           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...
Warning messages:
1: In st_cast.MULTILINESTRING(xg, x) : keeping first linestring only
2: In st_cast.MULTILINESTRING(xg, x) : keeping first coordinate only
Geometry set for 6 features 
geometry type:  GEOMETRY
dimension:      XY
bbox:           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...
Warning message:
In st_cast.MULTIPOINT(xg, x) : point from first coordinate only
Warning message:
In st_cast.MULTIPOLYGON(nc$geometry[[4]], "POLYGON") :
  polygon from first part only
Geometry set for 6 features 
geometry type:  GEOMETRY
dimension:      XY
bbox:           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...
Warning message:
In st_cast.POLYGON(xg, x) : point from first coordinate only
Warning message:
In st_cast.MULTIPOLYGON(nc$geometry[[4]], "LINESTRING") :
  line from first ring only
Geometry set for 6 features 
geometry type:  GEOMETRY
dimension:      XY
bbox:           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...
Warning message:
In st_cast.LINESTRING(xg, x) : point from first coordinate only
Warning message:
In st_cast.MULTIPOLYGON(nc$geometry[[4]], "POINT") :
  point from first coordinate only
Geometry set for 2 features 
geometry type:  GEOMETRY
dimension:      XY
bbox:           xmin: -76.00897 ymin: 36.3196 xmax: -76.00897 ymax: 36.3196
CRS:            NA
POINT (-76.00897 36.3196)
MULTIPOINT ((-76.00897 36.3196))
POINT (1 0)
Warning message:
In st_cast.MULTIPOINT(s, "POINT") : point from first coordinate only

sf documentation built on June 10, 2021, 1:06 a.m.