0

I need to create and SpatialLinesDataFrame with a predefined structure (9 fields). I'm performing a function over a list of shp which I want to append to this empty spatiallinesdataframe.

I already know how to save each shp within a folder and then merge all, but now, I'd like to merge all the sph within the iteration to avoid save and reread steps.

2

I'd use this trick, which is to create a 1-row SLDF with the right attributes, and then drop the first and only row, leaving you with nothing:

> library(sp)
> library(raster)

Here's the beef:

> s = SpatialLinesDataFrame(
  sl=SpatialLines(
   LinesList=list(
     Lines(
       list(
         Line(
           coords=matrix(c(0,1,0,1),2,2)
         )
       ),ID=1)
      )
    ),
  data=data.frame(a=1,b=2,c=3))[-1,]

Set up a one-line data frame with your attributes in the last line - here I've got three. The finale [-1,] drops the one row created, leaving you with:

> s
class       : SpatialLinesDataFrame 
features    : 0 
coord. ref. : NA 
variables   : 3
names       :    a,    b,    c 

A SLDF with no rows (features) and 3 variables.

  • This is exactly what I was looking for. Thanks a lot ¡ – César Arquero Jan 2 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.