Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add st_perimeter() #2279

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions R/geom-measures.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,47 @@ message_longlat = function(caller) {
message(paste0(m, collapse = "\n"))
}


#' @name geos_measures
#' @export
#' @examples
#' st_perimeter(poly)
#' st_perimeter(mpoly)
st_perimeter = function(x, ...) {
x = st_geometry(x)

# for spherical geometries we use s2
if (isTRUE(st_is_longlat(x))) {

if (!requireNamespace("s2", quietly = TRUE)) {
stop("package s2 required to calculate the perimeter of spherical geometries")
}

# ensure units are set to meters
units::set_units(
s2::s2_perimeter(x, ...),
"m",
mode = "standard"
)

# non-spherical geometries use lwgeom
} else {
if (!requireNamespace("lwgeom", quietly = TRUE)) {
stop("package lwgeom required, please install it first")
}

# note that units are handled appropriately by lwgeom
lwgeom::st_perimeter(x)
}
}

#' Compute geometric measurements
#'
#' Compute Euclidian or great circle distance between pairs of geometries; compute, the area or the length of a set of geometries.
#' Compute Euclidean or great circle distance between pairs of geometries; compute, the area or the length of a set of geometries.
JosiahParry marked this conversation as resolved.
Show resolved Hide resolved
#' @name geos_measures
#' @param x object of class \code{sf}, \code{sfc} or \code{sfg}
#' @param y object of class \code{sf}, \code{sfc} or \code{sfg}, defaults to \code{x}
#' @param ... passed on to \link[s2]{s2_distance} or \link[s2]{s2_distance_matrix}
#' @param ... passed on to \link[s2]{s2_distance}, \link[s2]{s2_distance_matrix}, or \link[s2]{s2_perimeter}
JosiahParry marked this conversation as resolved.
Show resolved Hide resolved
#' @param dist_fun deprecated
#' @param by_element logical; if \code{TRUE}, return a vector with distance between the first elements of \code{x} and \code{y}, the second, etc; an error is raised if \code{x} and \code{y} are not the same length. If \code{FALSE}, return the dense matrix with all pairwise distances.
#' @param which character; for Cartesian coordinates only: one of \code{Euclidean}, \code{Hausdorff} or \code{Frechet}; for geodetic coordinates, great circle distances are computed; see details
Expand Down
Loading