我有一个sf
多线串对象,我想将其转换为一系列均匀间隔的点。
我最初尝试使用st_cast
,但这似乎只创建了重新创建线条所需的最少点数:
# sample multilinestring sf object
sf_multiline <- structure(list(
OBJECTI = 2558325, REACHCO = "18010108001016",
TermnlP = 10013609, lnLngt_ = 327.450701655499, TtDASKM = 6.588,
StrmOrd = 2, TrmnlFl = "0", SLOPE = 0.04073394, FromNod = 10006177,
ToNode = 10006028, SegNum = 270, IP = 0.713471689535624,
IP_class = structure(2L, .Label = c("Low", "High", "Outside Navarro"
), class = "factor"), geometry = structure(list(structure(list(
structure(c(456878.59466448, 456746.114675578, 456718.025986089,
456667.149879425, 456646.355212286, 456625.81800052,
4328172.35209264, 4328073.83723204, 4328040.94292727,
4328020.82579635, 4328006.04738784, 4327973.17832125),
.Dim = c(6L, 2L))),
class = c("XY", "MULTILINESTRING", "sfg"))),
class = c("sfc_MULTILINESTRING", "sfc"),
precision = 0, bbox = structure(c(xmin = 456625.81800052, ymin = 4327973.17832125,
xmax = 456878.59466448, ymax = 4328172.35209264),
class = "bbox"),
crs = structure(list(epsg = 26910L, proj4string = "+proj=utm +zone=10 +datum=NAD83 +units=m +no_defs"),
class = "crs"), n_empty = 0L)),
row.names = 254L, class = c("sf", "data.frame"), sf_column = "geometry",
agr = structure(c(OBJECTI = NA_integer_,
REACHCO = NA_integer_, TermnlP = NA_integer_, lnLngt_ = NA_integer_,
TtDASKM = NA_integer_, StrmOrd = NA_integer_, TrmnlFl = NA_integer_,
SLOPE = NA_integer_, FromNod = NA_integer_, ToNode = NA_integer_,
SegNum = NA_integer_, IP = NA_integer_, IP_class = NA_integer_),
.Label = c("constant", "aggregate", "identity"), class = "factor")
)
# conver to point object
sf_points <- st_cast(sf_multiline, "POINT")
# plot multilinestring and point object
ggplot() + geom_sf(data=sf_multiline, color="blue") + geom_sf(data=sf_points, color="red") + theme(panel.grid=element_line(color="transparent"))
我知道如何使用带有形状文件的sp::spsample
函数来执行此操作,但是我正尝试迁移到sf
,所以想知道一种sf
的实现方法。
以下是使用sp
的所需输出:
# convert sf to shapefile
shp_multiline <- as(sf_multiline, "Spatial")
# regularly sample points along line
shp_points <- sp::spsample(shp_multiline, n=10, type="regular")
# plot
plot(shp_multiline, col="blue")
plot(shp_points, col="red", add=T)
答案 0 :(得分:2)
我认为您需要sf
函数st_line_sample()
。一个警告是,它期望的只是“ LINESTRING”而不是“ MULTILINESTRING”,因此您需要先使用st_cast()
使其一致。
sf_linestring <- st_cast(sf_multiline, "LINESTRING")
sampled_points <- st_line_sample(sf_linestring, n = 10)
# plot multilinestring and point object
ggplot() +
geom_sf(data=sf_multiline, color="blue") +
geom_sf(data=sf_points, color="red") +
geom_sf(data = sampled_points, color = "purple") +
theme(panel.grid=element_line(color="transparent"))