-
Notifications
You must be signed in to change notification settings - Fork 303
/
Copy pathgrid.Rout.save
83 lines (78 loc) · 2.86 KB
/
grid.Rout.save
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
R version 4.3.1 (2023-06-16) -- "Beagle Scouts"
Copyright (C) 2023 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.
> # MULTIPOLYGONS
> suppressPackageStartupMessages(library(sf))
> library(grid)
> nc = st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
> grid.newpage()
> # pushViewport(viewport(width = 0.8, height = 0.8))
> pushViewport(st_viewport(nc))
> invisible(lapply(st_geometry(nc), function(x) grid.draw(st_as_grob(x, gp = gpar(fill = 'red')))))
>
> # POLYGONS
> # nc = st_read(system.file("gpkg/nc.gpkg", package="sf"), "nc.gpkg", type = 3)
> nc = st_read(system.file("shape/nc.shp", package="sf"), type = 3, quiet = TRUE)
> grid.newpage()
> pushViewport(st_viewport(nc))
> invisible(lapply(st_geometry(nc), function(x) grid.draw(st_as_grob(x, gp = gpar(fill = 'red')))))
>
> # POINTS:
> if (suppressPackageStartupMessages(require(sp, quietly = TRUE))) {
+ data(meuse, package = "sp")
+ meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992, agr = "constant")
+ grid.newpage()
+ pushViewport(st_viewport(meuse_sf))
+ invisible(lapply(st_geometry(meuse_sf),
+ function(x) grid.draw(st_as_grob(x, gp = gpar(fill = 'red')))))
+ }
>
> # MULTIPOINTS
> mp = st_multipoint(cbind(runif(100), runif(100)))
> grid.newpage()
> pushViewport(st_viewport(mp))
> grid.draw(st_as_grob(mp, gp = gpar(fill = 'red')))
>
> # LINESTRING
> ls = st_linestring(cbind(1:10, rnorm(10)))
> grid.newpage()
> pushViewport(st_viewport(ls))
> grid.draw(st_as_grob(ls, gp = gpar(fill = 'red')))
>
> # MULTILINESTRING
> ls = st_multilinestring(list(cbind(1:10, 5+rnorm(10)), cbind(1:10, rnorm(10)), cbind(1:10, -5+rnorm(10))))
> grid.newpage()
> pushViewport(st_viewport(ls))
> grid.draw(st_as_grob(ls, gp = gpar(fill = 'red')))
>
> if (require(sp, quietly = TRUE)) {
+ # POINTS, right aspect in Long/Lat:
+ meuse_ll = st_transform(meuse_sf, 4326)
+ grid.newpage()
+ pushViewport(st_viewport(meuse_ll))
+ invisible(lapply(st_geometry(meuse_ll),
+ function(x) grid.draw(st_as_grob(x, gp = gpar(fill = 'red')))))
+
+ # WRONG aspect:
+ st_crs(meuse_ll) = NA
+ grid.newpage()
+ pushViewport(st_viewport(meuse_ll))
+ invisible(lapply(st_geometry(meuse_ll),
+ function(x) grid.draw(st_as_grob(x, gp = gpar(fill = 'red')))))
+ }
>
> gc = st_geometrycollection(list(st_point(0:1), st_linestring(matrix(1:4,2))))
> grb = st_as_grob(gc)
>
> proc.time()
user system elapsed
1.462 0.800 1.365