Let's assume a line of segments of class SpatialLines
of length len
. This particular line starts at the upper left corner.
library(sp)
x <- structure(list(x = c(-7.23437435517476, 6.35937810318614, -5.86718660792582,
7.96094089282062), y = c(7.08139459814975, 6.8633712983227, -7.61337581019376,
-6.2180266913006)), .Names = c("x", "y"))
xline <- SpatialLines(list(Lines(Line(x), ID = 1)))
#len <- LineLength(as.matrix(data.frame(x)))
len <- LineLength(as.matrix(data.frame(coordinates(xline))))
plot(0,0, xlim = c(-10, 10), ylim = c(-10, 10), type = "n")
lines(xline)
I would like to find a point on this line that is findme
units away from the start of the line. For instance, if I were to look for a point that is 10 units along the line from the beginning, I would get a point near the node between first and second segment. Your input on a more robust solution most welcome.
I've tried to find it using spsample
(see below), but this method is (too) unreliable and doesn't work for points the second half of the line.
# very approximate method, not very suitable
findme <- 11 # 11, 12 and 13 give same result
segs <- 1/(findme/xline.length)
xsam <- spsample(x = xline, n = segs, type = "regular", offset = 0)
points(xsam)