###################################################################### ### ### ### NEUTRAL LANDSCAPE MODELS ### ### ### ### https://ropensci.github.io/NLMR/articles/getstarted.html ### ### https://www.rdocumentation.org/packages/NLMR/versions/0.4.1/ ### ### ### ### ### ###################################################################### rm(list=ls()) ### remove data frames and objects from previous session library(raster) library(NLMR) library (landscapetools) library(rgdal) citation("NLMR") #palettes library(RColorBrewer) #col = rev(brewer.pal(9, "PuBu")) #col = rev (heat.colors(20)) #col = brewer.pal(9, "Blues") #col = rev(brewer.pal(9, "BrBG")) #col = brewer.pal(9, "Greens") #col = rev(brewer.pal(9, "Set3")) ###################################################################### ### ### ### Simulating and plotting a selection of neutral landscape maps ### ### ### ###################################################################### par(mfrow=c(2,2), mai = c(0.2, 0.2, 0.2, 0.2)) #mai reduces space between plots par(mfrow=c(2,4), mai = c(0.2, 0.2)) #mai reduces space between plots par(mfrow=c(1,4), mai = c(0.1, 0.1, 0.0, 0.1)) #mai reduces space between plotspar(mfrow=c(1,4), mai = c(0.2, 0.2, 0.2, 0.2)) #mai reduces space between plots# Random x <- nlm_random(100,100) # Planar pl <- nlm_planargradient(100, 100) # Tesselation mt <- nlm_mosaictess(50,50, germs = 200) # Midpoint displacement m <- nlm_mpd(100, 100,roughness = 0.5) #prøv forskjellige verdier #Legend plot(x, box = F, axes = F, legend = F, main="a)") plot(pl, box = F, axes = F, legend = F, main="b)") plot(mt, box = F, axes = F, legend = F, main="c)") plot(m, box = F, axes = F, legend = F, main="d)") plot(x , legend.only = T, horizontal = T, add = T, smallplot = c(0.25, 0.75, 0.08, 0.1)) plot(x) # nlm _ mosaictess # Simulates a patchy mosaic neutral landscape model based on # the tessellation of a random point process. The algorithm #randomly places points (parameter germs) in the landscape, # which are used as the centroid points for a voronoi tessellation. A higher number of points therefore leads to a more # fragmented landscape # nlm _ mosaic gibbs # Simulates a patchy mosaic neutral landscape model based on # the tessellation of an inhibition point process. This inhibition # point process starts with a given number of points and uses a # minimisation approach to fit a point pattern with a given # interaction parameter (0 - hardcore process; 1 - Poisson # process) and interaction radius (distance of points/germs being # apart) ###################################################################### ### ### ### Neutral landscape models with "NLMR" ### ### ### ###################################################################### ### Basic landscape generation ### The basic syntax used to produce a NLM landscape is: ### nlm_modeltype(ncol, nrow, resolution, ...) # The random map is the simplest neutral landscape model: x <- nlm_random(50,50) plot(x) plot (x, col = rev(brewer.pal(9, "RdYlBu"))) # nlm_curds - Simulates a randomly curdled # or wheyed neutral landscape model #hierarchical curdling ch <- nlm_curds(curds = c(0.5, 0.3, 0.6), recursion_steps = c(32, 6, 2)) plot(ch) #wheyed hierarchical curdling cw <- nlm_curds(curds = c(0.5, 0.3, 0.6), recursion_steps = c(32, 6, 2), wheyes = c(0.1, 0.05, 0.2)) plot(cw) #nlm _ distance gradient #Simulates a distance gradient neutral landscape model. The #gradient is always measured from a rectangle that one has to #specify in the function (parameter origin d <- nlm_distancegradient(50,50, origin = c(10, 10, 10, 10)) plot(d) #nlm _ edge gradient #Simulates a linear gradient orientated neutral model. The #gradient has a specified or random direction that has a central #peak, which runs perpendicular to the gradient direction e <- nlm_edgegradient(100, 100) plot(e) # nlm _ fbm - Fractional Brownian motion # Simulates neutral landscapes using fractional Brownian motion #(fBm). fBm is an extension of Brownian motion in which the #amount of spatial autocorrelation between steps is controlled #by the Hurst coefficient H fbm <- nlm_fbm(100, 200, fract_dim = 1.2) plot(fbm) # nlm _ gaussian field #spatially correlated random fields (Gaussian random fields) #Simulates a spatially correlated random fields (Gaussian random # fields) model, where one can control the distance and # magnitude of spatial autocorrelation gf <- nlm_gaussianfield(50,50, autocorr_range = 60, mag_var = 8,nug = 5) plot(gf) # nlm _ mosaic field # mosaic random field neutral landscape model #Simulates a mosaic random field neutral landscape model. #The algorithm imitates fault lines by repeatedly bisecting #the landscape and lowering the values of cells in one half #and increasing the values in the other half. If one sets #the parameter infinit to TRUE, the algorithm approaches #a fractal pattern mf <- nlm_mosaicfield(50,50, n = NA, infinit = TRUE, collect = FALSE) plot(mf) mf2 <- nlm_mosaicfield(50,50, n = 10, mosaic_mean = 0.5, mosaic_sd = 0.5, collect = FALSE, infinit = FALSE, rescale = TRUE) plot(mf2) # nlm _ neigh # neighbourhood clustering # Simulates a neutral landscape model with land cover classes and # clustering based on neighbourhood characteristics. The cluster # are based on the surrounding cells. If there is a neighbouring # cell of the current value/type, the target cell will more likely # turned into a cell of that type/valuen <- nlm_neigh(100, 100, p_neigh = 0.7, p_empty = 0.1,categories = 5, neighbourhood = 4) n <- nlm_neigh(50,50, p_neigh = 0.7, p_empty = 0.1, categories = 5, neighbourhood = 4) plot(n) hist(n) # nlm _ percolation # Simulates a binary neutral landscape model based on percolation theory. # The probability for a cell to be assigned habitat is drawn from # a uniform distribution pc <- nlm_percolation(ncol = 100, nrow = 100, prob = 0.5) plot(pc) hist(pc) #planar gradient # Simulates a planar gradient neutral landscape model. The # gradient is sloping in a specified or (by default) random # direction between 0 and 360 degree pl <- nlm_planargradient(100, 100) plot(pl) # nlm _ mosaictess # Simulates a patchy mosaic neutral landscape model based on # the tessellation of a random point process. The algorithm #randomly places points (parameter germs) in the landscape, # which are used as the centroid points for a voronoi tessellation. A higher number of points therefore leads to a more # fragmented landscape mt <- nlm_mosaictess(50,50, germs = 200) plot(mt) mt2 <- nlm_mosaictess(50,50, germs = 400) plot(mt2) # nlm _ mosaic gibbs # Simulates a patchy mosaic neutral landscape model based on # the tessellation of an inhibition point process. This inhibition # point process starts with a given number of points and uses a # minimisation approach to fit a point pattern with a given # interaction parameter (0 - hardcore process; 1 - Poisson # process) and interaction radius (distance of points/germs being # apart) mg <- nlm_mosaicgibbs(100, 100, germs = 20, R = 0.02, patch_classes = 12) plot(mg) # nlm_random # Simulates a spatially random neutral landscape model. The #values are drawn a uniform distribution r <- nlm_random(50,50) plot(r) hist(r) # nlm _ random cluster # random cluster nearest-neighbour # Simulates a random cluster nearest-neighbour neutral # landscape. The parameter ai controls for the number and # abundance of land cover classes and p controls for proportion # of elements randomly selected to form clusters rc <- nlm_randomcluster(50,50, p = 0.4, ai = c(0.25, 0.25, 0.5)) plot(rc) rc2 <- nlm_randomcluster(50,50, p = 0.1, ai = c(0.25, 0.25, 0.5)) plot(rc2) rc3 <- nlm_randomcluster(50,50, p = 0.4, ai = c(0.1, 0.25, 0.65)) plot(rc3) #midpoint displacement # Simulates a midpoint displacement neutral landscape model # where the parameter roughness controls the level of # spatial autocorrelation m <- nlm_mpd(50,50,roughness = 0.5) #prøv forskjellige verdier plot(m) hist(m) plot(m, col = rev (heat.colors(90))) # nlm _randomrectangularcluster # Simulates a random rectangular cluster neutral landscape # model. The algorithm randomly distributes overlapping # rectangles until the landscape is filled rrc <- nlm_randomrectangularcluster(50,50, minl = 5, maxl = 10) plot(rrc) ###################################################################### ### ### ### Landscape with "Landscapetools" ### ### ### ###################################################################### #Create primary landscape raster pL <- nlm_edgegradient(50,50) plot(pL) #Create secondary landscape rasters sL1 <- nlm_distancegradient(100, 100, origin = c(10, 10, 10, 10)) plot(sL1) sL2 <- NLMR::nlm_random(50,50) plot(sL2) mL1 <- pL + (sL1 + sL2) plot(mL1) ### Classifying categories par(mfrow = c(1,4)) nr <- nlm_fbm(50, 100, fract_dim = 1.2) plot (nr) nr_classified <- util_classify(nr, weighting = c(0.3, 0.3, 0.3)) plot(nr_classified) # Classify the landscape into land uses #classified_landscape <- util_classify(nr, weighting = c(0.3, 0.3, 0.3), level_names = c("Land Use 1", "Land Use 2", "Land Use 3")) # Binarize the landscape into habitat and matrix binarized_raster1 <- util_binarize(nr, breaks = 0.3) plot(binarized_raster1) binarized_raster2 <- util_binarize(nr, breaks = 0.6) plot(binarized_raster2) binarized_raster3 <- util_binarize(nr, breaks = 0.9) plot(binarized_raster3) ###################################################################### ### ### ### Neutral landscape maps of Norway as null models for prediction ### ### ### ###################################################################### memory.size() memory.limit(size=357500) memory.limit(size=56000000) memory.limit() workdir <- "C:/Users/trosim/Documents/PhD/Subproject_3/R_Subpro_3/R_WD_Subpro_3" setwd(workdir) ### Example with a random map LA_random <- nlm_random (14917, 11924, resolution = 100) LAgibbs <- nlm_mosaicgibbs (14917, 11924, germs = 40000, R = 10, patch_classes = 284) LAgibbs plot(LAgibbs) LAgibbs<-as.factor(LAgibbs) hist(LAgibbs) # Reading in raster stack with predictors and tables with recorded value for model target load ("predictors.rda") pred <- predictors$bioclim1 pred LA <- LA_random bb <- extent(-77253.96, 1115146, 6448314, 7940014) LA <- setExtent(LA, bb, keepres = FALSE) projection(LA) <- "+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0" plot(LA) LAresamp <- resample(LA, pred, method='ngb') LAresamp plot(LAresamp) setwd("~/PhD/Subproject_3/R_Subpro_3/Random_maps") writeRaster(LA, "LARANDOM", format = "GTiff", overwrite = TRUE, progress = "text", prj = TRUE) ### Edgegradient LAedge <- nlm_edgegradient (14917, 11924, resolution = 100, rescale = TRUE) LAedge plot(LAedge) hist(LAedge) LA <- LAedge bb <- extent(-77253.96, 1115146, 6448314, 7940014) LA <- setExtent(LA, bb, keepres = FALSE) projection(LA) <- "+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0" plot(LA) setwd("~/PhD/Subproject_3/R_Subpro_3/Random_maps") writeRaster(LA, "LAedge", format = "GTiff", overwrite = TRUE, progress = "text", prj = TRUE) ### planargradient d <- nlm_distancegradient(14917, 11924, origin = c(10, 10, 10, 10)) plot(d) pl <- nlm_planargradient(100, 100) plot(pl) #midpoint displacement # Simulates a midpoint displacement neutral landscape model # where the parameter roughness controls the level of # spatial autocorrelation m <- nlm_mpd(14917, 11924,roughness = 0.5) #prøv forskjellige verdier plot(m) hist(m) plot(m, col = rev (heat.colors(90))) LA <- m bb <- extent(-77253.96, 1115146, 6448314, 7940014) LA <- setExtent(LA, bb, keepres = FALSE) projection(LA) <- "+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0" plot(LA) setwd("~/PhD/Subproject_3/R_Subpro_3/Random_maps") writeRaster(LA, "LAmidpoint", format = "GTiff", overwrite = TRUE, progress = "text", prj = TRUE) # nlm _ random cluster # random cluster nearest-neighbour # Simulates a random cluster nearest-neighbour neutral # landscape. The parameter ai controls for the number and # abundance of land cover classes and p controls for proportion # of elements randomly selected to form clusters rc <- nlm_randomcluster(149, 119, p = 0.4, ai = c(0.10, 0,10, 0.5, 0.25, 0.5)) rc <- nlm_randomcluster(14917, 11924, p = 0.4, ai = c(0.25, 0.25, 0.5)) plot(rc) LA <- rc bb <- extent(-77253.96, 1115146, 6448314, 7940014) LA <- setExtent(LA, bb, keepres = FALSE) projection(LA) <- "+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0" plot(LA) setwd("~/PhD/Subproject_3/R_Subpro_3/Random_maps") writeRaster(LA, "LAcluster", format = "GTiff", overwrite = TRUE, progress = "text", prj = TRUE) ### Example with tesselation LA_random <- nlm_random (14917, 11924, resolution = 100) tess <- nlm_mosaictess(14917,11924, resolution = 100, germs = 40000) tess <- nlm_mosaictess(11924, 14917, germs = 40000) plot(tess) tess # Reading in raster stack load ("predictors.rda") pred <- predictors$bioclim1 pred tess LA <- tess bb <- extent(-77253.96, 1115146, 6448314, 7940014) LA <- setExtent(LA, bb, keepres = TRUE) LA plot(LA) projection(LA) <- "+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0" plot(LA) LAresamp <- resample(LA, pred, method='ngb') LAresamp plot(LAresamp) setwd("~/PhD/Subproject_3/R_Subpro_3/Random_maps") writeRaster(LAresamp, "LArandom", format = "GTiff", overwrite = TRUE, progress = "text", prj = TRUE) plot(LA_random) LA_random LA <- LA_random bb <- extent(-77253.96, 1115146, 6448314, 7940014) LA <- setExtent(LA, bb, keepres = TRUE) projection(LA) <- "+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0" setwd("~/PhD/Subproject_3/R_Subpro_3/Random_maps") writeRaster(LA_random, "LA_random", format = "GTiff", overwrite = TRUE, progress = "text", prj = TRUE) writeRaster(LA_random, "LA_random2", format = "ascii", overwrite = TRUE, progress = "text", prj = TRUE) writeRaster(r, filename="temp2_new.asc", format="ascii",overwrite=TRUE) write.asc(LA,'t.raster.asc') crs(LA) pred <- predictors$bioclim1 pred LA LA <- nlm_mosaictess(ncol = 14917, nrow = 11924, resolution = 100, germs = 200, rescale = TRUE) plot(LA) LA ?nlm_mosaictess plot(LA) LA1 <- LA plot(LA1) bb <- extent(-77253.96, 1115146, 6448314, 7940014) LA2 <- setExtent(LA, bb, keepres = TRUE) plot(LA2) pr1 <- projectRaster(r, crs=newproj) setExtent() library(raster) library(rgdal) myRaster1 <- raster(nrow=11924, ncol=14917) plot(myRaster1) myRaster1[]<- 1:ncell(myRaster1) ?1:ncell ?resample myRaster2=raster(nrow=16, ncol=16) myRaster1.resampled <- resample(myRaster1, myRaster2, method='bilinear') plot(myRaster1.resampled, main="Raster with 32 pixels") ?resample plot(LA) LA bb plot(bb) nlm_mosaictess(ncol = ncol(bb), nrow = nrow(bb), resolution = 100, germs = 200, rescale = TRUE) la3 <- (ncol = bb, bb, resolution = 100, germs = 200, rescale = FALSE) library(SDMTools) ?write.asc ?write.asc ?writeRaster predictors$bioclim1 LA class(LA) <- "spatial" plot(LA) plot(predictors$bioclim1) writeRaster(predictors$bioclim1, "BIOCLIM_TEST", format = "GTiff", overwrite = TRUE, progress = "text", prj = TRUE) predictors predictors$bioclim1 LA GDALinfo(predictors$bioclim1) LA ?spTransform ?projectRaster newproj <- "+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"