1
|
|
#' @include RcppExports.R raptr-internal.R
|
2
|
|
NULL
|
3
|
|
|
4
|
|
#' Calculate boundary data for planning units
|
5
|
|
#'
|
6
|
|
#' This function calculates boundary length data for
|
7
|
|
#' [PBSmapping::PolySet()], [sp::SpatialPolygons()], and
|
8
|
|
#' [sp::SpatialPolygonsDataFrame()] objects. Be aware that this
|
9
|
|
#' function is designed with performance in mind, and as a consequence, if this
|
10
|
|
#' function is used improperly then it may crash R. Furthermore, multipart
|
11
|
|
#' polygons with touching edges will likely result in inaccuracies.
|
12
|
|
#' If argument set to [sp::SpatialPolygons()] or
|
13
|
|
#' [sp::SpatialPolygonsDataFrame()], this will be converted to
|
14
|
|
#' PolySet before processing.
|
15
|
|
#'
|
16
|
|
#' @param x [PBSmapping::PolySet()],
|
17
|
|
#' [sp::SpatialPolygons()] or
|
18
|
|
#' [sp::SpatialPolygonsDataFrame()] object.
|
19
|
|
#'
|
20
|
|
#' @param tol `numeric` to specify precision of calculations. In other
|
21
|
|
#' words, how far apart vertices have to be to be considered different?
|
22
|
|
#'
|
23
|
|
#' @param length.factor `numeric` to scale boundary lengths.
|
24
|
|
#'
|
25
|
|
#' @param edge.factor `numeric` to scale boundary lengths for edges that
|
26
|
|
#' do not have any neighbors, such as those that occur along the margins.
|
27
|
|
#'
|
28
|
|
#' @return `data.frame` with 'id1' (`integer`), 'id2'
|
29
|
|
#' (`integer`), and 'amount' (`numeric`) columns.
|
30
|
|
#'
|
31
|
|
#' @seealso This function is based on the algorithm in QMARXAN
|
32
|
|
#' <https://github.com/tsw-apropos/qmarxan> for calculating boundary
|
33
|
|
#' length.
|
34
|
|
#'
|
35
|
|
#' @examples
|
36
|
|
#' # simulate planning units
|
37
|
|
#' sim_pus <- sim.pus(225L)
|
38
|
|
#'
|
39
|
|
#' # calculate boundary data
|
40
|
|
#' bound.dat <- calcBoundaryData(sim_pus)
|
41
|
|
#'
|
42
|
|
#' # print summary of boundary data
|
43
|
|
#' summary(bound.dat)
|
44
|
|
#'
|
45
|
|
#' @export
|
46
|
|
calcBoundaryData <- function(x, tol, length.factor, edge.factor)
|
47
|
|
UseMethod("calcBoundaryData")
|
48
|
|
|
49
|
|
#' @rdname calcBoundaryData
|
50
|
|
#'
|
51
|
|
#' @method calcBoundaryData PolySet
|
52
|
|
#'
|
53
|
|
#' @export
|
54
|
|
calcBoundaryData.PolySet <- function(x, tol = 0.001, length.factor = 1.0,
|
55
|
|
edge.factor = 1.0) {
|
56
|
|
assertthat::assert_that(inherits(x, "PolySet"), assertthat::is.scalar(tol),
|
57
|
|
assertthat::is.scalar(length.factor),
|
58
|
|
assertthat::is.scalar(edge.factor))
|
59
|
|
ret <- rcpp_calcBoundaryDF(x, tolerance = tol, lengthFactor = length.factor,
|
60
|
|
edgeFactor = edge.factor)
|
61
|
|
if (length(ret$warnings) != 0) {
|
62
|
|
warning(paste0("Invalid geometries detected, see \"warnings\" attribute ",
|
63
|
|
"for more information."))
|
64
|
|
attr(ret$bldf, "warnings") <- ret$warnings
|
65
|
|
}
|
66
|
|
return(ret$bldf)
|
67
|
|
}
|
68
|
|
|
69
|
|
#' @rdname calcBoundaryData
|
70
|
|
#'
|
71
|
|
#' @method calcBoundaryData SpatialPolygons
|
72
|
|
#'
|
73
|
|
#' @export
|
74
|
|
calcBoundaryData.SpatialPolygons <- function(x, tol = 0.001,
|
75
|
|
length.factor = 1.0,
|
76
|
|
edge.factor = 1.0) {
|
77
|
|
return(calcBoundaryData(rcpp_Polygons2PolySet(x@polygons), tol,
|
78
|
|
length.factor, edge.factor))
|
79
|
|
}
|