SpatialLinesDataFrame 中的段数

Number of segments in SpatialLinesDataFrame

在使用 readOGR 导入空间线数据框后,我想确定每条线中线段(节点)的数量。我找不到简单地将长度导出为向量的实用方法。

routes@lines 会让我找到线槽,但是如何获得每个槽的长度呢?

例如,在下面的示例数据中,我们看到第一行由 93 个段组成,第二行由 170 个段组成,第三行由 91 个段组成,依此类推。

最后,我想要一个由 1657 个数字组成的向量,表示 SpatialLinesDataFrame

中线段的长度

有没有快速的解决办法?

> class(routes)
[1] "SpatialLinesDataFrame"
attr(,"package")
[1] "sp"
> str(routes)
Formal class 'SpatialLinesDataFrame' [package "sp"] with 4 slots
  ..@ data       :'data.frame': 1657 obs. of  3 variables:
  .. ..$ start_time: Factor w/ 1631 levels "2016/09/09 00:00:02",..: 1 2 3 4 5 6 7 8 9 10 ...
  .. ..$ duration  : int [1:1657] 786 1248 738 786 651 660 616 889 408 475 ...
  .. ..$ difftime  :Class 'difftime'  atomic [1:1657] 2 4 19 67 92 119 170 202 206 213 ...
  .. .. .. ..- attr(*, "units")= chr "secs"
  ..@ lines      :List of 1657
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:93, 1:2] -79.9 -79.9 -79.9 -79.9 -79.9 ...
  .. .. .. ..@ ID   : chr "0"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:170, 1:2] -79.9 -79.9 -79.9 -79.9 -79.9 ...
  .. .. .. ..@ ID   : chr "1"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:91, 1:2] -79.9 -79.9 -79.9 -79.9 -79.9 ...
  .. .. .. ..@ ID   : chr "2"

基于输出结构,

sapply( routes@lines, function (x) dim(x@Lines[[1]]@coords)[1] )