[R-sig-Geo] Points to line

Michael Sumner mdsumner at gmail.com
Tue Jul 17 23:57:03 CEST 2012


There is support for coercion from "trip" objects in the trip package,
which might be overkill for your needs, but it handles multiple sets
of coordinates identified by ID and ordered by date-time, creating a
Lines object for each with summary data. (Overkill maybe since if you
only have one set of coordinates you will need an ID, and you need
date-times to be set in the right order).

library(trip)

d <- data.frame(x = 1:10, y = rnorm(10), tms = Sys.time() +  c(1:6,
c(2, 4, 6, 8)), id = c(rep(1, 6), rep(2, 4)))
coordinates(d) <- ~x+y

## this step enforces a number of validation checks
tr <- trip(d, c("tms", "id"))

## Now straightforward coercion works, dropping all data
## except a start/end and duration summary

as(tr, "SpatialLinesDataFrame")

An object of class "SpatialLinesDataFrame"
Slot "data":
  tripID           tripStart             tripEnd tripDur
1      1 2012-07-17 21:55:14 2012-07-17 21:55:19       5
2      2 2012-07-17 21:55:15 2012-07-17 21:55:21       6

Slot "lines":
[[1]]
An object of class "Lines"
Slot "Lines":
[[1]]
An object of class "Line"
Slot "coords":
     x          y
[1,] 1  2.1981132
[2,] 2 -0.1139012
[3,] 3  0.4010417
[4,] 4 -0.5567641
[5,] 5 -0.5245291
[6,] 6  0.2547033



Slot "ID":
[1] "1"


[[2]]
An object of class "Lines"
Slot "Lines":
[[1]]
An object of class "Line"
Slot "coords":
      x          y
[1,]  7  0.6552012
[2,]  8  2.0787520
[3,]  9  0.8308318
[4,] 10 -1.7930436



Slot "ID":
[1] "2"



Slot "bbox":
        min       max
x  1.000000 10.000000
y -1.793044  2.198113

Slot "proj4string":
CRS arguments: NA


Cheers, Mike

On Tue, Jul 17, 2012 at 11:34 PM, Agustin Lobo <alobolistas at gmail.com> wrote:
> Hi!
>
> Is there a function to convert a set of ordered points (i.e. in a
> Spatial Points Data Frame)
> into an Spatial Line object?
>
> Thanks
>
> Agus
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



-- 
Michael Sumner
Hobart, Australia
e-mail: mdsumner at gmail.com



More information about the R-sig-Geo mailing list