【问题标题】:How do I split/divide polyline shapefiles into equally-length-smaller segments?如何将折线 shapefile 拆分/划分为等长更小的段?
【发布时间】:2016-12-06 14:47:42
【问题描述】:

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

提供一个想法:

当我将折线 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 的经度和纬度,划分每个段(这里是五个)。

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

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

【问题讨论】:

    标签: r shapefile polyline


    【解决方案1】:

    下面的函数将空间线对象的每条线分成split_length长度的线段加上剩余的线段。它使用线段的向量表示法,创建一个向量 u 单位长度和要分割的线的方向,可以相乘以创建由许多段组成的更长的线(here 和 @ 987654322@我用过的参考资料)。

    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 中创建了一个包,以防有人愿意贡献。

    【讨论】:

      【解决方案2】:

      Duccio 提出的解决方案会重置每个新段的间距。我将脚本改编为具有运行间距的版本。下图显示了差异。

      按照 Duccio 的方法进行拆分

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

      按照我的方法进行拆分

      所需的间距被转移到拐角处。这种方法保证了这些点相对于原始线始终具有所需的间距。然而,这里的结果点彼此的间距都不是恒定的。此外,这种方法产生的线比原始线短。

      这种效果会随着分辨率的提高而减弱。

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

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

      我的方法不使用 Line 对象(sp 包),但完全适用于数据帧。因此,我不想分叉原始的 github 存储库。

      完整代码为:

          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")
        )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-23
        • 2022-01-26
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多