TravisGallo

Having trouble with rbind() and SpatialLinesDataFrame

Discussion created by TravisGallo on Aug 5, 2016

Hi all,

I am using R for some spatial data management and have the following problem:

 

I am getting the following error when trying to rbind several SpatialLinesDataFrames. I have been having a lot of trouble with ID's, in general, and have created a few workarounds further up in the code, but I am stuck here. Any info would be great!

 

Error in SpatialLinesDataFrame(ll, df) :

  row.names of data and Lines IDs do not match

 

Sample code up to the point of throwing the error:

library(sp)

library(maptools)

library(raster)

library(rgdal)

library(rgeos)

 

sites=cbind(rnorm(10), rnorm(10))

plot(sites)

points=SpatialPoints(sites) #make points spatial

plot(points)

 

#create dataframe to store SLDF

data=data.frame(1)

colnames(data)="ID"

#creates many SpatialLine objects and stores at list

all.lines=list()

for (i in 1:length(points)){

  l1 <- apply(points[-i]@coords, 1, function(x) Line(rbind(x, points[i]@coords)))

  l1 <- lapply(l1, FUN = function(x) {SpatialLines(LinesList = list(Lines(x, ID = paste( "ID", rnorm(1), sep=""))))})

  l1=lapply(l1, FUN=function(x) {SpatialLinesDataFrame(x, data, match.ID=FALSE)})

  all.lines <- c(all.lines, l1)

}

 

site.lines=all.lines[1:(length(all.lines)/2)] #grab only one path between

 

plot(points) #create plot space with points

#loop through to plot all SpatialLinesDF

for (i in 1:length(site.lines)) {

plot(site.lines[[i]], add=TRUE)

}

 

do.call("rbind",site.lines) #need to figure out how to get unique ID's for each object

 

Thanks,


Travis Gallo

Postdoctoral Researcher

Lincoln Park Zoo - Urban Wildlife Institute

Outcomes