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 ? Thanks! Agus
Extracting coordinates from SpatialLinesDataFrame object
2 messages · Agustin Lobo, Roger Bivand
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