[R-sig-Geo] cut a line at specific point
Michiel Faber
fabermichiel at planet.nl
Thu Jan 26 23:02:50 CET 2012
Dear all,
I'am using R extensively privately and during work.
I have just discovered the Spatial classes of R and want to use them in
my home project. I want to find the intersections between lines and cut
the lines at those intersections.
For example: Two lines crossing each other once will result in four new
lines.
I have searched intensively, but I can't find a simple way of cutting a
(Spatial) line in two. I can make a data.frame out of it, cut it in two
and make two (Spatial) lines again, but that doesn't seem logical.
Is there a simple way to do it?
The gIntersection() between the lines in the example yields two
intersections (int.s1_s2). I would like to have six new lines, each line
being cut in three parts by the points in int.s1_s2.
Thanks for any pointers,
Michiel
library(rgeos)
x1 <- c(3, 4, 6, 8, 10)
y1 <- c(0.23, 0.48, 0.45, 0.08, 0.18)
x2 <- c(1, 2, 6, 7, 10)
y2 <- c(0.27, 0.36, 0.16, 0.30, 0.85)
#is there also a easier way of doing this?
s1 <- SpatialLines(list(Lines(list(Line(cbind(x1,y1))),ID=1)))
s2 <- SpatialLines(list(Lines(list(Line(cbind(x2,y2))),ID=1)))
int.s1_s2 <- gIntersection(s1,s2)
plot(s1, type="b", col="green", xlim=c(1,10))
lines(s2, type="b", col="blue")
points(int.s1_s2, pch=4, col="black")
More information about the R-sig-Geo
mailing list