I need to find which lines are closed lines and which aren't inside a SpatialLines object in R
. A closed line mean that the first point is the same as the last point. Is there any tool or function in R
to find/check this? I give a reproducible example below.
How can I query the SpatialLines object called spLines
to know that the first line (blue) is closed and the second line (green) opened?
# Libraries
library("sp")
# Points
points.x1 <- c(-54.00000, -54.43489, -54.78560, -54.00000, -53.57810, -53.22097, -54.00000)
points.y1 <- c(-40.74859, -40.00000, -38.00000, -37.49485, -38.00000, -40.00000, -40.74859)
points.x2 <- c(-52.53557, -52.00000, -50.00000, -48.00000, -46.40190)
points.y2 <- c(-41.00000, -40.60742, -40.08149, -40.82503, -39.00000)
# Lines
line1 <- Line(coords = cbind(points.x1, points.y1))
line2 <- Line(coords = cbind(points.x2, points.y2))
# SpatialLines
spLines <- SpatialLines(LinesList = list(Lines(slinelist = list(line1, line2), ID = "A")))
# Plot
plot(spLines, cex = 0, xlab = "Longitude", ylab = "Latitude", main = "Plot")
lines(spLines@lines[[1]]@Lines[[1]], col = "#3465A4", lwd = 4)
lines(spLines@lines[[1]]@Lines[[2]], col = "#73D216", lwd = 4)
box()
Note: this is only a simple example with two lines, the answer should work with a SpatialLines object with many lines.