clc_diff = clc2018_tartu != clc2000_tartu
plot(clc_diff)Time for the above code chunk to run: 0.17 seconds
This document shows a set of examples related to the article “Comparing spatial patterns in raster data using R”. The examples are focused on comparing two layers of spatial categorical raster data, and are divided into three groups: raster outcome, single value outcome, and multiple value outcome.
The examples in this document use two raster layers representing the Corine Land Cover (CLC) data (European Environment Agency and European Environment Agency 2019) for Tartu, Estonia, for the years 2000 and 2018, and Poznań, Poland for 2018. The terra package (Hijmans 2024) is used for raster data manipulation.
The calculation time for each example is estimated and displayed at the end of each code chunk.
library(terra)
clc2000_tartu = rast("data/clc2000_tartu.tif")
clc2018_tartu = rast("data/clc2018_tartu.tif")
clc2018_poznan = rast("data/clc2018_poznan.tif")
plot(clc2000_tartu, main = "Tartu (2000)")
plot(clc2018_tartu, main = "Tartu (2018)")
plot(clc2018_poznan, main = "Poznań (2018)")Time for the above code chunk to run: 5.68 seconds
clc_diff = clc2018_tartu != clc2000_tartu
plot(clc_diff)Time for the above code chunk to run: 0.17 seconds
This example uses the landscapemetrics package (Hesselbarth et al. 2019).
library(landscapemetrics)
window = matrix(1, nrow = 5, ncol = 5)
clc2000_tartu_relmutinf_mw = window_lsm(clc2000_tartu, window = window, what = "lsm_l_relmutinf")
clc2018_tartu_relmutinf_mw = window_lsm(clc2018_tartu, window = window, what = "lsm_l_relmutinf")
clc2018_tartu_relmutinf_diff = abs(clc2018_tartu_relmutinf_mw[[1]][[1]] - clc2000_tartu_relmutinf_mw[[1]][[1]])
plot(clc2018_tartu_relmutinf_diff)Time for the above code chunk to run: 54.56 seconds
This example uses the spatialEco package (Evans and Murphy 2023).
library(spatialEco)
clc_ce = raster.change(clc2000_tartu, clc2018_tartu, s = 5, 
                       stat = "cross-entropy")
plot(clc_ce)Time for the above code chunk to run: 144.55 seconds
clc_diff = clc2018_tartu != clc2000_tartu
changed_pixels = freq(clc_diff)$count[2]
total_pixels = ncell(clc_diff)
proportion_changed = changed_pixels / total_pixels
proportion_changed[1] 0.05443125
Time for the above code chunk to run: 0.05 seconds
clc2000_tartu_freq = freq(clc2000_tartu)
clc2018_tartu_freq = freq(clc2018_tartu)
freq = merge(clc2000_tartu_freq, clc2018_tartu_freq, by = "value", all = TRUE)
freq[is.na(freq)] = 0
freq$diff = abs(freq$count.x - freq$count.y)
sum_diff = sum(freq$diff)
total_pixels = ncell(clc_diff)
1 - sum_diff / total_pixels[1] 0.9716375
Time for the above code chunk to run: 0.32 seconds
Overall difference metrics from the diffeR package (Pontius Jr. and Santacruz 2023).
library(diffeR)
clc_ct = crosstabm(clc2000_tartu, clc2018_tartu)
overallAllocD(clc_ct)[1] 6440
overallDiff(clc_ct)[1] 8709
overallExchangeD(clc_ct)[1] 5280
overallQtyD(clc_ct)[1] 2269
overallShiftD(clc_ct)[1] 1160
Time for the above code chunk to run: 4.52 seconds
This example uses the landscapemetrics package (Hesselbarth et al. 2019).
library(landscapemetrics)
clc2018_tartu_shdi = lsm_l_shdi(clc2018_tartu)
clc2018_poznan_shdi = lsm_l_shdi(clc2018_poznan)
abs(clc2018_poznan_shdi$value - clc2018_tartu_shdi$value)[1] 0.02981541
Time for the above code chunk to run: 0.21 seconds
This example uses the sabre package (J. Nowosad and Stepinski 2018).
library(sabre)
clc_sabre = vmeasure_calc(clc2000_tartu, clc2018_tartu)
plot(clc_sabre$map1)plot(clc_sabre$map2)Time for the above code chunk to run: 5.73 seconds
This example uses the landscapemetrics package (Hesselbarth et al. 2019).
library(landscapemetrics)
clc2018_tartu_ed = lsm_l_ed(clc2018_tartu)
clc2018_poznan_ed = lsm_l_ed(clc2018_poznan)
abs(clc2018_poznan_ed$value - clc2018_tartu_ed$value)[1] 5.63159
Time for the above code chunk to run: 0.17 seconds
This example uses the bespatial package (Jakub Nowosad 2024).
library(bespatial)
clc2018_tartu_bes = bes_m_zhao(clc2018_tartu)
clc2018_poznan_bes = bes_m_zhao(clc2018_poznan)
abs(clc2018_poznan_bes$value - clc2018_tartu_bes$value)[1] 0.00207152
Time for the above code chunk to run: 0.79 seconds
This example uses the motif package (Jakub Nowosad 2021).
library(motif)
library(philentropy)
clc2018_tartu_sign = lsp_signature(clc2018_tartu, type = "cove", threshold = 1)
clc2018_poznan_sign = lsp_signature(clc2018_poznan, type = "cove", threshold = 1)
jensen_shannon(clc2018_tartu_sign$signature[[1]],
               clc2018_poznan_sign$signature[[1]], 
               testNA = FALSE, unit = "log2")[1] 0.08261638
Time for the above code chunk to run: 0.43 seconds
table(values(clc2000_tartu), values(clc2018_tartu))   
        1     2     3     4     6     7
  1  4821   357     2    10     0     0
  2  1342 67915   684   389     0     0
  3    59   415 33670  2896     9    22
  4   122   638  1435  7040   229    24
  6     0     3    13    20  2177    37
  7     0     0     0     0     3  1995
Time for the above code chunk to run: 0.08 seconds