1
2R version 4.0.4 (2021-02-15) -- "Lost Library Book"
3Copyright (C) 2021 The R Foundation for Statistical Computing
4Platform: x86_64-pc-linux-gnu (64-bit)
5
6R is free software and comes with ABSOLUTELY NO WARRANTY.
7You are welcome to redistribute it under certain conditions.
8Type 'license()' or 'licence()' for distribution details.
9
10R is a collaborative project with many contributors.
11Type 'contributors()' for more information and
12'citation()' on how to cite R or R packages in publications.
13
14Type 'demo()' for some demos, 'help()' for on-line help, or
15'help.start()' for an HTML browser interface to help.
16Type 'q()' to quit R.
17
18> suppressPackageStartupMessages(library(sf))
19> library(testthat)
20> # "vertical" conversions:
21> # column 1:
22> mp = st_sfc(st_multipoint(matrix(0:3,,2)), st_multipoint(matrix(10:15,,2)))
23> (ls = st_cast(mp, "LINESTRING"))
24Geometry set for 2 features
25Geometry type: LINESTRING
26Dimension:     XY
27Bounding box:  xmin: 0 ymin: 2 xmax: 12 ymax: 15
28CRS:           NA
29LINESTRING (0 2, 1 3)
30LINESTRING (10 13, 11 14, 12 15)
31> st_cast(ls, "MULTIPOINT")
32Geometry set for 2 features
33Geometry type: MULTIPOINT
34Dimension:     XY
35Bounding box:  xmin: 0 ymin: 2 xmax: 12 ymax: 15
36CRS:           NA
37MULTIPOINT ((0 2), (1 3))
38MULTIPOINT ((10 13), (11 14), (12 15))
39>
40> # column 2:
41> mls = st_sfc(st_multilinestring(list(rbind(c(0,0), c(10,0), c(10,10), c(0,10)),
42+ 	rbind(c(5,5),c(5,6), c(6,6), c(6,5)))),
43+ 	st_multilinestring(list(rbind(c(0,0), c(1,0), c(1,1), c(0,1)))))
44> (pol = st_cast(mls, "POLYGON"))
45Geometry set for 2 features
46Geometry type: POLYGON
47Dimension:     XY
48Bounding box:  xmin: 0 ymin: 0 xmax: 10 ymax: 10
49CRS:           NA
50POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0), (5 5, 5...
51POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))
52> st_cast(pol, "MULTILINESTRING")
53Geometry set for 2 features
54Geometry type: MULTILINESTRING
55Dimension:     XY
56Bounding box:  xmin: 0 ymin: 0 xmax: 10 ymax: 10
57CRS:           NA
58MULTILINESTRING ((0 0, 10 0, 10 10, 0 10, 0 0),...
59MULTILINESTRING ((0 0, 1 0, 1 1, 0 1, 0 0))
60>
61> # "horizontal" conversions:
62>
63> (pt = st_cast(mp, "POINT"))
64Geometry set for 5 features
65Geometry type: POINT
66Dimension:     XY
67Bounding box:  xmin: 0 ymin: 2 xmax: 12 ymax: 15
68CRS:           NA
69POINT (0 2)
70POINT (1 3)
71POINT (10 13)
72POINT (11 14)
73POINT (12 15)
74> (i = attr(pt, "ids"))
75[1] 2 3
76> (xx = st_cast(pt, "MULTIPOINT", rep(seq_along(i), i)))
77Geometry set for 5 features
78Geometry type: MULTIPOINT
79Dimension:     XY
80Bounding box:  xmin: 0 ymin: 2 xmax: 12 ymax: 15
81CRS:           NA
82MULTIPOINT ((0 2))
83MULTIPOINT ((1 3))
84MULTIPOINT ((10 13))
85MULTIPOINT ((11 14))
86MULTIPOINT ((12 15))
87> (yy = st_cast(pt, "LINESTRING", rep(seq_along(i), i)))
88Geometry set for 5 features
89Geometry type: LINESTRING
90Dimension:     XY
91Bounding box:  xmin: 0 ymin: 2 xmax: 12 ymax: 15
92CRS:           NA
93LINESTRING (0 2)
94LINESTRING (1 3)
95LINESTRING (10 13)
96LINESTRING (11 14)
97LINESTRING (12 15)
98>
99> (zz = st_cast(yy, "MULTILINESTRING"))
100Geometry set for 5 features
101Geometry type: MULTILINESTRING
102Dimension:     XY
103Bounding box:  xmin: 0 ymin: 2 xmax: 12 ymax: 15
104CRS:           NA
105MULTILINESTRING ((0 2))
106MULTILINESTRING ((1 3))
107MULTILINESTRING ((10 13))
108MULTILINESTRING ((11 14))
109MULTILINESTRING ((12 15))
110> #(zz = st_cast(yy, "POLYGON"))
111>
112> st_cast(mls, "LINESTRING")
113Geometry set for 3 features
114Geometry type: LINESTRING
115Dimension:     XY
116Bounding box:  xmin: 0 ymin: 0 xmax: 10 ymax: 10
117CRS:           NA
118LINESTRING (0 0, 10 0, 10 10, 0 10)
119LINESTRING (5 5, 5 6, 6 6, 6 5)
120LINESTRING (0 0, 1 0, 1 1, 0 1)
121>
122> (g = st_sfc(c(mls, ls)))
123Geometry set for 4 features
124Geometry type: GEOMETRY
125Dimension:     XY
126Bounding box:  xmin: 0 ymin: 0 xmax: 12 ymax: 15
127CRS:           NA
128MULTILINESTRING ((0 0, 10 0, 10 10, 0 10), (5 5...
129MULTILINESTRING ((0 0, 1 0, 1 1, 0 1))
130LINESTRING (0 2, 1 3)
131LINESTRING (10 13, 11 14, 12 15)
132> st_cast(g, "MULTILINESTRING")
133Geometry set for 4 features
134Geometry type: MULTILINESTRING
135Dimension:     XY
136Bounding box:  xmin: 0 ymin: 0 xmax: 12 ymax: 15
137CRS:           NA
138MULTILINESTRING ((0 0, 10 0, 10 10, 0 10), (5 5...
139MULTILINESTRING ((0 0, 1 0, 1 1, 0 1))
140MULTILINESTRING ((0 2, 1 3))
141MULTILINESTRING ((10 13, 11 14, 12 15))
142> expect_warning(st_cast(g, "LINESTRING"))
143> st_cast(st_cast(g, "MULTILINESTRING"), "LINESTRING") # will not loose
144Geometry set for 5 features
145Geometry type: LINESTRING
146Dimension:     XY
147Bounding box:  xmin: 0 ymin: 0 xmax: 12 ymax: 15
148CRS:           NA
149LINESTRING (0 0, 10 0, 10 10, 0 10)
150LINESTRING (5 5, 5 6, 6 6, 6 5)
151LINESTRING (0 0, 1 0, 1 1, 0 1)
152LINESTRING (0 2, 1 3)
153LINESTRING (10 13, 11 14, 12 15)
154>
155> gc = st_sfc(st_geometrycollection(
156+   list(
157+     st_multilinestring(list(rbind(c(0,0), c(10,0), c(10,10), c(0,10)),
158+ 	rbind(c(5,5),c(5,6), c(6,6), c(6,5)))),
159+ 	st_multilinestring(list(rbind(c(0,0), c(1,0), c(1,1), c(0,1)))),
160+ 	st_point(0:1)
161+   )))
162> try(st_cast(mls, "POINT"))
163Geometry set for 12 features
164Geometry type: POINT
165Dimension:     XY
166Bounding box:  xmin: 0 ymin: 0 xmax: 10 ymax: 10
167CRS:           NA
168First 5 geometries:
169POINT (0 0)
170POINT (10 0)
171POINT (10 10)
172POINT (0 10)
173POINT (5 5)
174> try(st_cast(mls, "MULTIPOINT"))
175Geometry set for 3 features
176Geometry type: MULTIPOINT
177Dimension:     XY
178Bounding box:  xmin: 0 ymin: 0 xmax: 10 ymax: 10
179CRS:           NA
180MULTIPOINT ((0 0), (10 0), (10 10), (0 10))
181MULTIPOINT ((5 5), (5 6), (6 6), (6 5))
182MULTIPOINT ((0 0), (1 0), (1 1), (0 1))
183>
184> outer = matrix(c(0,0,10,0,10,10,0,10,0,0),ncol=2, byrow=TRUE)
185> hole1 = matrix(c(1,1,1,2,2,2,2,1,1,1),ncol=2, byrow=TRUE)
186> hole2 = matrix(c(5,5,5,6,6,6,6,5,5,5),ncol=2, byrow=TRUE)
187> pol1 = list(outer, hole1, hole2)
188> pol2 = list(outer + 12, hole1 + 12)
189> pol3 = list(outer + 24)
190> mp = list(pol1,pol2,pol3)
191> mp1 = st_multipolygon(mp)
192> s = st_sfc(mp1)
193> x = st_cast(s, "MULTIPOINT")
194> x = st_cast(s, "POINT")
195> expect_warning(st_cast(mp1, "LINESTRING"))
196> expect_warning(st_cast(mp1, "POINT"))
197> mls = mls[[1]]
198> class(mls)
199[1] "XY"              "MULTILINESTRING" "sfg"
200> #expect_error(st_cast(mls, "POLYGON"))
201> st_cast(mls, "POLYGON")
202POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0), (5 5, 5 6, 6 6, 6 5, 5 5))
203>
204> expect_warning(st_cast(mls, "POINT"))
205> p1 = st_polygon(pol1)
206> expect_warning(st_cast(p1, "POINT"))
207> ls = ls[[1]]
208> class(ls)
209[1] "XY"         "LINESTRING" "sfg"
210> expect_warning(st_cast(ls, "POINT"))
211>
212> mls = st_cast(p1, "MULTILINESTRING")
213> p2 = st_cast(mls, "POLYGON")
214>
215> # st_is:
216> st_is(st_point(0:1), "POINT")
217[1] TRUE
218> sfc = st_sfc(st_point(0:1), st_linestring(matrix(1:6,,2)))
219> st_is(sfc, "POINT")
220[1]  TRUE FALSE
221> st_is(sfc, "POLYGON")
222[1] FALSE FALSE
223> st_is(sfc, "LINESTRING")
224[1] FALSE  TRUE
225> st_is(st_sf(a = 1:2, sfc), "LINESTRING")
226[1] FALSE  TRUE
227> st_is(sfc, c("POINT", "LINESTRING"))
228[1] TRUE TRUE
229>
230> #1194:
231> 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)))"
232> g <- st_as_sfc(wkt)
233> g <- st_sf(demo = "test", geom = g, crs = 4326)
234> m = st_cast(g, "MULTILINESTRING")
235> identical(m$geom[[1]], st_cast(g$geom[[1]], "MULTILINESTRING"))
236[1] TRUE
237>
238> proc.time()
239   user  system elapsed
240  0.937   0.044   0.974
241