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.
1 Answer 1
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 Cabral– César Arquero Cabral2019年01月02日 13:17:19 +00:00Commented Jan 2, 2019 at 13:17