Debian Codegraph @

tests/grid.Rout.save

raw
1
2 R version 4.4.2 (2024-10-31) -- "Pile of Leaves"
3 Copyright (C) 2024 The R Foundation for Statistical Computing
4 Platform: x86_64-pc-linux-gnu
5
6 R is free software and comes with ABSOLUTELY NO WARRANTY.
7 You are welcome to redistribute it under certain conditions.
8 Type 'license()' or 'licence()' for distribution details.
9
10 R is a collaborative project with many contributors.
11 Type 'contributors()' for more information and
12 'citation()' on how to cite R or R packages in publications.
13
14 Type 'demo()' for some demos, 'help()' for on-line help, or
15 'help.start()' for an HTML browser interface to help.
16 Type 'q()' to quit R.
17
18 > options("rgdal_show_exportToProj4_warnings"="none")
19 > library(sp)
20 > data(meuse.grid)
21 > x = meuse.grid
22 > coordinates(x) = c("x", "y")
23 > gridded(x) = TRUE
24 > gridded(x)
25 [1] TRUE
26 > image(x["dist"])
27 >
28 > fullgrid(x) = TRUE
29 > fullgrid(x)
30 [1] TRUE
31 > # summary(x)
32 > gridparameters(x)
33 cellcentre.offset cellsize cells.dim
34 x 178460 40 78
35 y 329620 40 104
36 > class(as(x, "matrix"))
37 [1] "matrix" "array"
38 Warning message:
39 In as.matrix.SpatialGridDataFrame(from) :
40 as.matrix.SpatialGridDataFrame uses first column;
41 use subset or [] for other columns
42 >
43 > fullgrid(x) = FALSE
44 > fullgrid(x)
45 [1] FALSE
46 > # summary(x)
47 > class(as(x, "matrix"))
48 [1] "matrix" "array"
49 Warning message:
50 In as.matrix.SpatialGridDataFrame(x, ...) :
51 as.matrix.SpatialGridDataFrame uses first column;
52 use subset or [] for other columns
53 > gridparameters(x)
54 cellcentre.offset cellsize cells.dim
55 x 178460 40 78
56 y 329620 40 104
57 >
58 > df = data.frame(z = c(1:6,NA,8,9),
59 + xc = c(1,1,1,2,2,2,3,3,3),
60 + yc = c(rep(c(0, 1.5, 3),3)))
61 >
62 > coordinates(df) = ~xc+yc
63 > gridded(df) = TRUE
64 > gridparameters(df)
65 cellcentre.offset cellsize cells.dim
66 xc 1 1.0 3
67 yc 0 1.5 3
68 > as(df, "matrix")
69 [,1] [,2] [,3]
70 [1,] 3 2 1
71 [2,] 6 5 4
72 [3,] 9 8 NA
73 >
74 > # get grid topology:
75 > grd = points2grid(as(df, "SpatialPoints"), 1e-31)
76 > grd
77 xc yc
78 cellcentre.offset 1 0.0
79 cellsize 1 1.5
80 cells.dim 3 3.0
81 > getGridIndex(coordinates(df), grd)
82 [1] 7 4 1 8 5 2 9 6 3
83 >
84 > g = SpatialGrid(grid = grd)
85 > fullgrid(g)
86 [1] TRUE
87 > fullgrid(g) = TRUE
88 > class(g)
89 [1] "SpatialGrid"
90 attr(,"package")
91 [1] "sp"
92 > # the next one has to fail:
93 > fullgrid(g) <- FALSE
94 > class(g)
95 [1] "SpatialPixels"
96 attr(,"package")
97 [1] "sp"
98 >
99 > # summary(df)
100 > image(df["z"])
101 > as.image.SpatialGridDataFrame(df)
102 $x
103 [1] 1 2 3
104
105 $y
106 [1] 0.0 1.5 3.0
107
108 $z
109 [,1] [,2] [,3]
110 [1,] 1 2 3
111 [2,] 4 5 6
112 [3,] NA 8 9
113
114 > as.image.SpatialGridDataFrame(df["z"])
115 $x
116 [1] 1 2 3
117
118 $y
119 [1] 0.0 1.5 3.0
120
121 $z
122 [,1] [,2] [,3]
123 [1,] 1 2 3
124 [2,] 4 5 6
125 [3,] NA 8 9
126
127 > coordinatevalues(getGridTopology(df))
128 $xc
129 [1] 1 2 3
130
131 $yc
132 [1] 3.0 1.5 0.0
133
134 >
135 > as.data.frame(df)
136 z xc yc
137 1 1 1 0.0
138 2 2 1 1.5
139 3 3 1 3.0
140 4 4 2 0.0
141 5 5 2 1.5
142 6 6 2 3.0
143 7 NA 3 0.0
144 8 8 3 1.5
145 9 9 3 3.0
146 >
147 > fullgrid(df) = TRUE
148 > as.data.frame(df)
149 z xc yc
150 1 3 1 3.0
151 2 6 2 3.0
152 3 9 3 3.0
153 4 2 1 1.5
154 5 5 2 1.5
155 6 8 3 1.5
156 7 1 1 0.0
157 8 4 2 0.0
158 >
159 > fullgrid(df) = FALSE
160 > as.data.frame(df)
161 z xc yc
162 1 3 1 3.0
163 2 6 2 3.0
164 3 9 3 3.0
165 4 2 1 1.5
166 5 5 2 1.5
167 6 8 3 1.5
168 7 1 1 0.0
169 8 4 2 0.0
170 >
171 > fullgrid(df) = TRUE
172 > fullgrid(df) = FALSE
173 > as.data.frame(df)
174 z xc yc
175 1 3 1 3.0
176 2 6 2 3.0
177 3 9 3 3.0
178 4 2 1 1.5
179 5 5 2 1.5
180 6 8 3 1.5
181 7 1 1 0.0
182 8 4 2 0.0
183 >
184 > df = as.data.frame(df)
185 > set.seed(133331)
186 > df$xc = df$xc + rep(.001*rnorm(3), 3)[1:8]
187 > df.sp = SpatialPoints(df[c("xc", "yc")])
188 > df.grd = SpatialPixels(df.sp, tolerance = .01)
189 > df.grd[1:4,,tolerance=.01,drop=TRUE]
190 Object of class SpatialPixels
191 Grid topology:
192 cellcentre.offset cellsize cells.dim
193 xc 1.000433 0.9995197 3
194 yc 1.500000 1.5000000 2
195 SpatialPoints:
196 xc yc
197 1 1.000433 3.0
198 2 1.997926 3.0
199 3 2.999472 3.0
200 4 1.000433 1.5
201 Coordinate Reference System (CRS) arguments: NA
202 > df.grd[1:4,,tolerance=.01]
203 Object of class SpatialPixels
204 Grid topology:
205 cellcentre.offset cellsize cells.dim
206 xc 1.000433 0.9995197 3
207 yc 0.000000 1.5000000 3
208 SpatialPoints:
209 xc yc
210 1 1.000433 3.0
211 2 1.997926 3.0
212 3 2.999472 3.0
213 4 1.000433 1.5
214 Coordinate Reference System (CRS) arguments: NA
215 >
216 > proc.time()
217 user system elapsed
218 0.566 1.398 0.459