1 2R version 4.1.0 (2021-05-18) -- "Camp Pontanezen" 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> 20> nc = st_read(system.file("shape/nc.shp", package="sf"), "nc", crs = 4267, 21+ agr = c(AREA = "aggregate", PERIMETER = "aggregate", CNTY_ = "identity", 22+ CNTY_ID = "identity", NAME = "identity", FIPS = "identity", FIPSNO = "identity", 23+ CRESS_ID = "identity", BIR74 = "aggregate", SID74 = "aggregate", NWBIR74 = "aggregate", 24+ BIR79 = "aggregate", SID79 = "aggregate", NWBIR79 = "aggregate"), quiet = TRUE) 25> 26> st_is_valid(nc) 27 [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE 28 [16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE 29 [31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE 30 [46] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE 31 [61] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE 32 [76] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE 33 [91] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE 34> 35> st_is_simple(nc) 36 [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE 37 [16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE 38 [31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE 39 [46] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE 40 [61] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE 41 [76] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE 42 [91] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE 43> 44> nc_tr = st_transform(nc, 3857) 45> 46> x = st_buffer(nc_tr, 1000) 47> 48> x = st_boundary(nc) 49> 50> x = st_convex_hull(nc) 51> 52> x = st_simplify(nc_tr, dTolerance = 1e4) 53> 54> x = st_simplify(nc_tr, preserveTopology = TRUE) 55> 56> if (sf:::CPL_geos_version() >= "3.4.0") 57+ x = st_triangulate(nc_tr) 58> 59> mls = st_multilinestring(list(matrix(c(0,0,0,1,1,1,0,0),,2,byrow=TRUE))) 60> x = st_polygonize(mls) 61> 62> x = st_segmentize(nc_tr, 5e4) 63> 64> try(x <- st_segmentize(nc_tr, -0.1)) 65Error in CPL_gdal_segmentize(x, dfMaxLength) : 66 argument dfMaxLength should be positive 67 68> 69> x = st_centroid(nc_tr) 70Warning message: 71In st_centroid.sf(nc_tr) : 72 st_centroid assumes attributes are constant over geometries of x 73> x = st_point_on_surface(nc_tr) 74Warning message: 75In st_point_on_surface.sf(nc_tr) : 76 st_point_on_surface assumes attributes are constant over geometries of x 77> 78> a = nc[1:5,] 79> b = nc[4:10,] 80> 81> x <- st_intersection(a[1,] ,b) 82Warning message: 83attribute variables are assumed to be spatially constant throughout all geometries 84> 85> u = st_union(b) 86> 87> x <- st_intersection(st_geometry(a), st_geometry(u)) 88> 89> x = st_union(a[1,], b) 90Warning message: 91attribute variables are assumed to be spatially constant throughout all geometries 92> 93> x = st_union(a, st_union(b)) 94Warning message: 95attribute variables are assumed to be spatially constant throughout all geometries 96> 97> x = st_difference(a[1,], b) 98Warning message: 99attribute variables are assumed to be spatially constant throughout all geometries 100> 101> x = st_difference(a, st_union(b)) 102Warning message: 103attribute variables are assumed to be spatially constant throughout all geometries 104> 105> x = st_sym_difference(a[1,], b) 106Warning message: 107attribute variables are assumed to be spatially constant throughout all geometries 108> 109> x = st_sym_difference(a, st_union(b)) 110Warning message: 111attribute variables are assumed to be spatially constant throughout all geometries 112> 113> x = st_drivers() 114> #cat(paste("GDAL has", nrow(x), "drivers\n")) 115> 116> # GEOS ops: 117> 118> st_relate(a, b) 119although coordinates are longitude/latitude, st_relate assumes that they are planar 120 [,1] [,2] [,3] [,4] [,5] [,6] 121[1,] "FF2FF1212" "FF2FF1212" "FF2FF1212" "FF2FF1212" "FF2FF1212" "FF2FF1212" 122[2,] "FF2FF1212" "FF2FF1212" "FF2FF1212" "FF2FF1212" "FF2FF1212" "FF2FF1212" 123[3,] "FF2FF1212" "FF2FF1212" "FF2FF1212" "FF2FF1212" "FF2FF1212" "FF2FF1212" 124[4,] "2FFF1FFF2" "FF2FF1212" "FF2FF1212" "FF2F11212" "FF2FF1212" "FF2FF1212" 125[5,] "FF2FF1212" "2FFF1FFF2" "FF2F11212" "FF2FF1212" "FF2FF1212" "FF2F11212" 126 [,7] 127[1,] "FF2FF1212" 128[2,] "FF2FF1212" 129[3,] "FF2F11212" 130[4,] "FF2FF1212" 131[5,] "FF2FF1212" 132> 133> st_disjoint(a, b) 134Sparse geometry binary predicate list of length 5, where the predicate 135was `disjoint' 136 1: 1, 2, 3, 4, 5, 6, 7 137 2: 1, 2, 3, 4, 5, 6, 7 138 3: 1, 2, 3, 4, 5, 6 139 4: 2, 3, 5, 6, 7 140 5: 1, 4, 5, 7 141> 142> st_touches(a, b) 143Sparse geometry binary predicate list of length 5, where the predicate 144was `touches' 145 1: (empty) 146 2: (empty) 147 3: 7 148 4: 4 149 5: 3, 6 150> 151> st_crosses(a, b) 152although coordinates are longitude/latitude, st_crosses assumes that they are planar 153Sparse geometry binary predicate list of length 5, where the predicate 154was `crosses' 155 1: (empty) 156 2: (empty) 157 3: (empty) 158 4: (empty) 159 5: (empty) 160> 161> st_within(a, b) 162Sparse geometry binary predicate list of length 5, where the predicate 163was `within' 164 1: (empty) 165 2: (empty) 166 3: (empty) 167 4: 1 168 5: 2 169> 170> st_contains(a, b) 171Sparse geometry binary predicate list of length 5, where the predicate 172was `contains' 173 1: (empty) 174 2: (empty) 175 3: (empty) 176 4: 1 177 5: 2 178> 179> st_overlaps(a, b) 180although coordinates are longitude/latitude, st_overlaps assumes that they are planar 181Sparse geometry binary predicate list of length 5, where the predicate 182was `overlaps' 183 1: (empty) 184 2: (empty) 185 3: (empty) 186 4: (empty) 187 5: (empty) 188> 189> st_equals(a, b) 190Sparse geometry binary predicate list of length 5, where the predicate 191was `equals' 192 1: (empty) 193 2: (empty) 194 3: (empty) 195 4: 1 196 5: 2 197> 198> st_covers(a, b) 199Sparse geometry binary predicate list of length 5, where the predicate 200was `covers' 201 1: (empty) 202 2: (empty) 203 3: (empty) 204 4: 1 205 5: 2 206> 207> st_covered_by(a, b) 208Sparse geometry binary predicate list of length 5, where the predicate 209was `covered_by' 210 1: (empty) 211 2: (empty) 212 3: (empty) 213 4: 1 214 5: 2 215> 216> st_equals_exact(a, b, 0.01) 217Sparse geometry binary predicate list of length 5, where the predicate 218was `equals_exact' 219 1: (empty) 220 2: (empty) 221 3: (empty) 222 4: 1 223 5: 2 224> 225> # st_is_within_distance(a, b, 2) 226> 227> st_geometry_type(st_sfc(st_point(1:2), st_linestring(matrix(1:4,2,2)))) 228[1] POINT LINESTRING 22918 Levels: GEOMETRY POINT LINESTRING POLYGON MULTIPOINT ... TRIANGLE 230> 231> st_geometry_type(st_sfc(st_point(1:2), st_linestring(matrix(1:4,2,2))), by_geometry = FALSE) 232[1] GEOMETRY 23318 Levels: GEOMETRY POINT LINESTRING POLYGON MULTIPOINT ... TRIANGLE 234> 235> st_zm(list(st_point(1:3), st_linestring(matrix(1:6,2,3)))) 236[[1]] 237POINT (1 2) 238 239[[2]] 240LINESTRING (1 3, 2 4) 241 242> 243> st_zm(list(st_point(1:2), st_linestring(matrix(1:6,3,2))), add = TRUE, "Z") 244[[1]] 245POINT (1 2) 246 247[[2]] 248LINESTRING (1 4, 2 5, 3 6) 249 250> 251> st_transform(st_sfc(st_point(c(0,0)), crs=4326), st_crs("+proj=geocent"))[[1]] 252POINT Z (6378137 0 0) 253> 254> cbind(st_area(nc_tr[1:5,]), a$AREA) 255 [,1] [,2] 256[1,] 1760244967 0.114 257[2,] 946435667 0.061 258[3,] 2202248253 0.143 259[4,] 1074351919 0.070 260[5,] 2352539533 0.153 261> 262> st_area(st_polygon(list(rbind(c(0,0), c(1,0), c(1,1), c(0,1), c(0,0))))) 263[1] 1 264> 265> st_length(st_linestring(rbind(c(0,0),c(0,1)))) 266[1] 1 267> 268> st_length(st_multilinestring(list(rbind(c(0,0),c(0,1))))) 269[1] 1 270> 271> try(st_length(st_polygon(list(rbind(c(0,0), c(1,0), c(1,1), c(0,1), c(0,0)))))) 272[1] 0 273> 274> st_area(st_multilinestring(list(rbind(c(0,0),c(0,1))))) 275[1] 0 276> 277> # adds the (0.5 0.5) node: 278> st_union(st_multilinestring(list(rbind(c(0,0),c(1,1)), rbind(c(0,1), c(1,0))))) 279MULTILINESTRING ((0 0, 1 1), (0 1, 1 0)) 280> 281> p1 = st_point(c(7,52)) 282> p2 = st_point(c(-30,20)) 283> sfc = st_sfc(p1, p2) 284> try(st_buffer(sfc, units::set_units(1000, km))) # error: no crs 285Error in st_buffer.sfc(sfc, units::set_units(1000, km)) : 286 x does not have a crs set: can't convert units 287> sfc = st_sfc(p1, p2, crs = 4326) 288> try(st_buffer(sfc, units::set_units(1000, km))) # error: wrong units 289Geometry set for 2 features 290Geometry type: POLYGON 291Dimension: XY 292Bounding box: xmin: -39.65182 ymin: 10.93649 xmax: 21.97439 ymax: 61.16965 293Geodetic CRS: WGS 84 294POLYGON ((-1.035063 59.83701, -1.558398 59.8318... 295POLYGON ((-22.57219 14.45171, -22.42915 14.4660... 296> if (version$os == "linux-gnu") { # FIXME: why does this break on windows - degree symbol? 297+ x = st_buffer(sfc, units::set_units(0.1, rad)) # OK: will convert to arc_degrees 298+ } 299> x = st_transform(sfc, 3857) 300> x = st_buffer(x, units::set_units(1000, km)) # success 301> 302> cr = st_as_sfc("CIRCULARSTRING(0 0,1 0,1 1)") 303> cr1 = st_sf(a = 1, geometry = cr) 304> plot(cr) 305> st_as_grob(cr[[1]]) 306lines[GRID.lines.1] 307> 308> x = st_as_sfc("MULTISURFACE(CURVEPOLYGON(COMPOUNDCURVE(LINESTRING(-159.399779123 22.226016471, -159.399699153 22.226276431, -159.398736217 22.226118372, -159.398260872 22.226095318, -159.398140792 22.2260564590001, -159.398163058 22.2257268010001, -159.397882642 22.225394244, -159.397397157 22.225057335, -159.397318825 22.2251780230001, -159.396993115 22.225177984, -159.396748242 22.2248808800001, -159.396901679 22.224770398, -159.396876329 22.224673093, -159.399167008 22.224731392, -159.399502204 22.225551382), CIRCULARSTRING(-159.399502204 22.225551382, -159.399622762077 22.2257930044972, -159.399779123 22.226016471))))") 309> mp <- x[[1]] %>% st_cast("MULTIPOLYGON") 310> 311> x = st_as_sfc("COMPOUNDCURVE(CIRCULARSTRING(0 0, 1 1, 1 0),(1 0, 0 1))") 312> ls <- x[[1]] %>% st_cast() 313> class(ls) 314[1] "XY" "LINESTRING" "sfg" 315> 316> is.na(st_bbox(ls)) 317[1] FALSE 318> 319> mp = st_combine(st_buffer(st_sfc(lapply(1:3, function(x) st_point(c(x,x)))), 0.2 * 1:3)) 320> plot(st_centroid(mp), add = TRUE, col = 'red') # centroid of combined geometry 321> plot(st_centroid(mp, of_largest_polygon = TRUE), add = TRUE, col = 'blue', pch = 3) # center of largest sub-polygon 322> 323> x = st_sfc(st_polygon(list(rbind(c(0,0),c(0.5,0),c(0.5,0.5),c(0.5,0),c(1,0),c(1,1),c(0,1),c(0,0))))) 324> suppressWarnings(st_is_valid(x)) 325[1] FALSE 326> y = st_make_valid(x) 327> y = st_make_valid(x[[1]]) 328> y = st_make_valid(st_sf(a = 1, geom = x)) 329> st_is_valid(y) 330[1] TRUE 331> 332> proc.time() 333 user system elapsed 334 1.512 0.046 1.547 335