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.

asked Jan 2, 2019 at 10:32

1 Answer 1

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.

answered Jan 2, 2019 at 11:28
1
  • This is exactly what I was looking for. Thanks a lot ¡ Commented Jan 2, 2019 at 13:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.