---
bibliography: references.bib
---

# Introduction {#introductionx}

## Spatial in R : History and evolutions

Historically, 4 packages make it possible to import, manipulate and transform spatial data:

-   The package `rgdal` [@rgdal] which is an interface between R and the [GDAL](http://www.gdal.org/) [@GDAL] and [PROJ](https://proj.org/) [@PROJ] libraries allow you to import and export spatial data (shapefiles for example) and also to manage cartographic projections\
-   The package `sp` [@sp] provides class and methods for vector spatial data in R. It allows displaying background maps, inspectiong an attribute table etc.\
-   The package `rgeos` [@rgeos] gives access to the [GEOS](http://trac.osgeo.org/geos/) spatial operations library and therefore makes classic GIS operations available: calculation of surfaces or perimeters, calculation of distances, spatial aggregations, buffer zones, intersections, etc.\
-   The package `raster` [@raster] is dedicated to the import, manipulation and modeling of raster data.

Today, the main developments concerning vector data have moved away from the old 3 (`sp`, `rgdal`, `rgeos`) to rely mainly on the package `sf` ([@sf], [@pebesma2018]). In this manual we will rely exclusively on this package to manipulate vector data.

The packages `stars` [@stars] and `terra` [@terra] come to replace the package `raster` for processing raster data. We have chosen to use the package here `terra` for its proximity to the `raster`.

## The package `sf`

<img src="img/sf.gif" align="right" width="150"/> The package `sf` was released in late 2016 by Edzer Pebesma (also author of `sp`). Its goal is to combine the feature of `sp`, `rgeos` and `rgdal` in a single, more ergonomic package. This package offers simple objects (following the [*simple feature*](https://en.wikipedia.org/wiki/Simple_Features) standard) which are easier to manipulate. Particular attention has been paid to the compatibility of the package with the *pipe* syntax and the operators of the `tidyverse`.

`sf` directly uses the GDAL, GEOS and PROJ libraries.

![](img/sf_deps.png){fig-align="center" width="600"}

[From r-spatial.org](https://r-spatial.org/r/2020/03/17/wkt.html)

::: {.callout-note appearance="minimal" icon="false"}
Website of package `sf` :
[Simple Features for R](https://r-spatial.github.io/sf/)
:::

### Format of spatial objects `sf`

![](img/sf.png){fig-align="center" width="600"}

Objects`sf` are objects in `data.frame` which one of the columns contains geometries. This column is the class of sfc (*simple feature column*) and each individual of the column is a sfg *(simple feature geometry)*. This format is very practical insofa as the data and the geometries are intrinsically linked in the same object.

::: {.callout-note appearance="minimal" icon="false"}
Thumbnail describing the simple feature format:
[Simple Features for R](https://r-spatial.github.io/sf/articles/sf1.html)
:::

::: callout-tip
A benchmark of vector processing libraries is available [here](https://github.com/kadyb/vector-benchmark).
:::

## Package `mapsf`

The free R software spatial ecosystem is rich, dynamic and mature and several packages allow to import, process and represent spatial data. The package [`mapsf`](https://CRAN.R-project.org/package=maps) [@mapsf] relies on this ecosystem to integrate the creation of quality thematic maps into processing chains with R.

Other packages can be used to make thematic maps. The package `ggplot2` [@ggplot2], in association with the package `ggspatial` [@ggspatial], allows for example to display spatial objects and to make simple thematic maps. The package `tmap` [@tmap] is dedicated to the creation of thematic maps, it uses a syntax close to that of `ggplot2` (sequence of instructions combined with the '+' sign). Documentation and tutorials for using these two packages are readily available on the web.

Here, we will mainly use the package `mapsf` whose functionalities are quite complete and the handling rather simple. In addition, the package is relatively light.

<img src="img/logo_mapsf.png" align="right" width="120"/>

`mapsf` allows you to create most of the types of map usually used in statistical cartography (choropleth maps, typologies, proportional or graduated symbols, etc.). For each type of map, several parameters are used to customize the cartographic representation. These parameters are the same as those found in the usual GIS or cartography software (for example, the choice of discretizations and color palettes, the modification of the size of the symbols or the customization of the legends). Associated with the data representation functions, other functions are dedicated to cartographic dressing (themes or graphic charters, legends, scales, orientation arrows, title, credits, annotations, etc.), the creation of boxes or the exporting maps.\
`mapsf` is the successor of [`cartography`](http://riatelab.github.io/cartography/docs/) [@cartography], it offers the same main functionalities while being lighter and more ergonomic.

To use this package several sources can be consulted:

-   The package documentation accessible [on the internet](http://riatelab.github.io/mapsf/) or directly in R (`?mapsf`),

-   A [*cheat sheet*](https://raw.githubusercontent.com/riatelab/mapsf/master/vignettes/web_only/img/mapsf_cheatsheet.pdf),

![](img/mapsf_cheatsheet.png){fig-align="center" width="600"}

-   The [vignettes](https://riatelab.github.io/mapsf/articles/) associated with the package show sample scripts,

-   The [R Geomatics](https://rgeomatic.hypotheses.org/) blog which provides resources and examples related to the package and more generally to the R spatial ecosystem.

## The package `terra`

<img src="img/logo_terra.png" align="right" width="150"/> The package `terra` was release in early 2020 by Robert J. Hijmans (also author of `raster`). Its objective is to propose methods of treatment and analysis of raster data. This package is very similar to the package `raster`; but it has more features, it's easier to use, and it's faster.

::: {.callout-note appearance="minimal" icon="false"}
Website of package `terra` :
[Spatial Data Science with R and "terra"](https://rspatial.org/terra/)
:::

::: callout-tip
A benchmark of raster processing libraries is available [here](https://github.com/kadyb/raster-benchmark).
:::
