knitr::opts_chunk$set(echo = TRUE)

Examples with toy matrix

8x8 pixel toy matrix made of three land use classes:

  • 1 = SWF
  • 2 = Agriculture
  • 3 = forest
# source('/home/matteo/own_data/PoD/jrc/swf_molder/swf_molder.r')
require(SWFmolder)
require(terra)
#Define the toy matrix
toy.mt <- matrix(c(
    2,2,2,2,2,2,2,2,
    2,2,2,2,2,2,2,2,
    2,2,2,2,2,2,2,2,
    2,2,2,1,1,2,2,2,
    2,2,2,2,2,2,2,2,
    2,2,2,2,2,2,2,2,
    2,2,2,2,2,2,2,2,
    3,3,3,3,3,3,3,3), ncol=8, byrow=TRUE)

storage.mode(toy.mt) <- "integer"
color_map <- c('#ef8a62', '#f7f7f7', '#67a9cf')
plot(rast(t(toy.mt)), col = color_map, axes = FALSE, box = FALSE, main="Toy matrix", plg=list(legend=c("SWF","Agri","Forest")))
text(rast(t(toy.mt)), digits=1)

Simple ortogonal expansion

  • SWF pixel with 1 Agri Neighbour
  • 1 SWF px allocated per iteration
  • Allocation of SWF pixels favours ortogonal Agri neighbours
iters = 10
test.1 <- swf.molder(
Hmatrix = toy.mt, 
swfCover=0.9, 
swfCat=1, 
agriCat=2,
Q=1, # number of neighbour allocated per iterations
ExpDirection="orthogonal", 
iterations = iters, 
kernelCl=ncol(toy.mt), kernelRw=nrow(toy.mt), 
NNeighbors=1, # number of px that a SWF pixel must have to be selected
maxDistance = 1, 
queensCase=TRUE, 
np=1)

Simple diagonal expansion

  • SWF pixel with 1 Agri Neighbour
  • 1 SWF px allocated per iteration
  • Allocation of new SWF pixels favours ortogonal Agri neighbours
iters = 10
test.1 <- swf.molder(
Hmatrix = toy.mt, 
swfCover=0.90, 
swfCat=1, 
agriCat=2,
ExpDirection="mixed", # sets preferred direction of expantion 
Q=1, # number of neighbour allocated per iterations
iterations = iters, 
kernelCl=ncol(toy.mt), kernelRw=nrow(toy.mt), 
NNeighbors=1, # number of px that a SWF pixel must have to be selected
maxDistance = 1, 
queensCase=TRUE, 
np=1)

Simple lateral expansion

Here lateral expansion is sought chosing only edge pixels and allocatin 1 SWF px per time - SWF pixel with 7 Agri Neighbour - 1 SWF px allocated per iteration - Allocation of new SWF pixels favours diagonal Agri neighbours

iters = 3
test.1 <- swf.molder(
Hmatrix = toy.mt, 
swfCover=0.90, 
swfCat=1, 
agriCat=2,
ExpPriority="vertical",
ExpDirection="orthogonal", # sets preferred direction of expantion 
Q=1, # number of neighbour allocated per iterations
iterations = iters, 
kernelCl=ncol(toy.mt), kernelRw=nrow(toy.mt), 
NNeighbors=7, # number of px that a SWF pixel must have to be selected
maxDistance = 1, 
queensCase=FALSE, 
np=1)

Expansion driven by multiple neighbours and SWF allocation, ortogonal

  • SWF pixel with 5 Agri Neighbour
  • 2 SWF px allocated per iteration
  • Allocation of new SWF pixels favours orthogonal Agri neighbours
iters = 10
test.1 <- swf.molder(
Hmatrix = toy.mt, 
swfCover=0.90, 
swfCat=1, 
agriCat=2,
ExpDirection="orthogonal", # sets preferred direction of expantion 
Q=2, # number of neighbour allocated per iterations
iterations = iters, 
kernelCl=ncol(toy.mt), kernelRw=nrow(toy.mt), 
NNeighbors=5, # number of px that a SWF pixel must have to be selected
maxDistance = 1, 
queensCase=TRUE, 
np=1)

Expansion driven by multiple neighbours and SWF allocation, diagonal

  • SWF pixel with 5 Agri Neighbour
  • 2 SWF px allocated per iteration
  • Allocation of new SWF pixels favours diagonal Agri neighbours
iters = 10
test.1 <- swf.molder(
Hmatrix = toy.mt, 
swfCover=0.90, 
swfCat=1, 
agriCat=2,
ExpDirection="orthogonal", # sets preferred direction of expantion 
Q=2, # number of neighbour allocated per iterations
iterations = iters, 
kernelCl=ncol(toy.mt), kernelRw=nrow(toy.mt), 
NNeighbors=5, # number of px that a SWF pixel must have to be selected
maxDistance = 1, 
queensCase=TRUE, 
np=1)

Expansion following a 1st order “buffer”

This mimic a “buffer” approach. To obtain it the minimum number of Agri NN for each SWF pixel must be 1 and the number of new allocated SWF pixel has to be 7 which is the maximum number of Agri neighbours that a SWF pixel has in the toy example. Theoretically would be 9.

  • SWF pixel with 1 Agri Neighbour
  • 7 SWF px allocated per iteration
iters = 10
test.1 <- swf.molder(
Hmatrix = toy.mt, 
swfCover=0.90, 
swfCat=1, 
agriCat=2,
ExpDirection="orthogonal", # sets preferred direction of expansion 
Q=7, # number of neighbour allocated per iterations
iterations = iters, 
kernelCl=ncol(toy.mt), kernelRw=nrow(toy.mt), 
NNeighbors=1, #number of px that a SWF pixel must have to be selected 
maxDistance = 1, 
queensCase=TRUE, 
np=1)

Expansion following a 2nd order “buffer”

This mimics a 2nd order “buffer” approach. To obtain it, the minimum number of Agri NN for each SWF pixel must be 1 and the number of new allocated SWF pixel has to be the maximum number of 2nd order Agri neighbours that the SWF pixel has in the toy example (8+16=24).

# Remaking the toy matrix with only 1 SWF pixel to make it easier to interpret
toy.mt <- matrix(c(
    2,2,2,2,2,2,2,2,
    2,2,2,2,2,2,2,2,
    2,2,2,2,2,2,2,2,
    2,2,2,1,2,2,2,2,
    2,2,2,2,2,2,2,2,
    2,2,2,2,2,2,2,2,
    2,2,2,2,2,2,2,2,
    3,3,3,3,3,3,3,3), ncol=8, byrow=TRUE)
plot(rast(t(toy.mt)), col = color_map, axes = FALSE, box = FALSE, main="Toy matrix", plg=list(legend=c("SWF","Agri","Forest")))
text(rast(t(toy.mt)), digits=1)

  • SWF pixel with 1 Agri Neighbour
  • 24 SWF px allocated per iteration
iters = 10
test.1 <- swf.molder(
Hmatrix = toy.mt, 
swfCover=0.90, 
swfCat=1, 
agriCat=2,
ExpDirection="diagonal", # sets preferred direction of expantion 
Q=24, # number of neighbour allocated per iterations
iterations = iters, 
kernelCl=ncol(toy.mt), kernelRw=nrow(toy.mt), 
NNeighbors=1, #number of px that a SWF pixel must have to be selected 
maxDistance = 1, 
queensCase=TRUE, 
np=1)