forked from r-spatial/stars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.Rout.save
229 lines (220 loc) · 8.29 KB
/
extract.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
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
R version 4.0.3 (2020-10-10) -- "Bunny-Wunnies Freak Out"
Copyright (C) 2020 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.
> # Create 'stars' object
> set.seed(1331)
> library(stars)
Loading required package: abind
Loading required package: sf
Linking to GEOS 3.8.1, GDAL 3.1.3, PROJ 7.1.1
> volcano = rbind(volcano, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA) # add NA rows
> d = st_dimensions(x = 1:ncol(volcano), y = 1:nrow(volcano))
> (r = st_as_stars(t(volcano)))
stars object with 2 dimensions and 1 attribute
attribute(s):
A1
Min. : 94.0
1st Qu.:108.0
Median :124.0
Mean :130.2
3rd Qu.:150.0
Max. :195.0
NA's :732
dimension(s):
from to offset delta refsys point values x/y
X1 1 61 0 1 NA FALSE NULL [x]
X2 1 99 0 1 NA FALSE NULL [y]
> r = st_set_dimensions(r, 1, offset = 0, delta = 1)
> r = st_set_dimensions(r, 2, offset = nrow(volcano), delta = -1)
>
> # Create points
> pnt = st_sample(st_as_sfc(st_bbox(r)), 100)
> pnt = st_as_sf(pnt)
>
> # Extract - 'st_join'
> x = st_join(pnt, st_as_sf(r))
>
> # Extract - 'st_extract'
> y = st_extract(r, pnt)
>
> # check there are NA's:
> any(is.na(x))
[1] TRUE
> # Compare
> all.equal(x$A1, y[[1]])
[1] TRUE
>
> ## tic: segfaults
> # check equal results with stars_proxy:
> #x = st_extract(stars:::st_as_stars_proxy(r), pnt)
> #all.equal(x$A1, y[[1]])
> #all.equal(x, y)
>
> r = c(r, 2*r, 10*r)
> x = st_join(pnt, st_as_sf(r))
> y = st_as_sf(st_extract(r, pnt))
> all.equal(x, y)
[1] "Names: 1 string mismatch"
[2] "Attributes: < Component \"sf_column\": 1 string mismatch >"
>
> ## tic: segfaults
> #x = st_extract(stars:::st_as_stars_proxy(merge(r)), pnt)
> #all.equal(st_as_sf(x), y)
>
> tif = system.file("tif/L7_ETMs.tif", package = "stars")
> xp = read_stars(tif, proxy = TRUE)
> xm = read_stars(tif, proxy = FALSE)
> pts = st_sample(st_as_sfc(st_bbox(xp)), 10)
> pts = c(pts, st_as_sfc("POINT(0 0)"), pts)
> em = st_extract(xm, pts)
> if (utils::packageVersion("sf") >= "0.9-7") {
+ ep = st_extract(xp, pts)
+ print(all.equal(ep, em, check.attributes = TRUE))
+ }
[1] TRUE
>
> # two-attribute objects:
> library(stars)
> tif = system.file("tif/L7_ETMs.tif", package = "stars")
> xp = read_stars(c(tif, tif), proxy = TRUE)
> xm = read_stars(c(tif, tif), proxy = FALSE)
> pts = st_sample(st_as_sfc(st_bbox(xp)), 10)
> pts = c(pts, st_as_sfc("POINT(0 0)"), pts)
> em = st_extract(xm, pts)
> if (utils::packageVersion("sf") >= "0.9-7") {
+ ep = st_extract(xp, pts)
+ print(all.equal(ep, em, check.attributes = TRUE))
+ }
[1] "Names: 1 string mismatch"
>
> # single-attribute, single raster objects:
> tif1 = paste0(tempfile(), ".tif")
> write_stars(xm[1,,,1], "x.tif")
> xp = read_stars("x.tif", proxy = TRUE)
> xm = read_stars("x.tif", proxy = FALSE)
> em = st_extract(xm, pts)
> if (utils::packageVersion("sf") >= "0.9-7") {
+ ep = st_extract(xp, pts)
+ print(all.equal(ep, em, check.attributes = TRUE))
+ }
[1] TRUE
>
> if (require(starsdata)) {
+ # multiple-file attributes:
+ x = c(
+ "avhrr-only-v2.19810901.nc",
+ "avhrr-only-v2.19810902.nc",
+ "avhrr-only-v2.19810903.nc",
+ "avhrr-only-v2.19810904.nc",
+ "avhrr-only-v2.19810905.nc",
+ "avhrr-only-v2.19810906.nc",
+ "avhrr-only-v2.19810907.nc",
+ "avhrr-only-v2.19810908.nc",
+ "avhrr-only-v2.19810909.nc"
+ )
+ file_list = system.file(paste0("netcdf/", x), package = "starsdata")
+ y = read_stars(file_list, quiet = TRUE)
+ print(y)
+ st_crs(y) = "OGC:CRS84"
+ pts = st_sample(st_as_sfc(st_bbox(y)), 10)
+ em = st_extract(y, pts)
+
+ (y = read_stars(file_list, quiet = TRUE, proxy = TRUE))
+ print(y)
+ st_crs(y) = "OGC:CRS84"
+ if (utils::packageVersion("sf") >= "0.9-7") {
+ ep = st_extract(y, pts)
+ print(all.equal(em, ep))
+ }
+ }
Loading required package: starsdata
stars object with 4 dimensions and 4 attributes
attribute(s), summary of first 1e+05 cells:
sst [C*°] anom [C*°] err [C*°] ice [percent]
Min. :-1.80 Min. :-4.69 Min. :0.110 Min. :0.010
1st Qu.:-1.19 1st Qu.:-0.06 1st Qu.:0.300 1st Qu.:0.730
Median :-1.05 Median : 0.52 Median :0.300 Median :0.830
Mean :-0.32 Mean : 0.23 Mean :0.295 Mean :0.766
3rd Qu.:-0.20 3rd Qu.: 0.71 3rd Qu.:0.300 3rd Qu.:0.870
Max. : 9.36 Max. : 3.70 Max. :0.480 Max. :1.000
NA's :13360 NA's :13360 NA's :13360 NA's :27377
dimension(s):
from to offset delta refsys point values x/y
x 1 1440 0 0.25 NA NA NULL [x]
y 1 720 90 -0.25 NA NA NULL [y]
zlev 1 1 0 [m] NA NA NA NULL
time 1 9 1981-09-01 02:00:00 CEST 1 days POSIXct NA NULL
although coordinates are longitude/latitude, st_intersects assumes that they are planar
although coordinates are longitude/latitude, st_intersects assumes that they are planar
stars_proxy object with 4 attributes in files:
$sst
[1] "[...]/avhrr-only-v2.19810901.nc:sst" "[...]/avhrr-only-v2.19810902.nc:sst"
[3] "[...]/avhrr-only-v2.19810903.nc:sst" "[...]/avhrr-only-v2.19810904.nc:sst"
[5] "[...]/avhrr-only-v2.19810905.nc:sst" "[...]/avhrr-only-v2.19810906.nc:sst"
[7] "[...]/avhrr-only-v2.19810907.nc:sst" "[...]/avhrr-only-v2.19810908.nc:sst"
[9] "[...]/avhrr-only-v2.19810909.nc:sst"
$anom
[1] "[...]/avhrr-only-v2.19810901.nc:anom"
[2] "[...]/avhrr-only-v2.19810902.nc:anom"
[3] "[...]/avhrr-only-v2.19810903.nc:anom"
[4] "[...]/avhrr-only-v2.19810904.nc:anom"
[5] "[...]/avhrr-only-v2.19810905.nc:anom"
[6] "[...]/avhrr-only-v2.19810906.nc:anom"
[7] "[...]/avhrr-only-v2.19810907.nc:anom"
[8] "[...]/avhrr-only-v2.19810908.nc:anom"
[9] "[...]/avhrr-only-v2.19810909.nc:anom"
$err
[1] "[...]/avhrr-only-v2.19810901.nc:err" "[...]/avhrr-only-v2.19810902.nc:err"
[3] "[...]/avhrr-only-v2.19810903.nc:err" "[...]/avhrr-only-v2.19810904.nc:err"
[5] "[...]/avhrr-only-v2.19810905.nc:err" "[...]/avhrr-only-v2.19810906.nc:err"
[7] "[...]/avhrr-only-v2.19810907.nc:err" "[...]/avhrr-only-v2.19810908.nc:err"
[9] "[...]/avhrr-only-v2.19810909.nc:err"
$ice
[1] "[...]/avhrr-only-v2.19810901.nc:ice" "[...]/avhrr-only-v2.19810902.nc:ice"
[3] "[...]/avhrr-only-v2.19810903.nc:ice" "[...]/avhrr-only-v2.19810904.nc:ice"
[5] "[...]/avhrr-only-v2.19810905.nc:ice" "[...]/avhrr-only-v2.19810906.nc:ice"
[7] "[...]/avhrr-only-v2.19810907.nc:ice" "[...]/avhrr-only-v2.19810908.nc:ice"
[9] "[...]/avhrr-only-v2.19810909.nc:ice"
dimension(s):
from to offset delta refsys point values x/y
x 1 1440 0 0.25 NA NA NULL [x]
y 1 720 90 -0.25 NA NA NULL [y]
zlev 1 1 0 [m] NA NA NA NULL
time 1 9 1981-09-01 02:00:00 CEST 1 days POSIXct NA NULL
[1] TRUE
>
> # nearest & bilinear comparison:
> if (utils::packageVersion("sf") >= "0.9-7") {
+ set.seed(12331)
+ s = st_as_stars(matrix(rnorm(16), 4))
+ pts = st_sample(st_as_sfc(st_bbox(s)), 10000, type = "regular")
+ s1 = st_extract(s, pts, bilinear = FALSE)
+ s2 = st_extract(s, pts, bilinear = TRUE)
+ s1$s2 = s2[[1]]
+ names(s1)[c(1,3)] = c("nearest", "bilinear")
+ print(s1[sample(10000, 5),])
+ }
Simple feature collection with 5 features and 2 fields
geometry type: POINT
dimension: XY
bbox: xmin: 0.07447365 ymin: 1.53436 xmax: 3.074474 ymax: 3.53436
CRS: NA
nearest geometry bilinear
3808 -1.7065797 POINT (0.3144736 1.53436) -1.59473243
8377 -1.0415900 POINT (3.074474 3.33436) -0.07857302
7002 1.5486092 POINT (0.07447365 2.81436) 1.18530052
8840 -0.3508243 POINT (1.594474 3.53436) -0.19447096
6005 1.5486092 POINT (0.1944736 2.41436) 1.26983384
>
> proc.time()
user system elapsed
3.634 0.710 4.102