Klausos Klausos
Klausos Klausos

Reputation: 16050

How to create SpatialLine object

I am using sp package to create SpatialLines object and save it in the list of objects allLines. Later on I will need to compare SpatialLines to each other, but this goes beyond the current question.

So far I only need to construct SpatialLines objects. This is the last code based on the answer of hrbrmstr:

library(sp)
allLines <- NULL
x <- c(1,5,4,8)
y <- c(1,3,4,7)
xy <- cbind(x,y)
xy.sp = sp::SpatialPoints(xy)
spl <- SpatialLines(list(Lines(Line(xy.sp), ID="a")))
allLines <- rbind(allLines,spl)

Error message:

Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘proj4string’ for signature ‘"NULL"’

How to solve this issue?

Upvotes: 2

Views: 6775

Answers (3)

geneorama
geneorama

Reputation: 3720

If you came to this question to find out how to make a group of lines (as implied by the name of the function, SpatialLines) you can find examples in the sp library, filed under "SpatialLines-class".

I found their example a little strange, so I edited it to make more sense for how I normally see the data.

## Make some line segments from points 
## Note, l1a and l1b are a group of two lines
l1a <- rbind(c(1, 3), c(2,2) ,c(3,2))
l1b <- l1a + .05
l2 <- rbind(c(1,1), c(2,1.5), c(3,1))

## At this point it's just a matrix, and you can plot the points
plot(l1a, type="l", xlim=c(1,3.25), ylim=c(2,3.25), xlab="", ylab="")
lines(l1b)

line plot

## Make convert the matrix objects to line objects
Sl1a <- Line(l1a)
Sl1b <- Line(l1b)
Sl2 <- Line(l2)

## Group the individual lines into "lines"
S1 <- Lines(list(Sl1a, Sl1b), ID="a")
S2 <- Lines(list(Sl2), ID="b")

## Now combine the line groups into a "spatial line object"
Sl <- SpatialLines(list(S1,S2))

## Plot the group, then (for illustration) add each line 
## separately with color to illustrate the groups
plot(Sl)
plot(SpatialLines(list(S1)), add=T, col="red")
plot(SpatialLines(list(S2)), add=T, col="blue")

## Examine the properties
summary(Sl)
plot(Sl, col = c("red", "blue"))

Both spatial line plots look like this: enter image description here

Note the matrix object has named rows in the example. I don't see any benefit to doing this, and it's confusing because the names overlap but do not correspond with the IDs given.

Upvotes: 1

Edzer Pebesma
Edzer Pebesma

Reputation: 4121

Getting back to your last question, try

library(sp)
as(xy.spdf, "SpatialLines")

or, to create a Lines object (which may not be what you want),

as(xy.spdf, "SpatialLines")@lines[[1]]

Upvotes: 1

hrbrmstr
hrbrmstr

Reputation: 78792

Is:

library(sp)

x <- c(1,5,4,8)
y <- c(1,3,4,7)
SpatialLines(list(Lines(Line(cbind(x,y)), ID="a")))

## An object of class "SpatialLines"
## Slot "lines":
## [[1]]
## An object of class "Lines"
## Slot "Lines":
## [[1]]
## An object of class "Line"
## Slot "coords":
##      x y
## [1,] 1 1
## [2,] 5 3
## [3,] 4 4
## [4,] 8 7
## 
## 
## 
## Slot "ID":
## [1] "a"
## 
## 
## 
## Slot "bbox":
##   min max
## x   1   8
## y   1   7
## 
## Slot "proj4string":
## CRS arguments: NA

what you're looking for?

Upvotes: 10

Related Questions