2

After importing a spatial lines dataframe with readOGR, I would like to determine the number of line segments (nodes) in each line. I cannot find a practical way to simply export the lengths as a vector.

routes@lines will get me to the line slots, but then how does one get the length of each one?

For example, in the sample data below, we see that the first line is composed of 93 segments, the second line composed of 170 segments, the third 91 segments, and so on.

In the end, I'd like a vector of 1657 numbers representing the length of line segments in the SpatialLinesDataFrame

Is there a quick solution?

> 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"
  • You are asking about segments but you refer to coordinates. Do you want the number of coordinates in each part? – mdsumner Apr 7 '17 at 15:54
  • I want the number of coordinate pairs in each part – the_darkside Apr 7 '17 at 16:22
1

ggplot2 fortify is good for this. Once you have a table of all coordinates, identified by 'id' (row per SpatialDF object) and 'group' (parts per object), then you can group/by summarize.

library(dplyr)
library(ggplot2)
fortify(routes) %>% group_by(id, group) %>% 
 ## number of points per group
 summarize(npoint = n()) %>% 
 ## number of points per id
 summarize(npoint = sum(npoint))

If you really mean segments you could subtract from the point count first.

My pseudo code might be off the mark, but once you understand the fortify output it's easy to modify a summary to suit.

1

This method works

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.