Create interactive visualizations of spatial EE objects
(ee$FeatureCollection, ee$ImageCollection, ee$Geometry, ee$Feature, and
ee$Image.) using leaflet
in the backend.
Map
An object of class environment with the following functions:
addLayer(eeObject, visParams, name = NULL, shown = TRUE,
opacity = 1, titiler_viz_convert = TRUE,
titiler_server = "https://api.cogeo.xyz/"): Adds a given EE object to the
map as a layer.
eeObject: The object to add to the interactive map.
visParams: List of parameters for visualization.
See details.
name: The name of the layer.
shown: A flag indicating whether the
layer should be on by default.
opacity: The layer's opacity is represented as a number
between 0 and 1. Defaults to 1.
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.
titiler_server: TiTiler endpoint. Defaults to "https://api.cogeo.xyz/".
addLayers(eeObject, visParams, name = NULL, shown = TRUE,
opacity = 1): Adds a given ee$ImageCollection to the map
as multiple layers.
eeObject: The ee$ImageCollection to add to the interactive map.
visParams: List of parameters for visualization.
See details.
name: The name of layers.
shown: A flag indicating whether
layers should be on by default.
opacity: The layer's opacity is represented as a number
between 0 and 1. Defaults to 1.
nmax: Numeric. The maximum number of images to display. By default 5.
addLegend(visParams, name = "Legend", position = c("bottomright",
"topright", "bottomleft", "topleft"), color_mapping= "numeric", opacity = 1, ...):
Adds a given ee$ImageCollection to the map as multiple layers.
visParams: List of parameters for visualization.
name: The title of the legend.
position: Character. The position of the legend. By default bottomright.
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.
opacity: The legend's opacity is represented as a number between 0
and 1. Defaults to 1.
...: Extra legend creator arguments. See addLegend.
setCenter(lon = 0, lat = 0, zoom = NULL): Centers the map view at the given coordinates with the given zoom level. If no zoom level is provided, it uses 1 by default.
lon: The longitude of the center, in degrees.
lat: The latitude of the center, in degrees.
zoom: The zoom level, from 1 to 24.
setZoom(zoom = NULL): Sets the zoom level of the map.
zoom: The zoom level, from 1 to 24.
centerObject(eeObject, zoom = NULL, maxError = ee$ErrorMargin(1)): 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.
eeObject: EE object.
zoom: The zoom level, from 1 to 24.
maxError: Max error when input image must be reprojected to an explicitly requested result projection or geodesic state.
Object of class leaflet, with the following extra parameters: tokens, name, opacity, shown, min, max, palette, and legend. Use the $ method to retrieve the data (e.g. m$rgee$min).
Map
use the Earth Engine method
getMapId to fetch and return an ID dictionary being used to create
layers in a 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
ee$Image
, the
parameters available are:
Parameter | Description | Type |
bands | Comma-delimited list of three band (RGB) | list |
min | Value(s) to map to 0 | number or list of three numbers, one for each band |
max | Value(s) to map to 1 | number or list of three numbers, one for each band |
gain | Value(s) by which to multiply each pixel value | number or list of three numbers, one for each band |
bias | Value(s) to add to each Digital Number value | number or list of three numbers, one for each band |
gamma | Gamma correction factor(s) | number or list of three numbers, one for each band |
palette | List of CSS-style color strings (single-band only) | comma-separated list of hex strings |
opacity | The opacity of the layer (from 0 to 1) | number |
If you add an 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
ee$Geometry
, ee$Feature
, and ee$FeatureCollection
are:
color: A hex string in the format RRGGBB specifying the color to use for drawing the features. By default #000000.
pointRadius: The radius of the point markers. By default 3.
strokeWidth: The width of lines and polygon borders. By default 3.
if (FALSE) { # \dontrun{
library(rgee)
library(sf)
ee_Initialize()
# Case 1: Geometry*
geom1 <- ee$Geometry$Point(list(-73.53, -15.75))
Map$centerObject(geom1, zoom = 8)
m1 <- Map$addLayer(
eeObject = geom1,
visParams = list(
pointRadius = 10,
color = "FF0000"
),
name = "Geometry-Arequipa"
)
# Case 2: Feature
feature_arq <- ee$Feature(ee$Geometry$Point(list(-72.53, -15.75)))
m2 <- Map$addLayer(
eeObject = feature_arq,
name = "Feature-Arequipa"
)
m2 + m1
# Case 4: Image
image <- ee$Image("LANDSAT/LC08/C01/T1/LC08_044034_20140318")
Map$centerObject(image)
m4 <- Map$addLayer(
eeObject = image,
visParams = list(
bands = c("B4", "B3", "B2"),
max = 10000
),
name = "SF"
)
# Case 5: ImageCollection
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())
m5 <- Map$addLayers(ee_s2)
m5
# Case 6: Map comparison
image <- ee$Image("LANDSAT/LC08/C01/T1/LC08_044034_20140318")
Map$centerObject(image)
m_ndvi <- Map$addLayer(
eeObject = image$normalizedDifference(list("B5", "B4")),
visParams = list(min = 0, max = 0.7),
name = "SF_NDVI"
) + Map$addLegend(list(min = 0, max = 0.7), name = "NDVI", position = "bottomright", bins = 4)
m6 <- m4 | m_ndvi
m6
# Case 7: digging up the metadata
m6$rgee$tokens
m5$rgee$tokens
# Case 8: COG support
# See parameters here: https://api.cogeo.xyz/docs
server <- "https://storage.googleapis.com/pdd-stac/disasters/"
file <- "hurricane-harvey/0831/20170831_172754_101c_3B_AnalyticMS.tif"
resource <- paste0(server, file)
visParams <- list(bands = c("B3", "B2", "B1"), min = 3000, max = 13500, nodata = 0)
Map$centerObject(resource)
Map$addLayer(resource, visParams = visParams, shown = TRUE)
} # }