R6Map: R6 class to display Earth Engine (EE) spatial objects

R6MapR Documentation

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 leaflet.

Details

R6Map uses the Earth Engine method getMapId to fetch and return an ID dictionary 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.

Value

Object of class leaflet and 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).

Public fields

lon

The longitude of the center, in degrees.

lat

The latitude of the center, in degrees.

zoom

The zoom level, from 1 to 24.

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.

previous_map_left

Container on maps in the left side.

previous_map_right

Container on maps in the right side.

Methods

Public methods


Method new()

Constructor of R6Map.

Usage
R6Map$new(lon = 0, lat = 0, zoom = 1, save_maps = TRUE)
Arguments
lon

The longitude of the center, in degrees. By default -76.942478.

lat

The latitude of the center, in degrees. By default -12.172116.

zoom

The zoom level, from 1 to 24. By default 18.

save_maps

Should R6Map save previous maps?.

Returns

A new EarthEngineMap object.


Method reset()

Reset to initial arguments.

Usage
R6Map$reset(lon = 0, lat = 0, zoom = 1, save_maps = TRUE)
Arguments
lon

The longitude of the center, in degrees. By default -76.942478.

lat

The latitude of the center, in degrees. By default -12.172116.

zoom

The zoom level, from 1 to 24. By default 18.

save_maps

Should R6Map save previous maps?.

Returns

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
}

Method print()

Display a EarthEngineMap object.

Usage
R6Map$print()
Returns

An EarthEngineMap object.


Method setCenter()

Centers the map view at the given coordinates with the given zoom level. If no zoom level is provided, it uses 10 by default.

Usage
R6Map$setCenter(lon = 0, lat = 0, zoom = 10)
Arguments
lon

The longitude of the center, in degrees. By default -76.942478.

lat

The latitude of the center, in degrees. By default -12.172116.

zoom

The zoom level, from 1 to 24. By default 18.

Returns

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
}

Method setZoom()

Sets the zoom level of the map.

Usage
R6Map$setZoom(zoom = 10)
Arguments
zoom

The zoom level, from 1 to 24. By default 10.

Returns

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
}

Method centerObject()

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.

Usage
R6Map$centerObject(
  eeObject,
  zoom = NULL,
  maxError = ee$ErrorMargin(1),
  titiler_server = "https://api.cogeo.xyz/"
)
Arguments
eeObject

Earth Engine spatial object.

zoom

The zoom level, from 1 to 24. By default NULL.

maxError

Max error when input image must be reprojected to an explicitly requested result projection or geodesic state.

titiler_server

TiTiler endpoint. Defaults to "https://api.cogeo.xyz/".

Returns

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
}

Method addLayer()

Adds a given Earth Engine spatial object to the map as a layer

Usage
R6Map$addLayer(
  eeObject,
  visParams = NULL,
  name = NULL,
  shown = TRUE,
  opacity = 1,
  position = NULL,
  titiler_viz_convert = TRUE,
  titiler_server = "https://api.cogeo.xyz/"
)
Arguments
eeObject

The Earth Engine spatial object to display in 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.

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).

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/".

Returns

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()
}

Method addLayers()

Adds a given ee$ImageCollection to the map as multiple layers.

Usage
R6Map$addLayers(
  eeObject,
  visParams = NULL,
  nmax = 5,
  name = NULL,
  shown = TRUE,
  position = NULL,
  opacity = 1
)
Arguments
eeObject

ee$ImageCollection to display in the interactive map.

visParams

List of parameters for visualization. See details.

nmax

Numeric. The maximum number of images to display. By default 5.

name

The name of layers.

shown

A flag indicating whether layers should be on by default.

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).

opacity

The layer's opacity is represented as a number between 0 and 1. Defaults to 1.

Returns

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()
}

Method addLegend()

Adds a color legend to an EarthEngineMap.

Usage
R6Map$addLegend(
  visParams,
  name = "Legend",
  position = c("bottomright", "topright", "bottomleft", "topleft"),
  color_mapping = "numeric",
  opacity = 1,
  ...
)
Arguments
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.

Returns

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")
}

Method clone()

The objects of this class are cloneable with this method.

Usage
R6Map$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples


## ------------------------------------------------
## Method `R6Map$reset`
## ------------------------------------------------

## Not run: 
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

## End(Not run)

## ------------------------------------------------
## Method `R6Map$setCenter`
## ------------------------------------------------

## Not run: 
library(rgee)

ee_Initialize()

Map <- R6Map$new()
Map$setCenter(lon = -76, lat = 0, zoom = 5)
Map

# Map$lat
# Map$lon
# Map$zoom

## End(Not run)

## ------------------------------------------------
## Method `R6Map$setZoom`
## ------------------------------------------------

## Not run: 
library(rgee)

ee_Initialize()

Map <- R6Map$new()
Map$setZoom(zoom = 4)
Map

# Map$lat
# Map$lon
# Map$zoom

## End(Not run)

## ------------------------------------------------
## Method `R6Map$centerObject`
## ------------------------------------------------

## Not run: 
library(rgee)

ee_Initialize()

Map <- R6Map$new()
image <- ee$Image("LANDSAT/LC08/C01/T1/LC08_044034_20140318")
Map$centerObject(image)
Map

## End(Not run)

## ------------------------------------------------
## Method `R6Map$addLayer`
## ------------------------------------------------

## Not run: 
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()

## End(Not run)

## ------------------------------------------------
## Method `R6Map$addLayers`
## ------------------------------------------------

## Not run: 
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()

## End(Not run)

## ------------------------------------------------
## Method `R6Map$addLegend`
## ------------------------------------------------

## Not run: 
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")

## End(Not run)

r-spatial/rgee documentation built on July 4, 2024, 9:33 a.m.