Skip to contents

gs_geometry() returns a node geometry column of a GraphSpace object, and gs_geometry<- adds, replaces or removes one. Geometry columns are sfc objects from the sf package, with one geometry per node, such as node shapes or cell boundaries.

Usage

# S4 method for class 'GraphSpaceInterface'
gs_geometry(x, name = "geometry", raw = FALSE, unit = NULL)

# S4 method for class 'GraphSpaceInterface'
gs_geometry(x, name = "geometry") <- value

Arguments

x

A GraphSpace object.

name

Name of the geometry column (a single string).

raw

A single logical value. If FALSE (default), return the working copy; if TRUE, the pristine copy, in the frame of the raw coordinates. As in gs_coords.

unit

Optional length unit, such as "um", to return the geometry in, using the unit set with gs_coords_unit; scaled as gs_coords(x, raw, unit), so that both stay aligned.

value

An sfc geometry column, an sf object (its geometry is used) or a list of sfg geometries, with one geometry per node; or NULL to remove the column.

Value

gs_geometry() returns an sfc object, or NULL if name isn't a geometry column. gs_geometry<- returns the updated GraphSpace object.

Details

Like node coordinates, geometry is kept in two forms: a raw copy, which matches the raw coordinates, and a working copy, which matches the node positions (see normalizeGeometry). Both follow coordinate transformations and the scale factor. gs_geometry() returns the working copy, or the raw copy with raw = TRUE. Setting a geometry column updates both; invalid geometries are repaired with sf::st_make_valid().

If name is already used by another node field, that field is removed and replaced, with a warning; structural columns and coordinates can't be replaced. Geometry columns can also be removed with gs$name <- NULL.

Examples

library(RGraphSpace)
data("gtoy1", package = "RGraphSpace")
gs <- GraphSpace(gtoy1)
#> Validating the 'igraph' object...
#> Ignoring graph-level attributes: 'name', 'mode', 'center'
#> Creating a 'GraphSpace' object...

# Add node shapes as a geometry column (requires the 'sf' package)
if (requireNamespace("sf", quietly = TRUE)) {
  gs_geometry(gs) <- sfshape_ngons(n = gs_vcount(gs))
  gs_geometry(gs)
  
  # The raw copy, in the frame of gs_coords(gs, raw = TRUE)
  gs_geometry(gs, raw = TRUE)

  # A second column, under another name
  gs_geometry(gs, name = "stars") <- sfshape_stars(n = gs_vcount(gs))

  # Remove a geometry column
  gs_geometry(gs, name = "stars") <- NULL
}