library(raster)
library(RColorBrewer)
# Make random forest cover map
a <- raster(extent(0,1e2,0,1e2), res = 1)
hab <- gaussMap(a,speedup = 1) # if raster is large (>1e6 pixels), use speedup>1
names(hab) = "hab"
cells <- loci <- b <- as.integer(sample(1:ncell(a),1e1))
mask <- raster(a)
mask <- setValues(mask, 0)
mask[1:5000] <- 1
numCol <- ncol(a)
numCell <- ncell(a)
directions <- 8
# Transparency involves putting two more hex digits on the color code: 00 is fully transparent.
setColors(hab) <- paste(c("#FFFFFF", brewer.pal(8, "Greys")), c("00", rep("FF", 8)), sep = "")
#dev(4)
Plot(hab, new = TRUE, speedup = 3) # note speedup is equivalent to making pyramids,
# so, some details are lost
# initiate 10 fires at to loci
fires <- spread(hab, loci = as.integer(sample(1:ncell(hab), 10)),
0.235, 0, NULL, 1e8, 8, 1e6, mapID = TRUE)
#set colors of raster, including a transparent layer for zeros
setColors(fires, 10) <- c("#00000000", brewer.pal(8,"Reds")[5:8])
Plot(fires)
Plot(fires,addTo = "hab")
#alternatively, set colors using cols= in the Plot function
Plot(hab, new = TRUE)
Plot(fires) # default color range makes zero transparent.
# Instead, to give a color to the zero values, use \\code{zero.color=}
Plot(fires, addTo = "hab",
cols = colorRampPalette(c("orange","darkred"))(10))
hab2 <- hab
Plot(hab2)
Plot(fires, addTo = "hab2", zero.color = "white",
cols = colorRampPalette(c("orange","darkred"))(10))
# or overplot the original (NOTE: legend stays at original values)
Plot(fires,
cols = topo.colors(10))
## Use interrupt a spread event using iterations - need returnIndices=TRUE to use outputs
## as new inputs in next iteration
fires <- spread(hab, loci = as.integer(sample(1:ncell(hab), 10)), returnIndices=TRUE,
0.235, 0, NULL, 1e8, 8, iterations = 3, mapID = TRUE)
fires[,list(size=length(initialLocus)), by=eventID] # See sizes of fires
## Continue event by passing interrupted object into spreadState
fires2 <- spread(hab, loci=NA_real_, returnIndices=TRUE, 0.235,
0, NULL, 1e8, 8, iterations = 2, mapID = TRUE,
spreadState=fires)
# NOTE events are assigned arbitrary IDs, starting at 1
## Add new fires to the already burning fires
fires3 <- spread(hab, loci = as.integer(sample(1:ncell(hab), 10)), returnIndices=TRUE,
0.235, 0, NULL, 1e8, 8, iterations = 1, mapID = TRUE,
spreadState=fires)
fires3[,list(size=length(initialLocus)), by=eventID] # See sizes of fires
# NOTE old eventIDs are maintained, new events get ids begining above previous
# maximum (e.g., new fires 11 to 20 here)
## Use data.table and loci...
fires <- spread(hab, loci = as.integer(sample(1:ncell(hab), 10)), returnIndices=TRUE,
0.235, 0, NULL, 1e8, 8, iterations = 2, mapID = TRUE)
fullRas <- raster(hab)
fullRas[] <- 1:ncell(hab)
burned <- fires[active == FALSE]
burnedMap <- rasterizeReduced(burned, fullRas, "eventID", "indices")
Plot(burnedMap, new=TRUE)
Run the code above in your browser using DataLab