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 4
calcBoundaryData <- function(x, tol, length.factor, edge.factor)
47 4
  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 4
  assertthat::assert_that(inherits(x, "PolySet"), assertthat::is.scalar(tol),
57 4
                          assertthat::is.scalar(length.factor),
58 4
                          assertthat::is.scalar(edge.factor))
59 4
  ret <- rcpp_calcBoundaryDF(x, tolerance = tol, lengthFactor = length.factor,
60 4
                             edgeFactor = edge.factor)
61 4
  if (length(ret$warnings) != 0) {
62 0
    warning(paste0("Invalid geometries detected, see \"warnings\" attribute ",
63 0
                   "for more information."))
64 0
    attr(ret$bldf, "warnings") <- ret$warnings
65
  }
66 4
  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 4
  return(calcBoundaryData(rcpp_Polygons2PolySet(x@polygons), tol,
78 4
                          length.factor, edge.factor))
79
}

Read our documentation on viewing source code .

Loading