0

Is there a way to find out the number of continuous spatial lines in a SpatialLinesDataFrame ?

EG this would be 2 (the outer one and the inner one) :

small alpha

And this would be 1 :

big alpha

(Also, is there a way to get the total length of each of them ?)

fdetsch
  • 5,239
  • 3
  • 30
  • 58
François M.
  • 4,027
  • 11
  • 30
  • 81

1 Answers1

1

You can use length(sln) (where 'sln' is your SpatialLinesDataFrame) to get the number of features (e.g., lines) and gLength(sln, byid = TRUE) from rgeos to get the single line lengths.

## sample data
library(trajectories)
data(storms)

sln <- as(storms, "SpatialLinesDataFrame")

## number of features
length(sln)
# [1] 4

## length of each feature (throws warning since 'sln' is not projected)
rgeos::gLength(sln, byid = TRUE)
# 2012_TONY-ALBERTO  2011_SEAN-ARLENE   2010_TOMAS-ALEX      2009_IDA-ONE 
#      831.0649          705.1888          804.7706          355.7562 

But be aware that length does not automatically return the number of lines when dealing with multi-line features. If you do the following, for instance,

plot(sln[1, ])

plot

you see that the first feature of our 4-line SpatialLinesDataFrame is a non-continuous spatial line consisting of several sub-lines. To get the number of sub-lines from a discontinuous line feature, you are required to do something like the following.

length(sln[1, ]@lines[[1]]@Lines)
# [1] 19
fdetsch
  • 5,239
  • 3
  • 30
  • 58