[R-sig-Geo] Extracting coordinates from SpatialLinesDataFrame object

Roger Bivand Roger.Bivand at nhh.no
Mon Jul 6 14:46:54 CEST 2009


On Mon, 6 Jul 2009, Agustin Lobo wrote:

> I  want to extract the coordinates of each line
> in a SpatialLinesDataFrame object as a 2D matrix (not as a list). I'm using
> as.matrix(coordinates(fl1[1,])[[1]][[1]])
> for the first line,
> which works fine but it's really odd and difficult to remember.
>
> Is there an easier way of doing it?
> Also, perhaps we could have something like
> coordinates(fl1[1,], matrix=T)
> in the future ?

No, because a SpatialLines object contains one or more Lines objects, each 
of which contains one or more Line objects. You are choosing the first 
Lines object, which may consist of multiple Line objects. So lists are the 
best way to represent them, and:

res <- lapply(slot(fl1, "lines"), function(x) lapply(slot(x, "Lines"),
   function(y) slot(y, "coords")))

gets everything out tidily. Isn't reality "really odd and difficult to 
remember"? Other users have richer Lines objects, so doing what you 
request would be simplifying too far.

Roger

>
> Thanks!
>
> Agus
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list