centroids: Centroid(s) of a vector

centroids,GVector-methodR Documentation

Centroid(s) of a vector

Description

This function locates the centroid of each geometry of a GVector.

To use this function, you must a) have correctly specified the addonsDir option using faster(), and b) installed the GRASS addon v.centerpoint. See addons() and vignette("addons", package = "fasterRaster").

Usage

## S4 method for signature 'GVector'
centroids(x, method = NULL, fail = TRUE)

Arguments

x

A GVector.

method

Character or NULL (default): Method used for calculating centroids. The method of calculation depends on whether the input is a points, lines, or polygons GVector. If the value is NULL, then the default method will be chosen, depending on the geometry type of the GVector:

  • points:

    • "mean" (default for points): Mean of coordinates.

    • "median": Geometric median; more robust to outliers.

    • "pmedian": Point in x closest to the geometric median.

  • lines:

    • "mid" (default for lines): Mid-point on each line; will fall exactly on the line.

    • "mean": Center of gravity of all line segments; may not fall on the line.

    • ⁠"median⁠: Geometric median; may not fall on the line.

  • polygons:

    • "mean" (default for polygons): Center of gravity (area), calculated using area triangulation.

    • "median": Geometric mean; may not fall inside the polygon.

    • "bmedian": Geometric mean; minimum distance to boundaries; may not fall inside the polygon.

Partial matching is used and case is ignored.

fail

Logical: If TRUE (default), and the addons folder is not correctly specified, the exit the function with an error. If FALSE, then NULL will be returned with a warning.

Value

A points GVector.

See Also

terra::centroids(); GRASS addon module v.centerpoint.

Examples

if (grassStarted()) {

# Setup
library(sf)
library(terra)

# Points, lines, and polygons
madDypsis <- fastData("madDypsis")
madRivers <- fastData("madRivers")
madCoast4 <- fastData("madCoast4")

# Convert to  GVectors:
dypsis <- fast(madDypsis)
rivers <- fast(madRivers)
coast4 <- fast(madCoast4)

# Point centroids:
dypMean <- centroids(dypsis, fail = FALSE)
dypMedian <- centroids(dypsis, method = "median", fail = FALSE)
dypPMedian <- centroids(dypsis, method = "pmedian", fail = FALSE)

if (!is.null(dypMean)) {

plot(dypsis)
plot(dypMean, col = "red", add = TRUE)
plot(dypMedian, col = "green", pch = 2, add = TRUE)
plot(dypPMedian, col = "orange", pch = 1, add = TRUE)
legend("bottomright",
   legend = c("mean", "median", "pmedian"),
   col = c("red", "green", "orange"),
   pch = c(16, 2, 1),
   xpd = NA
)

}

# Line centroids:
riversMid <- centroids(rivers, fail = FALSE)
riversMean <- centroids(rivers, method = "mean", fail = FALSE)
riversMedian <- centroids(rivers, method = "median", fail = FALSE)

if (!is.null(riversMid)) {

plot(rivers)
plot(riversMid, col = "red", add = TRUE)
plot(riversMean, col = "green", pch = 2, add = TRUE)
plot(riversMedian, col = "orange", pch = 1, add = TRUE)
legend("bottomright",
   legend = c("mid", "mean", "median"),
   col = c("red", "green", "orange"),
   pch = c(16, 2, 1),
   xpd = NA
)

}

# Polygon centroids:
coastMean <- centroids(coast4, fail = FALSE)
coastMedian <- centroids(coast4, method = "median", fail = FALSE)
coastBMedian <- centroids(coast4, method = "bmedian", fail = FALSE)

if (!is.null(coastMean)) {

plot(coast4)
plot(coastMean, col = "red", add = TRUE)
plot(coastMedian, col = "green", pch = 2, add = TRUE)
plot(coastBMedian, col = "orange", pch = 1, add = TRUE)
legend("bottomright",
   legend = c("mean", "median", "bmedian"),
   col = c("red", "green", "orange"),
   pch = c(16, 2, 1),
   xpd = NA
)

}

}

adamlilith/fasterRaster documentation built on Dec. 21, 2024, 2:04 a.m.