Quantcast

Points to line

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Points to line

Agustin Lobo
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
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Points to line

Barry Rowlingson
On Tue, Jul 17, 2012 at 2:34 PM, Agustin Lobo <[hidden email]> 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?

 No, because there's no such thing as a Spatial Line object, or even a
SpatialLine object :)

There are Line objects, Lines objects, SpatialLines objects and
SpatialLinesDataFrame objects, all with different structures and
meanings.

 To convert to SpatialLines, get the coordinates and build in the
usual convoluted manner:

 > s=data.frame(x=runif(10),y=1:10,z=rnorm(10))
 > coordinates(s)=~x+y
 > L = SpatialLines(list(Lines(list(Line(coordinates(s))),"X")))
 > plot(L)


My spatial cheatsheat might help:

http://www.maths.lancs.ac.uk/~rowlings/Teaching/UseR2012/cheatsheet.html

Barry

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Points to line

Michael Sumner-2
In reply to this post by Agustin Lobo
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 <[hidden email]> 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
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



--
Michael Sumner
Hobart, Australia
e-mail: [hidden email]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Loading...