class: inverse, left, nonum, clear background-image: url("figs/cover.jpg") background-size: cover .titlestyle[Introduction] <br> .titlestyle[to] <br> .titlestyle[landscape] <br> .titlestyle[ecology] <br> .titlestyle[with R] <link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Lora" /> .captionstyle[Jakub Nowosad and Maximilian H.K. Hesselbarth] .captionstyle[2020-05-14, the IALE-North America 2020 Annual Meeting] <!-- https://www.si.edu/object/bright-scene-cattle-near-stream:saam_1983.104.2 --> --- # Rules of the workshop - Please set your microphones to **mute** - We encourage participants to use their **real names and a profile picture** - Participants will be split into **random groups during the practical parts**. Feel free to discuss your problems and solutions with your group - **The workshop will be recorded** and the video will be publicly posted afterward <img src="figs/zoom.png" width="841" style="display: block; margin: auto;" /> - There are **two ways to ask questions** during the workshop: 1. **The zoom chat** - this is a place to ask questions related to the currently discussed content and questions related to the practicals. We will try to answer these questions as soon as possible. 2. **The slido portal** - https://www.sli.do/IALENA - this is a place to ask questions related to overall spatial data analysis in R and landscape ecology with R. We will answer these questions at the end of the workshop --- # Prerequisites **Packages:** - You can install the packages used in the workshop as follows: ```r install.packages(c("landscapemetrics", "sf", "dplyr", "spData", "usethis", "remotes") install.packages("spDataLarge", repos = "https://nowosad.github.io/drat/", type = "source") remotes::install_github("mtennekes/tmaptools", upgrade = TRUE) remotes::install_github("mtennekes/tmap", upgrade = TRUE) remotes::install_github("rspatial/raster", upgrade = TRUE) ``` **Slides:** - https://r-spatialecology.github.io/ialena-2020/ **Scripts and data:** - The code below can be used to download all the scripts and data for the workshop: ```r usethis::use_course("https://github.com/r-spatialecology/ialena-2020/raw/master/ialena-2020.zip") ``` --- # Workshop agenda - **Part I** (90 minutes): Introduction to the spatial data analysis in R: - Spatial data representations and reading - *First practical part* - Making maps in R - R spatial processing power - *Second practical part* - **Break** (30 minutes) - **Part II** (90 minutes): *landscapemetrics*: An open-source R tool to calculate landscape metrics: - Introdution to landscape metrics and how to calculate landscape metrics in R - *First practical part* - Sample metrics in buffer areas, moving window, and building blocks - *Second practical part* - **Q and A**: - You can ask us any questions related to spatial data analysis in R and the *landscapemetrics* package during the workshop using https://www.sli.do/IALENA - The event code is `IALENA` <!-- MH: Do we need one more slide already here talking quickly about the structure of the exercises? Like...they should download the scripts without solutions and try to fill them out etc... ?--> --- class: inverse, left, bottom, clear, nonum layout: false background-image: url("figs/geocompr-logo.png") background-position: 85% 10% background-size: 30% # Part I ## Introduction to the spatial data analysis in R Jakub Nowosad Institute of Geoecology and Geoinformation, Adam Mickiewicz University, Poznan, Poland --- # Spatial data representations: raster data .lc[ - The **raster** package - Classes for spatial raster data: `RasterLayer`, and multilayer `RasterStack` and `RasterBrick` - Raster data input/output - Raster algebra and raster processing - ?`raster-package`, [link1](https://geocompr.robinlovelace.net/spatial-class.html#raster-data), [link2](https://rspatial.org/raster/index.html), and [link3](http://www.rpubs.com/etiennebr/visualraster) ] .rc[ <img src="figs/raster-intro-plot-1.png" height="200" style="display: block; margin: auto;" /> <!-- MH: The source will always be the path of the last person that knitted the slides. Is that a problem? Not really, right? --> ```r library(raster) elev_data <- raster("data/example_elevation.tif") elev_data ``` ``` ## class : RasterLayer ## dimensions : 550, 753, 414150 (nrow, ncol, ncell) ## resolution : 0.000322, 0.000266 (x, y) ## extent : -82.41659, -82.17413, 33.45642, 33.60272 (xmin, xmax, ymin, ymax) ## crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 ## source : /home/jn/Science/conferences/2020/ialena-2020/data/example_elevation.tif ## names : example_elevation ## values : 57, 178.9385 (min, max) ``` ] --- # Spatial data representations: vector data .pull-left[
] .pull-right[ ```r library(sf) study_area <- read_sf("data/study_area.gpkg") head(study_area) ``` ``` ## Simple feature collection with 1 feature and 1 field ## geometry type: POLYGON ## dimension: XY ## bbox: xmin: 1254413 ymin: 1247861 xmax: 1267196 ymax: 1258095 ## proj4string: +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs ## # A tibble: 1 x 2 ## id geom ## <dbl> <POLYGON [m]> ## 1 1 ((1264853 1249278, 1266468 1252680, 1267196 1256017, 1264424 1257485, 1… ``` - The **sf** package - A class system for spatial vector data - Vector data input/output - Transformations between different coordinate reference systems (CRS) - Geometric operations - [link1](https://geocompr.robinlovelace.net/spatial-class.html#vector-data), [link2](https://r-spatial.github.io/sf/), [link3](https://github.com/rstudio/cheatsheets/blob/master/sf.pdf) ] --- # Spatial data reading .pull-left[ <!-- https://www.mrlc.gov/data/legends/national-land-cover-database-2011-nlcd2011-legend --> ```r library(sf) library(raster) study_area <- read_sf("data/study_area.gpkg") lc_data <- raster("data/example_landscape.tif") ``` ] .pull-right[ ```r plot(lc_data, axes = TRUE) plot(st_geometry(study_area), add = TRUE) ``` <img src="index_files/figure-html/unnamed-chunk-11-1.png" style="display: block; margin: auto;" /> ] --- # Making maps in R: tmap ```r library(tmap) ``` - Thematic mapping package - It allows you to create static maps, animated maps and interactive maps. - It works by adding subsequent layers to visualize and then modifying them. - https://github.com/mtennekes/tmap - list of additional materials about the **tmap** package <img src="figs/tmap-examples.png" width="75%" style="display: block; margin: auto;" /> --- class: left, middle, clear, nonum ## First practical part Try to work through *Exercise_1_1.R*. The exercises will throw you in at the deep end - **you need to modify the provided code to improve the resulting map**. During the exercise there are part where you are supposed to type your own solution indicated by the following structure: ```r # /Start Code/ # print("Hello World") # This would be your code contribution # /End Code/ # ``` In case you run into problems or have any questions, please contact us at any time via *Zoom* private chat. --- # Making maps in R .left-code[ ```r *tm_shape(study_area) ``` ``` ## Error: no layer elements defined after tm_shape ``` ] --- # Making maps in R .left-code[ ```r tm_shape(study_area) + * tm_borders() ``` ] .right-plot[ ![](index_files/figure-html/tm2-1.png) ] --- # Making maps in R .left-code[ ```r tm_shape(study_area) + tm_borders() + * tm_scale_bar(position = c("right", * "bottom")) + * tm_compass(position = c("left", "top")) ``` ] .right-plot[ ![](index_files/figure-html/tm3-1.png) ] --- # Making maps in R .left-code[ ```r tm_shape(study_area) + tm_borders() + tm_scale_bar(position = c("right", "bottom")) + tm_compass(position = c("left", "top")) + * tm_layout(main.title = "Study area") ``` ] .right-plot[ ![](index_files/figure-html/tm4-1.png) ] --- # Making maps in R ```r *tmap_mode("view") tm_shape(study_area) + tm_borders() + tm_layout(main.title = "Study area") ``` ```r *tmap_mode("plot") tm_shape(study_area) + tm_borders() + tm_layout(main.title = "Study area") ``` --- # Making maps in R .left-code[ ```r *tm_shape(lc_data) + * tm_raster() ``` ] .right-plot[ ![](index_files/figure-html/tm5-1.png) ] --- # Making maps in R .left-code[ ```r tm_shape(lc_data) + tm_raster() + * tm_layout(legend.outside = TRUE) ``` ] .right-plot[ ![](index_files/figure-html/tm6-1.png) ] --- # Making maps in R .left-code[ ```r tm_shape(lc_data) + * tm_raster(drop.levels = TRUE, * title = "Land cover:") + tm_layout(legend.outside = TRUE) ``` ] .right-plot[ ![](index_files/figure-html/tm7-1.png) ] --- # Making maps in R .left-code[ ```r tm_shape(lc_data) + tm_raster(drop.levels = TRUE, title = "Land cover:") + * tm_shape(study_area) + * tm_borders() + * tm_scale_bar(position = c("right", * "bottom")) + * tm_compass(position = c("left", "top")) + * tm_layout(main.title = "Study area", legend.outside = TRUE) ``` ] .right-plot[ ![](index_files/figure-html/tm8-1.png) ] --- # Making maps in R .left-code[ ```r tm_shape(lc_data) + tm_raster(drop.levels = TRUE, title = "Land cover:") + tm_shape(study_area) + * tm_borders(lwd = 3, col = "black") + tm_scale_bar(position = c("right", "bottom")) + tm_compass(position = c("left", "top")) + tm_layout(main.title = "Study area", legend.outside = TRUE) ``` ] .right-plot[ ![](index_files/figure-html/tm9-1.png) ] --- # Making maps in R ```r elev_data <- raster("data/example_elevation.tif") ``` .left-code[ ```r *tm_shape(elev_data) + * tm_raster() ``` ] .right-plot[ ![](index_files/figure-html/tm10-1.png) ] --- # Making maps in R .left-code[ ```r tm_shape(elev_data) + * tm_raster(style = "cont", * title = "Elevation (m asl)") + * tm_layout(legend.outside = TRUE) ``` ] .right-plot[ ![](index_files/figure-html/tm11-1.png) ] --- # Making maps in R .left-code[ ```r tm_shape(elev_data) + tm_raster(style = "cont", title = "Elevation (m asl)", * palette = "-RdYlGn") + tm_layout(legend.outside = TRUE) ``` ```r # tmaptools::palette_explorer() ``` ] .right-plot[ ![](index_files/figure-html/tm12-1.png) ] --- # Making maps in R .left-code[ ```r map1 <- tm_shape(elev_data) + tm_raster(style = "cont", title = "Elevation (m asl)", palette = "-RdYlGn") + tm_layout(legend.outside = TRUE) *tmap_save(map1, "my_first_map.png") *tmap_save(map1, "my_first_map.html") ``` ] .right-plot[ ![](index_files/figure-html/tm12-1.png) ] --- # R spatial processing power .pull-left[ Attribute data operations: - Vector attribute subsetting, aggregation and joins - Creating new vector attributes - Raster subsetting - Summarizing raster objects Spatial data operations: - Spatial subsetting - Topological relations - Spatial joining - Aggregation - Map algebra - Local, focal, and zonal raster operations ] .pull-right[ Geometry operations: - Geometric operations on vector data - Geometric operations on raster data - **Vector-raster interactions** Coordinate reference systems: - Understanding map projections - **Reprojecting spatial data** - Modifying map projections ] --- # Vector-raster interactions: crop .lc[ ```r lc_data ``` <img src="index_files/figure-html/unnamed-chunk-19-1.png" style="display: block; margin: auto;" /> ] .rc[ ```r *lc_data_cropped <- crop(lc_data, study_area) ``` <img src="index_files/figure-html/unnamed-chunk-21-1.png" style="display: block; margin: auto;" /> ] --- # Vector-raster interactions: mask .lc[ ```r lc_data ``` <img src="index_files/figure-html/unnamed-chunk-23-1.png" style="display: block; margin: auto;" /> ] .rc[ ```r # lc_data_masked <- mask(crop(lc_data, study_area), study_area) *lc_data_masked <- mask(lc_data_cropped, study_area) ``` <img src="index_files/figure-html/unnamed-chunk-25-1.png" style="display: block; margin: auto;" /> ] --- # Spatial data coordinates - **Geographic coordinates**: angles (degrees; longitude and latitude), pointing out locations on a spherical or ellipsoidal surface - **Projected coordinates**: measured on a two-dimensional flat space (e.g. in metres; x and y), related to an ellipsoid by projection .pull-left[ <img src="index_files/figure-html/unnamed-chunk-27-1.png" style="display: block; margin: auto;" /> ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-28-1.png" style="display: block; margin: auto;" /> ] --- # Coordinate reference systems .pull-left[ ```r *st_crs(study_area) ``` ``` ## Coordinate Reference System: ## No user input ## wkt: ## PROJCS["unnamed", ## GEOGCS["GRS 1980(IUGG, 1980)", ## DATUM["unknown", ## SPHEROID["GRS80",6378137,298.257222101], ## TOWGS84[0,0,0,0,0,0,0]], ## PRIMEM["Greenwich",0], ## UNIT["degree",0.0174532925199433]], ## PROJECTION["Albers_Conic_Equal_Area"], ## PARAMETER["standard_parallel_1",29.5], ## PARAMETER["standard_parallel_2",45.5], ## PARAMETER["latitude_of_center",23], ## PARAMETER["longitude_of_center",-96], ## PARAMETER["false_easting",0], ## PARAMETER["false_northing",0], ## UNIT["Meter",1]] ``` ] -- .pull-right[ ```r *crs(lc_data) ``` ``` ## CRS arguments: ## +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 ## +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs ``` ] --- # Reprojecting spatial data .pull-left[ - [A vast topic](https://geocompr.robinlovelace.net/reproj-geo-data.html#when-to-reproject) - **Geographic Coordinate Reference Systems:** data integration, web mapping. Usually WGS84 (EPSG: 4326) - **Projected Coordinate Reference Systems:** data analysis, local maps. Often some CRS is already used in the project/by the institution - You can find different CRSs and their representations at https://epsg.io/ Vector data: 1. `study_area2 <- st_transform(study_area, st_crs(another_sf_obj))` 1. `study_area2 <- st_transform(study_area, crs(lc_data))` 1. `study_area2 <- st_transform(study_area,4326)` 1. `study_area2 <- st_transform(study_area, "ESRI:54030")` ] -- .pull-right[ Raster data: 1. `elev_data2 <- projectRaster(elev_data, crs = "+proj=longlat +datum=WGS84 +no_defs")` 1. `elev_data2 <- projectRaster(elev_data, crs = lc_data)` 1. `elev_data2 <- projectRaster(elev_data, crs = st_crs(study_area)$proj4string)` Method is important here: 1. `elev_data2 <- projectRaster(elev_data, crs = "+proj=longlat +datum=WGS84 +no_defs", method = "bilinear")` 1. `lc_data2 <- projectRaster(lc_data, crs = "+proj=longlat +datum=WGS84 +no_defs", method = "ngb")` ] --- # Reprojecting spatial data <!-- ?resample --> <!-- MH: Maybe that would be a bit too much? --> .pull-left[ ```r elev_data ``` <br> <br> <img src="index_files/figure-html/unnamed-chunk-32-1.png" style="display: block; margin: auto;" /> ] .pull-right[ ```r *elev_data2 <- projectRaster(elev_data, * crs = lc_data, * method = "bilinear") ``` <img src="index_files/figure-html/unnamed-chunk-34-1.png" style="display: block; margin: auto;" /> ] --- class: left, middle, clear, nonum ## Second practical part Try to work through *Exercise_1_2.R*. The exercises will focus on understanding spatial data, interactions between raster and vector representation, and reprojecting spatial objects. During the exercise there are part where you are supposed to type your own solution indicated by the following structure: ```r # /Start Code/ # print("Hello World") # This would be your code contribution # /End Code/ # ``` In case you run into problems or have any questions, please contact us at any time via *Zoom* private chat. --- # Summary <!-- add something about r spatial ecosystem?? --> .lc[ <a href="https://geocompr.robinlovelace.net/"> <img src="figs/book_cover.jpg" width="100%" style="display: block; margin: auto;" /> </a> ] .rc[ Recent talks: - "The landscape of spatial data analysis in R": [video](https://www.youtube.com/watch?v=bLgoqnqJ4DY) and [slides](https://nowosad.github.io/whyr_19) - "Recent changes in R spatial and how to be ready for them": [video](https://www.youtube.com/watch?v=Va0STgco7-4) and [slides](https://nowosad.github.io/whyr_webinar004) Resources: - [The Geocomputation with R website](https://geocompr.github.io/): open source book, blog posts, workshop materials, and more - [The Spatial Data Science with R website (focus on raster)](https://www.rspatial.org/): materials focused on the **raster** package - [The r-spatial website](https://www.r-spatial.org/): blog posts and a book-in-progress - [The GIS Stackexchange website](https://gis.stackexchange.com/questions/tagged/r): place to ask questions related to spatial data analysis in R - `#rspatial` and `#geocompr` on Twitter ] --- class: inverse, left, bottom, clear, nonum # Break (30 minutes) --- # Break (30 minutes) .large[ - You can ask us any questions related to spatial data analysis in R and the *landscapemetrics* package during the workshop using https://www.sli.do/IALENA - The event code is `IALENA` ] <!--We could ask for questions during the intro, and then during the break, we could look at them and prepare our answers.--> <!--we should use some online timer (or rmarkdown timer), visible to the participants--> <!-- MH: I assume there will be plenty of question coming up during the practical as well --> --- class: inverse, left, bottom, clear, nonum layout: false background-image: url("figs/landscapemetrics.png") background-position: 85% 10% background-size: 25% # Part II ## *landscapemetrics*: An open-source R tool to calculate landscape metrics Maximilian H.K. Hesselbarth Department of Ecosystem Modelling, University of Goettingen, Germany --- # Introdution to landscape metrics -- - Tools to quantify composition and configuration of landscapes - Composition: number and abundance of land-cover classes - Configuration: spatial arrangement of land-cover classes -- - Designed for discrete patches of different land-cover classes <center> -- .pull-left[ <img src="figs/aerial_lsm.png" width="75%" height="75%" style="display: block; margin: auto;" /> ] .pull-right[ <img src="index_files/figure-html/example_lsm-1.png" width="75%" height="75%" style="display: block; margin: auto;" /> ] </center> --- layout: false background-image: url("figs/landscapemetrics.png") background-position: 90% 5% background-size: 15% # The R package *landscapemetrics* -- - Based on [*raster*](https://CRAN.R-project.org/package=raster) package <font size="3" color="grey"> (Hijmans 2017) </font> -- - Allows to calculate landscape metrics in transparent and reproducible workflow -- - Tidy <font size="3" color="grey"> (sensu Wickham 2014) </font> and type-stable output of all metric functions -- - Includes many utility functions for e.g. plotting or sampling <br> <center> -- <img src="index_files/figure-html/levels_lsm-1.png" width="30%" /><img src="index_files/figure-html/levels_lsm-2.png" width="30%" /><img src="index_files/figure-html/levels_lsm-3.png" width="30%" /> </center> --- # Getting ready to use *landscapemetrics* -- Import and check data ```r # install.packages("landscapemetrics") library(landscapemetrics) library(dplyr) lc_data <- raster("data/toy_landscape.tif") check_landscape(lc_data) ``` ``` ## layer crs units class n_classes OK ## 1 1 NA NA integer 3 ?⃝ ``` -- List available metrics ```r list_lsm() ``` ``` ## # A tibble: 132 x 5 ## metric name type level function_name ## <chr> <chr> <chr> <chr> <chr> ## 1 area patch area area and edge metric patch lsm_p_area ## 2 cai core area index core area metric patch lsm_p_cai ## 3 circle related circumscribing circle shape metric patch lsm_p_circle ## 4 contig contiguity index shape metric patch lsm_p_contig ## 5 core core area core area metric patch lsm_p_core ## # … with 127 more rows ``` --- # How to calculate landscape metrics -- .pull-left[ Calculate single metric ```r lsm_p_area(lc_data) ``` ``` ## # A tibble: 27 x 6 ## layer level class id metric value ## <int> <chr> <int> <int> <chr> <dbl> ## 1 1 patch 1 1 area 0.0001 ## 2 1 patch 1 2 area 0.0005 ## 3 1 patch 1 3 area 0.0148 ## 4 1 patch 1 4 area 0.0001 ## 5 1 patch 1 5 area 0.0001 ## # … with 22 more rows ``` ] -- .pull-right[ Combine several metrics to one *tibble* ```r class_mean <- lsm_c_area_mn(lc_data) lsm_total <- lsm_l_ta(lc_data) bind_rows(class_mean, lsm_total) ``` ``` ## # A tibble: 4 x 6 ## layer level class id metric value ## <int> <chr> <int> <int> <chr> <dbl> ## 1 1 class 1 NA area_mn 0.00199 ## 2 1 class 2 NA area_mn 0.00173 ## 3 1 class 3 NA area_mn 0.0120 ## 4 1 landscape NA NA ta 0.09 ``` ] --- # Easy integration into larger workflows -- Integrate easily into larger workflows ```r set.seed(42) sample(x = c(1, 2, 3), prob = c(0.5, 0.25, 0.25), size = 2500, replace = TRUE) %>% matrix(nrow = 50, ncol = 50) %>% raster() %>% * lsm_p_enn() %>% filter(value <= quantile(value, probs = 0.25) | value >= quantile(value, probs = 0.75)) %>% group_by(class) %>% summarise(n = n()) %>% arrange(-n) ``` ``` ## # A tibble: 3 x 2 ## class n ## <int> <int> ## 1 2 150 ## 2 3 130 ## 3 1 21 ``` --- # Calculation of multiple metrics -- Easily calculate multiple metrics based on e.g. level and type ```r calculate_lsm(landscape = lc_data, level = "landscape", type = "diversity metric", classes_max = 5) ``` ``` ## # A tibble: 9 x 6 ## layer level class id metric value ## <int> <chr> <int> <int> <chr> <dbl> ## 1 1 landscape NA NA msidi 0.929 ## 2 1 landscape NA NA msiei 0.845 ## 3 1 landscape NA NA pr 3 ## 4 1 landscape NA NA prd 3333. ## 5 1 landscape NA NA rpr 60 ## # … with 4 more rows ``` --- class: left, middle, clear, nonum ## First practical part Try to work through *Exercise_2_1.R*. The exercise will teach you how to calculate single and multiple landscape metrics and how to integrate *landscapemetrics* into larger workflows. During the exercise there are part where you are supposed to type your own solution indicated by the following structure: ```r # /Start Code/ # print("Hello World") # This would be your code contribution # /End Code/ # ``` In case you run into problems or have any questions, please contact us at any time via *Zoom* private chat. For a general overview of *landscapemetrics*, please see <https://r-spatialecology.github.io/landscapemetrics>. --- # Visualize patches -- Easily visualize patches of several classes or the whole landscape ```r show_patches(augusta_nlcd, labels = FALSE, class = 42) ``` <img src="index_files/figure-html/show_patches-1.png" style="display: block; margin: auto;" /> --- # Further visualization -- .pull-left[ Visualize core areas of patches ```r show_cores(augusta_nlcd, labels = FALSE, class = 42) ``` <img src="index_files/figure-html/show_cores-1.png" style="display: block; margin: auto;" /> ] -- .pull-right[ Visualize landscape metrics on patch level ```r show_lsm(augusta_nlcd, labels = FALSE, class = 42, what = "lsm_p_area") ``` <img src="index_files/figure-html/show_lsm-1.png" style="display: block; margin: auto;" /> ] --- # Sample metrics in buffer areas -- - Samples metrics in a buffer area (sample plot) around sample points -- - Also able to use sample lines or polygons (as [*sp*](https://CRAN.R-project.org/package=sp)-objects; <font size="3" color="grey"> Pebesma & Bivand, 2005) </font>. -- - Metrics can be specified identical to `list_lsm()` and `calculate_lsm()` -- <img src="index_files/figure-html/plot_sample_points-1.png" style="display: block; margin: auto;" /> --- # Sample metrics in buffer areas -- Slightly different output *tibble* ```r sample_points <- matrix(c(1253709, 1249455, 1261857, 1256055, 1252009, 1257055), ncol = 2, byrow = TRUE) sample_lsm(landscape = augusta_nlcd, y = sample_points, size = 1000, what = "lsm_l_np") ``` ``` ## # A tibble: 3 x 8 ## layer level class id metric value plot_id percentage_inside ## <int> <chr> <int> <int> <chr> <dbl> <int> <dbl> ## 1 1 landscape NA NA np 103 1 99.5 ## 2 1 landscape NA NA np 116 2 99.5 ## 3 1 landscape NA NA np 80 3 99.5 ``` --- # Moving window -- - Show gradients and variability in the landscape -- - Use matrix to specify moving window and focal cell -- <img src="index_files/figure-html/moving_window_plot-1.gif" style="display: block; margin: auto;" /> --- # Moving window Results in list with *raster* objects ```r window <- matrix(1, nrow = 5, ncol = 5) window_result <- window_lsm(lc_data, window = window, what = "lsm_l_np") ``` <img src="index_files/figure-html/moving_window_result-1.png" width="40%" style="display: block; margin: auto;" /> --- # Building blocks -- Fast building blocks to create your own metrics .pull-left[ *raster* package <font size="3" color="grey"> (Hijmans 2017) </font> ```r adj_raster <- function(x) { adjacencies <- adjacent(x,cells = 1:ncell(x)) table(x[adjacencies[, 1]],x[adjacencies[, 2]]) } adj_raster(lc_data) ``` ``` ## ## 1 2 3 ## 1 520 43 137 ## 2 43 704 184 ## 3 137 184 1528 ``` ] .pull-right[ *landscapemetrics* package ```r get_adjacencies(lc_data) ``` ``` ## [[1]] ## 1 2 3 ## 1 520 43 137 ## 2 43 704 184 ## 3 137 184 1528 ``` ] -- ```r # A tibble: 2 x 3 expression mean mem_alloc <chr> <bch:tm> <bch:byt> 1 adj_raster(landscape) 8900 μs 1470 KB 2 get_adjacencies(landscape) 562 μs 6 KB ``` --- ## Second practical part Try to work through *Exercise_2_2.R*. The exercise will teach you how to use all utility functions of the *landscapemetrics* package. During the exercise there are parts where you are supposed to code your own solution indicated by the following structure: ```r # /Start Code/ # print("Hello World") # This would be your code contribution # /End Code/ # ``` In case you run into problems or have any questions, please contact us at any time via *Zoom* private chat. For a general overview of *landscapemetrics*, please see <https://r-spatialecology.github.io/landscapemetrics>. --- # Citation - Questions, feature requests or bugs at [https://github.com/r-spatialecology/landscapemetrics/issues](https://github.com/r-spatialecology/landscapemetrics/issues) ```r citation("landscapemetrics") ``` ``` ## ## To cite landscapemetrics or acknowledge its use, please cite this ## Software note, substituting the version of the application that you ## used for ‘version 0’. ## ## Hesselbarth, M.H.K., Sciaini, M., With, K.A., Wiegand, K., Nowosad, ## J. 2019. landscapemetrics: an open-source R tool to calculate ## landscape metrics. - Ecography 42:1648-1657(ver. 0). ## ## A BibTeX entry for LaTeX users is ## ## @Article{, ## title = {landscapemetrics: an open-source R tool to calculate landscape metrics}, ## author = {Maximilian H.K. Hesselbarth and Marco Sciaini and Kimberly A. With and Kerstin Wiegand and Jakub Nowosad}, ## journal = {Ecography}, ## volume = {42}, ## year = {2019}, ## pages = {1648-1657}, ## } ``` --- class: left, top, clear .pull-left[ <img src="figs/geocompr-logo.png" width="25%" style="display: block; margin: auto auto auto 0;" /> ## Summary: - R gives a ["console-based ability to seamlessly switch between geographic and non-geographic data processing, modeling and visualization tasks"](https://geocompr.robinlovelace.net/intro.html) - **landscapemetrics** allows for calculating landscape metrics for categorical landscape patterns in a tidy workflow - We encourage bug reports, new metrics or functions suggestions, and code contributions. For more information see [CONTRIBUTING](https://github.com/r-spatialecology/landscapemetrics/blob/master/CONTRIBUTING.md) - This workshop is just a tip of an iceberg (of possibilities), *but you do not need to know it all* - Questions? ] .pull-right[ <img src="figs/landscapemetrics.png" width="20%" style="display: block; margin: auto auto auto 0;" /> ## Contact:
[jakub_nowosad](https://twitter.com/jakub_nowosad)
[MHKHesselbarth](https://twitter.com/mhkhesselbarth) ## Resources: https://nowosad.github.io https://mhesselbarth.rbind.io/ ] --- # References Hijmans, R.J., 2019. raster: Geographic data analysis and modeling. R package version 2.9-5. <https://cran.r-project.org/package=raster>. <!-- McGarigal, K., Cushman, S.A., Ene, E., 2012. FRAGSTATS v4: Spatial pattern analysis program for categorical and continuous maps. Computer software program produced by the authors at the University of Massachusetts, Amherst. <http://www.umass.edu/landeco/research/fragstats/fragstats.html>. University of Massachusetts, Amherst. --> Pebesma, E.J., Bivand, R.S., 2005. Classes and methods for spatial data in R. R News 5(2). <!-- Porta, C., Spano, L.D., Pontedera, F., 2017. r.li - Toolset for multiscale analysis of landscape structure. --> <!-- VanDerWal, J., Falconi, L., Januchowski, S., Shoo, L., Storlie, C., 2019. SDMTools: Species distribution modelling tools: Tools for processing data associated with species distribution modelling exercises. R package version 1.1-221.1. <https://CRAN.R-project.org/package=SDMTools>. --> Wickham, H., 2014. Tidy Data. J. Stat. Softw. 59, 1–23. <!-- Wickham, H., Averick, M., Bryan, J., Chang, W., McGowan, L., François, R., Grolemund, G., Hayes, A., Henry, L., Hester, J., Kuhn, M., Pedersen, T., Miller, E., Bache, S., Müller, K., Ooms, J., Robinson, D., Seidel, D., Spinu, V., Takahashi, K., Vaughan, D., Wilke, C., Woo, K., Yutani, H., 2019. Welcome to the Tidyverse. J. Open Source Softw. 4, 1686. -->