geomtype: Geometry of a GVector (points, lines, or polygons)

geomtype,GVector-methodR Documentation

Geometry of a GVector (points, lines, or polygons)

Description

geomtype() reports whether a GVector represents points, lines, or polygons. The "⁠is.*⁠" functions test whether the GVector represents points, lines, or polygons.

Usage

## S4 method for signature 'GVector'
geomtype(x, grass = FALSE)

## S4 method for signature 'GVector'
is.points(x)

## S4 method for signature 'GVector'
is.lines(x)

## S4 method for signature 'GVector'
is.polygons(x)

Arguments

x

A GVector.

grass

Logical: If FALSE (default), return terra-like geometry types ("points", "lines", or "polygons"). If TRUE, return GRASS-like geometry types ("point", "line", "area"–note that these are a subset of the available types and may not be the "true" GRASS type).

Value

geomtype() returns either "points", "lines", or "polygons" if the grass arguments is FALSE, or "point", "line", "area" if grass is TRUE. The "⁠is.*⁠" functions return TRUE or FALSE.

See Also

terra::geomtype()

Examples

if (grassStarted()) {

# Setup
library(sf)

# Example data:
madCoast4 <- fastData("madCoast4")
madRivers <- fastData("madRivers")
madDypsis <- fastData("madDypsis")

# Convert sf vectors to GVectors:
coast <- fast(madCoast4)
rivers <- fast(madRivers)
dypsis <- fast(madDypsis)

# Geographic properties:
ext(rivers) # extent
crs(rivers) # coordinate reference system

# Column names and data types:
names(coast)
datatype(coast)

# Points, lines, or polygons?
geomtype(dypsis)
geomtype(rivers)
geomtype(coast)

is.points(dypsis)
is.points(coast)

is.lines(rivers)
is.lines(dypsis)

is.polygons(coast)
is.polygons(dypsis)

# Number of dimensions:
topology(rivers)
is.2d(rivers) # 2-dimensional?
is.3d(rivers) # 3-dimensional?

# Just the data table:
as.data.frame(rivers)
as.data.table(rivers)

# Top/bottom of the data table:
head(rivers)
tail(rivers)

# Vector or table with just selected columns:
names(rivers)
rivers$NAME
rivers[[c("NAM", "NAME_0")]]
rivers[[c(3, 5)]]

# Select geometries/rows of the vector:
nrow(rivers)
selected <- rivers[2:6]
nrow(selected)

# Plot:
plot(coast)
plot(rivers, col = "blue", add = TRUE)
plot(selected, col = "red", lwd = 2, add = TRUE)

# Vector math:
hull <- convHull(dypsis)

un <- union(coast, hull)
sameAsUnion <- coast + hull
plot(un)
plot(sameAsUnion)

inter <- intersect(coast, hull)
sameAsIntersect <- coast * hull
plot(inter)
plot(sameAsIntersect)

er <- erase(coast, hull)
sameAsErase <- coast - hull
plot(er)
plot(sameAsErase)

xr <- xor(coast, hull)
sameAsXor <- coast / hull
plot(xr)
plot(sameAsXor)

# Vector area and length:
expanse(coast, unit = "km") # polygons areas
expanse(rivers, unit = "km") # river lengths

# Fill holes
# First, we will make some holes by creating buffers around points, then
# removing them from a polygons GVector.
buffs <- buffer(dypsis, 500)

holes <- coast - buffs
plot(holes)

filled <- fillHoles(holes, fail = FALSE)

}

adamlilith/fasterRaster documentation built on Oct. 26, 2024, 4:06 p.m.