Skip to content
Prev 7477 / 29559 Next

coordinates() of SpatialLines DF

On Wed, 27 Jan 2010, Agustin Lobo wrote:

            
Because "usually" you do get a SpatialPointsDataFrame from coordinates<- 
(set) on a data.frame. coordinates (get) is a matrix for SpatialPoints* 
and the classes that inherit (SpatialPixels*, SpatialGrids* - cell 
centres), and for SpatialPolygons* - label points.

See for the extraction method:

getMethod("coordinates", "Line")
getMethod("coordinates", "Lines")
getMethod("coordinates", "SpatialLines")

to see what is happening in this case - you get a list of lists of 
coordinate matrices. Using the Norwegian coastline from maptools:

library(maptools)
library(maps)
nor_coast_lines <- map("world", interior=FALSE, plot=FALSE, xlim=c(4,32),
   ylim=c(58,72))
nor_coast_lines <- pruneMap(nor_coast_lines, xlim=c(4,32), ylim=c(58,72))
nor_coast_lines_sp <- map2SpatialLines(nor_coast_lines,
   proj4string=CRS("+proj=longlat +datum=wgs84"))
plot(nor_coast_lines_sp, axes=TRUE)
crdl0 <- coordinates(nor_coast_lines_sp)
crdl1 <- sapply(crdl0, function(x) do.call("rbind", x))
crdl2 <- do.call("rbind", crdl1)
lines(crdl2[chull(crdl2),], type="l", col="red")

using do.call() of "rbind" to stack the coordinate matrices.

Roger