In R I have a SpatialLinesDataFrame (for example from a coastline shapefile) and I want to convert it to a SpatialPointsDataFrame. I want to keep all the vertices from the line shapefile to become point shapes in the point shapefile. I also need each point to have the attributes from the original line. For example if the SpatialLinesDataFrame has a line with 5 points and a "name" attribute, then all 5 new points should retain the attribute value.
I figured out some R code to convert the geometry part:
library(rworldmap)
library(sp)
data(coastsCoarse)
lineshape = coastsCoarse
point_coordinates = c()
i = 1
nLines = nrow(lineshape)
for (i in 1: nLines) {
line1 <- lineshape[i,]@lines[[1]]@Lines[[1]]
line1coords <- line1@coords
point_coordinates = rbind(point_coordinates, line1coords)
}
pointshape <- data.frame(x=point_coordinates[,1], y=point_coordinates[,2])
coordinates(pointshape) <- ~x+y
#test result
plot(lineshape)
points(pointshape)
#now how do I transfer the attributes from the lineshape to the pointshape?
I'm stuck on how to transfer the attributes from the SpatialLinesDataFrame to the SpatialPointsDataFrame. I want my script to be reusable for different kinds of SpatialLinesDataFrame variables and so I don't know beforehand the number, names and data types of the attributes.
How can I transfer the attributes?