-
Notifications
You must be signed in to change notification settings - Fork 4
/
viz.R
167 lines (124 loc) · 4.58 KB
/
viz.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
#' Viz-ualize GEE Image/ImageCollections
#' @description This function allows users to quickly view a previously created get_*() object or ee.image.Image.
#' @param x A previously created get_* object or ee.image.Image
#' @param scale A \code{numeric} value indicating scale in meters. 250 (default)
#' @param band A \code{character} indicating what bands/type to visualize.
#' Can select more than one, e.g. c('Red', 'Green', 'Blue').
#' @param palette \code{vector} of a color palette.
#' @param range \code{vector} indicating a numerical range to set min/max visualization.
#' @param gamma \code{numeric} gamma correction factor.
#' @param opacity \code{numeric} transparent display value.
#' @param ... additional arguments for mapview.
#' @note This function uses a scale argument which is used to generate a min and max value for viewing.
#' Because this uses getInfo(), it can take a while
#' depending on the scale. Since this is used for viewing, I would suggest to go bigger on the scale. If a user selects more than one band,
#' the 'up-to' three bands will be overlayed like earth engine.
#' @return A leaflet map.
#' @export
#'
#' @examples \dontrun{
#'
#' # Load Libraries
#'
#' library(rgee)
#' ee_Initialize()
#' library(exploreRGEE)
#'
#' # Bring in data
#' huc <- exploreRGEE::huc
#'
#' ld8 <- get_landsat(huc, method = 'ld8', startDate = '2014-01-01',
#' endDate = '2018-12-31', c.low = 6, c.high = 11)
#'
#' ld8 %>% ee_viz(scale = 30, band = 'NDVI', palette = 'RdYlGn')
#'
#' # or without scale, when min and max are used scale is irrelevant.
#'
#' ld8 %>% ee_viz(min = 0, max = 1, band = 'NDVI', palette = 'RdYlGn')
#'
#' }
#'
#'
ee_viz <- function(x, ...) {
UseMethod('ee_viz')
}
#' @name ee_viz
#' @param geom A sf object to use as 'aoi/geom' for an 'ee.image.Image'.
#' @param ... extra args to pass on
#' @export
ee_viz.ee.image.Image <- function(x,
scale = 1000,
geom = NULL,
band = NULL,
palette = hcl.colors(11, 'RdBu'),
range = NULL,
gamma = NULL,
opacity = NULL,
...){
# error catching
if(missing(x))stop({"Need an 'ee.image.Image'"})
if(is.null(band))stop({"Need to provide band(s)."})
stopifnot(inherits(x, "ee.image.Image"))
if(inherits(geom, c('sf', 'sfc'))){
geom <- setup(geom)
}
viz_helper(x, geom, band, scale, range, gamma, opacity, palette)
}
#' @name ee_viz
#' @param ... extra args to pass on
#' @export
ee_viz.exploreList <- function(x,
scale = 1000,
band = NULL,
palette = hcl.colors(11, 'RdBu'),
range = NULL,
gamma = NULL,
opacity = NULL,
...){
# error catching
if(missing(x))stop({"Need an 'exploreList' class"})
if(is.null(band))stop({"Need to provide band(s)."})
stopifnot(inherits(x, "exploreList"))
geom <- x$geom
x <- x$image
viz_helper(x, geom, band, scale, range, gamma, opacity, palette)
}
#'
#' @title viz_helper
#' @param x image
#' @param geom geometry of image
#' @param band a band(s) input
#' @param scale what to scale the min/max getInfo() by
#' @param range min/max vector
#' @param gamma gamma value
#' @param opacity opacity of image
#' @param palette palette to use
#'
#'
viz_helper <- function(x, geom, band, scale, range, gamma, opacity, palette){
image = x$select(band)
image_tag <- image$bandNames()$getInfo()
if(is.null(range)){
range <- reducers_min_max(image, geom, scale)
}
image <- if(is.null(geom)){image} else {image$clip(geom)}
if(!is.null(geom)){
bbox <- geom$getInfo()
lon <- bbox[["coordinates"]][[1]][[1]][[1]]
lat <- bbox[["coordinates"]][[1]][[1]][[2]]
rgee::Map$setCenter(lon, lat, 8)
}
if(length(band) > 1){
map <- rgee::Map$addLayer(image,
visParams = list(min = range[1], max = range[2],
gamma = gamma),
paste(image_tag, collapse = ', '), opacity = opacity)
} else {
map <- rgee::Map$addLayer(image,
visParams = list(min = range[1], max = range[2],
palette = palette,
gamma = gamma),
image_tag, opacity = opacity)
}
map
}