Visualising and exporting data

This Chapter is intended for programmers and experts who would like to extend the capabilities of sits by including new data sources, machine learning algorithms, exporting data to be used in Python or QGIS, or including new display colors.

How colors work in sits

In examples provided in the book, the color legend is taken from a predefined color table provided by sits. This default color table is displayed using sits_colors_show(). This color definition file assigns colors to 99 class names, including the IPCC and IGBP land classes.

Default colors used in the sits package (Source: Authors).

Figure 95: Default colors used in the sits package (Source: Authors).

The color table can be extended or adjusted by accessing and modifying the default color table, which is retrieved using sits_colors().

# Retrieve the color table
color_tb <- sits_colors()
# Show the color table
color_tb
#> # A tibble: 99 × 3
#>    name                       color   group          
#>    <chr>                      <chr>   <chr>          
#>  1 Evergreen_Broadleaf_Forest #1E8449 Tropical_Forest
#>  2 Forest                     #1E8449 Tropical_Forest
#>  3 Closed_Forest              #1E8449 Tropical_Forest
#>  4 Woodland                   #27AE60 Tropical_Forest
#>  5 Dense_Woodland             #27AE60 Tropical_Forest
#>  6 Woody_Savanna              #27AE60 Tropical_Forest
#>  7 Open_Forest                #27AE60 Tropical_Forest
#>  8 Cerradao                   #27AE60 Tropical_Forest
#>  9 Mixed_Forest               #27AE60 Tropical_Forest
#> 10 Sparse_Forest              #27AE60 Tropical_Forest
#> # ℹ 89 more rows

The default color table can be redefined using sits_colors_set(). As an example of a user-defined color table, consider a definition that covers level 1 of the Anderson Classification System used in the US National Land Cover Data, obtained by defining a new color table, as shown below. The colors can be defined by HEX values or by names accepted as R color codes.

# Define a color table based on the Anderson Land Classification System
us_nlcd <- tibble::tibble(name = character(), color = character())
us_nlcd <- us_nlcd %>%
  tibble::add_row(name = "Urban Built Up", color = "#85929E") %>%
  tibble::add_row(name = "Agricultural Land", color = "#F0B27A") %>%
  tibble::add_row(name = "Rangeland", color = "#F1C40F") %>%
  tibble::add_row(name = "Forest Land", color = "#27AE60") %>%
  tibble::add_row(name = "Water", color = "#2980B9") %>%
  tibble::add_row(name = "Wetland", color = "#D4E6F1") %>%
  tibble::add_row(name = "Barren Land", color = "#FDEBD0") %>%
  tibble::add_row(name = "Tundra", color = "#EBDEF0") %>%
  tibble::add_row(name = "Snow and Ice", color = "#F7F9F9")
# Load the color table into `sits`
sits_colors_set(us_nlcd)
# Show the new color table used by sits
sits_colors_show()
Example of Anderson Land Classification Scheme use in sits (Source: Authors).

Figure 96: Example of Anderson Land Classification Scheme use in sits (Source: Authors).

The original default sits color table can be restored using sits_colors_reset().

As an alternative, a legend can be used directly as a parameter to plot(). Please see the example provided in Section “Map Reclassification” in Chapter Image classification in data cubes.

Exporting data to JSON

Both the data cube and the time series tibble can be exported to exchange formats such as JSON.

library(jsonlite)
# Export the data cube to JSON
jsonlite::write_json(
  x = s2_20LKP_cube_MPC,
  path = "./data_cube.json",
  pretty = TRUE
)

# Export the time series to JSON
jsonlite::write_json(
  x = samples_prodes_4classes,
  path = "./time_series.json",
  pretty = TRUE
)