将 sf 多线对象转换为均匀分布的点

Convert sf multiline object to evenly spaced points

我有一个 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"))

我知道如何使用带有 shapefile 的 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)

我认为您需要 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"))