使用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"
答案 0 :(得分:2)
基于输出结构,
sapply( routes@lines, function (x) dim(x@Lines[[1]]@coords)[1] )