Skip to content

How to segment or split a spatial line in R

6 messages · Manuel Spínola, Mathieu Rajerison, Mathieu Basille +2 more

#
Dear list members,

How to segment or split a spatial line in shorter equal segments, and also,
how to get the mid point of ecah segment.

Best,

Manuel
#
Hi,

With spatstat, you can find the mid point of line segments
http://lojze.lugos.si/~darja/software/r/library/spatstat/html/midpoints.psp.html

But I don't know any package that would split your line into equal segments.

One method would be to arrange equally spaced points along your line with
spatstat::pointsOnLinesthen reconstruct lines from these points.

There may be a better method than mine...

2014-10-23 16:10 GMT+02:00 Manuel Sp?nola <mspinola10 at gmail.com>:

  
  
#
May be a little bit far fetched, but maybe check 'redisltraj' from the 
adehabitatLT package (and section 4.2.2 of the vignette [1]). Note that it 
was intended for animal trajectories so might be a little bit out of scope 
for you.

Mathieu.


[1] 
http://cran.r-project.org/web/packages/adehabitatLT/vignettes/adehabitatLT.pdf

Le 23/10/2014 10:10, Manuel Sp?nola a ?crit :

  
    
#
sp::spsample also offers regular sampling of points on SpatialLines objects.
On 10/23/2014 05:21 PM, Mathieu Basille wrote:

  
    
#
I think the below is a solution:

library(raster)
# create some lines
cds1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60))
cds2 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55))
cds3 <- rbind(c(-125,0), c(0,60), c(40,5), c(15,-45))
lns <- SpatialLines(list(Lines(list(Line(cds1)), "1"),
     Lines(list(Line(cds2)), "2"), Lines(list(Line(cds3)), "3") ))


# get coordinates
xy <- as.data.frame(lns, xy=TRUE)

# get midpoints
x <- xy$x[-1] + (xy$x[-nrow(xy)] - xy$x[-1]) / 2
y <- xy$y[-1] + (xy$y[-nrow(xy)] - xy$y[-1]) / 2
m <- cbind(x,y)

# remove mid-points between non-segments
i <- xy$cump[-1] == xy$cump[-nrow(xy)]
m <- m[i,]

plot(lns)
points(m)


On Thu, Oct 23, 2014 at 8:56 AM, Edzer Pebesma
<edzer.pebesma at uni-muenster.de> wrote:
#
I should add that you can use geosphere::midPoint if you wanted the
great circle (longitude/latitude) mid-point instead of the Cartesian
midpoint.
But I now also see that you did not ask for splitting into
pre-existing segments, but into new ones ---- Edzer's solution seems
appropriate for that...
Robert
On Thu, Oct 23, 2014 at 9:46 AM, Robert J. Hijmans <r.hijmans at gmail.com> wrote: