class: center, top ![:scale 30%](/assets/images/coding_club_logo_1.png) # 25 NOVEMBER 2021 ## INBO coding club Back to INBOflix --- class: left, top ## Introduction: open software While preparing the coding club, I got this revealing quote from R: ![:scale 100%](/assets/images/20211125/20211125_quote_about_arcgis.png) --- class: center, middle ![:scale 90%](/assets/images/20211125/20211125_rasters_badge.png) --- class: left, top ## raster, terra, stars, sf, sp, ... HELP! R is rapidly evolving to fill the gap with Python and C++. Downside: new packages are being developed, old ones are modified or superseded. ### Spatial vector data Topic of the coding club of October - **sf** : newest and recommended package - **sp** : its predecessor ### Spatial raster data - [**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** For more info, read this great [article](https://geocompr.github.io/post/2021/spatial-classes-conversion/) about conversions among different spatial classes in R. --- class: left, top ## A raster is ... - a `SpatRaster` object in `terra` - a `RasterLayer` object in `raster` - a `stars` object in `stars` --- class: left, top ## Continuous vs categorical rasters ![:scale 100%](/assets/images/20211125/20211125_continuous_vs_categorical_rasters.png) --- class: left, top Install the packages: ```r install.packages(c("raster", "terra", "exactextractr", "sf")) ``` Load the package: ```r library(raster) library(terra) library(exactextractr) library(sf) ``` --- 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 ## Coding club play tables **Like** a card club, we will split us in different tables (aka _breakout rooms_) and we will help each other! We return all together after a fixed amount of time to discuss each challenge apart. **Unlike** a card club, in our club there is no winner, neither within a table nor among tables! ![:scale 65%](/assets/images/breakout_rooms.png) --- class: left, top ![:scale 100%](/assets/images/coding_club_sticky_concept.png)
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/i544o05cSs6bYbxKBxIbDA?both
--- class: left, top # Download data and code - Download everything automatically via `inborutils::setup_codingclub_session()` - manually*, from [data/20211125](https://github.com/inbo/coding-club/blob/master/data/20211125/) and [src/20211125](https://github.com/inbo/coding-club/blob/master/src/20211125). Place the R script in your folder `src/20211125/` and data in `data/20211125/`. 1. [20211125_challenges.R](https://github.com/inbo/coding-club/blob/master/src/20211125/20211125_challenges.R): R script to start from 2. [`20211125_lu_nara_2016_100m.tif`](https://github.com/inbo/coding-club/blob/master/data/20211125/20211125_lu_nara_2016_100m.tif): a categorical raster of land use of Flanders with 9 land use categories. Thanks, NARA team! 3. [`20211125_lu_nara_legend.txt`](https://github.com/inbo/coding-club/blob/master/data/20211125/20211125_lu_nara_legend.txt): legend to map land use index (1-9) to meaningful land use category ("Grasland", "Bos", "Akker", ...) 4. [`20211125_protected_areas_Lambert72.gpkg`](https://github.com/inbo/coding-club/blob/master/data/20211125/20211125_protected_areas_Lambert72.gpkg): geopackage with NATURA2000 protected areas in Lambert 1972 coordinate reference system 5. [`20211125_cir_ename.tif`](https://github.com/inbo/coding-club/blob/master/data/20211125/20211125_cir_ename.tif): a [color-infrared image](https://eo.belspo.be/en/colour-composites) of Ename, East Flanders 6. [`20211125_dsm_ename.tif`](https://github.com/inbo/coding-club/blob/master/data/20211125/20211125_dsm_ename.tif): a digital **surface** model of Ename 7. [`20211125_dtm_ename.tif`](https://github.com/inbo/coding-club/blob/master/data/20211125/20211125_dtm_ename.tif): a digital **terrain** model of Ename
* __Note__: check the getting started instructions on [how to download a single file](https://inbo.github.io/coding-club/gettingstarted.html#each-session-setup)
--- background-image: url(/assets/images/background_challenge_1.png) class: left, top # Challenge 1 1. Read `20211125_lu_nara_2016_100m.tif` as a SpatRaster (terra) and call it `lu_nara_2016`. Tip: [basics](https://rspatial.github.io/terra/reference/terra-package.html#i-creating-combining-and-sub-setting-spatraster-objects) from terra documentation 2. Which layers (**names**) does it contain? What is the **crs**? The **ext**ent? The **res**olution? What are the **minmax** values of the raster? 3. **plot** the raster. Note: ignore the errors "`Error in x$.self$finalize() : attempt to apply non-function`" 4. Read `20211125_lu_nara_legend.txt` (code is provided!) so that you can set the color table associated with `lu_nara_2016` 5. Calculate the most and least occurring land use category in each of the Natura2000 protected areas. Tip: use `exactextractr::exact_extract()` with the right [summary operations](https://isciences.gitlab.io/exactextractr/#summary-operations) ![:scale 90%](/assets/images/20211125/20211125_set_color_table_raster.png) --- background-image: url(/assets/images/background_challenge_2.png) class: left, top # Challenge 2 1. Read the color-infrared image `20211125_cir_ename.tif` as `cir` 2. How many layers does it contain? What are their names? What is the CRS? The extent? The resolution? 3. Rename the layers: `fill_1` to `nir` (near-infrared), `fill_2` to `red`, `fill_3` to `green`. Plot the raster 4. Calculate the [Normalized difference vegetation index](https://en.wikipedia.org/wiki/Normalized_difference_vegetation_index) (NDVI) based on near infrared and red layers: `ndvi = (nir - red)/(nir + red)` 5. Notice how the calculated `ndvi` is a raster itself. Assign name `ndvi` to the raster layer you have just created 6. Plot `ndvi` and write it as a new GeoTIFF (*.tif) file: `ndvi_ename.tif` --- class: left, top ## INTERMEZZO: big files? No problem Big rasters? No problem! From [`raster` documentation](https://rspatial.github.io/raster/reference/raster-package.html): _The package can work with large files because the objects it creates from these files only contain information about the structure of the data, such as the number of rows and columns, the spatial extent, and the filename, but it does not attempt to read all the cell values in memory. In computations with these objects, data is processed in chunks._ This holds true for `terra` and `stars` as well, of course. --- background-image: url(/assets/images/background_challenge_3.png) class: left, top # Challenge 3 Calculate a canopy height raster from a digital **terrain** model (DTM) and a digital **surface** model (DSM). In other words: `dtm` is the height of the terrain in meters while `dsm` is the height of the top of the vegetation canopy in meters. Both heights are measured using mean sea level as reference zero. Read `20211125_dtm_ename.tif` and `20211125_dsm_ename.tif` as `dtm` and `dsm` respectively. This doesn't work: ```r # canopy height model chm <- dsm - dtm ``` How to fix it? ![:scale 60%](/assets/images/20211125/20211125_sample.png) --- class: left, top ## Bonus challenge ### Appendices to challenge 1 1. Calculate the areal proportion of land cover classes in each of the Natura2000 protected areas. Note: `exact_extract()` should be combined with a function you have to write. It requires data manipulation knowledge (e.g. dplyr). 2. We do now something similarb as in challenge 1 but with continuous rasters. Calculate dataframe with min max and mean altitude for each Belgian province using `exact_extract()`. How to include the province name in the output? (code to get the data is provided in R script!). Tip: check the basic usage of this tutorial https://isciences.gitlab.io/exactextractr/#basic-usage ### Appendices to challenge 2 Add layers `nir` and `red` from `cir_ename` to GeoTIFF `ndvi_ename.tif` --- class: left, top # Resources - [solutions](https://github.com/inbo/coding-club/blob/master/src/20211125/20211125_challenges_solutions.R) and [video recording](https://vimeo.com/652008081) are available - [Spatial Data Science with R and `terra`](https://rspatial.org/terra/index.html): THE place to be to learn a lot about rasters and spatial data - great [workshop](https://nowosad.github.io/SIGR2021/workshop2/workshop2.html#1) with a lot of examples, exercises and references. Author: Jakub Nowosad - `terra` package [documentation](https://rspatial.github.io/terra/reference/terra-package.html): extensive and easy to use at the same time - `raster` package [documentation](https://rspatial.github.io/raster/reference/raster-package.html): written in the same style as the one for `terra` package - `exactextractr` package [documentation](https://isciences.gitlab.io/exactextractr/) very well written documentation - [INBO tutorial](https://inbo.github.io/tutorials/tutorials/spatial_standards_raster/) about open rasters file formats and comparison table. Thanks, Floris! - [spatial classes conversion](https://geocompr.github.io/post/2021/spatial-classes-conversion/) with very useful conversion tables --- class: left, top # Ideas for 2022 We propose. You decide! - Coding club of 2.5 hrs: from 10am to **12.30pm**? - More real data and questions from you: ask for it at each announcement (begin of the month) ![:scale 50%](/assets/images/20211125/20211125_ask_feedback_icon.png) --- class: left, top # Ideas for 2022 ## Timeline and topics As for 2021, the coding club would like to reflect the research process: read, process, visualize, report. What do you think about? Which topics do you miss? Would you like to give a special edition on a specific topic? ![:scale 120%](/assets/images/20211125/20211125_timeline_coding_clubs_2022.png) --- class: center, top ![:scale 30%](/assets/images/coding_club_logo_1.png) Topic: spatial data visualization in R (together with GIS club)
Room: living, kitchen, bedroom ... The choice is yours
Date: **09/12/2021**, van **14:00** tot **16:00** Topic: write reports with RMarkdown Room: 01.19 - Paul Van Ostaijen(??)
Date: **16/12/2021**, van **10:00** tot **12:00**
(registration announced via DG_useR@inbo.be)