Landscape metrics calculation exercise using the Landscapemetrics library

Download raster data:

Enhanced Vegetation Index downloaded from Nasa Giovanni

raster<-raster("./raster/GIOVANNI-g4.timeAvgMap.MYD13C2_006_CMG_0_05_Deg_Monthly_EVI.20200101-20200531.74W_6N_72W_7N.tif")

check_landscape(raster) %>% 
  kable(caption="EVI features") %>% 
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"),
                fixed_thead = T,
                full_width = F, 
                position = "left")
EVI features
layer crs units class n_classes OK
1 geographic degrees non-integer 578 x

The raster has a geographic CRS, is necesary to transform to projected CRS using epsg:3117

crs(raster)<- "+init=epsg:3117 +proj=longlat +datum=WGS84 +no_defs"

check_landscape(raster) %>% kable()
layer crs units class n_classes OK
1 projected m non-integer 578 x

Summary of raster data

##         GIOVANNI.g4.timeAvgMap.MYD13C2_006_CMG_0_05_Deg_Monthly_EVI.20200101.20200531.74W_6N_72W_7N
## Min.                                                                                        0.18222
## 1st Qu.                                                                                     0.35004
## Median                                                                                      0.45538
## 3rd Qu.                                                                                     0.53242
## Max.                                                                                        0.61878
## NA's                                                                                        0.00000

Raster histogram

ggplot(dat, aes(x = breaks, y = counts, fill =counts)) + ## Note the new aes fill here
  geom_bar(stat = "identity",alpha = 1)+
  labs(x="Pixel value",y="Frequency",
       title = "Raster histogram",
       caption = "EVI from Nasa Giovanny:\nGIOVANNI-g4.timeAvgMap.MYD13C2_006_CMG_0_05_Deg_Monthly_EVI.20200101-20200531.74W_6N_72W_7N")+
  scale_x_continuous(breaks = seq(-0.1,1,0.1),
                   labels = seq(-0.1,1,0.1))+
  scale_fill_gradient(low="grey80", high="white")+          ## to play with colors limits
  theme_light()+
  theme(plot.title=element_text(size=19),
        plot.caption = element_text(size = 7, color="grey60"),
        panel.background = element_rect(fill="steelblue"),
        panel.grid.major = element_line(size = 0.3, linetype = 'dotted',
                                colour = "white"),
        panel.grid.minor = element_line(size = 0.3, linetype = 'dotted',
                                colour = "white"),
        legend.position = "none")

Raster reclasification

clasification matrix

This is where the different classes and the cutoff points of each one are defined. In this example we are going to reclassify the raster into tree classes.

# create classification matrix
reclass_df <- c(0.181, 0.3, 1,
              0.3, 0.4, 1,
             0.4, 0.5, 2,
             0.5, Inf, 3)
reclass_df
##  [1] 0.181 0.300 1.000 0.300 0.400 1.000 0.400 0.500 2.000 0.500   Inf 3.000
# reshape the object into a matrix with columns and rows
reclass_m <- matrix(reclass_df,
                ncol = 3,
                byrow = TRUE)
reclass_m
##       [,1] [,2] [,3]
## [1,] 0.181  0.3    1
## [2,] 0.300  0.4    1
## [3,] 0.400  0.5    2
## [4,] 0.500  Inf    3

And here is where the reclassification process is made

# reclassify the raster using the reclass object - reclass_m
raster_classified <- reclassify(raster,reclass_m)

Reclassification result

ggplot(dat2, aes(x = breaks, y = counts, fill =counts)) + ## Note the new aes fill here
  geom_bar(stat = "identity",alpha = 1)+
  labs(x="Class",y="",
       title = "Number of pixels in each class",
       caption = "EVI from Nasa Giovanny:\nGIOVANNI-g4.timeAvgMap.MYD13C2_006_CMG_0_05_Deg_Monthly_EVI.20200101-20200531.74W_6N_72W_7N")+
  scale_x_continuous(breaks = seq(1,3,1),
                   labels = seq(1,3,1))+
  scale_fill_gradient(low="white", high="white")+          ## to play with colors limits
  theme_light()+
  theme(plot.title=element_text(size=19),
        plot.caption = element_text(size = 7, color="grey60"),
        panel.background = element_rect(fill="steelblue"),
        panel.grid.major = element_line(size = 0.3, linetype = 'dotted',
                                colour = "white"),
        panel.grid.minor = element_line(size = 0.3, linetype = 'dotted',
                                colour = "white"),
        legend.position = "none")

Plot the reclassified raster

# plot reclassified data
show_landscape(raster_classified)

check_landscape(raster_classified)
##   layer       crs units   class n_classes OK
## 1     1 projected     m integer         3  v

Here we see the tree different patch

landscape metrics

  • Table of metrics by each patch
list_lsm(level = 'patch') 
## # A tibble: 12 x 5
##    metric name                            type               level function_name
##    <chr>  <chr>                           <chr>              <chr> <chr>        
##  1 area   patch area                      area and edge met~ 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   
##  6 enn    euclidean nearest neighbor dis~ aggregation metric patch lsm_p_enn    
##  7 frac   fractal dimension index         shape metric       patch lsm_p_frac   
##  8 gyrate radius of gyration              area and edge met~ patch lsm_p_gyrate 
##  9 ncore  number of core areas            core area metric   patch lsm_p_ncore  
## 10 para   perimeter-area ratio            shape metric       patch lsm_p_para   
## 11 perim  patch perimeter                 area and edge met~ patch lsm_p_perim  
## 12 shape  shape index                     shape metric       patch lsm_p_shape
metrics<- calculate_lsm(raster_classified, level = "patch") 

kable(metrics, caption = "Landscape Metrics by patch") %>% 
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"),
                fixed_thead = T,
                full_width = F, 
                position = "left") %>% 
  scroll_box(width = "400px", height = "400px")
Landscape Metrics by patch
layer level class id metric value
1 patch 1 1 area 0.0000002
1 patch 1 2 area 0.0000005
1 patch 1 3 area 0.0000542
1 patch 1 4 area 0.0000005
1 patch 1 5 area 0.0000002
1 patch 2 6 area 0.0000002
1 patch 2 7 area 0.0000017
1 patch 2 8 area 0.0000012
1 patch 2 9 area 0.0000015
1 patch 2 10 area 0.0000005
1 patch 2 11 area 0.0000007
1 patch 2 12 area 0.0000012
1 patch 2 13 area 0.0000002
1 patch 2 14 area 0.0000005
1 patch 2 15 area 0.0000002
1 patch 2 16 area 0.0000002
1 patch 2 17 area 0.0000145
1 patch 2 18 area 0.0000002
1 patch 2 19 area 0.0000055
1 patch 2 20 area 0.0000002
1 patch 2 21 area 0.0000002
1 patch 2 22 area 0.0000002
1 patch 2 23 area 0.0000007
1 patch 2 24 area 0.0000002
1 patch 3 25 area 0.0000610
1 patch 1 1 cai 0.0000000
1 patch 1 2 cai 0.0000000
1 patch 1 3 cai 51.6129032
1 patch 1 4 cai 0.0000000
1 patch 1 5 cai 0.0000000
1 patch 2 6 cai 0.0000000
1 patch 2 7 cai 0.0000000
1 patch 2 8 cai 0.0000000
1 patch 2 9 cai 0.0000000
1 patch 2 10 cai 0.0000000
1 patch 2 11 cai 0.0000000
1 patch 2 12 cai 0.0000000
1 patch 2 13 cai 0.0000000
1 patch 2 14 cai 0.0000000
1 patch 2 15 cai 0.0000000
1 patch 2 16 cai 0.0000000
1 patch 2 17 cai 15.5172414
1 patch 2 18 cai 0.0000000
1 patch 2 19 cai 4.5454545
1 patch 2 20 cai 0.0000000
1 patch 2 21 cai 0.0000000
1 patch 2 22 cai 0.0000000
1 patch 2 23 cai 0.0000000
1 patch 2 24 cai 0.0000000
1 patch 3 25 cai 53.2786885
1 patch 1 1 circle 0.3633802
1 patch 1 2 circle 0.4907042
1 patch 1 3 circle 0.6183799
1 patch 1 4 circle 0.4907042
1 patch 1 5 circle 0.3633802
1 patch 2 6 circle 0.3633802
1 patch 2 7 circle 0.8019405
1 patch 2 8 circle 0.5102925
1 patch 2 9 circle 0.7365711
1 patch 2 10 circle 0.4907042
1 patch 2 11 circle 0.7061755
1 patch 2 12 circle 0.6816901
1 patch 2 13 circle 0.3633802
1 patch 2 14 circle 0.6816901
1 patch 2 15 circle 0.3633802
1 patch 2 16 circle 0.3633802
1 patch 2 17 circle 0.7529836
1 patch 2 18 circle 0.3633802
1 patch 2 19 circle 0.7955382
1 patch 2 20 circle 0.3633802
1 patch 2 21 circle 0.3633802
1 patch 2 22 circle 0.3633802
1 patch 2 23 circle 0.6180281
1 patch 2 24 circle 0.3633802
1 patch 3 25 circle 0.5917603
1 patch 1 1 contig 0.0000000
1 patch 1 2 contig 0.1666667
1 patch 1 3 contig 0.8026114
1 patch 1 4 contig 0.1666667
1 patch 1 5 contig 0.0000000
1 patch 2 6 contig 0.0000000
1 patch 2 7 contig 0.2857143
1 patch 2 8 contig 0.4333333
1 patch 2 9 contig 0.3055556
1 patch 2 10 contig 0.1666667
1 patch 2 11 contig 0.1666667
1 patch 2 12 contig 0.3333333
1 patch 2 13 contig 0.0000000
1 patch 2 14 contig 0.0833333
1 patch 2 15 contig 0.0000000
1 patch 2 16 contig 0.0000000
1 patch 2 17 contig 0.5747126
1 patch 2 18 contig 0.0000000
1 patch 2 19 contig 0.5075758
1 patch 2 20 contig 0.0000000
1 patch 2 21 contig 0.0000000
1 patch 2 22 contig 0.0000000
1 patch 2 23 contig 0.2222222
1 patch 2 24 contig 0.0000000
1 patch 3 25 contig 0.8053279
1 patch 1 1 core 0.0000000
1 patch 1 2 core 0.0000000
1 patch 1 3 core 0.0000280
1 patch 1 4 core 0.0000000
1 patch 1 5 core 0.0000000
1 patch 2 6 core 0.0000000
1 patch 2 7 core 0.0000000
1 patch 2 8 core 0.0000000
1 patch 2 9 core 0.0000000
1 patch 2 10 core 0.0000000
1 patch 2 11 core 0.0000000
1 patch 2 12 core 0.0000000
1 patch 2 13 core 0.0000000
1 patch 2 14 core 0.0000000
1 patch 2 15 core 0.0000000
1 patch 2 16 core 0.0000000
1 patch 2 17 core 0.0000022
1 patch 2 18 core 0.0000000
1 patch 2 19 core 0.0000002
1 patch 2 20 core 0.0000000
1 patch 2 21 core 0.0000000
1 patch 2 22 core 0.0000000
1 patch 2 23 core 0.0000000
1 patch 2 24 core 0.0000000
1 patch 3 25 core 0.0000325
1 patch 1 1 enn 0.1000000
1 patch 1 2 enn 0.1000000
1 patch 1 3 enn 0.1118034
1 patch 1 4 enn 0.1118034
1 patch 1 5 enn 0.1118034
1 patch 2 6 enn 0.1000000
1 patch 2 7 enn 0.1000000
1 patch 2 8 enn 0.1414214
1 patch 2 9 enn 0.1000000
1 patch 2 10 enn 0.1500000
1 patch 2 11 enn 0.1414214
1 patch 2 12 enn 0.1000000
1 patch 2 13 enn 0.1000000
1 patch 2 14 enn 0.1500000
1 patch 2 15 enn 0.1118034
1 patch 2 16 enn 0.1000000
1 patch 2 17 enn 0.1118034
1 patch 2 18 enn 0.1000000
1 patch 2 19 enn 0.1000000
1 patch 2 20 enn 0.1000000
1 patch 2 21 enn 0.1000000
1 patch 2 22 enn 0.1000000
1 patch 2 23 enn 0.1000000
1 patch 2 24 enn 0.4000000
1 patch 3 25 enn NA
1 patch 1 1 frac 1.0000000
1 patch 1 2 frac 0.9777697
1 patch 1 3 frac -2.0557305
1 patch 1 4 frac 0.9777697
1 patch 1 5 frac 1.0000000
1 patch 2 6 frac 1.0000000
1 patch 2 7 frac 0.6853421
1 patch 2 8 frac 0.9490775
1 patch 2 9 frac 0.8300437
1 patch 2 10 frac 0.9777697
1 patch 2 11 frac 0.8499916
1 patch 2 12 frac 0.8658642
1 patch 2 13 frac 1.0000000
1 patch 2 14 frac 0.8691760
1 patch 2 15 frac 1.0000000
1 patch 2 16 frac 1.0000000
1 patch 2 17 frac -0.1447544
1 patch 2 18 frac 1.0000000
1 patch 2 19 frac 0.5133325
1 patch 2 20 frac 1.0000000
1 patch 2 21 frac 1.0000000
1 patch 2 22 frac 1.0000000
1 patch 2 23 frac 0.9412036
1 patch 2 24 frac 1.0000000
1 patch 3 25 frac -3.0972023
1 patch 1 1 gyrate 0.0000000
1 patch 1 2 gyrate 0.0250000
1 patch 1 3 gyrate 0.3519576
1 patch 1 4 gyrate 0.0250000
1 patch 1 5 gyrate 0.0000000
1 patch 2 6 gyrate 0.0000000
1 patch 2 7 gyrate 0.0897433
1 patch 2 8 gyrate 0.0423901
1 patch 2 9 gyrate 0.0690102
1 patch 2 10 gyrate 0.0250000
1 patch 2 11 gyrate 0.0431546
1 patch 2 12 gyrate 0.0518030
1 patch 2 13 gyrate 0.0000000
1 patch 2 14 gyrate 0.0353553
1 patch 2 15 gyrate 0.0000000
1 patch 2 16 gyrate 0.0000000
1 patch 2 17 gyrate 0.2106883
1 patch 2 18 gyrate 0.0000000
1 patch 2 19 gyrate 0.1724022
1 patch 2 20 gyrate 0.0000000
1 patch 2 21 gyrate 0.0000000
1 patch 2 22 gyrate 0.0000000
1 patch 2 23 gyrate 0.0333333
1 patch 2 24 gyrate 0.0000000
1 patch 3 25 gyrate 0.3403119
1 patch 1 1 ncore 0.0000000
1 patch 1 2 ncore 0.0000000
1 patch 1 3 ncore 2.0000000
1 patch 1 4 ncore 0.0000000
1 patch 1 5 ncore 0.0000000
1 patch 2 6 ncore 0.0000000
1 patch 2 7 ncore 0.0000000
1 patch 2 8 ncore 0.0000000
1 patch 2 9 ncore 0.0000000
1 patch 2 10 ncore 0.0000000
1 patch 2 11 ncore 0.0000000
1 patch 2 12 ncore 0.0000000
1 patch 2 13 ncore 0.0000000
1 patch 2 14 ncore 0.0000000
1 patch 2 15 ncore 0.0000000
1 patch 2 16 ncore 0.0000000
1 patch 2 17 ncore 2.0000000
1 patch 2 18 ncore 0.0000000
1 patch 2 19 ncore 1.0000000
1 patch 2 20 ncore 0.0000000
1 patch 2 21 ncore 0.0000000
1 patch 2 22 ncore 0.0000000
1 patch 2 23 ncore 0.0000000
1 patch 2 24 ncore 0.0000000
1 patch 3 25 ncore 3.0000000
1 patch 1 1 para 80.0000000
1 patch 1 2 para 60.0000000
1 patch 1 3 para 13.8248848
1 patch 1 4 para 60.0000000
1 patch 1 5 para 80.0000000
1 patch 2 6 para 80.0000000
1 patch 2 7 para 57.1428571
1 patch 2 8 para 40.0000000
1 patch 2 9 para 46.6666667
1 patch 2 10 para 60.0000000
1 patch 2 11 para 66.6666667
1 patch 2 12 para 48.0000000
1 patch 2 13 para 80.0000000
1 patch 2 14 para 80.0000000
1 patch 2 15 para 80.0000000
1 patch 2 16 para 80.0000000
1 patch 2 17 para 31.7241379
1 patch 2 18 para 80.0000000
1 patch 2 19 para 34.5454545
1 patch 2 20 para 80.0000000
1 patch 2 21 para 80.0000000
1 patch 2 22 para 80.0000000
1 patch 2 23 para 53.3333333
1 patch 2 24 para 80.0000000
1 patch 3 25 para 14.0983607
1 patch 1 1 perim 0.2000000
1 patch 1 2 perim 0.3000000
1 patch 1 3 perim 7.5000000
1 patch 1 4 perim 0.3000000
1 patch 1 5 perim 0.2000000
1 patch 2 6 perim 0.2000000
1 patch 2 7 perim 1.0000000
1 patch 2 8 perim 0.5000000
1 patch 2 9 perim 0.7000000
1 patch 2 10 perim 0.3000000
1 patch 2 11 perim 0.5000000
1 patch 2 12 perim 0.6000000
1 patch 2 13 perim 0.2000000
1 patch 2 14 perim 0.4000000
1 patch 2 15 perim 0.2000000
1 patch 2 16 perim 0.2000000
1 patch 2 17 perim 4.6000000
1 patch 2 18 perim 0.2000000
1 patch 2 19 perim 1.9000000
1 patch 2 20 perim 0.2000000
1 patch 2 21 perim 0.2000000
1 patch 2 22 perim 0.2000000
1 patch 2 23 perim 0.4000000
1 patch 2 24 perim 0.2000000
1 patch 3 25 perim 8.6000000
1 patch 1 1 shape 0.0500000
1 patch 1 2 shape 0.0750000
1 patch 1 3 shape 1.8750000
1 patch 1 4 shape 0.0750000
1 patch 1 5 shape 0.0500000
1 patch 2 6 shape 0.0500000
1 patch 2 7 shape 0.2500000
1 patch 2 8 shape 0.1250000
1 patch 2 9 shape 0.1750000
1 patch 2 10 shape 0.0750000
1 patch 2 11 shape 0.1250000
1 patch 2 12 shape 0.1500000
1 patch 2 13 shape 0.0500000
1 patch 2 14 shape 0.1000000
1 patch 2 15 shape 0.0500000
1 patch 2 16 shape 0.0500000
1 patch 2 17 shape 1.1500000
1 patch 2 18 shape 0.0500000
1 patch 2 19 shape 0.4750000
1 patch 2 20 shape 0.0500000
1 patch 2 21 shape 0.0500000
1 patch 2 22 shape 0.0500000
1 patch 2 23 shape 0.1000000
1 patch 2 24 shape 0.0500000
1 patch 3 25 shape 2.1500000
  • Table of metrics by patch class
metrics %>% 
  group_by(class, metric) %>% 
  summarise(Val=sum(value)) %>% 
  kable(caption="Landscape Metrics by patch class") %>% 
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"),
                fixed_thead = T,
                full_width = F, 
                position = "left") %>% 
  scroll_box(width = "250px", height = "400px")
Landscape Metrics by patch class
class metric Val
1 area 0.0000557
1 cai 51.6129032
1 circle 2.3265487
1 contig 1.1359447
1 core 0.0000280
1 enn 0.5354102
1 frac 1.8998090
1 gyrate 0.4019576
1 ncore 2.0000000
1 para 293.8248848
1 perim 8.5000000
1 shape 2.1250000
2 area 0.0000305
2 cai 20.0626959
2 circle 10.0460359
2 contig 3.0791138
2 core 0.0000025
2 enn 2.4064495
2 frac 16.3370465
2 gyrate 0.7728803
2 ncore 3.0000000
2 para 1238.0791163
2 perim 12.7000000
2 shape 3.1750000
3 area 0.0000610
3 cai 53.2786885
3 circle 0.5917603
3 contig 0.8053279
3 core 0.0000325
3 enn NA
3 frac -3.0972023
3 gyrate 0.3403119
3 ncore 3.0000000
3 para 14.0983607
3 perim 8.6000000
3 shape 2.1500000

Patches plot

show_patches(raster_classified, labels = T)

# show patches of all classes
show_patches(raster_classified, class = "all", labels = FALSE)

Patches area

# fill patch according to area
show_lsm(raster_classified, what = "lsm_p_area", class = "global", label_lsm = TRUE)

Correlation matrix

show_correlation(metrics)