-
Notifications
You must be signed in to change notification settings - Fork 17
/
nlm_mosaictess.R
91 lines (80 loc) · 3.07 KB
/
nlm_mosaictess.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#' nlm_mosaictess
#'
#' @description Simulate the NLM introduced in Gaucherel (2008).
#'
#' @details
#' \code{nlm_mosaictess} offers the first option of simulating a neutral landscape model
#' described in Gaucherel (2008). It generates a random point pattern (the germs)
#' with an independent distribution and uses the voronoi tessellation to simulate
#' the mosaic landscapes.
#'
#' @param ncol [\code{numerical(1)}]\cr
#' Number of columns for the raster.
#' @param nrow [\code{numerical(1)}]\cr
#' Number of rows for the raster.
#' @param resolution [\code{numerical(1)}]\cr
#' Resolution of the raster.
#' @param germs [\code{numerical(1)}]\cr
#' Intensity parameter (non-negative integer).
#' @param rescale [\code{logical(1)}]\cr
#' If \code{TRUE} (default), the values are rescaled between 0-1.
#'
#' @return RasterLayer
#'
#' @examples
#' # simulate polygonal landscapes
#' (mosaictess <- nlm_mosaictess(ncol = 30, nrow = 60, germs = 200))
#'
#' \dontrun{
#' # visualize the NLM
#' rasterVis::levelplot(mosaictess, margin = FALSE, par.settings = rasterVis::viridisTheme())
#' }
#'
#' @references
#' Gaucherel, C. (2008) Neutral models for polygonal landscapes with linear
#' networks. \emph{Ecological Modelling}, 219, 39 - 48.
#'
#' @aliases nlm_polylands
#' @rdname nlm_polylands
#'
#' @export
#'
nlm_mosaictess <- function(ncol,
nrow,
resolution = 1,
germs,
rescale = TRUE) {
# Check function arguments ----
checkmate::assert_count(ncol, positive = TRUE)
checkmate::assert_count(nrow, positive = TRUE)
checkmate::assert_numeric(resolution)
checkmate::assert_numeric(germs)
# bounding box placing germs and clipping ----
bounding_box <- sf::st_sfc(sf::st_polygon(list(rbind(c(0, 0),
c(ncol, 0),
c(ncol, nrow),
c(0, nrow),
c(0, 0)))))
# distribute the germs from which the polygons are build ----
rnd_points <- sf::st_union(sf::st_sample(bounding_box, germs))
# compute the voronoi tessellation ----
voronoi_tess <- sf::st_voronoi(rnd_points)
# clip and give random values ----
voronoi_tess <- sf::st_intersection(sf::st_cast(voronoi_tess), bounding_box)
voronoi_tess <- sf::st_sf(value = stats::runif(germs),
geometry = sf::st_sfc(voronoi_tess))
# (f)rasterize with lightning speed ----
vx <- velox::velox(matrix(0, nrow = nrow, ncol = ncol),
extent = raster::extent(voronoi_tess),
res = c(resolution, resolution))
vx$rasterize(voronoi_tess, field = "value")
r <- vx$as.RasterLayer()
# return to fasterize when it is on CRAN
# r <- raster::raster(raster::extent(voronoi_tess), res = resolution)
# r <- fasterize::fasterize(voronoi_tess, r, field = "value", fun = "sum")
# Rescale values to 0-1 ----
if (rescale == TRUE) {
r <- util_rescale(r)
}
return(r)
}