Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/brazil-data-cube/rwlts

R Client Library for Web Land Trajectory Service
https://github.com/brazil-data-cube/rwlts

earth-observation earth-science geoinformatics geoscience geospatial land-cover r trajectory

Last synced: about 1 month ago
JSON representation

R Client Library for Web Land Trajectory Service

Awesome Lists containing this project

README

        

---
output: github_document
editor_options:
markdown:
wrap: 72
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
out.width = "100%",
fig.path = "inst/extdata/img/README-"
)
library(tibble)
```

# rwlts

R Client Library for Web Land Trajectory Service (WLTS)

[![Software
License](https://img.shields.io/badge/license-MIT-green)](https://github.com/brazil-data-cube/rstac/blob/master/LICENSE)

[![codecov](https://codecov.io/gh/brazil-data-cube/rwlts/branch/main/graph/badge.svg?token=6WTJI6K93Y)](https://codecov.io/gh/brazil-data-cube/rwlts)
[![Software Life
Cycle](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html)
[![Join us at
Discord](https://img.shields.io/discord/689541907621085198?logo=discord&logoColor=ffffff&color=7389D8)](https://discord.com/channels/689541907621085198#)

# About

Information on Land Use and Land Cover (LULC) is essential to support
governments in making decisions about the impact of human activities on
the environment, planning the use of natural resources, conserving
biodiversity and monitoring climate change.

Currently, several projects systematically provide information on the
dynamics of land use and cover. Well known projects include PRODES,
DETER and TerraClass. These projects are developed by INPE and they
produce information on land use and coverage used by the Brazilian
Government to make public policy decisions. Besides these projects there
are other initiatives from universities and space agencies devoted to
the creation of national and global maps.

Although these projects follow open data policies and provide a rich
collection of data, there is still a gap in tools that facilitate the
integrated use of these collections. Each project adopts its own land
use and land cover classification system, providing different class
names and meanings for the elements of these collections. The forms of
distribution of project data can be carried out in different ways,
through files or web services. In addition, the data has different
spatial and temporal resolutions and storage systems (raster or vector).

In this context, the **W**eb **L**and **T**rajectory **S**ervice (WLTS)
is a service that aims to facilitate the access to these approach
consists of using a data model that defines a minimum set of temporal
and spatial information to represent different sources and types of
data, but with a focus on land use and land cover.

WLTS can be used in a variety of applications, such as validating land
cover data sets, selecting training samples to support Machine Learning
algorithms used in the generation of new classification maps.

If you want to know more about WLTS service, please, take a look at its
[specification](https://github.com/brazil-data-cube/wlts-spec).

## Installation

To install the development version of `rwlts`, run the following
commands:

```{R, eval=FALSE}
# load necessary libraries
library(devtools)
devtools::install_github("brazil-data-cube/rwlts")
```

Importing `rwlts` package:

```{R, echo=TRUE, warning=FALSE, message=FALSE}
library(rwlts)
```

## Usage

`rwlts` implements the following WLTS operations:

```{R, echo=FALSE}
tribble(
~"**WLTS** operations", ~"`rwlts` functions",
"`/list_collections`", "`list_collections(URL,)`",
"`/describe_collections`", "`describe_collection(URL, collection_id)`",
"`/trajectory`", "`get_trajectory(URL, latitude, longitude)`"
) %>% as.data.frame() %>% knitr::kable(format = "markdown")
```

These functions can be used to retrieve information from a WLTS API
service. The code bellow creates a `wlts` object and list the available
collections of the WLTS API of the Brazil Data Cube project of the
Brazilian National Institute for Space Research INPE.

### List Collections

The first operation, `list_collections`, retrieves all available
collections in WLTS service.

```{R, echo=TRUE}
wlts_bdc <- "https://brazildatacube.dpi.inpe.br/wlts/"

list_collections(wlts_bdc)
```

### Describe Collection

Each collection returned by the WLTS service can be used for retrieving
LULC trajectories. To get information about the collections, we use the
`describe_collection` function. This function returns the metadata of a
given collection, which includes the description and specific details.
The metadata also describes the spatio-temporal extent of the
collection.

In the code below, we retrieve the metadata from the
`deter_amazonia_legal` collection using the `describe_collection`
function.

```{R, echo=TRUE}
describe_collection(wlts_bdc, "deter_amazonia_legal")
```

### Trajectory

LULC trajectories can be extracted from collections. These LULC
trajectories in WLTS services are associated with a point (lat, long) in
geographic space. In `rwlts`, we can use the `get_trajectory` function
to retrieve trajectories. For example, in the code below, the point
`(-54, -12)` trajectory is retrieved from the `mapbiomas_amazonia-v5`
collection.

```{r}
get_trajectory(wlts_bdc,
latitude = -12,
longitude = -54,
collections = "mapbiomas_amazonia-v5",
config = httr::add_headers("x-api-key" = "change-me"))
```

The `get_trajectory` function returns a `list` of class `wlts`. This
object contains the `query` and `result` attributes. The `query`
attribute stores the query performed to retrieve the data. By default,
it will be `NULL`. For this information to be stored, the
`query_info = TRUE` parameter is required. For example:

```{r, eval=FALSE}
get_trajectory(wlts_bdc,
latitude = -12,
longitude = -54,
collections = "mapbiomas_amazonia-v5",
query_info = TRUE,
config = httr::add_headers("x-api-key" = "change-me"))
```

The `result` attribute stores the retrieved trajectories. The data is
stored in a `tibble` for easy manipulation, which has the columns:

- `class`: LULC class;
- `collection`: Data Collection;
- `date`: Time instants of the trajectory;
- `point_id`: ID of the point that was queried.

The `point_id` column of the result `tibble` is used in `rwlts` to
identify the entry point. This ID is necessary since the
`get_trajectory` function can be used with vectors as input. For
example, the code below retrieves the trajectory of data from the
`mapbiomas_amazonia-v5` collection for two points `(-54, -12)` and
`(-54, -11.01)`.

```{r}
get_trajectory(wlts_bdc,
latitude = c(-12, -11.01),
longitude = c(-54, -54),
collections = "mapbiomas_amazonia-v5",
config = httr::add_headers("x-api-key" = "change-me"))
```

In this case, the point `(-54, -12)` will have `point_id` equal to 1,
while `(-54, -11.01)` will have `point_id` equal to 2.

In addition to multiple point retrieval, the `get_trajectory` function
allows multiple collections to be queried for the composition of the
trajectory. To do this, in the `collections` parameter, the collections
must be assigned. For example, data from the collections
`mapbiomas_amazonia-v5` and `terraclass_amazonia-v2` are retrieved in
the code below.

```{r}
get_trajectory(wlts_bdc,
latitude = c(-12, -11.01),
longitude = c(-54, -54),
collections = c("mapbiomas_amazonia-v5", "terraclass_amazonia-v2"),
config = httr::add_headers("x-api-key" = "change-me"))
```

Finally, the `get_trajectory` function, through the `start_date` and
`end_date` parameters, allows you to specify the time intervals used in
the trajectory. To exemplify its use, in the code below, trajectories
are retrieved for the points `(-54, -12)` and `(-54, -11.01)`, from the
`mapbiomas_amazonia-v5` and `terraclass_amazonia-v2` collections in the
time interval `[2003-01-01, 2004-01-01]`.

```{r}
get_trajectory(wlts_bdc,
latitude = c(-12, -11.01),
longitude = c(-54, -54),
start_date = "2003-01-01",
end_date = "2004-01-01",
collections = c("mapbiomas_amazonia-v5","terraclass_amazonia-v2"),
config = httr::add_headers("x-api-key" = "change-me"))
```

### Alluvial plot

To visualize the trajectories and fully understand their time dynamics,
the `rwlts` package implements the Alluvial-based visualization method.
To create this plot, use the `plot` function, as shown in the example
below:

```{r, fig.width = 9, fig.height = 4}
# import data from package
data("mt_500_mapbiomas_cerrado")

plot(mt_500_mapbiomas_cerrado)
```

Besides, you can fully customize the returned plot. This customization
is possible since the `plot` function returns a ggplot2 object.

```{r, fig.width = 10}
library(ggplot2)
library(ggalluvial) # use to create plot
library(cowplot) # use different theme

plot(mt_500_mapbiomas_cerrado, show_count = TRUE) +
cowplot::theme_minimal_hgrid() +
labs(title = "Changes in Primavera do Leste (2004-2014)",
x = "Timeline",
y = "Number of points",
fill = "Class") +
theme(legend.position = "bottom",
plot.title = element_text(hjust = 0.5)) +
scale_fill_manual(values = c("#129912",
"#32CD32",
"#BDB76B",
"#FFD966",
"#FFFFB2"),
labels = c("Formação Florestal",
"Formação Savânica",
"Outras Lavouras Temporárias",
"Pastagem",
"Soja"))
```

> The numbers inside each bar correspond to the quantity of points
> extracted in each year. You can see that according to the change of
> LULC classes, the quantity of points in each class also changes.

# License

Copyright (C) 2021 INPE.

R client for WLTS is free software; you can redistribute it and/or
modify it under the terms of the MIT License; see LICENSE file for more
details.