Skip to content

Commit

Permalink
version 1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
dariomasante authored and cran-robot committed Nov 1, 2024
1 parent 86978ca commit aecf52e
Show file tree
Hide file tree
Showing 37 changed files with 550 additions and 371 deletions.
29 changes: 15 additions & 14 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
Package: landscapeR
Type: Package
Title: Categorical Landscape Simulation Facility
Version: 1.2
Date: 2017-07-05
Authors@R: c(person(given = "Dario", family = "Masante", role = c("aut", "cre"), email = "[email protected]"))
Version: 1.3
Date: 2024-10-31
Authors@R: c(person(given = "Dario", family = "Masante", role = c("aut", "cre"), email = "[email protected]"), person(given="Lora", family="Murphy", role="ctb", email="[email protected]"))
Maintainer: Dario Masante <[email protected]>
Copyright: Natural Environment Research Council (NERC) and Centre for
Ecology and Hydrology (CEH)
URL: https://github.com/dariomasante/landscapeR
BugReports: https://github.com/dariomasante/landscapeR/issues
Imports: raster, Rcpp
Suggests: knitr, rmarkdown
Description: Simulates categorical maps on actual geographical realms, starting from either empty landscapes or landscapes provided by the user (e.g. land use maps). Allows to tweak or create landscapes while retaining a high degree of control on its features, without the hassle of specifying each location attribute. In this it differs from other tools which generate null or neutral landscapes in a theoretical space. The basic algorithm currently implemented uses a simple agent style/cellular automata growth model, with no rules (apart from areas of exclusion) and von Neumann neighbourhood (four cells, aka Rook case). Outputs are raster dataset exportable to any common GIS format.
License: GPL (>= 3)
LazyLoad: yes
LazyData: TRUE
NeedsCompilation: yes
RoxygenNote: 6.0.1
License: GPL (>= 2)
Imports: terra, Rcpp (>= 1.0.3)
Suggests: markdown, knitr
VignetteBuilder: knitr
LinkingTo: Rcpp
Packaged: 2017-07-05 12:46:53 UTC; masanda
Author: Dario Masante [aut, cre]
Maintainer: Dario Masante <[email protected]>
RoxygenNote: 7.3.2
Encoding: UTF-8
NeedsCompilation: yes
Packaged: 2024-11-01 20:18:02 UTC; masante
Author: Dario Masante [aut, cre],
Lora Murphy [ctb]
Repository: CRAN
Date/Publication: 2017-07-05 14:54:40 UTC
Date/Publication: 2024-11-01 21:10:02 UTC
58 changes: 35 additions & 23 deletions MD5
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
635cbadccce3f453f67e0a3700dd9fb2 *DESCRIPTION
ec18e2ccb5dd767ede9dd4667618c269 *DESCRIPTION
8cb35e234ffdef13f971f80f618992d6 *NAMESPACE
c6d36ae0e1a07ab41f1e636a73666f21 *NEWS.md
66325721520b6166fc43b30120d92efe *R/RcppExports.R
e5bc5591c800db13c86bfc9d3ce35a61 *R/expandClass.R
cf831291457e08215afcca621c9464dd *R/landscapeR-package.R
bdf9b4d20560afbd60d521a1fbf2e732 *R/makeClass.R
1a345dd95f03a3f5a1d521de23f4fc92 *R/makeLine.R
5551b59d16704db38c520670ace84a86 *R/makePatch.R
f88406fe7e71b669fa84ad396486a799 *R/rmSingle.R
332b9e5c43055838eef2af68eb49f3bf *R/zzz.R
892f937b1044f7090ff6980db2c7a0af *build/vignette.rds
4057d20c5d9ee89b2d4ea419eaba577e *NEWS.md
a4ab04b34a6d2113834a96da10f06bb1 *R/RcppExports.R
168ca0ccf582456039467371f1e83157 *R/expandClass.R
b9d53aa85ad2c547d7c52f4a9f34b059 *R/landscapeR-package.R
af244cdb4ac0364338618cf8c136f9e5 *R/makeClass.R
6ffbe05f5adca845f4a31d47e5ae01ab *R/makeLine.R
2e938d300cc5794a3da6c914c703dd41 *R/makePatch.R
7ba55e788d85d98272daaa8293a6d9b9 *R/rmSingle.R
87f902189223e5c7d422ad9c0939bfb1 *R/zzz.R
d8f455396216d93c4566d2e7d27c79c0 *build/vignette.rds
62583f9b09873cae8b1462ab7a194b29 *inst/CITATION
6fbe40d2a4c543685ea49b7d68d82b2c *inst/COPYRIGHTS
7b735af5155c2c74c559cde104f62bac *inst/doc/landscapeR.R
96aa2511ef0b7ba5105ce2f91c068ecf *inst/doc/landscapeR.Rmd
4030c8bb7fe2d0cd92e6c34da759fca8 *inst/doc/landscapeR.html
898c0bf34293df5b324bb37c4a6bd7d0 *man/expandClass.Rd
2d65e8077c0e3ec6352d517541dc35c0 *man/landscapeR-package.Rd
1dd5e286a3562d5dec8ebb27b0d090c9 *man/makeClass.Rd
cc68acb717a59cb4692a2354f757df59 *man/makeLine.Rd
ad9f5a48e5e8bf83366a82e68ea2997b *man/makePatch.Rd
32fc6e2675d2583c2551aa0f11fa07db *man/rmSingle.Rd
a947e62ede9bc4c0b93d4cc252d078fe *src/RcppExports.cpp
252f0946024f3bbf733966c6d6491bc3 *inst/doc/landscapeR.R
c83a1bf9c22389834a2386e938d7ca95 *inst/doc/landscapeR.Rmd
d87cbd9624b2bb5e3b896320761a9e41 *inst/doc/landscapeR.html
cf0295d2d592fbc5ed8a1af87bd955ea *man/expandClass.Rd
d6702d535b78103d3d2fc871b5f141c5 *man/landscapeR-package.Rd
82cf92f26f6a6c7647c1fab4cf6e7e69 *man/makeClass.Rd
4f25d11d502ba8675fabf33a4130ea40 *man/makeLine.Rd
92c7f17b4a82f8599b8d284c470c5ee0 *man/makePatch.Rd
b517983d400497707a97cf95e66a5105 *man/rmSingle.Rd
e54b43411f0dfd289205a84ef2f54ad3 *src/RcppExports.cpp
a75ee32e13bfc69ed50076851d2c5806 *src/hidden.cpp
0492eaed7b43f1b319e7d4ade16a607f *src/init.c
96aa2511ef0b7ba5105ce2f91c068ecf *vignettes/landscapeR.Rmd
c83a1bf9c22389834a2386e938d7ca95 *vignettes/landscapeR.Rmd
313c6f0599ccc474c1b06e1276c59212 *vignettes/unnamed-chunk-10-1.png
0a5c0fbb64c8ea2043ddf75aa03520ec *vignettes/unnamed-chunk-11-1.png
19c27aee4ba9df1340a5b8d513336db2 *vignettes/unnamed-chunk-12-1.png
85d3ac8ed33aeec12c518b2352e6ad8b *vignettes/unnamed-chunk-13-1.png
606dd60c9f9f40f704505284fdf53cad *vignettes/unnamed-chunk-14-1.png
62406792a0fa29e5f8c82c2e55bffab9 *vignettes/unnamed-chunk-15-1.png
22830f630e0d80715395620aa8460fed *vignettes/unnamed-chunk-3-1.png
af089806b6d9cc49d826296ffd1b4d61 *vignettes/unnamed-chunk-4-1.png
b52966a06f38e4b42aa758f8f7b872e4 *vignettes/unnamed-chunk-5-1.png
3f61be83400ce8b45e74c6b426d95da9 *vignettes/unnamed-chunk-6-1.png
7d7be70420416c176496a5d3ecd22f6a *vignettes/unnamed-chunk-8-1.png
e5613818fa82a525d27fcbc7a73acc2b *vignettes/unnamed-chunk-9-1.png
14 changes: 14 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
landscapeR v1.3 (Release date: 2024-10-31)
-----------------------------------------------
CHANGES:
* use package terra instead of raster
* Fixing data types for Rcpp
* Added options for vector value to makeClass

landscapeR v1.2.1 (Release date: 2020-02-05)
-----------------------------------------------
CHANGES:

* Added Ecological Applications paper citation


landscapeR v1.2 (Release date: 2017-07-05)
-----------------------------------------------
CHANGES:
Expand Down
6 changes: 3 additions & 3 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

.contigCells <- function(pt, bgr, mtx) {
.Call("landscapeR_contigCells_cpp", PACKAGE = "landscapeR", pt, bgr, mtx)
.Call("_landscapeR_contigCells_cpp", PACKAGE = "landscapeR", pt, bgr, mtx)
}

.assignValues <- function(val, ad, mtx) {
.Call("landscapeR_assignValues_cpp", PACKAGE = "landscapeR", val, ad, mtx)
.Call("_landscapeR_assignValues_cpp", PACKAGE = "landscapeR", val, ad, mtx)
}

.indexTranspose <- function(id, dim1, dim2) {
.Call("landscapeR_indexTranspose_cpp", PACKAGE = "landscapeR", id, dim1, dim2)
.Call("_landscapeR_indexTranspose_cpp", PACKAGE = "landscapeR", id, dim1, dim2)
}

22 changes: 14 additions & 8 deletions R/expandClass.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
#' @param class The raster value of class (or patch) to expand.
#' @param size integer. Size of expansion, as number of raster cells.
#' @param bgr integer. The background available where expansion is allowed (i.e. shrinking classes).
#" @return A RasterLayer object. If \code{rast=FALSE} returns a list of vectors, each containing the \code{context} cells assigned to each patch.
#" @return A SpatRaster object. If \code{rast=FALSE} returns a list of vectors, each containing the \code{context} cells assigned to each patch.
#' @examples
#" library(raster)
#" library(terra)
#'
#' m = matrix(0, 33, 33)
#' r = raster(m, xmn=0, xmx=10, ymn=0, ymx=10)
#' r = rast(m)
#' ext(r) = c(0, 10, 0, 10)
#' r = makeClass(r, 5, 10)
#' plot(r)
#'
Expand All @@ -19,7 +20,8 @@
#'
#' ## This function can be used to mimic shapes, by providing a skeleton:
#' m[,17] = 1
#' r = raster(m, xmn=0, xmx=10, ymn=0, ymx=10)
#' r = rast(m)
#' ext(r) = c(0, 10, 0, 10)
#' plot(r)
#'
#' rr = expandClass(r, 1, 100)
Expand All @@ -29,10 +31,14 @@ expandClass <- function(context, class, size, bgr=0, pts = NULL) {
if(length(class) > 1){ stop('A single value only is admitted for "class" argument.') }
if(class %in% bgr){ warning('Value to attribute to patches same as background cells value (arg. "class" equals "bgr").') }
if(any(is.na(size) | size <=0)){ stop('Invalid "size" argument provided.') }
bd <- raster::boundaries(context, type='outer', classes=TRUE, directions=8)
bd <- t(raster::as.matrix(bd))
bd <- terra::boundaries(context, inner=FALSE, classes=TRUE, directions=8)
#----- LEM: the as.numeric is required for type matching in Rcpp -----------#
bd <- terra::as.matrix(bd, wide=T)
bd <- t(matrix(as.numeric(bd), ncol=ncol(bd), nrow=nrow(bd)))
if(!is.matrix(context)) {
mtx <- t(raster::as.matrix(context))
#----- LEM: the as.numeric is required for type matching in Rcpp ---------#
mtx <- terra::as.matrix(context, wide=T)
mtx <- t(matrix(as.numeric(mtx), ncol=ncol(mtx), nrow=nrow(mtx)))
} else {
mtx <- context
}
Expand Down Expand Up @@ -90,6 +96,6 @@ expandClass <- function(context, class, size, bgr=0, pts = NULL) {
# mtx[p_bgr[id]] <- vals[id]
mtx[.subset(p_bgr, id)] <- .subset(vals, id)
}
context[] <- t(mtx)
terra::values(context) <- t(mtx)
return(context)
}
3 changes: 1 addition & 2 deletions R/landscapeR-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
#' }
#' @details Check out the vignette illustrating the use of landscapeR.\cr
#' Also: \url{https://github.com/dariomasante/landscapeR}
#' @docType package
#' @name landscapeR-package
#' @aliases landscapeR
#" @author Dario Masante
#" @author Dario Masante [aut, cre], Lora Murphy [ctb]
NULL
36 changes: 23 additions & 13 deletions R/makeClass.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#' Create a class of patches.
#'
#' @inheritParams makePatch
#" @param context Raster object, a raster of an empty landscape or a mask, indicating where the patch cannot be generated (see \code{bgr} argument).
#" @param context SpatRaster object, a raster of an empty landscape or a mask, indicating where the patch cannot be generated (see \code{bgr} argument).
#' @param npatch number of patches per class
#' @param size integer. The size of patches, as number of raster cells. A single integer can be provided, in which case all patches will have that size.
#' @param pts integer or matrix. The seed point location around which the patches are built (random points are given by default). It can be an integer, as indexes of the cells in the raster, or a two columns matrix indicating x and y coordinates.
#" @return A RasterLayer object, or a vector of cell numbers if \code{rast=FALSE}.
#" @return A SpatRaster object, or a vector of cell numbers if \code{rast=FALSE}.
#' @details The patches created can be contiguous, therefore resembling a single patch with size
#' equal to the sum of contiguous cells. The patches are created starting from the seed points (if provided) and iteratively sampling randomly neighbouring cells at the edge of the patch, according to von Neumann neighbourhood (four cells, aka Rook case).
#' There is a tolerance of +/- 3 cells from the patch size declared in \code{size} argument.
#' @examples
#" library(raster)
#"
#" m = matrix(0, 33, 33)
#" r = raster(m, xmn=0, xmx=10, ymn=0, ymx=10)
#" library(terra)
#" mtx = matrix(0, 33, 33)
#" r = rast(mtx)
#" ext(r) = c(0, 10, 0, 10)
#' num = 5
#' size = 15
#' rr = makeClass(r, num, size)
Expand All @@ -27,19 +27,27 @@
#' @export
makeClass <- function(context, npatch, size, pts = NULL, bgr=0, edge=FALSE, rast=TRUE, val=1){
if(length(npatch) != 1){ stop('A single integer value must be provided to argument "npatch"') }
if(length(val) > 1){ stop("A single value only is admitted for "val" argument.") }
if(val %in% bgr){ warning("Value to attribute to patches same as background cells value (arg. "val" equals "bgr").") }
if(length(val) > 1 && length(val) != npatch){ stop("The "val" argument must either be of length 1 or npatch.") }
if(any(val %in% bgr)){ warning("Value to attribute to patches same as background cells value (arg. "val" equals "bgr").") }
if(length(size) != npatch & length(size) > 1){ stop('Number of patches not matching the length of size vector') }
if(any(is.na(size) | size <=0)){ stop('Invalid "size" argument provided.') }
if(npatch <= 0 | npatch > length(context) | is.na(npatch)) { stop("Invalid number of patches required (e.g. more than available landscape cells). Check argument "npatch".") }
if(npatch <= 0 | npatch > terra::ncell(context) | is.na(npatch)) { stop("Invalid number of patches required (e.g. more than available landscape cells). Check argument "npatch".") }
if(rast==TRUE & edge==TRUE){
edge=FALSE
warning('Edge output reset to FALSE. edge=TRUE only when raster output is not required (i.e. rast=FALSE)')
}
mtx <- t(raster::as.matrix(context))
#----- LEM: the as.numeric is required for type matching in Rcpp -----------#
mtx <- terra::as.matrix(context, wide=T)
mtx <- t(matrix(as.numeric(mtx), ncol=ncol(mtx), nrow=nrow(mtx)))
if(length(size)==1){
size <- rep(size, npatch)
}

#----- LEM: If val is single, expand to npatch -----------------------------#
if (length(val) == 1) {
val = rep(val, npatch)
}

bgrCells <- which(is.element(mtx, bgr))
if(length(bgrCells) == 0){
stop('No background cells available with value ', bgr, '. Try checking argument "bgr".')
Expand Down Expand Up @@ -68,18 +76,20 @@ makeClass <- function(context, npatch, size, pts = NULL, bgr=0, edge=FALSE, rast
}
lst <- list()
for(np in 1:npatch){
l <- makePatch(context=mtx, spt=pts[np], size=size[np], bgr=bgr, edge=edge, val=val, rast = FALSE)
l <- makePatch(context=mtx, spt=pts[np], size=size[np], bgr=bgr, edge=edge, val=val[np], rast = FALSE)
if(edge==TRUE){
eg <- l[[2]]
l <- l[[1]]
lst[[np]] <- list(l, eg)
} else {
lst[[np]] <- l
}
.assignValues(val, l, mtx) #mtx[l] <- val
.assignValues(val[np], l, mtx) #mtx[l] <- val
}
if(rast == TRUE) {
context[unlist(lst)] <- val
for(np in 1:npatch){
context[unlist(lst[[np]])] <- val[np]
}
return(context)
} else {
return(lst)
Expand Down
19 changes: 12 additions & 7 deletions R/makeLine.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
#' @param direction numeric. The direction towards which the patch will point and grow, expressed in degrees between 0 and 360.
#' @param convol numeric. Level of convolution to be assigned to the patch (default is \code{convol=0.5}). A value
#' between 0 and 1, where 0 is no convolution at all (basically a straight line) and 1 is maximum convolution (i.e. tends to form a circular patch).
#" @return A vector of raster cell numbers, or a RasterLayer object if \code{rast=TRUE}. If \code{edge=TRUE} a
#" @return A vector of raster cell numbers, or a SpatRaster object if \code{rast=TRUE}. If \code{edge=TRUE} a
#' list of two vectors is returned: one for the inner raster cells and the second for cells at the edge of the patch.
#' @details For any values of \code{convol} > 0.8, no big differences are observed noted. Also direction is progressively lost
#' as convolution increases.
#' @examples
#" library(raster)
#" library(terra)
#' r <- matrix(0,33,33)
#' r <- raster(r, xmn=0, xmx=10, ymn=0, ymx=10)
#' r <- rast(r)
#' ext(r) = c(0, 10, 0, 10)
#' plot(makeLine(r, size=50, spt = 545, direction=45, convol=0.05, rast=TRUE))
#'
#' @export
Expand All @@ -28,7 +29,9 @@ makeLine <- function(context, size, direction=NULL, convol=0.5, spt=NULL, bgr=0,
}
spt <- .toCellIndex(context, spt)
}
mtx <- t(raster::as.matrix(context))
#----- LEM: the as.numeric is required for type matching in Rcpp ---------#
mtx <- terra::as.matrix(context, wide=T)
mtx <- t(matrix(as.numeric(mtx), ncol=ncol(mtx), nrow=nrow(mtx)))
} else {
mtx <- context
}
Expand Down Expand Up @@ -79,7 +82,7 @@ makeLine <- function(context, size, direction=NULL, convol=0.5, spt=NULL, bgr=0,
}
}
if(rast == TRUE) {
context[] <- t(mtx)
terra::values(context) <- t(mtx)
return(context)
} else if (edge == TRUE) {
edgVal <- ifelse(val+1 == bgr, val+2, val+1)
Expand All @@ -101,7 +104,9 @@ makeLine <- function(context, size, direction=NULL, convol=0.5, spt=NULL, bgr=0,
}
spt <- .toCellIndex(context, spt)
}
mtx <- t(raster::as.matrix(context))
#----- LEM: the as.numeric is required for type matching in Rcpp ---------#
mtx <- terra::as.matrix(context, wide=T)
mtx <- t(matrix(as.numeric(mtx), ncol=ncol(mtx), nrow=nrow(mtx)))
} else {
mtx <- context
}
Expand Down Expand Up @@ -149,7 +154,7 @@ makeLine <- function(context, size, direction=NULL, convol=0.5, spt=NULL, bgr=0,
}
}
if(rast == TRUE) {
context[] <- t(mtx)
terra::values(context) <- t(mtx)
return(context)
} else if (edge == TRUE) {
if(val+1 == bgr){
Expand Down
Loading

0 comments on commit aecf52e

Please sign in to comment.