[R-sig-Geo] How to segment or split a spatial line in R

Robert J. Hijmans r.hijmans at gmail.com
Thu Oct 23 18:46:49 CEST 2014


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:
> sp::spsample also offers regular sampling of points on SpatialLines objects.
>
> On 10/23/2014 05:21 PM, Mathieu Basille wrote:
>> 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 :
>>> 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
>>>
>>
>
> --
> Edzer Pebesma,  Co-Editor-in-Chief Computers & Geosciences
> Institute for Geoinformatics (ifgi), University of Münster
> Heisenbergstraße 2, 48149 Münster, Germany. Phone: +49 251
> 83 33081 http://ifgi.uni-muenster.de GPG key ID 0xAC227795
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



More information about the R-sig-Geo mailing list