st_project_on_network | R Documentation |
Project spatial points on a spatial network
st_project_on_network(x, network, on = "edges")
x |
The spatial features to be projected, either as object of class
|
network |
An object of class |
on |
On what component of the network should the points be projected?
Setting it to |
This function uses st_nearest_feature
to find
the nearest edge or node to each feature in x
. When projecting on
edges, it then finds the nearest point on the nearest edge by calling
st_nearest_points
in a pairwise manner.
The same object as x
but with its geometries replaced by the
projections.
Due to internal rounding of rational numbers, even a point projected
on an edge may not be evaluated as actually intersecting that edge when
calling st_intersects
.
library(sf, quietly = TRUE)
oldpar = par(no.readonly = TRUE)
par(mar = c(1,1,1,1))
# Create a spatial network.
n1 = st_point(c(0, 0))
n2 = st_point(c(1, 0))
n3 = st_point(c(2, 0))
e1 = st_sfc(st_linestring(c(n1, n2)), crs = 3857)
e2 = st_sfc(st_linestring(c(n2, n3)), crs = 3857)
net = as_sfnetwork(c(e1, e2))
# Create spatial points to project in.
p1 = st_sfc(st_point(c(0.25, 0.1)))
p2 = st_sfc(st_point(c(1, 0.2)))
p3 = st_sfc(st_point(c(1.75, 0.15)))
pts = st_sf(foo = letters[1:3], geometry = c(p1, p2, p3), crs = 3857)
# Project points to the edges of the network.
p1 = st_project_on_network(pts, net)
plot(net)
plot(st_geometry(pts), pch = 20, col = "orange", add = TRUE)
plot(st_geometry(p1), pch = 4, col = "orange", add = TRUE)
# Project points to the nodes of the network.
p2 = st_project_on_network(pts, net, on = "nodes")
plot(net)
plot(st_geometry(pts), pch = 20, col = "orange", add = TRUE)
plot(st_geometry(p2), pch = 4, col = "orange", add = TRUE)
par(oldpar)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.