1suppressPackageStartupMessages(library(sf)) 2 3nc = st_read(system.file("shape/nc.shp", package="sf"), "nc", crs = 4267, 4 agr = c(AREA = "aggregate", PERIMETER = "aggregate", CNTY_ = "identity", 5 CNTY_ID = "identity", NAME = "identity", FIPS = "identity", FIPSNO = "identity", 6 CRESS_ID = "identity", BIR74 = "aggregate", SID74 = "aggregate", NWBIR74 = "aggregate", 7 BIR79 = "aggregate", SID79 = "aggregate", NWBIR79 = "aggregate"), quiet = TRUE) 8 9st_is_valid(nc) 10 11st_is_simple(nc) 12 13nc_tr = st_transform(nc, 3857) 14 15x = st_buffer(nc_tr, 1000) 16 17x = st_boundary(nc) 18 19x = st_convex_hull(nc) 20 21x = st_simplify(nc_tr, dTolerance = 1e4) 22 23x = st_simplify(nc_tr, preserveTopology = TRUE) 24 25if (sf:::CPL_geos_version() >= "3.4.0") 26 x = st_triangulate(nc_tr) 27 28mls = st_multilinestring(list(matrix(c(0,0,0,1,1,1,0,0),,2,byrow=TRUE))) 29x = st_polygonize(mls) 30 31x = st_segmentize(nc_tr, 5e4) 32 33try(x <- st_segmentize(nc_tr, -0.1)) 34 35x = st_centroid(nc_tr) 36x = st_point_on_surface(nc_tr) 37 38a = nc[1:5,] 39b = nc[4:10,] 40 41x <- st_intersection(a[1,] ,b) 42 43u = st_union(b) 44 45x <- st_intersection(st_geometry(a), st_geometry(u)) 46 47x = st_union(a[1,], b) 48 49x = st_union(a, st_union(b)) 50 51x = st_difference(a[1,], b) 52 53x = st_difference(a, st_union(b)) 54 55x = st_sym_difference(a[1,], b) 56 57x = st_sym_difference(a, st_union(b)) 58 59x = st_drivers() 60#cat(paste("GDAL has", nrow(x), "drivers\n")) 61 62# GEOS ops: 63 64st_relate(a, b) 65 66st_disjoint(a, b) 67 68st_touches(a, b) 69 70st_crosses(a, b) 71 72st_within(a, b) 73 74st_contains(a, b) 75 76st_overlaps(a, b) 77 78st_equals(a, b) 79 80st_covers(a, b) 81 82st_covered_by(a, b) 83 84st_equals_exact(a, b, 0.01) 85 86# st_is_within_distance(a, b, 2) 87 88st_geometry_type(st_sfc(st_point(1:2), st_linestring(matrix(1:4,2,2)))) 89 90st_geometry_type(st_sfc(st_point(1:2), st_linestring(matrix(1:4,2,2))), by_geometry = FALSE) 91 92st_zm(list(st_point(1:3), st_linestring(matrix(1:6,2,3)))) 93 94st_zm(list(st_point(1:2), st_linestring(matrix(1:6,3,2))), add = TRUE, "Z") 95 96st_transform(st_sfc(st_point(c(0,0)), crs=4326), st_crs("+proj=geocent"))[[1]] 97 98cbind(st_area(nc_tr[1:5,]), a$AREA) 99 100st_area(st_polygon(list(rbind(c(0,0), c(1,0), c(1,1), c(0,1), c(0,0))))) 101 102st_length(st_linestring(rbind(c(0,0),c(0,1)))) 103 104st_length(st_multilinestring(list(rbind(c(0,0),c(0,1))))) 105 106try(st_length(st_polygon(list(rbind(c(0,0), c(1,0), c(1,1), c(0,1), c(0,0)))))) 107 108st_area(st_multilinestring(list(rbind(c(0,0),c(0,1))))) 109 110# adds the (0.5 0.5) node: 111st_union(st_multilinestring(list(rbind(c(0,0),c(1,1)), rbind(c(0,1), c(1,0))))) 112 113p1 = st_point(c(7,52)) 114p2 = st_point(c(-30,20)) 115sfc = st_sfc(p1, p2) 116try(st_buffer(sfc, units::set_units(1000, km))) # error: no crs 117sfc = st_sfc(p1, p2, crs = 4326) 118try(st_buffer(sfc, units::set_units(1000, km))) # error: wrong units 119if (version$os == "linux-gnu") { # FIXME: why does this break on windows - degree symbol? 120 x = st_buffer(sfc, units::set_units(0.1, rad)) # OK: will convert to arc_degrees 121} 122x = st_transform(sfc, 3857) 123x = st_buffer(x, units::set_units(1000, km)) # success 124 125cr = st_as_sfc("CIRCULARSTRING(0 0,1 0,1 1)") 126cr1 = st_sf(a = 1, geometry = cr) 127plot(cr) 128st_as_grob(cr[[1]]) 129 130x = 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))))") 131mp <- x[[1]] %>% st_cast("MULTIPOLYGON") 132 133x = st_as_sfc("COMPOUNDCURVE(CIRCULARSTRING(0 0, 1 1, 1 0),(1 0, 0 1))") 134ls <- x[[1]] %>% st_cast() 135class(ls) 136 137is.na(st_bbox(ls)) 138 139mp = st_combine(st_buffer(st_sfc(lapply(1:3, function(x) st_point(c(x,x)))), 0.2 * 1:3)) 140plot(st_centroid(mp), add = TRUE, col = 'red') # centroid of combined geometry 141plot(st_centroid(mp, of_largest_polygon = TRUE), add = TRUE, col = 'blue', pch = 3) # center of largest sub-polygon 142 143x = 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))))) 144suppressWarnings(st_is_valid(x)) 145y = st_make_valid(x) 146y = st_make_valid(x[[1]]) 147y = st_make_valid(st_sf(a = 1, geom = x)) 148st_is_valid(y) 149