forked from r-spatialecology/ialena-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1460 lines (1062 loc) · 39.1 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<title>Introduction to landscape ecology with R</title>
<meta charset="utf-8" />
<meta name="author" content="Jakub Nowosad and Maximilian H.K. Hesselbarth" />
<meta name="date" content="2020-05-14" />
<link href="libs/remark-css-0.0.1/default.css" rel="stylesheet" />
<link href="libs/remark-css-0.0.1/metropolis.css" rel="stylesheet" />
<link href="libs/remark-css-0.0.1/metropolis-fonts.css" rel="stylesheet" />
<script src="libs/htmlwidgets-1.5.1/htmlwidgets.js"></script>
<script src="libs/jquery/jquery.min.js"></script>
<link href="libs/leaflet-1.3.1/leaflet.css" rel="stylesheet" />
<script src="libs/leaflet-1.3.1/leaflet.js"></script>
<link href="libs/leafletfix-1.0.0/leafletfix.css" rel="stylesheet" />
<script src="libs/Proj4Leaflet-1.0.1/proj4-compressed.js"></script>
<script src="libs/Proj4Leaflet-1.0.1/proj4leaflet.js"></script>
<link href="libs/rstudio_leaflet-1.3.1/rstudio_leaflet.css" rel="stylesheet" />
<script src="libs/leaflet-binding-2.0.3/leaflet.js"></script>
<script src="libs/leaflet-providers-1.9.0/leaflet-providers_1.9.0.js"></script>
<script src="libs/leaflet-providers-plugin-2.0.3/leaflet-providers-plugin.js"></script>
<link href="libs/ionicons-2.0.1/css/ionicons.min.css" rel="stylesheet" />
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<textarea id="source">
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[
<div id="htmlwidget-008aea96a7279dd32b94" style="width:504px;height:504px;" class="leaflet html-widget"></div>
<script type="application/json" data-for="htmlwidget-008aea96a7279dd32b94">{"x":{"options":{"crs":{"crsClass":"L.CRS.EPSG3857","code":null,"proj4def":null,"projectedBounds":null,"options":{}}},"calls":[{"method":"createMapPane","args":["tmap401",401]},{"method":"addProviderTiles","args":["Esri.WorldGrayCanvas",null,"Esri.WorldGrayCanvas",{"minZoom":0,"maxZoom":18,"tileSize":256,"subdomains":"abc","errorTileUrl":"","tms":false,"noWrap":false,"zoomOffset":0,"zoomReverse":false,"opacity":1,"zIndex":1,"detectRetina":false,"pane":"tilePane"}]},{"method":"addProviderTiles","args":["OpenStreetMap",null,"OpenStreetMap",{"minZoom":0,"maxZoom":18,"tileSize":256,"subdomains":"abc","errorTileUrl":"","tms":false,"noWrap":false,"zoomOffset":0,"zoomReverse":false,"opacity":1,"zIndex":1,"detectRetina":false,"pane":"tilePane"}]},{"method":"addProviderTiles","args":["Esri.WorldTopoMap",null,"Esri.WorldTopoMap",{"minZoom":0,"maxZoom":18,"tileSize":256,"subdomains":"abc","errorTileUrl":"","tms":false,"noWrap":false,"zoomOffset":0,"zoomReverse":false,"opacity":1,"zIndex":1,"detectRetina":false,"pane":"tilePane"}]},{"method":"addPolygons","args":[[[[{"lng":[-82.247983,-82.225345,-82.212312,-82.239751,-82.274736,-82.311779,-82.339904,-82.352937,-82.356367,-82.347449,-82.31658,-82.28091,-82.247983],"lat":[33.486435,33.514492,33.543112,33.559707,33.569433,33.572294,33.561995,33.539678,33.520217,33.496743,33.481854,33.47899,33.486435]}]]],"X1","study_area",{"interactive":false,"className":"","pane":"tmap401","stroke":true,"color":"#666666","weight":1,"opacity":1,"fill":true,"fillOpacity":0,"dashArray":"none","smoothFactor":1,"noClip":false},null,null,"1",{"interactive":false,"permanent":false,"direction":"auto","opacity":1,"offset":[0,0],"textsize":"10px","textOnly":false,"className":"","sticky":true},null]},{"method":"addLayersControl","args":[["Esri.WorldGrayCanvas","OpenStreetMap","Esri.WorldTopoMap"],"study_area",{"collapsed":true,"autoZIndex":true,"position":"topleft"}]}],"limits":{"lat":[33.47899,33.572294],"lng":[-82.356367,-82.212312]},"fitBounds":[33.47899,-82.356367,33.572294,-82.212312,[]]},"evals":[],"jsHooks":[]}</script>
]
.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)
```