forked from r-spatial/mapview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcubeView.R
More file actions
277 lines (251 loc) · 8.58 KB
/
Copy pathcubeView.R
File metadata and controls
277 lines (251 loc) · 8.58 KB
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
#' View a RasterStack or RasterBrick as 3-dimensional data cube.
#'
#' @description
#' Create a 3D data cube from a RasterStack or RasterBrick. The cube can be
#' freely rotated so that Hovmoller views of x - z and y - z are possible.
#'
#' @param x a RasterStack or RasterBrick
#' @param at the breakpoints used for the visualisation. See
#' \code{\link{levelplot}} for details.
#' @param col.regions color (palette).See \code{\link{levelplot}} for details.
#' @param na.color color for missing values.
#' @param legend logical. Whether to plot a legend.
#' @param ... currently not used.
#'
#' @details
#' The visible layers are alterable by keys: \cr
#' x-axis: LEFT / RIGHT arrow key \cr
#' y-axis: DOWN / UP arrow key \cr
#' z-axis: PAGE_DOWN / PAGE_UP key \cr
#'
#' Note: In RStudio cubeView may show a blank viewer window. In this case open the view in
#' a web-browser (RStudio button at viewer: "show in new window").
#'
#' Note: Because of key focus issues key-press-events are not always
#' recognised within RStudio at Windows. In this case open the view in
#' a web-browser (RStudio button at viewer: "show in new window").
#'
#' Press and hold left mouse-button to rotate the cube.
#' Press and hold right mouse-button to move the cube.
#' Spin mouse-wheel or press and hold middle mouse-button and
#' move mouse down/up to zoom the cube.
#'
#' @author
#' Stephan Woellauer and Tim Appelhans
#'
#' @examples
#' \dontrun{
#' library(raster)
#'
#' kili_data <- system.file("extdata", "kiliNDVI.tif", package = "mapview")
#' kiliNDVI <- stack(kili_data)
#'
#' cubeView(kiliNDVI)
#'
#' clr <- viridisLite::viridis
#' cubeView(kiliNDVI, at = seq(-0.15, 0.95, 0.1), col.regions = clr)
#' }
#'
#' @export cubeView
#' @name cubeView
cubeView <- function(x,
at,
col.regions = mapviewGetOption("raster.palette"),
na.color = mapviewGetOption("na.color"),
legend = TRUE) {
.Deprecated(new = "cubeview::cubeView", package = "mapview",
old = "mapview::cubeView")
stopifnot(inherits(x, "RasterStack") | inherits(x, "RasterBrick"))
#v <- raster::as.matrix(flip(x, direction = "y"))
v <- raster::as.matrix(x)
if (missing(at)) at <- lattice::do.breaks(range(v, na.rm = TRUE), 256)
cols <- lattice::level.colors(v,
at = at,
col.regions)
cols[is.na(cols)] = na.color
cols = col2Hex(cols, alpha = TRUE)
tst <- grDevices::col2rgb(cols, alpha = TRUE)
x_size <- raster::ncol(x)
y_size <- raster::nrow(x)
z_size <- raster::nlayers(x)
leg_fl <- NULL
if (legend) {
## unique temp dir
dir <- tempfile()
dir.create(dir)
rng <- range(x[], na.rm = TRUE)
if (missing(at)) at <- lattice::do.breaks(rng, 256)
leg_fl <- paste0(dir, "/legend", ".png")
png(leg_fl, height = 200, width = 80, units = "px",
bg = "transparent", pointsize = 14, antialias = "none")
rasterLegend(
list(
col = col.regions,
at = at,
height = 0.9,
space = "right"
)
)
dev.off()
}
cubeViewRaw(red = tst[1, ],
green = tst[2, ],
blue = tst[3, ],
x_size = x_size,
y_size = y_size,
z_size = z_size,
leg_fl = leg_fl)
}
# ' View a cube of 3-dimensional data filled with points (voxels).
# '
# ' A variation of Hovmoeller diagram: Each voxel is colored with a RGB-color (or grey) value.
# '
# ' @param x_size integer. size of x-dimension
# '
# ' @param y_size integer. size of y-dimension
# '
# ' @param z_size integer. size of z-dimension
# '
# ' @param grey optional integer vector with 0 <= value <= 255.
# '
# ' @param red optional integer vector with 0 <= value <= 255.
# '
# ' @param green optional integer vector with 0 <= value <= 255.
# '
# ' @param blue optional integer vector with 0 <= value <= 255.
# '
# ' @details
# '
# ' The cube faces show a selectable layer of data within the cube.
# '
# ' The visible layers are alterable by keys:
# '
# ' x-axis: LEFT / RIGHT arrow key
# '
# ' y-axis: DOWN / UP arrow key
# '
# ' z-axis: PAGE_DOWN / PAGE_UP key
# '
# ' Note: Because of key focus issues key-press-events are not always
# ' recognised within RStudio at Windows.
# ' In this case open the view in a web-browser (RStudio button: "show in new window").
# '
# '
# ' Press and hold left mouse-button to rotate the cube.
# '
# ' Press and hold right mouse-button to move the cube.
# '
# ' Spin mouse-wheel or press and hold middle mouse-button and move mouse
# ' down/up to zoom the cube.
# '
# ' Press SPACE to toggle showing cross section lines on the cube.
# '
# ' The color resp. grey vectors contain sequentially values of each voxel.
# ' So each vector is length == x_size * y_size * z_size.
# ' Color component values overwrite grey values.
# '
# ' Sequence of coordinates (x,y,z) for values in vectors:
# '
# ' (1,1,1), (2,1,1), (3,1,1), ... (1,2,1), (2,2,1), (3,2,1), ... (1,1,2), (2,1,2), (3,1,2), ...
# '
# '
# ' @author
# ' Stephan Woellauer
# '
# ' @import htmlwidgets
# '
# ' @export
cubeViewRaw <- function(grey = NULL,
red = NULL,
green = NULL,
blue = NULL,
x_size,
y_size,
z_size,
width = NULL,
height = NULL,
leg_fl = NULL) {
total_size <- x_size*y_size*z_size
object_list <- list(x_size = x_size,
y_size = y_size,
z_size = z_size,
legend = !is.null(leg_fl))
if(!is.null(grey)) {
if(length(grey)!=total_size) {
stop("length of grey vector not correct: ", length(grey), " should be ", total_size)
}
object_list <- c(object_list, list(grey=base64enc::base64encode(as.raw(as.integer(grey)))))
}
if(!is.null(red)) {
if(length(red)!=total_size) {
stop("length of red vector not correct: ", length(red), " should be ", total_size)
}
object_list <- c(object_list, list(red=base64enc::base64encode(as.raw(as.integer(red)))))
}
if(!is.null(green)) {
if(length(green)!=total_size) {
stop("length of green vector not correct: ", length(green), " should be ", total_size)
}
object_list <- c(object_list, list(green=base64enc::base64encode(as.raw(as.integer(green)))))
}
if(!is.null(blue)) {
if(length(blue)!=total_size) {
stop("length of blue vector not correct: ", length(blue), " should be ", total_size)
}
object_list <- c(object_list, list(blue=base64enc::base64encode(as.raw(as.integer(blue)))))
}
deps <- list()
if(!is.null(leg_fl)) {
images_dir <- dirname(leg_fl)
legend_file <- basename(leg_fl)
attachments <- list(legend=legend_file)
dep1 <- htmltools::htmlDependency(name = "images", version = "1", src = c(file = images_dir), attachment = attachments, all_files = FALSE)
deps <- list(dep1)
}
# create widget
htmlwidgets::createWidget(
name = 'cubeView',
x = object_list,
width = width,
height = height,
package = 'mapview',
sizingPolicy = htmlwidgets::sizingPolicy(padding = 0, browser.fill = TRUE),
dependencies = deps
)
}
#' Widget output function for use in Shiny
#'
#' @param outputId Output variable to read from
#' @param width,height the width and height of the map
#' (see \code{\link{shinyWidgetOutput}})
#'
#' @export
cubeViewOutput <- function(outputId, width = '100%', height = '400px'){
.Deprecated(new = "cubeview::cubeViewOutput", package = "mapview",
old = "mapview::cubeViewOutput")
htmlwidgets::shinyWidgetOutput(outputId, 'cubeView',
width, height, package = 'mapview')
}
#' Widget render function for use in Shiny
#'
#' @param expr An expression that generates an HTML widget
#' @param env The environment in which to evaluate expr
#' @param quoted Is expr a quoted expression (with quote())?
#' This is useful if you want to save an expression in a variable
#'
#' @export
renderCubeView <- function(expr, env = parent.frame(), quoted = FALSE) {
.Deprecated(new = "cubeview::renderCubeView", package = "mapview",
old = "mapview::renderCubeView")
if (!quoted) { expr <- substitute(expr) } # force quoted
htmlwidgets::shinyRenderWidget(expr, cubeViewOutput, env, quoted = TRUE)
}
## cubeview ===============================================================
#' @describeIn cubeView alias for ease of typing
#' @aliases cubeview
#' @export cubeview
cubeview <- function(...) {
.Deprecated(new = "cubeview::cubeview", package = "mapview",
old = "mapview::cubeview")
cubeView(...)
}