Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: multiple casts in to argument #2233

Open
JosiahParry opened this issue Sep 12, 2023 · 1 comment
Open

Feature Request: multiple casts in to argument #2233

JosiahParry opened this issue Sep 12, 2023 · 1 comment
Labels
feature a feature request or enhancement

Comments

@JosiahParry
Copy link
Contributor

It is common that I will want to expand a multipolygon into its component linestrings. To do this I will have to cast the geometry multiple times. For example:

library(sf)
nc_path <- system.file("shape/nc.shp", package = "sf")

nc <- st_read(nc_path, quiet = TRUE) |> 
  st_geometry()

nc |> 
  st_cast("MULTILINESTRING") |> 
  st_cast("LINESTRING")
#> Geometry set for 108 features 
#> Geometry type: LINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS:  NAD27
#> First 5 geometries:
#> LINESTRING (-81.47276 36.23436, -81.54084 36.27...
#> LINESTRING (-81.23989 36.36536, -81.24069 36.37...
#> LINESTRING (-80.45634 36.24256, -80.47639 36.25...
#> LINESTRING (-76.00897 36.3196, -76.01735 36.337...
#> LINESTRING (-76.02717 36.55672, -75.99866 36.55...

It would be a quite nice experience to be able to provide a character vector of length 2, 3, etc to specify the casts in order. For example it would be nice to be able to rewrite the above as:

nc |> 
  st_cast(c("MULTILINESTRING", "LINESTRING"))

Created on 2023-09-12 with reprex v2.0.2

@edzer edzer added the feature a feature request or enhancement label Sep 13, 2023
@dcooley
Copy link
Contributor

dcooley commented Oct 24, 2023

In the absence of a native function, sfheaders::sf_cast() will do this

nc <- st_read(nc_path, quiet = TRUE) |> 
  sfheaders::sf_cast(to = "LINESTRING")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants