File: cast.Rout.save

package info (click to toggle)
r-cran-sf 0.9-7%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,796 kB
  • sloc: cpp: 5,333; sh: 18; makefile: 2
file content (240 lines) | stat: -rw-r--r-- 7,439 bytes parent folder | download
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240

R version 3.6.2 (2019-12-12) -- "Dark and Stormy Night"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

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
bbox:           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
bbox:           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
bbox:           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
bbox:           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
bbox:           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
bbox:           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))
> (yy = st_cast(pt, "LINESTRING", rep(seq_along(i), i)))
Geometry set for 5 features 
geometry type:  LINESTRING
dimension:      XY
bbox:           xmin: 0 ymin: 2 xmax: 12 ymax: 15
CRS:            NA
LINESTRING (0 2)
LINESTRING (1 3)
LINESTRING (10 13)
LINESTRING (11 14)
LINESTRING (12 15)
> 
> (zz = st_cast(yy, "MULTILINESTRING"))
Geometry set for 5 features 
geometry type:  MULTILINESTRING
dimension:      XY
bbox:           xmin: 0 ymin: 2 xmax: 12 ymax: 15
CRS:            NA
MULTILINESTRING ((0 2))
MULTILINESTRING ((1 3))
MULTILINESTRING ((10 13))
MULTILINESTRING ((11 14))
MULTILINESTRING ((12 15))
> #(zz = st_cast(yy, "POLYGON"))
> 
> st_cast(mls, "LINESTRING")
Geometry set for 3 features 
geometry type:  LINESTRING
dimension:      XY
bbox:           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
bbox:           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
bbox:           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
bbox:           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
bbox:           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
bbox:           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
> 
> proc.time()
   user  system elapsed 
  0.358   0.052   0.400