如何将折线形状文件拆分/分割成等长的小段?

时间:2016-08-01 13:32:29

标签: r shapefile polyline

我有一个折线shapefile,它代表了城市道路网络的一部分。我的shapefile包含几个行/街段(在我的例子中为58)。 通过使用R-cran,我想进一步将折线段分成具有相等长度(例如10m)的较小部分。

提供一个想法:

当我将折线shapefile导入R时,我创建了一个数据帧,它看起来像:

# Import the polyline shapefile into a SpatialLinesDataFrame object:
# library(sp)
sp.roads <- readOGR(dsn="/Users/mgv/Documents/project",layer="roads_sr_corr") 

# Create a dataframe from the SpatialLinesDataFrame 
# library(stplanr)    
linedf <- line2df(sp.roads)
str(linedf)
> str(linedf)
'data.frame':   58 obs. of  4 variables:
 $ fx: num  13.39991 13.40138 13.40606 13.40232 13.40177 ...
 $ fy: num  42.35066 42.35412 42.35599 42.34514 42.34534 ...
 $ tx: num  13.40150 13.40119 13.40591 13.40246 13.40182 ...
 $ ty: num  42.35026 42.35386 42.35602 42.34530 42.34525 ...

其中(fx,fy,tx,ty)分别是点(x,y)_from和(x,y)_to的经度和纬度,分隔每个段(此处为5)。

我们的想法是获得更密集的折线shapefile,我可以将其用作空间分析,作为一种“网格”,将沿道路收集的地理参考数据点与每个段相关联。

非常感谢您的帮助以及解决此问题的任何建议。

2 个答案:

答案 0 :(得分:4)

以下函数将空间线对象的每一行划分为 split_length 长度的段加上剩余的段。它使用一个线段的矢量符号,创建一个单位长度的矢量 u 和要分割的线的方向,可以相乘以创建一个由许多线段组成的较长线(herehere我使用过的参考文献。

SplitLines = function(spatial_line,
                      split_length = 20,
                      return.dataframe = F,
                      plot.results = F) {
  # The function splits each line of the spatial line object into segments of a given length
  # spatial_line: a spatial line object
  # split_length: the length of the segments to split the lines into, in units of the SpatialLine object
  # return.dataframe: if true it returns the segments in the form of a dataframe, otherwise in a SpatialLine object
  # plot.results: 
  require(sp)
  #### Define support functions ####
  # SpatialLines2df extracts start and end point coordinates of each segment of a SpatialLine object
  # spatial_line: an object class SpatialLinesDataFrame of the package sp
  SpatialLines2df = function(spatial_line) {
    df = data.frame(
      id = character(),
      mline_id = character(),
      segment_id = character(),
      fx = numeric(),
      fy = numeric(),
      tx = numeric(),
      ty = numeric(),
      stringsAsFactors = FALSE
    )
    for (i in 1:length(spatial_line)) {
      coords = spatial_line@lines[[i]]@Lines[[1]]@coords # For each line takes the coords of the vertex
      row_nums = 1:(nrow(coords) - 1)
      mline_id = formatC(i, width = 9, flag = '0') # Creates id for the line
      segment_id = formatC(row_nums, width = 3, flag = '0') # Creates id for each single segment belonging to the line
      id = paste0(mline_id, '_', segment_id) # Creates a composite id
      for (j in row_nums) {
        # For each segment stores ids and coordinates
        df[nrow(df) + 1, ] = c(id[j],
                               mline_id,
                               segment_id[j],
                               coords[j, 1],
                               coords[j, 2],
                               coords[j + 1, 1],
                               coords[j + 1, 2])
      }
    }
    row.names(df) = NULL
    df$fx = as.numeric(df$fx)
    df$fy = as.numeric(df$fy)
    df$tx = as.numeric(df$tx)
    df$ty = as.numeric(df$ty)
    return(df)
  }

  # linedf2SpatialLines converts a dataframe of IDs and coordinates into a spatial line
  # linedf: a data.frame with columns as:
  #         id = generic ids of the lines,
  #         fx = coordinates x of the first point of the line
  #         fy = coordinates y of the first point of the line
  #         tx = coordinates x of the second point of the line
  #         tx = coordinates y of the second point of the line

  require(sp)
  linedf2SpatialLines = function(linedf) {
    sl = list()
    for (i in 1:nrow(linedf)) {
      c1 = cbind(rbind(linedf$fx[i], linedf$tx[i]),
                 rbind(linedf$fy[i], linedf$ty[i]))
      l1 = Line(c1)
      sl[[i]] = Lines(list(l1), ID = linedf$id[i])
    }
    SL = SpatialLines(sl)
    return(SL)
  }


  #### Split the lines ####
  # Convert the input SpatialLine object into a dataframe and create an empty output dataframe
  linedf = SpatialLines2df(spatial_line)
  df = data.frame(
    id = character(),
    fx = numeric(),
    fy = numeric(),
    tx = numeric(),
    ty = numeric(),
    stringsAsFactors = FALSE
  )


  for (i in 1:nrow(linedf)) {
    # For each line of the dataframe, corresponding to a single line of the spatial object
    # skips if length is less then split_length
    v_seg = linedf[i, ]
    seg_length = sqrt((v_seg$fx - v_seg$tx) ^ 2 + (v_seg$fy - v_seg$ty) ^
                        2) # segment length
    if (seg_length <= split_length) {
      df[nrow(df) + 1,] = c(paste0(v_seg$id, '_', '0000'),
                            v_seg$fx,
                            v_seg$fy,
                            v_seg$tx,
                            v_seg$ty)
      next()
    }
    # Create a vector of direction as the line and unit length
    # vector v corresponding to the line
    v = c(v_seg$tx  -  v_seg$fx,
          v_seg$ty  -  v_seg$fy)
    # vector of direction v and unit length
    u = c(v[1]  /  sqrt(v[1]  ^  2 + v[2]  ^  2), v[2]  /  sqrt(v[1]  ^  2 + v[2]  ^ 2))
    # Calculates how many segment the line is split into and the leftover
    num_seg = floor(seg_length  /  split_length)
    seg_left = seg_length - (num_seg  *  split_length)

    # Add to the output dataframe each segment plus the leftover
    for (i in 0:(num_seg  -  1)) {
      # Add num_seg segments
      df[nrow(df)  +  1,] = c(
        paste0(v_seg$id, '_', formatC(i, width = 4, flag = '0')),
        v_seg$fx + u[1]  *  split_length  *  i,
        v_seg$fy + u[2]  *  split_length  *  i,
        v_seg$fx + u[1]  *  split_length  *  (i  +  1),
        v_seg$fy + u[2]  *  split_length  *  (i  +  1)
      )
    }
    df[nrow(df) + 1,] = c(
      paste0(v_seg$id, '_', formatC(
        num_seg, width = 4, flag = '0'
      )),
      # Add leftover segment
      v_seg$fx + u[1] * split_length * num_seg,
      v_seg$fy + u[2] * split_length * num_seg,
      v_seg$tx,
      v_seg$ty
    )

  }

  #### Visualise the results to check ####
  if (plot.results) {
    plot(spatial_line)
    coords = cbind(as.numeric(df$fx), as.numeric(df$fy))
    coords = rbind(coords, as.numeric(df$tx[nrow(df)]), as.numeric(df$ty)[nrow(df)])
    sp_points = SpatialPoints(coords)
    plot(sp_points, col = 'red', add = T)
  }

  #### Output ####
  df$fx = as.numeric(df$fx)
  df$fy = as.numeric(df$fy)
  df$tx = as.numeric(df$tx)
  df$ty = as.numeric(df$ty)
  if (return.dataframe) {
    return(df)
  } # Return a dataframe
  sl = linedf2SpatialLines(df)
  return(sl) # Return a SpatialLine object
}

您可以使用以下方法测试该功能:

Sl = SpatialLines(list(Lines(list(Line(cbind(c(1,2,3, 4),c(3,2,2,4)))), ID="a")))
plot(Sl)
Sl_split = SplitLines(Sl, split_length = 0.1, return.dataframe = FALSE, plot.results = TRUE)

我确信这个功能可以用更简洁有效的方式编写。我已经在git repository创建了一个包,以防有人愿意贡献。

答案 1 :(得分:1)

Duccio提出的解决方案会重置每个新细分的间距。我将脚本修改为运行间距的版本。差异在下面的图像中可视化。

按照Duccio

的方法进行拆分

所需的间距(由绿色条的长度表示)始终从每个段开始。与前一段的剩余距离不会转移到下一段。因此,在整条线上产生的间距不恒定

按我的方法分割

所需间距沿圆角转移。这种方法证明了这些点总是与原始线相关的所需间距。但是,这里的结果点之间的间距都不是常数。此外,这种方法产生的线比原线短。

此效果会随着分辨率的提高而降低。

或者可以通过将可选参数“add_original_points”设置为TRUE来防止。

注意:“正确使用的算法”取决于您的应用程序。 免责声明:我的版本完全基于Duccio A的解决方案,我给予他充分的信任。

我的方法不使用Line对象(sp包),但完全依赖于数据框。因此,我不想分叉原来的github repos。

完整的代码是:

    resample_polyline  = function(polyline, interval_length = 20, add_original_points = TRUE, add_final_point = FALSE) {

  # The function splits a polyline into segments of a given length.
  # polyline: a spatial polyline data frame
  # interval_length: the length of the segments to split the lines into, in units of the polyline coordinates
  # add_original_points: whether or not the original points of the polyline should be added to the resulting line
  #                      if set FALSE, the resulting line will be shorter
  # add_final_point: whether or not the final point of the polyline should be added to the resulting line

  # transform input polyline
  linedf = data.frame(
    x  = polyline$x[1:nrow(polyline)-1],
    y  = polyline$y[1:nrow(polyline)-1],
    x2 = polyline$x[2:nrow(polyline)],
    y2 = polyline$y[2:nrow(polyline)]
  )

  # prepare output
  df = data.frame(
    x  = numeric(),
    y  = numeric()
  )

  residual_seg_length = 0
  for (i in 1:nrow(linedf)) {

    # for each line of the dataframe calculate segment length   
    v_seg      = linedf[i, ]
    seg_length = sqrt((v_seg$x - v_seg$x2) ^ 2 + (v_seg$y - v_seg$y2) ^ 2) 

    # create a vector of direction for the segment 
    v = c(v_seg$x2 - v_seg$x, v_seg$y2 - v_seg$y)

    # unit length
    u = c(v[1]  /  sqrt(v[1]  ^  2 + v[2]  ^  2), v[2]  /  sqrt(v[1]  ^  2 + v[2]  ^ 2))

    # calculate number of segment the segment is split into
    num_seg = floor((seg_length - residual_seg_length)  /  interval_length)

    # skip if next vertex is before interval_length
    if(num_seg >= 0) {      

      # add interpolated segments
      for (i in 0:(num_seg)) {
        df[nrow(df) + 1,] = c(
          v_seg$x  +  u[1] * residual_seg_length  +  u[1]  *  interval_length  *  i ,
          v_seg$y  +  u[2] * residual_seg_length  +  u[2]  *  interval_length  *  i
        )
      }

      # add original point (optional)
      if(add_original_points){
        df[nrow(df) + 1,] = c(
          v_seg$x2,
          v_seg$y2
        )
      }

    } else {

      # add original point (optional)
      if(add_original_points){
        df[nrow(df) + 1,] = c(
          v_seg$x2,
          v_seg$y2
        )
      }

      residual_seg_length = residual_seg_length - seg_length
      next()

    }

    # calculate residual segment length
    residual_seg_length = interval_length - ((seg_length - residual_seg_length) - (num_seg  *  interval_length))

  }

  # add final point (optional)
  if(add_final_point){
    df = rbind(df, data.frame(
      x = tail(polyline$x, n=1),
      y = tail(polyline$y, n=1)
    ))
  }

  return(df)

}

测试
polyline = data.frame(
  x = c(-5,1,5,7,8,12,14,16,17,13), # x
  y = c(0,11,3,8,2,15,9,13,15,23)   # y
)

plot(polyline$x, polyline$y, type="l", asp=1, lwd=1)
points(polyline$x, polyline$y, pch=4, cex=4, col="gray")

polyline2 = resample_polyline(polyline, interval_length = 5, add_final_point = FALSE, add_original_points = TRUE)
lines(polyline2$x,  polyline2$y, col="red", lty=4, lwd=3)
points(polyline2$x, polyline2$y, pch=19)

legend("topleft",
  c("original points", "added points", "resulting line"),
  pch = c(4, 19, NA),
  lty = c(NA, NA, 2),
  pt.cex = c(4,1,1),
  col = c("gray", "black", "red")
  )