EVOLUTION-MANAGER
Edit File: st_cast.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Cast geometry to another type: either simplify, or cast...</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for st_cast {sf}"><tr><td>st_cast {sf}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Cast geometry to another type: either simplify, or cast explicitly</h2> <h3>Description</h3> <p>Cast geometry to another type: either simplify, or cast explicitly </p> <h3>Usage</h3> <pre> ## 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, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>object of class <code>sfg</code>, <code>sfc</code> or <code>sf</code></p> </td></tr> <tr valign="top"><td><code>to</code></td> <td> <p>character; target type, if missing, simplification is tried; when <code>x</code> is of type <code>sfg</code> (i.e., a single geometry) then <code>to</code> needs to be specified.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>ignored</p> </td></tr> <tr valign="top"><td><code>ids</code></td> <td> <p>integer vector, denoting how geometries should be grouped (default: no grouping)</p> </td></tr> <tr valign="top"><td><code>group_or_split</code></td> <td> <p>logical; if TRUE, group or split geometries; if FALSE, carry out a 1-1 per-geometry conversion.</p> </td></tr> <tr valign="top"><td><code>warn</code></td> <td> <p>logical; if <code>TRUE</code>, warn if attributes are assigned to sub-geometries</p> </td></tr> <tr valign="top"><td><code>do_split</code></td> <td> <p>logical; if <code>TRUE</code>, allow splitting of geometries in sub-geometries</p> </td></tr> </table> <h3>Details</h3> <p>the <code>st_cast</code> method for <code>sf</code> objects can only split geometries, e.g. cast <code>MULTIPOINT</code> into multiple <code>POINT</code> 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 <a href="aggregate.sf.html">aggregate</a> or <a href="tidyverse.html">summarise</a>. </p> <h3>Value</h3> <p>object of class <code>to</code> if successful, or unmodified object if unsuccessful. If information gets lost while type casting, a warning is raised. </p> <p>In case <code>to</code> is missing, <code>st_cast.sfc</code> 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 <code>to</code> is specified, if <code>to</code> is "GEOMETRY", geometries are not converted, else, <code>st_cast</code> will try to coerce all elements into <code>to</code>; <code>ids</code> 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 <code>group_or_split</code> is <code>FALSE</code>. </p> <h3>Examples</h3> <pre> # 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") </pre> <hr /><div style="text-align: center;">[Package <em>sf</em> version 0.9-5 <a href="00Index.html">Index</a>]</div> </body></html>