R Under development (unstable) (2023-12-13 r85679 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > suppressPackageStartupMessages(library(sf)) > library(testthat) > # "vertical" conversions: > # column 1: > mp = st_sfc(st_multipoint(matrix(0:3,,2)), st_multipoint(matrix(10:15,,2))) > (ls = st_cast(mp, "LINESTRING")) Geometry set for 2 features Geometry type: LINESTRING Dimension: XY Bounding box: xmin: 0 ymin: 2 xmax: 12 ymax: 15 CRS: NA LINESTRING (0 2, 1 3) LINESTRING (10 13, 11 14, 12 15) > st_cast(ls, "MULTIPOINT") Geometry set for 2 features Geometry type: MULTIPOINT Dimension: XY Bounding box: xmin: 0 ymin: 2 xmax: 12 ymax: 15 CRS: NA MULTIPOINT ((0 2), (1 3)) MULTIPOINT ((10 13), (11 14), (12 15)) > > # column 2: > mls = st_sfc(st_multilinestring(list(rbind(c(0,0), c(10,0), c(10,10), c(0,10)), + rbind(c(5,5),c(5,6), c(6,6), c(6,5)))), + st_multilinestring(list(rbind(c(0,0), c(1,0), c(1,1), c(0,1))))) > (pol = st_cast(mls, "POLYGON")) Geometry set for 2 features Geometry type: POLYGON Dimension: XY Bounding box: xmin: 0 ymin: 0 xmax: 10 ymax: 10 CRS: NA POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0), (5 5, 5... POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)) > st_cast(pol, "MULTILINESTRING") Geometry set for 2 features Geometry type: MULTILINESTRING Dimension: XY Bounding box: xmin: 0 ymin: 0 xmax: 10 ymax: 10 CRS: NA MULTILINESTRING ((0 0, 10 0, 10 10, 0 10, 0 0),... MULTILINESTRING ((0 0, 1 0, 1 1, 0 1, 0 0)) > > # "horizontal" conversions: > > (pt = st_cast(mp, "POINT")) Geometry set for 5 features Geometry type: POINT Dimension: XY Bounding box: xmin: 0 ymin: 2 xmax: 12 ymax: 15 CRS: NA POINT (0 2) POINT (1 3) POINT (10 13) POINT (11 14) POINT (12 15) > (i = attr(pt, "ids")) [1] 2 3 > (xx = st_cast(pt, "MULTIPOINT", rep(seq_along(i), i))) Geometry set for 5 features Geometry type: MULTIPOINT Dimension: XY Bounding box: xmin: 0 ymin: 2 xmax: 12 ymax: 15 CRS: NA MULTIPOINT ((0 2)) MULTIPOINT ((1 3)) MULTIPOINT ((10 13)) MULTIPOINT ((11 14)) MULTIPOINT ((12 15)) > try(yy <- st_cast(pt, "LINESTRING", rep(seq_along(i), i))) > > #(zz = st_cast(yy, "MULTILINESTRING")) > #(zz = st_cast(yy, "POLYGON")) > > st_cast(mls, "LINESTRING") Geometry set for 3 features Geometry type: LINESTRING Dimension: XY Bounding box: xmin: 0 ymin: 0 xmax: 10 ymax: 10 CRS: NA LINESTRING (0 0, 10 0, 10 10, 0 10) LINESTRING (5 5, 5 6, 6 6, 6 5) LINESTRING (0 0, 1 0, 1 1, 0 1) > > (g = st_sfc(c(mls, ls))) Geometry set for 4 features Geometry type: GEOMETRY Dimension: XY Bounding box: xmin: 0 ymin: 0 xmax: 12 ymax: 15 CRS: NA MULTILINESTRING ((0 0, 10 0, 10 10, 0 10), (5 5... MULTILINESTRING ((0 0, 1 0, 1 1, 0 1)) LINESTRING (0 2, 1 3) LINESTRING (10 13, 11 14, 12 15) > st_cast(g, "MULTILINESTRING") Geometry set for 4 features Geometry type: MULTILINESTRING Dimension: XY Bounding box: xmin: 0 ymin: 0 xmax: 12 ymax: 15 CRS: NA MULTILINESTRING ((0 0, 10 0, 10 10, 0 10), (5 5... MULTILINESTRING ((0 0, 1 0, 1 1, 0 1)) MULTILINESTRING ((0 2, 1 3)) MULTILINESTRING ((10 13, 11 14, 12 15)) > expect_warning(st_cast(g, "LINESTRING")) > st_cast(st_cast(g, "MULTILINESTRING"), "LINESTRING") # will not loose Geometry set for 5 features Geometry type: LINESTRING Dimension: XY Bounding box: xmin: 0 ymin: 0 xmax: 12 ymax: 15 CRS: NA LINESTRING (0 0, 10 0, 10 10, 0 10) LINESTRING (5 5, 5 6, 6 6, 6 5) LINESTRING (0 0, 1 0, 1 1, 0 1) LINESTRING (0 2, 1 3) LINESTRING (10 13, 11 14, 12 15) > > gc = st_sfc(st_geometrycollection( + list( + st_multilinestring(list(rbind(c(0,0), c(10,0), c(10,10), c(0,10)), + rbind(c(5,5),c(5,6), c(6,6), c(6,5)))), + st_multilinestring(list(rbind(c(0,0), c(1,0), c(1,1), c(0,1)))), + st_point(0:1) + ))) > try(st_cast(mls, "POINT")) Geometry set for 12 features Geometry type: POINT Dimension: XY Bounding box: xmin: 0 ymin: 0 xmax: 10 ymax: 10 CRS: NA First 5 geometries: POINT (0 0) POINT (10 0) POINT (10 10) POINT (0 10) POINT (5 5) > try(st_cast(mls, "MULTIPOINT")) Geometry set for 3 features Geometry type: MULTIPOINT Dimension: XY Bounding box: xmin: 0 ymin: 0 xmax: 10 ymax: 10 CRS: NA MULTIPOINT ((0 0), (10 0), (10 10), (0 10)) MULTIPOINT ((5 5), (5 6), (6 6), (6 5)) MULTIPOINT ((0 0), (1 0), (1 1), (0 1)) > > outer = matrix(c(0,0,10,0,10,10,0,10,0,0),ncol=2, byrow=TRUE) > hole1 = matrix(c(1,1,1,2,2,2,2,1,1,1),ncol=2, byrow=TRUE) > hole2 = matrix(c(5,5,5,6,6,6,6,5,5,5),ncol=2, byrow=TRUE) > pol1 = list(outer, hole1, hole2) > pol2 = list(outer + 12, hole1 + 12) > pol3 = list(outer + 24) > mp = list(pol1,pol2,pol3) > mp1 = st_multipolygon(mp) > s = st_sfc(mp1) > x = st_cast(s, "MULTIPOINT") > x = st_cast(s, "POINT") > expect_warning(st_cast(mp1, "LINESTRING")) > expect_warning(st_cast(mp1, "POINT")) > mls = mls[[1]] > class(mls) [1] "XY" "MULTILINESTRING" "sfg" > #expect_error(st_cast(mls, "POLYGON")) > st_cast(mls, "POLYGON") POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0), (5 5, 5 6, 6 6, 6 5, 5 5)) > > expect_warning(st_cast(mls, "POINT")) > p1 = st_polygon(pol1) > expect_warning(st_cast(p1, "POINT")) > ls = ls[[1]] > class(ls) [1] "XY" "LINESTRING" "sfg" > expect_warning(st_cast(ls, "POINT")) > > mls = st_cast(p1, "MULTILINESTRING") > p2 = st_cast(mls, "POLYGON") > > # st_is: > st_is(st_point(0:1), "POINT") [1] TRUE > sfc = st_sfc(st_point(0:1), st_linestring(matrix(1:6,,2))) > st_is(sfc, "POINT") [1] TRUE FALSE > st_is(sfc, "POLYGON") [1] FALSE FALSE > st_is(sfc, "LINESTRING") [1] FALSE TRUE > st_is(st_sf(a = 1:2, sfc), "LINESTRING") [1] FALSE TRUE > st_is(sfc, c("POINT", "LINESTRING")) [1] TRUE TRUE > > #1194: > wkt <- "MULTICURVE (COMPOUNDCURVE (LINESTRING (-83.62333 35.55244, -83.62328 35.55232, -83.62323 35.55223, -83.62319 35.55216, -83.62312 35.55209, -83.6231 35.55207), CIRCULARSTRING (-83.6231 35.55207, -83.62307 35.55205, -83.62302 35.55204), LINESTRING (-83.62302 35.55204, -83.62299 35.55203, -83.62289 35.55198, -83.62281 35.55189, -83.62271 35.55182)))" > g <- st_as_sfc(wkt) > g <- st_sf(demo = "test", geom = g, crs = 4326) > m = st_cast(g, "MULTILINESTRING") > identical(m$geom[[1]], st_cast(g$geom[[1]], "MULTILINESTRING")) [1] TRUE > > st_cast(st_sfc(st_geometrycollection(), st_multipolygon()), 'MULTIPOLYGON') #1961 Geometry set for 2 features (with 2 geometries empty) Geometry type: MULTIPOLYGON Dimension: XY Bounding box: xmin: NA ymin: NA xmax: NA ymax: NA CRS: NA MULTIPOLYGON EMPTY MULTIPOLYGON EMPTY > st_cast(st_sfc(st_geometrycollection(), st_multipolygon(), st_point(0:1)), 'POINT') #1961 Geometry set for 3 features (with 2 geometries empty) Geometry type: POINT Dimension: XY Bounding box: xmin: 0 ymin: 1 xmax: 0 ymax: 1 CRS: NA POINT EMPTY POINT EMPTY POINT (0 1) > > proc.time() user system elapsed 2.20 0.23 2.42