sf_methods: sf methods for sfnetworks

sf_methodsR Documentation

sf methods for sfnetworks

Description

sf methods for sfnetwork objects.

Usage

## S3 method for class 'sfnetwork'
st_as_sf(x, active = NULL, focused = TRUE, ...)

## S3 method for class 'sfnetwork'
st_geometry(obj, active = NULL, focused = TRUE, ...)

## S3 replacement method for class 'sfnetwork'
st_geometry(x) <- value

## S3 method for class 'sfnetwork'
st_drop_geometry(x, ...)

## S3 method for class 'sfnetwork'
st_bbox(obj, active = NULL, ...)

## S3 method for class 'sfnetwork'
st_coordinates(x, active = NULL, ...)

## S3 method for class 'sfnetwork'
st_is(x, ...)

## S3 method for class 'sfnetwork'
st_is_valid(x, ...)

## S3 method for class 'sfnetwork'
st_as_s2(x, active = NULL, focused = TRUE, ...)

## S3 method for class 'sfnetwork'
st_crs(x, ...)

## S3 replacement method for class 'sfnetwork'
st_crs(x) <- value

## S3 method for class 'sfnetwork'
st_precision(x)

## S3 method for class 'sfnetwork'
st_set_precision(x, precision)

## S3 method for class 'sfnetwork'
st_shift_longitude(x, ...)

## S3 method for class 'sfnetwork'
st_transform(x, ...)

## S3 method for class 'sfnetwork'
st_wrap_dateline(x, ...)

## S3 method for class 'sfnetwork'
st_normalize(x, ...)

## S3 method for class 'sfnetwork'
st_zm(x, ...)

## S3 method for class 'sfnetwork'
st_m_range(obj, active = NULL, ...)

## S3 method for class 'sfnetwork'
st_z_range(obj, active = NULL, ...)

## S3 method for class 'sfnetwork'
st_agr(x, active = NULL, ...)

## S3 replacement method for class 'sfnetwork'
st_agr(x) <- value

## S3 method for class 'sfnetwork'
st_reverse(x, ...)

## S3 method for class 'sfnetwork'
st_segmentize(x, ...)

## S3 method for class 'sfnetwork'
st_simplify(x, ...)

## S3 method for class 'sfnetwork'
st_join(x, y, ..., ignore_multiple = TRUE)

## S3 method for class 'morphed_sfnetwork'
st_join(x, y, ...)

## S3 method for class 'sfnetwork'
st_filter(x, y, ...)

## S3 method for class 'morphed_sfnetwork'
st_filter(x, y, ...)

## S3 method for class 'sfnetwork'
st_crop(x, y, ...)

## S3 method for class 'morphed_sfnetwork'
st_crop(x, y, ...)

## S3 method for class 'sfnetwork'
st_difference(x, y, ...)

## S3 method for class 'morphed_sfnetwork'
st_difference(x, y, ...)

## S3 method for class 'sfnetwork'
st_intersection(x, y, ...)

## S3 method for class 'morphed_sfnetwork'
st_intersection(x, y, ...)

## S3 method for class 'sfnetwork'
st_intersects(x, y, ...)

## S3 method for class 'sfnetwork'
st_sample(x, ...)

## S3 method for class 'sfnetwork'
st_nearest_points(x, y, ...)

## S3 method for class 'sfnetwork'
st_area(x, ...)

Arguments

x

An object of class sfnetwork.

active

Which network element (i.e. nodes or edges) to activate before extracting. If NULL, it will be set to the current active element of the given network. Defaults to NULL.

focused

Should only features that are in focus be extracted? Defaults to TRUE. See focus for more information on focused networks.

...

Arguments passed on the corresponding sf function.

obj

An object of class sfnetwork.

value

The value to be assigned. See the documentation of the corresponding sf function for details.

precision

The precision to be assigned. See st_precision for details.

y

An object of class sf, or directly convertible to it using st_as_sf. In some cases, it can also be an object of sfg or bbox. Always look at the documentation of the corresponding sf function for details.

ignore_multiple

When performing a spatial join with the nodes table, and there are multiple matches for a single node, only the first one of them is joined into the network. But what should happen with the others? If this argument is set to TRUE, they will be ignored. If this argument is set to FALSE, they will be added as isolated nodes to the returned network. Nodes at equal locations can then be merged using the spatial morpher to_spatial_unique. Defaults to TRUE.

Details

See the sf documentation. The following methods have a special behavior:

  • st_geometry<-: The geometry setter requires the replacement geometries to have the same CRS as the network. Node replacements should all be points, while edge replacements should all be linestrings. When replacing node geometries, the boundaries of the edge geometries are replaced as well to preserve the valid spatial network structure. When replacing edge geometries, new edge boundaries that do not match the location of their specified incident node are added as new nodes to the network.

  • st_transform: No matter if applied to the nodes or edge table, this method will update the coordinates of both tables. The same holds for all other methods that update the way in which the coordinates are encoded without changing their actual location, such as st_precision, st_normalize, st_zm, and others.

  • st_join: When applied to the nodes table and multiple matches exist for the same node, only the first match is joined. A warning will be given in this case. If ignore_multiple = FALSE, multiple mathces are instead added as isolated nodes to the returned network.

  • st_intersection, st_difference and st_crop: These methods clip edge geometries when applied to the edges table. To preserve a valid spatial network structure, clipped edge boundaries are added as new nodes to the network.

  • st_reverse: When reversing edge geometries in a directed network, the indices in the from and to columns will be swapped as well.

  • st_segmentize: When segmentizing edge geometries, the edge boundaries are forced to remain the same such that the valid spatial network structure is preserved. This may lead to slightly inaccurate results.

Geometric unary operations are only supported on sfnetwork objects if they do not change the geometry type nor the spatial location of the original features, since that would break the valid spatial network structure. When applying the unsupported operations, first extract the element of interest (nodes or edges) using st_as_sf.

Value

The methods for st_join, st_filter, st_intersection, st_difference and st_crop, as well as the methods for all setter functions and the geometric unary operations preserve the class of the object it is applied to, i.e. either a sfnetwork object or its morphed equivalent. When dropping node geometries, an object of class tbl_graph is returned. All other methods return the same type of objects as their corresponding sf function. See the sf documentation for details.

Examples

library(sf, quietly = TRUE)

oldpar = par(no.readonly = TRUE)
par(mar = c(1,1,1,1), mfrow = c(1,2))

net = as_sfnetwork(roxel)

# Extract the active network element as sf object.
st_as_sf(net)

# Extract any network element as sf object.
st_as_sf(net, "edges")

# Get the geometry of the active network element.
st_geometry(net)

# Get the geometry of any network element.
st_geometry(net, "edges")

# Replace the geometry of the nodes.
# This will automatically update edge geometries to match the new nodes.
newnet = net
newnds = rep(st_centroid(st_combine(st_geometry(net))), n_nodes(net))
st_geometry(newnet) = newnds

plot(net)
plot(newnet)

# Drop the geometries of the edges.
# This returns an sfnetwork with spatially implicit edges.
st_drop_geometry(activate(net, "edges"))

# Drop the geometries of the nodes.
# This returns a tbl_graph.
st_drop_geometry(net)

# Get the bounding box of the active network element.
st_bbox(net)

# Get CRS of the network.
st_crs(net)

# Get agr factor of the active network element.
st_agr(net)

# Get agr factor of any network element.
st_agr(net, "edges")

# Spatial join applied to the active network element.
net = st_transform(net, 3035)
codes = st_as_sf(st_make_grid(net, n = c(2, 2)))
codes$post_code = as.character(seq(1000, 1000 + nrow(codes) * 10 - 10, 10))

joined = st_join(net, codes, join = st_intersects)
joined

plot(net, col = "grey")
plot(codes, col = NA, border = "red", lty = 4, lwd = 4, add = TRUE)
text(st_coordinates(st_centroid(st_geometry(codes))), codes$post_code)

plot(st_geometry(joined, "edges"))
plot(st_as_sf(joined, "nodes"), pch = 20, add = TRUE)
par(oldpar)

# Spatial filter applied to the active network element.
p1 = st_point(c(4151358, 3208045))
p2 = st_point(c(4151340, 3207520))
p3 = st_point(c(4151756, 3207506))
p4 = st_point(c(4151774, 3208031))

poly = st_multipoint(c(p1, p2, p3, p4)) |>
  st_cast('POLYGON') |>
  st_sfc(crs = 3035) |>
  st_as_sf()

filtered = st_filter(net, poly, .pred = st_intersects)

plot(net, col = "grey")
plot(poly, border = "red", lty = 4, lwd = 4, add = TRUE)
plot(filtered)

par(oldpar)


luukvdmeer/sfnetworks documentation built on Nov. 21, 2024, 4:54 a.m.