-
Notifications
You must be signed in to change notification settings - Fork 94
/
raster.R
387 lines (363 loc) · 13.3 KB
/
raster.R
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#' @name st_as_stars
#' @param att see \link[raster:factor]{factorValues}; column in the RasterLayer's attribute table
#' @param ignore_file logical; if \code{TRUE}, ignore the Raster object file name
#' @export
st_as_stars.Raster = function(.x, ..., att = 1, ignore_file = FALSE) {
if (!requireNamespace("raster", quietly = TRUE))
stop("package raster required, please install it first") # nocov
#0 360 -90 90
e = as.vector(raster::extent(.x)) # xmin xmax ymin ymax
RasterIO = if ("data" %in% slotNames(.x) && inherits(.x@data, ".SingleLayerData"))
list(bands = .x@data@band)
else
list()
if (!ignore_file) {
file = if ("file" %in% slotNames(.x))
.x@file@name
else if ("filename" %in% slotNames(.x))
.x@filename
else
""
if (file != "") {
r = try(read_stars(file, RasterIO = RasterIO, ...), silent = TRUE)
if (!inherits(r, "try-error")) {
if (is.na(st_crs(r)))
r = st_set_crs(r, st_crs(raster::crs(.x)))
r = fix_dims(r, e)
if (inherits(.x, "RasterLayer"))
names(r) <- names(.x)
return(r)
}
}
}
if (!requireNamespace("sp", quietly = TRUE))
stop("package sp required, please install it first") # nocov
v = raster::values(.x)
dim(v) = dim(.x)[c(2,1,3)]
if (all(raster::is.factor(.x))) {
l = raster::levels(.x)[[1]]$levels
if (length(l) == 0) # get the layer's RAT, column att:
l = raster::factorValues(.x, seq_len(max(v, na.rm = TRUE)), att = att)[[1]]
colors = try(.x@legend@colortable, silent = TRUE)
if (inherits(colors, "try-error") || length(colors) == 0)
colors = NULL
else
colors = colors[-1]
v = structure(v, class = "factor", levels = as.character(l), colors = colors)
# FIXME: should we handle levels for all layers here, or break on multiple different ones?
}
dimensions = list(
x = create_dimension(from = 1, to = dim(v)[1], offset = e[1],
delta = (e[2]-e[1])/dim(v)[1], refsys = st_crs(raster::crs(.x))),
y = create_dimension(from = 1, to = dim(v)[2], offset = e[4],
delta = (e[3]-e[4])/dim(v)[2], refsys = st_crs(raster::crs(.x))))
z = raster::getZ(.x)
dimensions$band = if (is.null(z))
create_dimension(values = names(.x))
else
create_dimension(values = z)
l = if (length(names) > 1)
setNames(list(v), deparse(substitute(.x), 50))
else
setNames(list(v), names(.x)[1])
ret = st_as_stars(l, dimensions = create_dimensions(dimensions, get_raster()))
if (dim(ret)[3] == 1)
adrop(ret, 3)
else
ret
}
#' Coerce stars object into a Raster raster or brick
#'
#' Coerce stars object into a Raster raster or brick
#' @param from object to coerce
#' @name as
#' @rdname coerce-methods
#' @aliases coerce,stars,Raster-method
#' @aliases coerce,stars_proxy,Raster-method
#' @returns RasterLayer or RasterBrick
#' @details If the stars object has more than three dimensions, all dimensions higher than the third will be collapsed into the third dimensions. If the stars object has only an x/y raster but multiple attributes, these are merged first, then put in a raster brick.
setAs("stars", "Raster", function(from) {
if (!requireNamespace("sp", quietly = TRUE))
stop("package sp required, please install it first") # nocov
if (!requireNamespace("raster", quietly = TRUE))
stop("package raster required, please install it first") # nocov
if (!is_regular_grid(from))
stop("only regular rasters can be converted to Raster* objects")
st_as_raster(from, class = "Raster")
})
setAs("stars_proxy", "Raster", function(from) {
if (!requireNamespace("raster", quietly = TRUE))
stop("package raster required, please install it first") # nocov
if (!is_regular_grid(from))
stop("only regular rasters can be converted to Raster* objects")
if (length(attr(from, "call_list"))) {
fname = paste0(tempfile(), ".tif")
write_stars(from, fname)
from = fname
}
raster::brick(unlist(from))
})
get_terra_levels = function(x, colors) {
# create factor levels, as used by stars, from SpatRaster levels in a data.frame
# see https://github.com/r-spatial/stars/pull/484
x = x[order(x[[1]]), ] # sort table on level
missing_labels = x[[2]] == ""
levels = x[[1]]
if (any(levels < 0))
stop("negative IDs in SpatRaster levels not supported")
ex = setdiff(0:max(levels), levels)
exclude = rep(FALSE, max(levels) + 1)
exclude[ex + 1] = TRUE # 0-based vector
list(levels = levels[!missing_labels],
labels = x[[2]][!missing_labels],
exclude = exclude,
colors = colors[!missing_labels])
}
#' @name st_as_stars
#' @param ignore_file logical; if \code{TRUE}, ignore the SpatRaster object file name
#' @param as_attributes logical; if \code{TRUE} and \code{.x} has more than one layer, load these as separate attributes rather than as a band or time dimension (only implemented for the case where \code{ignore_file} is \code{TRUE})
#' @export
st_as_stars.SpatRaster = function(.x, ..., ignore_file = FALSE,
as_attributes = all(terra::is.factor(.x))) {
if (!requireNamespace("terra", quietly = TRUE))
stop("package terra required, please install it first") # nocov
#0 360 -90 90
e = as.vector(terra::ext(.x)) # xmin xmax ymin ymax
src = terra::sources(.x, bands=TRUE)
attr_name = basename(src$source[1])
if (!ignore_file && all(src$source != "")) {
# there can be multiple files, but only the first one is used here.
# perhaps a warning should be given; better would be to iterate over "sid"
# but you might have a situation where some sources are filenames and others are not
if (attr_name == "")
attr_name = "values"
lst = vector("list", length(unique(src$sid)))
for (i in unique(src$sid)) {
file = unique(src$source[src$sid == i])
if (length(file) > 1)
stop("more than one file per sid: giving up; try ignore_file=FALSE")
# RasterIO = if (dim(.x)[3] == 1)
# > 1 would be more sensible?
# But this can only be ignored if the _file_ has 1 band
RasterIO = list(bands = src$bands[src$sid == i])
r = try(read_stars(file, RasterIO = RasterIO, ...), silent = TRUE)
if (! inherits(r, "try-error")) {
if (is.na(st_crs(r)) && terra::crs(.x) != "")
r = st_set_crs(r, st_crs(terra::crs(.x)))
r = fix_dims(r, e)
if (length(unique(src$sid)) > 1 && length(dim(r)) > 2)
r = split(r)
#transfer the layer/band names as well?
# ... = names(.x)[1:(dim(r)[3])]
# perhaps check whether they represent a time dimension (all(!is.na(time(.x))))
} else
stop(paste("error reading", file, "bands", paste0(RasterIO$bands, collapse = " ")))
lst[[i]] = r
}
ret = if (length(lst) > 1)
merge(setNames(do.call(c, lst), names(.x)))
else
r
if (!all(is.na(terra::time(.x))))
ret = st_set_dimensions(ret, 3, values = terra::time(.x), names = "time")
} else { # ignore_file TRUE:
if (terra::nlyr(.x) > 1 && as_attributes) {
ret = do.call(c, lapply(seq_len(terra::nlyr(.x)), function(i) st_as_stars(.x[[i]], ignore_file = TRUE)))
if (!is.null(names(.x)))
names(ret) = names(.x)
return(ret) # RETURNS
}
if (attr_name == "") {
if (all(names(.x) == ""))
attr_name = "values"
else
attr_name = paste(names(.x)[1], collapse = ".")
}
v = terra::values(.x, mat = FALSE)
dimv = dim(v) = setNames(dim(.x)[c(2,1,3)], c("x", "y", "band"))
if (all(terra::is.factor(.x))) {
if (length(terra::levels(.x)) > 1)
warning("ignoring categories/levels for all but first layer")
colors = try(rgb(terra::coltab(.x)[[1]][-1], maxColorValue = 255), silent = TRUE)
if (inherits(colors, "try-error") || length(colors) == 0)
colors = NULL
if (inherits(l <- terra::levels(.x)[[1]], "data.frame"))
l = get_terra_levels(l, colors)
else
stop("terra levels should return a list of data.frame's; pls update terra")
v = structure(factor(as.vector(v), levels = l$levels, labels = l$labels),
dim = dimv, colors = l$colors, exclude = l$exclude)
}
crs = if (terra::crs(.x) == "")
NA_crs_
else
st_crs(terra::crs(.x))
dimensions = list(
x = create_dimension(from = 1, to = dim(v)[1], offset = e[1],
delta = (e[2]-e[1])/dim(v)[1], refsys = crs),
y = create_dimension(from = 1, to = dim(v)[2], offset = e[4],
delta = (e[3]-e[4])/dim(v)[2], refsys = crs))
dimensions$band = create_dimension(values = names(.x))
ret = st_as_stars(setNames(list(v), attr_name), dimensions = create_dimensions(dimensions, get_raster()))
if (dim(ret)[3] == 1)
ret = adrop(ret, 3)
else if (!all(is.na(terra::time(.x))))
ret = st_set_dimensions(ret, 3, values = terra::time(.x), names = "time")
}
ret
}
#' Coerce stars object into a terra SpatRaster
#'
#' Coerce stars object into a terra SpatRaster
#' @param from object to coerce
#' @name as
#' @rdname coerce-methods
#' @aliases coerce,stars,Terra-method
#' @aliases coerce,stars_proxy,Terra-method
#' @returns SpatRaster
#' @details If the stars object has more than three dimensions, all dimensions higher than the third will be collapsed into the third dimensions. If the stars object has only an x/y raster but multiple attributes, these are merged first, then put in a SpatRaster.
setAs("stars", "SpatRaster", function(from) {
if (!requireNamespace("terra", quietly = TRUE))
stop("package terra required, please install it first") # nocov
if (!is_regular_grid(from))
stop("only regular rasters can be converted to SpatRaster objects")
st_as_raster(from, class = "SpatRaster")
})
setAs("stars_proxy", "SpatRaster", function(from) {
if (!requireNamespace("terra", quietly = TRUE))
stop("package terra required, please install it first") # nocov
if (!is_regular_grid(from))
stop("only regular rasters can be converted to SpatRaster objects")
if (length(attr(from, "call_list"))) {
fname = paste0(tempfile(), ".tif")
write_stars(from, fname)
from = fname
}
terra::rast(unlist(from))
})
fix_dims = function(r, e){
e = unname(e)
rdims = attr(r, "dimensions")
dxy = attr(rdims, "raster")$dimensions
dimx = rdims[[dxy[1]]]
dimy = rdims[[dxy[2]]]
xrev = dimx$delta < 0
yrev = dimy$delta < 0
if (xrev) {
dimx$offset = e[2]
dimx$delta = (e[1] - e[2]) / (dimx$to - dimx$from + 1)
} else {
dimx$offset = e[1]
dimx$delta = (e[2] - e[1]) / (dimx$to - dimx$from + 1)
}
if (yrev) {
dimy$offset = e[4]
dimy$delta = (e[3] - e[4]) / (dimy$to - dimy$from + 1)
} else {
dimy$offset = e[3]
dimy$delta = (e[4] - e[3]) / (dimy$to - dimy$from + 1)
}
attr(r, "dimensions")[[dxy[1]]] = dimx
attr(r, "dimensions")[[dxy[2]]] = dimy
r
}
st_as_raster = function(x, class, ...) {
stopifnot(inherits(x, "stars"))
if (is.null(names(x)))
names(x) = make.names(seq_along(x)) # FIXME: why? sdsr_exercises
x_crs = st_crs(x)
x = st_upfront(x) # x/y dimensions first
if (length(dim(x)) > 3) {
warning("folding all higher dimensions into the third dimension") # nocov
x = st_apply(x, 1:2, as.vector) # fortunes::fortune("side effect") # nocov
}
if (length(dim(x)) == 2 && length(x) > 1)
x = merge(x)
d = st_dimensions(x)
if (d[[2]]$delta > 0) { # swap:
ny = dim(x)[2]
d[[2]]$offset = d[[2]]$offset + ny * d[[2]]$delta # top
d[[2]]$delta = -d[[2]]$delta # going down
x[[1]] = if (length(dim(x)) == 2)
x[[1]][,ny:1]
else
x[[1]][,ny:1,]
}
dxy = attr(d, "raster")$dimensions
stopifnot(all(dxy %in% names(d)))
bb = st_bbox(x)
levels = NULL
coltab = vector("list", length(x))
values = if (all(sapply(x, is.factor))) {
ex = attr(x[[1]], "exclude")
if (is.null(ex) || class != "SpatRaster")
structure(merge(x)[[1]], dim = NULL) # return the factor
else {
v = vector("list", length(x))
levels = vector("list", length(x))
for (i in seq_along(v)) {
ex = attr(x[[i]], "exclude")
ix = which(!ex) - 1 # 0-based index
n = as.numeric(structure(x[[i]], dim = NULL)) # factor -> numeric
v[[i]] = ix[n]
levels[[i]] = data.frame(IDs = ix, categories = levels(x[[i]]))
if (!is.null(ct <- attr(x[[i]], "colors"))) {
coltab[[i]] = t(col2rgb(rep("#000000", length(ex)), alpha = TRUE))
coltab[[i]][which(!ex),] = t(col2rgb(ct, alpha = TRUE))
}
}
do.call(c, v)
}
} else {
if (any(sapply(x, is.factor)))
warning("mix of factor and non-factor attributes: all factor levels are ignored")
as.vector(merge(x)[[1]])
}
if (class == "SpatRaster") {
third = setdiff(names(d), dxy)
b = terra::rast(nrows = dim(x)[ dxy[2] ], ncols=dim(x)[ dxy[1] ],
xmin = bb[1], xmax = bb[3], ymin = bb[2], ymax = bb[4],
nlyrs = ifelse(length(dim(x)) == 2, 1, dim(x)[third]),
crs = x_crs$wkt)
terra::values(b) = values
if (!is.null(levels)) {
levels(b) = levels
if (!all(sapply(coltab, is.null)))
for (i in seq_len(terra::nlyr(b))) {
m = coltab[[i]]
terra::coltab(b, layer = i) = cbind(value = seq_len(nrow(m)) - 1, m)
}
}
if (length(dim(x)) != 2) {
z = seq(d[[third]])
if (!any(is.na(z))) {
if (is.character(z)) {
names(b) = z
} else {
names(b) = paste0(third, z)
}
}
} else if (length(x) == terra::nlyr(b))
names(b) = names(x)
} else {
if (length(dim(x)) == 2) {
b = raster::raster(nrows=dim(x)[ dxy[2] ], ncols=dim(x)[ dxy[1] ],
xmn = bb[1], xmx = bb[3], ymn = bb[2], ymx = bb[4],
crs = as(x_crs, "CRS"), vals = values)
} else {
third = setdiff(names(d), dxy)
b = raster::brick(nrows=dim(x)[ dxy[2] ], ncols=dim(x)[ dxy[1] ],
xmn = bb[1], xmx = bb[3], ymn = bb[2], ymx = bb[4], nl = dim(x)[third],
crs = as(x_crs, "CRS"))
raster::values(b) = values
z = seq(d[[third]])
if (!any(is.na(z))) {
if (is.character(z))
names(b) = z
else
b = raster::setZ(b, z)
}
}
}
return(b)
}