Design and extensibility considerations
This section is intended for programmers and experts that would like to extend the capabilities of sits
, either by including new data sources, ML algorithms, exporting data to be used in Python or QGIS, or including new display colors.
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
)
Extending the color table
In examples provided in the book, the color legend is taken for the predefined color table provided by sits
. This default color table is displayed using the command sits_colors_show
. This color definition file assigns colors to 99 class names, including the IPCC and IGBP land use classes.
The color table can be extended or adjusted by accessing and modifying the default color table, using the commands sits_colors()
to retrieve the table and sits_colors_set()
to update the table according to user choices, as shown in the example below.
# 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
#> # … with 89 more rows
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()
As an alternative, users may define their own legends and pass them as parameters to to the plot
function. Please see the example provided in Section “Map Reclassification” on Chapter “Image Classification in Data Cubes”.