-
Notifications
You must be signed in to change notification settings - Fork 148
/
R6Map.R
1291 lines (1216 loc) · 45.2 KB
/
R6Map.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' R6 class to display Earth Engine (EE) spatial objects
#'
#' @description Create interactive visualizations of spatial EE objects
#' (ee$Geometry, ee$Image, ee$Feature, and ee$FeatureCollection)
#' using \code{leaflet}.
#'
#' @details
#' `R6Map` uses the Earth Engine method
#' \href{https://developers.google.com/earth-engine/api_docs#ee.data.getmapid/}{
#' getMapId} to fetch and return an ID dictionary used to create
#' layers in a \code{leaflet} object. Users can specify visualization
#' parameters to Map$addLayer by using the visParams argument. Each Earth
#' Engine spatial object has a specific format. For
#' \code{ee$Image}, the
#' \href{https://developers.google.com/earth-engine/guides/image_visualization}{
#' parameters} available are:
#'
#' \tabular{lll}{
#' \strong{Parameter}\tab \strong{Description} \tab \strong{Type}\cr
#' \strong{bands} \tab Comma-delimited list of three band (RGB) \tab list \cr
#' \strong{min} \tab Value(s) to map to 0 \tab number or list of three
#' numbers, one for each band \cr
#' \strong{max} \tab Value(s) to map to 1 \tab number or list of three
#' numbers, one for each band \cr
#' \strong{gain} \tab Value(s) by which to multiply each pixel value \tab
#' number or list of three numbers, one for each band \cr
#' \strong{bias} \tab Value(s) to add to each Digital Number
#' value \tab number or list of three numbers, one for each band \cr
#' \strong{gamma} \tab Gamma correction factor(s) \tab number or list of
#' three numbers, one for each band \cr
#' \strong{palette} \tab List of CSS-style color strings
#' (single-band only) \tab comma-separated list of hex strings \cr
#' \strong{opacity} \tab The opacity of the layer (from 0 to 1) \tab number \cr
#' }
#'
#' If you add an \code{ee$Image} to Map$addLayer without any additional
#' parameters. By default it assigns the first three bands to red,
#' green, and blue bands, respectively. The default stretch is based on the
#' min-max range. On the other hand, the available parameters for
#' \code{ee$Geometry}, \code{ee$Feature}, and \code{ee$FeatureCollection}
#' are:
#'
#' \itemize{
#' \item \strong{color}: A hex string in the format RRGGBB specifying the
#' color to use for drawing the features. By default #000000.
#' \item \strong{pointRadius}: The radius of the point markers. By default 3.
#' \item \strong{strokeWidth}: The width of lines and polygon borders. By
#' default 3.
#' }
#'
#' @returns Object of class \code{leaflet} and \code{EarthEngineMap}, with the
#' following extra parameters: tokens, name, opacity, shown, min, max, palette,
#' position, and legend. Use the $ method to retrieve the data (e.g., m$rgee$min).
#' @export
R6Map <- R6::R6Class(
classname = "EarthEngineMap",
public = list(
#' @field lon The longitude of the center, in degrees.
lon = NULL,
#' @field lat The latitude of the center, in degrees.
lat = NULL,
#' @field zoom The zoom level, from 1 to 24.
zoom = NULL,
#' @field save_maps Should `R6Map` save the previous maps?. If TRUE, Map
#' will work in an OOP style. Otherwise it will be a functional programming
#' style.
save_maps = NULL,
#' @field previous_map_left Container on maps in the left side.
previous_map_left = NULL,
#' @field previous_map_right Container on maps in the right side.
previous_map_right = NULL,
#' @description Constructor of R6Map.
#' @param lon The longitude of the center, in degrees. By default -76.942478.
#' @param lat The latitude of the center, in degrees. By default -12.172116.
#' @param zoom The zoom level, from 1 to 24. By default 18.
#' @param save_maps Should `R6Map` save previous maps?.
#' @return A new `EarthEngineMap` object.
initialize = function(lon = 0, lat = 0, zoom = 1, save_maps = TRUE) {
self$lon <- lon
self$lat <- lat
self$zoom <- zoom
self$save_maps <- save_maps
self$previous_map_left = private$ee_mapview()
self$previous_map_right = private$ee_mapview()
},
#' @description Reset to initial arguments.
#' @param lon The longitude of the center, in degrees. By default -76.942478.
#' @param lat The latitude of the center, in degrees. By default -12.172116.
#' @param zoom The zoom level, from 1 to 24. By default 18.
#' @param save_maps Should `R6Map` save previous maps?.
#' @return A new `EarthEngineMap` object.
#' @examples
#' \dontrun{
#' library(rgee)
#' ee_Initialize()
#'
#' # Load an Image
#' image <- ee$Image("LANDSAT/LC08/C01/T1/LC08_044034_20140318")
#'
#' # Create
#' Map <- R6Map$new()
#' Map$centerObject(image)
#'
#' # Simple display: Map just will
#' Map$addLayer(
#' eeObject = image,
#' visParams = list(min=0, max = 10000, bands = c("B4", "B3", "B2")),
#' name = "l8_01"
#' )
#' Map # display map
#'
#' Map$reset() # Reset arguments
#' Map
#' }
reset = function(lon = 0, lat = 0, zoom = 1, save_maps = TRUE) {
self$lon <- lon
self$lat <- lat
self$zoom <- zoom
self$save_maps <- save_maps
self$previous_map_left = private$ee_mapview()
self$previous_map_right = private$ee_mapview()
},
#' @description
#' Display a \code{EarthEngineMap} object.
#' @return An `EarthEngineMap` object.
print = function() {
if (isFALSE(self$save_maps)) {
print(private$ee_mapview())
} else {
m1 <- private$get_previous_map_right()
m2 <- private$get_previous_map_left()
if (is.null(m1$rgee$position) & is.null(m2$rgee$position)) {
m3 <- m1
} else {
m3 <- m2 | m1
}
print(m3)
}
},
#' @description
#' Centers the map view at the given coordinates with the given zoom level. If
#' no zoom level is provided, it uses 10 by default.
#' @param lon The longitude of the center, in degrees. By default -76.942478.
#' @param lat The latitude of the center, in degrees. By default -12.172116.
#' @param zoom The zoom level, from 1 to 24. By default 18.
#' @return No return value, called to set initial coordinates and zoom.
#' @examples
#' \dontrun{
#' library(rgee)
#'
#' ee_Initialize()
#'
#' Map <- R6Map$new()
#' Map$setCenter(lon = -76, lat = 0, zoom = 5)
#' Map
#'
#' # Map$lat
#' # Map$lon
#' # Map$zoom
#' }
setCenter = function(lon = 0, lat = 0, zoom = 10) {
if(!is.numeric(lon)) {
stop(
sprintf(
"lon should be object of class numeric not %s.",
bold(class(lon)[1])
)
)
}
if(!is.numeric(lat)) {
stop(
sprintf(
"lat should be object of class numeric not %s.",
bold(class(lat)[1])
)
)
}
if(!is.numeric(zoom)) {
stop(
sprintf(
"zoom should be object of class numeric not %s.",
bold(class(zoom)[1])
)
)
}
private$upgrade_center_right(lon, lat, zoom)
private$upgrade_center_left(lon, lat, zoom)
private$set_lat(lat)
private$set_lon(lon)
private$set_zoom(zoom)
},
#' @description
#' Sets the zoom level of the map.
#' @param zoom The zoom level, from 1 to 24. By default 10.
#' @return No return value, called to set zoom.
#' @examples
#' \dontrun{
#' library(rgee)
#'
#' ee_Initialize()
#'
#' Map <- R6Map$new()
#' Map$setZoom(zoom = 4)
#' Map
#'
#' # Map$lat
#' # Map$lon
#' # Map$zoom
#' }
setZoom = function(zoom = 10) {
if(!is.numeric(zoom)) {
stop(
sprintf(
"zoom should be object of class numeric not %s.",
bold(class(zoom)[1])
)
)
}
private$upgrade_center_right(self$lon, self$lat, zoom)
private$upgrade_center_left(self$lon, self$lat, zoom)
private$set_zoom(zoom)
},
#' @description
#' Centers the map view on a given object. If no zoom level is provided, it
#' will be predicted according to the bounds of the Earth Engine object
#' specified.
#' @param eeObject Earth Engine spatial object.
#' @param zoom The zoom level, from 1 to 24. By default NULL.
#' @param maxError Max error when input image must be reprojected to an
#' explicitly requested result projection or geodesic state.
#' @param titiler_server TiTiler endpoint. Defaults to "https://api.cogeo.xyz/".
#' @return No return value, called to set zoom.
#' @examples
#' \dontrun{
#' library(rgee)
#'
#' ee_Initialize()
#'
#' Map <- R6Map$new()
#' image <- ee$Image("LANDSAT/LC08/C01/T1/LC08_044034_20140318")
#' Map$centerObject(image)
#' Map
#' }
centerObject = function(eeObject,
zoom = NULL,
maxError = ee$ErrorMargin(1),
titiler_server = "https://api.cogeo.xyz/") {
if (inherits(eeObject, "character")) {
viewer_params <- private$centerObject_COG(eeObject, titiler_server)
} else {
viewer_params <- private$get_center(eeObject, zoom, maxError)
}
self$setCenter(viewer_params$lon, viewer_params$lat, viewer_params$zoom)
},
#' @description
#'
#' Adds a given Earth Engine spatial object to the map as a layer
#'
#' @param eeObject The Earth Engine spatial object to display in the interactive map.
#' @param visParams List of parameters for visualization. See details.
#' @param name The name of layers.
#' @param shown A flag indicating whether layers should be on by default.
#' @param opacity The layer's opacity is represented as a number between 0 and 1. Defaults to 1.
#' @param position Character. Activate panel creation. If "left" the map will be displayed in
#' the left panel. Otherwise, if it is "right" the map will be displayed in the right panel.
#' By default NULL (No panel will be created).
#' @param titiler_viz_convert Logical. If it is TRUE, Map$addLayer will transform the
#' visParams to titiler style. Ignored if eeObject is not a COG file.
#' @param titiler_server TiTiler endpoint. Defaults to "https://api.cogeo.xyz/".
#' @return An `EarthEngineMap` object.
#'
#' @examples
#' \dontrun{
#' library(rgee)
#' ee_Initialize()
#'
#' # Load an Image
#' image <- ee$Image("LANDSAT/LC08/C01/T1/LC08_044034_20140318")
#'
#' # Create
#' Map <- R6Map$new()
#' Map$centerObject(image)
#'
#' # Simple display: Map just will
#' Map$addLayer(
#' eeObject = image,
#' visParams = list(min=0, max = 10000, bands = c("B4", "B3", "B2")),
#' name = "l8_01"
#' )
#'
#' Map$addLayer(
#' eeObject = image,
#' visParams = list(min=0, max = 20000, bands = c("B4", "B3", "B2")),
#' name = "l8_02"
#' )
#'
#' # Simple display: Map just will (if the position is not specified it will
#' # be saved on the right side)
#' Map$reset() # Reset Map to the initial arguments.
#' Map$centerObject(image)
#' Map$addLayer(
#' eeObject = image,
#' visParams = list(min=0, max=10000, bands = c("B4", "B3", "B2")),
#' name = "l8_left",
#' position = "left"
#' )
#'
#' Map$addLayer(
#' eeObject = image,
#' visParams = list(min=0, max=20000, bands = c("B4", "B3", "B2")),
#' name = "l8_right"
#' )
#'
#' Map$reset()
#'}
addLayer = function(eeObject,
visParams = NULL,
name = NULL,
shown = TRUE,
opacity = 1,
position = NULL,
titiler_viz_convert = TRUE,
titiler_server = "https://api.cogeo.xyz/") {
# check packages
ee_check_packages("Map$addLayer", c("jsonlite", "leaflet", "leafem"))
if (inherits(eeObject, "character")) {
ee_check_packages("Map$addLayer", c("jsonlite", "leaflet", "leafem", "httr"))
return(private$addCOG(
resource = eeObject,
visParams = visParams,
name = name,
shown = shown,
opacity = opacity,
position = position,
titiler_viz_convert = titiler_viz_convert,
titiler_server = titiler_server
))
}
if (is.null(visParams)) {
visParams <- list()
}
# Remove values element (It is useful for Map$addLegend)
visParams[["values"]] <- NULL
# Earth Engine Spatial object
ee_spatial_object <- ee_get_spatial_objects("Simple")
if (!any(class(eeObject) %in% ee_spatial_object)) {
stop(
"The eeObject argument must be an instance of one",
" of ee$Image, ee$Geometry, ee$Feature, or ee$FeatureCollection."
)
}
if (any(class(eeObject) %in% ee_get_spatial_objects("Table"))) {
features <- ee$FeatureCollection(eeObject)
# If vizparams is NULL
width <- 2
if (!is.null(visParams[["width"]])) {
width <- visParams[["width"]]
}
color <- "000000"
if (!is.null(visParams[["color"]])) {
color <- visParams[["color"]]
}
# Convert features to a images
image_fill <- features %>%
ee$FeatureCollection$style(fillColor = color) %>%
ee$Image$updateMask(ee$Image$constant(0.5))
image_outline <- features %>%
ee$FeatureCollection$style(
color = color,
fillColor = "00000000",
width = width
)
image <- ee$Image$blend(image_fill, image_outline)
} else {
ee_img_viz <- function(...) ee$Image$visualize(eeObject, ...)
image <- do.call(ee_img_viz, visParams)
}
# If name is null try to obtain from image metadata if not untitled_
# would be the name.
if (is.null(name)) {
name <- tryCatch(
expr = ee_get_system_id(eeObject),
error = function(e) basename(tempfile(pattern = "untitled_"))
)
if (is.null(name)) name <- basename(tempfile(pattern = "untitled_"))
}
# Get token
tile <- get_ee_image_url(image)
# Using the previous token create a map using leaflet package
map <- private$ee_addTile(
tile = tile,
name = name,
visParams = visParams,
shown = shown,
opacity = opacity,
position = position
)
if (isTRUE(self$save_maps)) {
# Save the previous map in previous_map_left or previous_map_right
# according to posisa tion argument.
private$save_map(map, position = position)
} else {
map
}
},
#' @description
#'
#' Adds a given ee$ImageCollection to the map as multiple layers.
#'
#' @param eeObject ee$ImageCollection to display in the interactive map.
#' @param visParams List of parameters for visualization. See details.
#' @param nmax Numeric. The maximum number of images to display. By default 5.
#' @param name The name of layers.
#' @param shown A flag indicating whether layers should be on by default.
#' @param opacity The layer's opacity is represented as a number between 0 and 1. Defaults to 1.
#' @param position Character. Activate panel creation. If "left" the map will be displayed in
#' the left panel. Otherwise, if it is "right" the map will be displayed in the right panel.
#' By default NULL (No panel will be created).
#'
#' @return A `EarthEngineMap` object.
#' @examples
#' \dontrun{
#' library(sf)
#' library(rgee)
#'
#' ee_Initialize()
#'
#' Map <- R6Map$new()
#'
#' nc <- st_read(system.file("shape/nc.shp", package = "sf")) %>%
#' st_transform(4326) %>%
#' sf_as_ee()
#'
#' ee_s2 <- ee$ImageCollection("COPERNICUS/S2")$
#' filterDate("2016-01-01", "2016-01-31")$
#' filterBounds(nc)
#' ee_s2 <- ee$ImageCollection(ee_s2$toList(2))
#'
#' Map$centerObject(nc$geometry())
#' Map$addLayers(eeObject = ee_s2,position = "right")
#'
#' # digging up the metadata
#' Map$previous_map_right$rgee$tokens
#'
#' Map$reset()
#' }
addLayers = function(eeObject,
visParams = NULL,
nmax = 5,
name = NULL,
shown = TRUE,
position = NULL,
opacity = 1) {
# check packages
ee_check_packages("Map$addLayers", c("jsonlite", "leaflet"))
# is an ee.imagecollection.ImageCollection?
if (!any(class(eeObject) %in% "ee.imagecollection.ImageCollection")) {
stop("eeObject argument is not an ee$imagecollection$ImageCollection")
}
if (is.null(visParams)) {
visParams <- list()
}
# size of objects
eeObject_size <- eeObject %>%
ee$ImageCollection$size() %>%
ee$Number$getInfo()
m_img_list <- list()
if (is.null(name)) {
# Get names (system:id) for each image from the ImageCollection
name <- tryCatch(
expr = eeObject %>%
ee$ImageCollection$aggregate_array("system:id") %>%
ee$List$getInfo() %>%
ee_utils_py_to_r() %>%
basename(),
error = function(e) sprintf("untitled_%02d", seq_len(eeObject_size))
)
# if name is NULL
if (length(name) == 0 | is.null(name)) name <- sprintf("untitled_%02d", seq_len(eeObject_size))
# all the images from the ee.ImageCollection must have a system:id
if (length(name) != eeObject_size) {
message(
paste0(
"Some ee.Image does not have a 'system:id' property, locating does ee.Image ...",
" This could take some time ..."
)
)
lnames <- rep(NA, eeObject_size)
null_counter <- 1
for (index in seq_len(eeObject_size) - 1) {
lname <- ee_get(eeObject, index = index)$first()$get("system:id")$getInfo()
if (is.null(lname)) {
lname <- sprintf("layer_%02d", null_counter)
null_counter <- null_counter + 1
message(
sprintf("Assigning name %s to the ee.Image of index [%s]", lname, index + 1)
)
}
lnames[index + 1] <- basename(lname)
}
name <- lnames
}
}
if (length(name) == 1) {
name <- sprintf("%s_%02d", name, seq_len(eeObject_size))
}
if (length(name) == length(eeObject_size)) {
stop("name does not have the same length than eeObject$size()$getInfo().")
}
m_img_list <- list()
for (index in seq_len(eeObject_size)) {
py_index <- index - 1
if (index == 1) {
m_img <- self$addLayer(
eeObject = ee_get(eeObject, index = py_index)$first(),
visParams = visParams,
name = name[index],
shown = shown,
opacity = opacity,
position = position
)
} else {
m_img <- self$addLayer(
eeObject = ee_get(eeObject, index = py_index)$first(),
visParams = visParams,
name = name[index],
shown = shown,
opacity = opacity,
position = position
)
}
m_img_list[[index]] <- m_img
}
if (isFALSE(self$save_maps)) {
Reduce('+', m_img_list)
}
},
#' @description
#'
#' Adds a color legend to an EarthEngineMap.
#'
#' @param visParams List of parameters for visualization.
#' @param name The title of the legend.
#' @param position Character. The position of the legend. By default bottomright.
#' @param color_mapping Map data values (numeric or factor/character) to
#' colors according to a given palette. Use "numeric" ("discrete") for continuous
#' (categorical) data. For display characters use "character" and add to visParams
#' the element "values" containing the desired character names.
#' @param opacity The legend's opacity is represented as a number between 0
#' and 1. Defaults to 1.
#' @param ... Extra legend creator arguments. See \link[leaflet]{addLegend}.
#'
#' @return A `EarthEngineMap` object.
#'
#' @examples
#' \dontrun{
#' library(leaflet)
#' library(rgee)
#' ee_Initialize()
#'
#' Map$reset()
#'
#' # Load MODIS ImageCollection
#' imgcol <- ee$ImageCollection$Dataset$MODIS_006_MOD13Q1
#'
#' # Parameters for visualization
#' labels <- c("good", "marginal", "snow", "cloud")
#' cols <- c("#999999", "#00BFC4", "#F8766D", "#C77CFF")
#' vis_qc <- list(min = 0, max = 3, palette = cols, bands = "SummaryQA", values = labels)
#'
#' # Create interactive map
#' m_qc <- Map$addLayer(imgcol$median(), vis_qc, "QC")
#'
#' # continous palette
#' Map$addLegend(vis_qc)
#'
#' # categorical palette
#' Map$addLegend(vis_qc, name = "Legend1", color_mapping = "discrete")
#'
#' # character palette
#' Map$addLegend(vis_qc, name = "Legend2", color_mapping = "character")
#' }
addLegend = function(visParams,
name = "Legend",
position = c("bottomright", "topright", "bottomleft", "topleft"),
color_mapping = "numeric",
opacity = 1,
...) {
if (!is.list(visParams)) {
stop("visParams should be a list")
}
visParams_is_null <- (is.null(visParams[["min"]]) | is.null(visParams[["max"]]))
if (visParams_is_null) {
stop("visParams should have at least the following elements: min and max.")
}
if (is.null(visParams[["palette"]])) {
visParams[["palette"]] <- c("black", "white")
}
# Select one position
position <- match.arg(position)
# Create leaflet color mapping
if (is.character(color_mapping)) {
if (color_mapping == "numeric") {
pal <- leaflet::colorNumeric(visParams$palette, domain = NULL)
values <- c(visParams$min, visParams$max)
} else if (color_mapping == "discrete" | color_mapping == "categorical") {
pal <- leaflet::colorFactor(visParams$palette, domain = NULL)
values <- visParams$min:visParams$max
} else if (color_mapping == "character") {
pal <- leaflet::colorFactor(visParams$palette, domain = NULL)
if (is.null(visParams$values)) {
stop(
"visParams needs the argument values. For instance:\n",
"visParams <- list(palette = c(\"red\",\"blue\",\"green\"), values = LETTERS[1:3])"
)
}
values_chr <- visParams$values
values <- factor(values_chr, levels = values_chr)
}
} else {
stop(
sprintf("color_mapping is a %s. ", class(color_mapping))
)
}
# add legend to the map
extra_args <- list(...)
legend_args <- list(
position = position,
pal = pal,
values = values,
opacity = opacity,
title = name
) %>% append(extra_args)
if (isTRUE(self$save_maps)) {
# Save the previous map in previous_map_left or previous_map_right
# according to position argument.
private$save_map(legend_args, position = NULL)
} else {
legend_args
}
}
),
private = list(
get_previous_map_right = function() {
self$previous_map_right
},
set_previous_map_right = function(val) {
self$previous_map_right <- val
},
get_previous_map_left = function() {
self$previous_map_left
},
set_previous_map_left = function(val) {
self$previous_map_left <- val
},
set_lat = function(val) {
self$lat <- val
},
set_lon = function(val) {
self$lon <- val
},
set_zoom = function(val) {
self$zoom <- val
},
centerObject_COG = function(resource, titiler_server) {
# check packages
ee_check_packages("Map$centerObject_COG", c("jsonlite", "leaflet", "leafem", "httr"))
# COG service
titiler_server_service <- sprintf("%s/%s", titiler_server, "cog/tilejson.json")
# GET tilejson.json
response <- httr::GET(
url = titiler_server_service,
config = httr::accept_json(),
query = list(
"url" = resource
)
)
if (response$status_code != 200) {
message <- httr::content(response, type="application/json")$detail
stop(message)
}
jsonInfo <- httr::content(response, type="application/json")
lon <- jsonInfo$center[[1]]
lat <- jsonInfo$center[[2]]
zoom <- ee_getZoom(jsonInfo)
list(lon = lon, lat = lat, zoom = zoom)
},
convert_eevizparam_to_titiler = function(resource, vizparam) {
# get metadata
metadata_bands <- ee_get_metadata(resource)
# Convert bands to expression
if (is.null(vizparam[["bands"]])) {
expression = "B1"
} else {
band_names <- sapply(
X = seq_along(metadata_bands$band_descriptions),
FUN = function(x) metadata_bands$band_descriptions[[x]][[2]]
)
if (any(sapply(band_names, function(x) x == ""))) {
expression <- paste0(
sprintf(fmt = "%s", vizparam[["bands"]]),
collapse = ", "
)
} else {
binorder <- sapply(
X = seq_along(vizparam[["bands"]]),
FUN = function(x) which(band_names %in% vizparam[["bands"]][x])
)
expression <- paste0(
sprintf(fmt = "B%s", binorder),
collapse = ", "
)
}
}
# Convert min to rescale
if (is.null(vizparam[["min"]])) {
vmin <- 0
} else {
vmin <- vizparam[["min"]]
}
# Convert max to rescale
if (is.null(vizparam[["max"]])) {
vmax <- 1
} else {
vmax <- vizparam[["max"]]
}
rescale <- paste0(c(vmin, vmax), collapse = ", ")
# Convert max to rescale
if (length(strsplit(expression, ",")[[1]]) == 1) {
if (is.null(vizparam[["palette"]])) {
vpalette <- "ocean_r"
} else {
if (length(vpalette) > 2) {
stop(
"Titiler does not support custom colorbar. Please select one",
" from the list below. \n",
"above, accent, accent_r, afmhot, afmhot_r, autumn, ",
"autumn_r, binary, binary_r, blues, blues_r, bone, bone_r, brbg, brbg_r, ",
"brg, brg_r, bugn, bugn_r, bupu, bupu_r, bwr, bwr_r, cfastie, cividis, ",
"cividis_r, cmrmap, cmrmap_r, cool, cool_r, coolwarm, coolwarm_r, copper, ",
"copper_r, cubehelix, cubehelix_r, dark2, dark2_r, flag, flag_r, gist_earth, ",
"gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, ",
"gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, ",
"gist_yarg, gist_yarg_r, gnbu, gnbu_r, gnuplot, gnuplot2, gnuplot2_r, ",
"gnuplot_r, gray, gray_r, greens, greens_r, greys, greys_r, hot, hot_r, ",
"hsv, hsv_r, inferno, inferno_r, jet, jet_r, magma, magma_r, nipy_spectral, ",
"nipy_spectral_r, ocean, ocean_r, oranges, oranges_r, orrd, orrd_r, paired, ",
"paired_r, pastel1, pastel1_r, pastel2, pastel2_r, pink, pink_r, piyg, ",
"piyg_r, plasma, plasma_r, prgn, prgn_r, prism, prism_r, pubu, pubu_r, ",
"pubugn, pubugn_r, puor, puor_r, purd, purd_r, purples, purples_r, rainbow, ",
"rainbow_r, rdbu, rdbu_r, rdgy, rdgy_r, rdpu, rdpu_r, rdylbu, rdylbu_r, ",
"rdylgn, rdylgn_r, reds, reds_r, rplumbo, schwarzwald, seismic, seismic_r, ",
"set1, set1_r, set2, set2_r, set3, set3_r, spectral, spectral_r, spring, ",
"spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, ",
"tab20b_r, tab20c, tab20c_r, terrain, terrain_r, twilight, twilight_r, ",
"twilight_shifted, twilight_shifted_r, viridis, viridis_r, winter, ",
"winter_r, wistia, wistia_r, ylgn, ylgn_r, ylgnbu, ylgnbu_r, ylorbr, ",
"ylorbr_r, ylorrd, ylorrd_r"
)
} else {
vpalette <- vizparam[["palette"]]
}
}
}
# Upgrade your vizparams
vizparam[["expression"]] <- expression
vizparam[["rescale"]] <- rescale
vizparam[["palette"]] <- NULL
vizparam[["min"]] <- NULL
vizparam[["max"]] <- NULL
vizparam[["bands"]] <- NULL
return(vizparam)
},
addCOG = function(resource,
visParams = NULL,
name = NULL,
shown = TRUE,
opacity = 1,
position = NULL,
titiler_viz_convert = TRUE,
titiler_server = "https://api.cogeo.xyz/") {
# check packages
ee_check_packages("Map$addCOG", c("jsonlite", "leaflet", "leafem", "httr"))
# COG service
titiler_server_service <- sprintf("%s/%s", titiler_server, "cog/tilejson.json")
# Remove values element (It is useful for Map$addLegend)
visParams[["values"]] = NULL
# If name is null try to obtain from image metadata if not untitled_
# would be the name.
if (is.null(name)) {
name <- tryCatch(
expr = ee_get_system_id(eeObject),
error = function(e) basename(tempfile(pattern = "untitled_"))
)
if (is.null(name)) name <- basename(tempfile(pattern = "untitled_"))
}
# GET tilejson.json
if (titiler_viz_convert) {
visParams <- private$convert_eevizparam_to_titiler(resource, visParams)
}
response <- httr::GET(
url = titiler_server_service,
config = httr::accept_json(),
query = c(list("url" = resource), visParams)
)
if (response$status_code != 200) {
message <- httr::content(response, type="application/json")$detail
stop(message)
}
jsonInfo <- httr::content(response, type="application/json")
tile <- jsonInfo$tiles[[1]]
# Using the previous token create a map using leaflet package
map <- private$ee_addTile(
tile = tile,
name = name,
visParams = visParams,
shown = shown,
opacity = opacity,
position = position
)
if (isTRUE(self$save_maps)) {
# Save the previous map in previous_map_left or previous_map_right
# according to posisa tion argument.
private$save_map(map, position = position)
} else {
map
}
},
get_center = function(eeObject, zoom, maxError) {
if (any(class(eeObject) %in% "ee.featurecollection.FeatureCollection")) {
message("NOTE: Center obtained from the first element.")
eeObject <- ee$Feature(ee$FeatureCollection$first(eeObject))
}
if (any(class(eeObject) %in% ee_get_spatial_objects("Nongeom"))) {
center <- tryCatch(
expr = eeObject$
geometry()$
centroid(maxError)$
getInfo() %>%
'[['('coordinates') %>%
ee_utils_py_to_r(),
error = function(e) {
message(
"The centroid coordinate was not possible",
" to estimate, assigning: c(0,0)"
)
c(0, 0)
}
)
if (is.null(center)) {
message(
"The centroid coordinate was not possible",
" to estimate, assigning: c(0,0)"
)
center <- c(0, 0)
}
} else if (any(class(eeObject) %in% "ee.geometry.Geometry")) {
center <- tryCatch(
expr = eeObject$
centroid(maxError)$
coordinates()$
getInfo() %>%
ee_utils_py_to_r(),
error = function(e) {
message(
"The centroid coordinate was not possible",
" to estimate, assigning: c(0,0)"
)
c(0, 0)
}
)
} else {
stop("Spatial Earth Engine Object not supported")
}
if (is.null(zoom)) {
zoom <- ee_getZoom(eeObject, maxError = maxError)
}
# Set new initial view
list(lon = center[1], lat = center[2], zoom = zoom)
},
ee_mapview = function() {
# check packages
private$ee_check_packages("ee_mapview", "leaflet")
m <- private$leaflet_default()
m$x$setView[[1]] <- c(self$lat, self$lon)
m$x$setView[[2]] <- if (is.null(self$zoom)) 1 else self$zoom
m
},
ee_addTile = function(tile, name, visParams, shown, opacity, position) {
tile_params <- leaflet::tileOptions(opacity = opacity)
tile_params$maxZoom <- 24
# check packages
ee_check_packages("Map$addLayer", c("leaflet"))
m <- private$ee_mapview() %>%
leaflet::addTiles(
urlTemplate = tile,
layerId = name,
group = name,
options = tile_params
) %>%
ee_mapViewLayersControl(names = name) %>%
leaflet::hideGroup(if (!shown) name else NULL)
# map parameters
m$rgee$tokens <- tile
m$rgee$name <- name
m$rgee$opacity <- opacity
m$rgee$shown <- shown
m$rgee$position <- position
m
},
save_map = function(map, position = NULL) {
if (is.null(position)) {
private$set_previous_map_right(self$previous_map_right + map)
} else {
if (tolower(position) == "right") {
private$set_previous_map_right(self$previous_map_right + map)
} else if (tolower(position) == "left") {
private$set_previous_map_left(self$previous_map_left + map)
} else {
stop("position should be a character either 'right' or 'left'")
}
}
},
leaflet_default = function(lon = -76.942478, lat = -12.172116, zoom = 18, default_maps = NULL) {
if (is.null(default_maps)) {
default_maps <- c(
"CartoDB.Positron", "OpenStreetMap",
"CartoDB.DarkMatter", "Esri.WorldImagery",