class: center, top  # 25 FEBRUARY 2026 ## INBO coding club Herman Teirlinck
00.48 - Keldermans --- class: left, top # Reminders 1. Did we confirm the room reservation on the _roomie_? 2. Did we start the recording? --- class: left, top # Bye bye Dirk, welcome Charlotte! Dirk Maes leaves the INBO coding club core team after many years of great contributions. He will become a honorary member and will still be around to help us with his experience. Thanks Dirk for all your work and see you soon in the coding club audience! Welcome Charlotte Van Driessche, a young, full of energy and talented colleague from the Genetic Diversity team. We are very happy to have you on board, Charlotte! --- class: center, middle  --- class: left, top What do we mean with geospatial data? ### Spatial vector data Points, lines, polygons, etc were the topic of the coding club of [26 January 2026](https://coding-club.inbo.be/sessions/20260126_spatial_data_in_r.html). - **sf** : newest and recommended package - **sp** : its predecessor (retired) ### Spatial raster data It's quite a lot of time we don't visualize rasters: last time was during the coding club of [31 August 2023](https://coding-club.inbo.be/sessions/20230831_visualize_spatial_data_in_r.html). - [**terra**](https://rspatial.github.io/terra/reference/terra-package.html) : newest and recommended package*. - [**raster**](https://rspatial.org/raster/pkg/index.html) : its predecessor (no support for sf), match with sp. - [**stars**](https://r-spatial.github.io/stars/): for **s**patio**t**emporal **ar**ray**s**. Important for people involved in remote sensing where data often comes in the form of dense arrays, with space and time being array dimensions. For more info, read this great [article](https://geocompx.org/post/2021/spatial-classes-conversion/) about conversions among different spatial classes in R.
\* The terra package handles spatial vector data as well. It's worth a try!
--- class: left, top # Plot vs maps You can **plot** geospatial data. But a plot is not a map, as chocolate is not (yet) a chocolate pie. You need one "ingredient" more. What do we need?  --- class: left, top # From plots to maps You need **tiles**.  Example above uses [OpenStreetMap](https://www.openstreetmap.org/) tiles. --- class: left, top # Tiles There are a lot of tiles. Some are available for being used with an open license, others not. Tiles are nothing more than images (pngs or jpgs). They are tiny small images that are put together to form a map. The tiles are usually 256x256 pixels and are arranged in a grid. The tiles are generated by a server and are requested by the client (your browser) when you zoom in or out on a map. There are tiles covering the entire world, or just a specific region. For Flanders, see the [Web Map (Tile) Service](https://www.vlaanderen.be/digitaal-vlaanderen/onze-oplossingen/geografische-webdiensten/ons-gis-aanbod/raadpleegdiensten#wat-is-een-wmts) (WMTS) of the Flemish Authority. So, if you have ever seen a map on the web, you have seen tiles! And if you have ever heard a "know-it-all expert" speaking about WMTS, well, do not be afraid about that! --- class: left, top # Static vs dynamic maps **Static maps**: the data visualised on the map and the background tiles are fixed: no zooming in/out. Easy for pdfs or other static content. **Dynamic maps**: zooming in/out is possible. Perfect for web pages. No PDFs! --- class: left, top ## Dynamic maps: leaflet vs mapview [Leaflet](https://leafletjs.com/) is a very popular JavaScript library for interactive maps by [Vladamir Agafonkin](http://agafonkin.com/en/). The R package [leaflet](https://rstudio.github.io/leaflet/) is a wrapper for that library. It allows to create interactive maps that can be embedded in a web page. The package is very flexible and allows to create maps with a lot of functionalities. The documentation is wonderful and there are a lot of examples available (check the "Articles"!). However, if you are looking for a quick and easy way to create interactive maps, you can use [mapview](https://r-spatial.github.io/mapview/), which at some extent can be considered a wrapper around the leaflet package. It allows to create interactive maps with just one line of code = very useful to quickly explore your data and to create maps probably good enough for presentations or reports. The documentation is also very good and there are a lot of examples available in the "Articles". Last but not least, "it makes use of some advanced rendering functionality that will enable viewing of much larger data than is possible with leaflet"*.
\* Source: [mapview documentation](https://r-spatial.github.io/mapview/index.html)
--- class: left, top ## Today We already used mapview during the coding club of January 2026 to easily visualise what we were doing. Today we build up from the basic function call `mapview(my_sf_object)`: ``` library(mapview) mapview(breweries) ``` And see what happens! Try clicking on a circle: beautiful, isn't? --- class: left, top ## Geospatial visualisation in R Geospatial visualisation in R is evolving fast. The packages are numerous and the choice is not always easy. The [R-Spatial CRAN Task View](https://github.com/cran-task-views/Spatial/tree/main)* is a handy source to get an updated overview of R spatial packages. See section related to [visualisation](https://github.com/cran-task-views/Spatial/blob/main/Spatial.md#visualizing-spatial-data). Another tip: choose packages actively maintained!
* The CRAN Task View is a curated list of R packages for a particular task. It is maintained by the community. And thanks Floris Vanderhaeghe for the tip!
--- class: left, top ## INBO goes spatial Do you know INBO is very active in spatial data? Just think about the [qgisprocess](https://r-spatial.github.io/qgisprocess/articles/qgisprocess.html) package (see coding club of Dec 2023). But check also the R package [inbospatial](https://inbo.github.io/inbospatial/): a collection of useful R functions for spatial data.  --- class: left, top ## `inbospatial` And maybe you noticed that `inbospatial` was mentioned few days ago in a thread of our DG_useR mailing list. This answer of Hans shows how well documented inbospatial is!  --- class: left, top # Packages used today Today we will mainly use: - [mapview](https://r-spatial.github.io/mapview/) - [leaflet](https://rstudio.github.io/leaflet/) Today we work around **dynamic maps** ONLY = no static maps today! Check the coding clubs of 28 November 2024 ([challenges 1 & 2](https://coding-club.inbo.be/sessions/20241128_visualize_spatial_data_in_r.html#18)) and 26 August 2025 ([challenge 2 & 3](https://coding-club.inbo.be/sessions/20250826_visualise_spatial_data_in_r.html#22)) for static maps with [ggplot2](https://ggplot2.tidyverse.org/) and [ggspatial](https://paleolimbot.github.io/ggspatial/index.html). --- class: left, top ## Install and load packages Load the packages. Install if needed: ```r library(sf) library(terra) library(tidyverse) # In place of dplyr, readr library(mapview) # For (leaflet) maps library(viridis) # For nice color palettes library(leaflet) # For more flexible maps than mapview (but more code) library(leafem) # For providing extensions to leaflet maps library(leafpop) # For making great pop-ups in leaflet maps library(inbospatial) # For dealing with INBO made spatial data and function ``` --- class: center, top ### How to get started? Check the [Each session setup](https://inbo.github.io/coding-club/gettingstarted.html#each-session-setup) to get started. ### First time coding club? Check the [First time setup](https://inbo.github.io/coding-club/gettingstarted.html#first-time-setup) section to setup. --- class: left, top 
No yellow sticky notes online. We use hackmd (see next slide) but basic principle doesn't change. --- class: center, top ### Share your code during the coding session! Go to https://hackmd.io/j90LerTrRxavEKDTCU-f7w?edit
--- class: left, top # Download data and code - Download everything automatically via `inborutils::setup_codingclub_session()` - manually*, from [data/20260225](https://github.com/inbo/coding-club/blob/master/data/20260225/) and [src/20260225](https://github.com/inbo/coding-club/blob/master/src/20260225). Place the R script in your folder `src/20260225/` and data in `data/20260225/`.
* __Note__: check the getting started instructions on [how to download a single file](https://inbo.github.io/coding-club/gettingstarted.html#each-session-setup)
--- class: left, top # Data and code files description 1. [20260225_butterflies_love_plants.csv](https://github.com/inbo/coding-club/blob/master/data/20260225/20260225_butterflies_love_plants.csv): visits of butterflies to their host plants in a specific transect in Flanders. Used in Challenge 1. Thanks Dirk for the dataset! 2. [20260225_tdiff_tmax_tmin_01.tif](https://github.com/inbo/coding-club/blob/master/data/20260225/20260225_tdiff_tmax_tmin_01.tif): raster with three layers about the average maximum temperature, the average minimum temperature and the average temperature fluctuation world wide in January*. Used in Challenge 2. 3. [20260225_moth_captures.csv](https://github.com/inbo/coding-club/blob/master/data/20260225/20260225_moth_captures.csv): a very small data frame (3 rows!) with (re)captures of an individual of _Theria primaria_ (early moth). Used in Challenge 3. Thanks Dirk for the dataset! 4. [20260225_challenges.R](https://github.com/inbo/coding-club/blob/master/src/20260225/20260225_challenges.R): R script to start from.
\* Source: [WorldClim](https://worldclim.org/data/worldclim21.html) historical data.
--- background-image: url(/assets/images/background_challenge_1.png) class: left, top # Challenge 1 - dynamic maps **IMPORTANT**: The code to read the spatial file is provided! Using [mapview](https://r-spatial.github.io/mapview/): 1. Create a basic map of `visits` as shown in introduction. 2. Set color based on species and add a legend. 3. Set color based on `type`. In particular, use dark green for trees and light green for shrubs. 4. Let's fine tune the map in 1.2. Set the opacity of the areas to 0.4 and the contour lines to 0.5. Set color of the contour line to red. Set `"Plants type"` as legend title and layer name. Show only `species` and `type` in the popups. Hint: check the [mapview advanced controls](https://r-spatial.github.io/mapview/articles/mapview_02-advanced.html) article. 5. Start from the map in 1.2. Use Open Street Map tiles only, show the mouse coordinates (lat, lon) and zoom level in the map. Remove the "zoom-to-layer" button from the map. Hint: check [mapview advanced controls](https://r-spatial.github.io/mapview/articles/mapview_02-advanced.html) and the [extra leaflet functionalities](https://r-spatial.github.io/mapview/articles/mapview_06-add.html) and [mapview popups](https://r-spatial.github.io/mapview/articles/mapview_04-popups.html) articles. 6. (EXTRA) Create two layers for trees and shrubs. Set color based on species and add a legend for each layer. Be sure that colors are ordered by type and species. Use the color palette `viridis::inferno()` from the viridis package. --- background-image: url(/assets/images/background_challenge_2.png) class: left, top # Challenge 2 - raster visualisation **IMPORTANT**: The code to read the raster file is provided! 1. Create a map with the first layer of the raster, `tdiff_01`. Add the legend. Set layer name and legend title to `Average temperature fluctuation in January`. Use the provided color palette, `viridis::magma()`. 2. Sometimes it's better to have less colors :-) Set breakpoints at every 5 degrees, starting from the minimum value (`min_value_diff`) to the maximum value (`max_value_diff`). Hint: check the [mapview advanced controls](https://r-spatial.github.io/mapview/articles/mapview_02-advanced.html) article. 3. Show all three layers of the raster, with the same color palette. Set layer names and legend titles to `Average temperature fluctuation in January`, `Average maximum temperature in January` and `Average minimum temperature in January`. Make only the first layer visible and hide the other two layers. Remove the "zoom-to-layer" button as well. Hint: check the [mapview advanced controls](https://r-spatial.github.io/mapview/articles/mapview_02-advanced.html) article again. --- class: left, top ## Intermezzo: Web Coverage Service (WCS) A Web Coverage Service (WCS) is a standard protocol for serving geospatial raster data over the web. It allows users to access and retrieve raster data in a standardized format, making it easier to integrate and use in various applications. The package inbospatial allows you to retrieve raster data from several WCS services, e.g. the DTM (Digital Terrain Model) of Flanders. Check the documentation of the function [get_coverage_wcs()](https://inbo.github.io/inbospatial/reference/get_coverage_wcs.html) for more details. Let's run the Hans' code shown before: ```r library(inbospatial) bbox <- c(xmin = 142600, ymin = 153800, xmax = 142650, ymax = 153850) bbox <- sf::st_bbox(bbox, crs = sf::st_crs(31370)) dtm <- get_coverage_wcs( wcs = "dtm", bbox = bbox, layername = "EL.GridCoverage.DTM", resolution = 1) terra::plot(dtm) ``` --- background-image: url(/assets/images/background_challenge_3.png) class: left, top # Challenge 3 - leaflet The leaflet package is more flexible than mapview, but you need to write more code. But we love code, right? So let's go! **IMPORTANT**: The code to read the data frame is provided! 1. Create a map of moth captures using data frame `moth_captures`. Use only the `Esri.WorldImagery` as tile provider (background). Show lines connecting the capture points. Use red color and a line weight of 3. 2. Show the total distance (column `total_dist`) in meters traveled by the moth when hovering with the mouse over the line. Example: "Total distance: 1 m". Hint: use `label()`. 3. Add to the previous map the capture locations as circle markers. Set the radius to 6, the color to white and the fill color to turquoise. Show the capture date (column `date`) as label when hovering over the points. 4. Slightly change the previous map to make the labels of the circle markers always visible, not only when hovering. Also display only the text, not the standard surrounding box. Hint: use argument `labelOptions` and the `labelOptions()` function from the leaflet package. 5. Probably you noticed that default black text is not easy to read. We need to **style** the text. Set the text color to white, the font to sans-serif, the font size to 12px and the font weight to bold. Hint: use argument `style` in `labelOptions()`. --- class: left, top # The packages of the month Do you dream of switching from static to dynamic maps with one line of code? Well, give [tmap](https://r-tmap.github.io/tmap/) a try! Give a look to the [Get Started](https://r-tmap.github.io/tmap/articles/tmap-getstarted.html) page: ``` tmap_mode("view") # to dynamic maps tmap_mode("plot") # to static maps ``` Less flexible than mapview but the possibility to switch from static to dynamic visualisation is a nice feature.  --- class: left, top # The packages of the month (advanced) Have you already used the [targets](https://docs.ropensci.org/targets/) R package and have you ever struggled to include geospatial data formats in your worklows? The package [geotargets](https://docs.ropensci.org/geotargets/) can help you!  --- class: left, top # Resources - Comprehensive [solutions](https://github.com/inbo/coding-club/blob/main/src/20260225/20260225_challenges_solutions.R) are available on GitHub. You can opt to download the solutions automatically by using `inborutils::setup_codingclub_session("20260225")`. - The edited [video recording](https://vimeo.com/1171754154) is available on our [vimeo channel](https://vimeo.com/user/8605285/folder/1978815). - [ggplot2](https://ggplot2.tidyverse.org/): our standard package for plots, which can also be used for displaying spatial data. - [ggspatial](https://paleolimbot.github.io/ggspatial/index.html) documentation. Suit well for static maps and it is ggplot2 compatible. - For the ggplot fanatics there is the [ggmap](https://github.com/dkahle/ggmap#readme) package! - [mapview](https://r-spatial.github.io/mapview/) documentation. - [leaflet](https://rstudio.github.io/leaflet/) for R documentation. - [leafem](https://r-spatial.github.io/leafem/) documentation. - [inbospatial](https://github.com/inbo/inbospatial): a collection of useful R functions for spatial data "made in INBO". - [tmap](https://r-tmap.github.io/tmap/) documentation. - [geotargets](https://docs.ropensci.org/geotargets/) documentation. --- class: center, middle  Room: HT - 00.48 - Keldermans
Date: **31/03/2026**, van **10:00** tot **12:30**
Subject: not yet decided
(registration announced via DG_useR@inbo.be)