Image Visualization

2020-01-05 1507 words 8 mins read

Adapted from Google Earth Engine Documentation.

You can visualize an image using Map$addLayer(). If you add a layer to the map without any additional parameters, by default the rgee assigns the first three bands to red, green and blue, respectively. The default stretch is based on the type of data in the band (e.g. floats are stretched in [0,1], 16-bit data are stretched to the full range of possible values), which may or may not be suitable. To achieve desirable visualization effects, you can provide visualization parameters to Map$addLayer(). Specifically, the parameters are:

Visualization parameters for Map$addLayer()
Parameter Description Type
bands Comma-delimited list of three band names to be mapper to 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 255 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 DN 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 images only) comma-separated list hex strings
opacity The opacity of the layer (0.0 is fully transparent and 1.0 is fully opaque) number

1. RGB composites

The following illustrates the use of parameters to style a Landsat 8 image as a false-color composite:

library(rgee)
ee_Initialize()

# Load an image.
landsat <- ee$Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318')

# Define the visualization parameters.
vizParams <- list(
  bands = c('B5', 'B4', 'B3'),
  min = 0,
  max = 0.5,
  gamma = c(0.95, 1.1, 1)
)

# Center the map and display the image.
Map$setCenter(lon = -122.1899, lat = 37.5010, zoom = 10) # San Francisco Bay
Map$addLayer(landsat, vizParams, 'false color composite')

In this example, band ‘B5’ is assigned to red, ‘B4’ is assigned to green, and ‘B3’ is assigned to blue. The result should look something like Figure 1.


Figure N°01: Landsat 8 false color composite of San Francisco bay area, California, USA.

2. Color palettes

To display a single band of an image in color, set the palette parameter with a color ramp represented by a list of CSS-style color strings. (See this reference for more information). The following example illustrates how to use colors from cyan (‘00FFFF’) to blue (‘0000FF’) to render a Normalized Difference Water Index (NDWI) image:

# Load an image.
landsat <- ee$Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318')

# Create an NDWI image.
ndwi <- landsat$normalizedDifference(c('B3', 'B5'))

# define visualization parameters and display image.
ndwiViz <- list(
  min = 0.5,
  max = 1,
  palette = c('00FFFF', '0000FF')
)

Map$addLayer(
  eeObject = ndwi,
  visParams = ndwiViz,
  name = 'NDWI',
  shown = FALSE
)

In this example, note that the min and max parameters indicate the range of pixel values to which the palette should be applied. Intermediate values are linearly stretched. Also note that if you define shown parameter as FALSE, this results in the visibility of the layer being off when it is added to the map. It can always be turned on again using the Layer Manager in the upper left corner of the map. The result should look something like Figure 2.


Figure N°02: Landsat 8 NDWI, San Francisco bay area, USA. Same area as Figure 1. Cyan are low values, blue are high values.

3. Masking

You can use image$updateMask() to set the opacity of individual pixels based on where pixels in a mask image are non-zero. Pixels equal to zero in the mask are excluded from computations and the opacity is set to 0 for display. The following example uses an NDWI threshold to update the mask on the NDWI layer created previously:

# Mask the non-watery parts of the image, where NDWI < 0.4.
ndwiMasked <- ndwi$updateMask(ndwi$gte(0.4))
Map$addLayer(ndwiMasked, ndwiViz, 'NDWI masked')

Figure N°03: Landsat 8 NDWI, same area as Figure 2, was masked for values greater than 0,4.

4. Visualization images

Use the image$visualize() method to convert an image into an 8-bit RGB image for display or export. For example, to convert the false-color composite and NDWI to 3-band display images, use:

# Load an image.
landsat <- ee$Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318')

# Create visualization layers.
imageRGB <- landsat$visualize(
  list(
    bands = list("B5", "B4", "B3"),
    max = 0.5
  )
)

ndwiRGB <- ndwiMasked$visualize(
  list(
    min = 0.5,
    max = 1,
    palette = c('00FFFF', '0000FF')
  )
)

5. Mosaicking

You can use masking and imageCollection$mosaic() to achieve various cartographic effects. The mosaic() method renders layers in the output image according to their order in the input collection. The following example uses mosaic() to combine the masked NDWI and the false color composite and obtain a new visualization:

# Mosaic the visualization layers and display (or export).
mosaic <- ee$ImageCollection(list(imageRGB, ndwiRGB))$mosaic()
Map$addLayer(eeObject = mosaic, list(), name = 'mosaic')

In this example, observe that a list of the two visualization images is provided to the ImageCollection constructor. The order of the list determines the order in which the images are rendered on the map. The result should look something like Figure 3.


Figure N°04: Mosaic of a Landsat 8 false color composite and NDWI. San Francisco bay area, USA.

6. Integration with other R packages

Map$addLayer() creates a leaflet object with the following extra attributes: tokens, name, opacity, shown, min, max, palette, and legend. This extra data help to the users to customize their interactive maps and/or integrate Map$addLayer with other R packages such as {mapview}, {mapedit} and {leaflet}.

library(rgee)
ee_Initialize()

# Load an image.
landsat <- ee$Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318')

# Define the visualization parameters.
vizParams <- list(
  bands = c('B5', 'B4', 'B3'),
  min = 0,
  max = 0.5,
  gamma = c(0.95, 1.1, 1)
)

# Center the map and display the image.
Map$setCenter(lon = -122.1899, lat = 37.5010, zoom = 10) # San Francisco Bay
m1 <- Map$addLayer(landsat, vizParams, 'false color composite')
m1$rgee

#> $tokens
#> [1] "https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/maps/6f68ea5563f0787171eef294011e5b1a-54a29dea5a564831913fb1c8c1653628/tiles/{z}/{x}/{y}"
#> 
#> $name
#> [1] "false color composite"
#> 
#> $opacity
#> [1] 1
#> 
#> $shown
#> [1] TRUE
#> 
#> $min
#> [1] NA
#> 
#> $max
#> [1] NA
#> 
#> $palette
#> $palette[[1]]
#> [1] NA
#> 
#> 
#> $legend
#> [1] FALSE

{leaflet}: Is a R package binding (develop by Rstudio) for leaflet a open-source JavaScript library for mobile-friendly interactive maps.

library(leaflet)
library(rgee)
ee_Initialize()

# Load an image.
landsat <- ee$Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318')

# Define the visualization parameters.
vizParams <- list(
  bands = c('B5', 'B4', 'B3'),
  min = 0,
  max = 0.5,
  gamma = c(0.95, 1.1, 1)
)

# Center the map and display the image.
Map$setCenter(lon = -122.1899, lat = 37.5010, zoom = 10) # San Francisco Bay
m1 <- Map$addLayer(landsat, vizParams, 'false color composite')

# Integrate with leaflet
leaflet() %>% 
  addTiles() %>% 
  setView(-122.1899, 37.5010, 9) %>% 
  addTiles(
    urlTemplate = m1$rgee$tokens,
    layerId = "leaflet_false_color",
    options = leaflet::tileOptions(opacity = 1)
  )

Figure N°05: Map$addLayer and {leaflet} integration

{mapview}: R package develop by Tim Appelhans which provides functions to very quickly and conveniently create interactive visualisations of R spatial data. It support the most popular R package for spatial data ({sp}, {sf}, {stars}, and {raster}).

library(mapview)
library(rgee)

ee_Initialize()

# Load an image.
landsat <- ee$Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318')

# Define the visualization parameters.
vizParams <- list(
  bands = c('B5', 'B4', 'B3'),
  min = 0,
  max = 0.5,
  gamma = c(0.95, 1.1, 1)
)

# Center the map and display the image.
Map$setCenter(lon = -122.1899, lat = 37.5010, zoom = 10) # San Francisco Bay
m1 <- Map$addLayer(landsat, vizParams, 'false color composite')

# Integrate with mapview
stp <- st_sfc(st_point(c(-122.27234, 37.46941)), crs=4326)
mapview(stp, m1)

Figure N°06:Map$addLayer and {mapview} integration.

{mapedit}: adds spatial data edition functionality to leaflet interactive maps (Similar to the Code Editor geometry tool).

library(mapedit)
library(rgee)

ee_Initialize()

# Load an image.
landsat <- ee$Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318')

# Define the visualization parameters.
vizParams <- list(
  bands = c('B5', 'B4', 'B3'),
  min = 0,
  max = 0.5,
  gamma = c(0.95, 1.1, 1)
)

# Center the map and display the image.
Map$setCenter(lon = -122.1899, lat = 37.5010, zoom = 10) # San Francisco Bay
m1 <- Map$addLayer(landsat, vizParams, 'false color composite')

# Integrate with mapedit
my_geom <- editMap(m1)$drawn

Figure N°07:Map$addLayer and {mapedit} integration.

7. Map Operators

From the version 1.0.5, rgee support two map operators:

library(mapedit)
library(rgee)

ee_Initialize()

# Load an image.
landsat <- ee$Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318')
ndwi <- landsat$normalizedDifference(c('B3', 'B5'))
ndwiMasked <- ndwi$updateMask(ndwi$gte(0.4))


# Define the visualization parameters.
vizParams <- list(
  bands = c('B5', 'B4', 'B3'),
  min = 0,
  max = 0.5,
  gamma = c(0.95, 1.1, 1)
)

ndwiViz <- list(
  min = 0.5,
  max = 1,
  palette = c('00FFFF', '0000FF')
)

# Center the map and display the image.
Map$setCenter(lon = -122.1899, lat = 37.5010, zoom = 10) # San Francisco Bay
m1 <- Map$addLayer(landsat, vizParams, 'false color composite')
m2 <- Map$addLayer(ndwiMasked, ndwiViz, 'NDWI masked')
  • m1 + m2: Overlay layers.
m1 + m2

Figure N°08: Overlay map example

  • m1|m2: Side by side view.
m1 | m2

Figure N°08: Side by side example


author
Fernando Prudencio

This article is licensed under a Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it