1

I am trying to create a spatial line for multiple individuals in my dataset to then extract raster values across the line.

I have tried a few things but unfortunately the code I am using is assigning all the line to one individual.

WD is the dataset which has 37 individuals in it. x and y are the coordinates.

line_obj <- sp::Line(cbind(WDm2$x,WDm2$y))

lines_obj <- sp::Lines(list(line_obj),ID=WDm2$DogName)
Error in sp::Lines(list(line_obj), ID = WDm2$DogName) : 
  Single ID required


firstLine <- sp::SpatialLines(list(lines_obj))

If anyone knows how to do this, I would appreciate the help. thanks!

I would like to be able to extract raster values along the lines.

  • Welcome! In order to make your example reproducible for others, would you please provide some data used? You can e.g. make use of `head(WDm2) |> dput()` and edit your question with this additional information. – dimfalk Jan 06 '23 at 12:39

1 Answers1

0

Some example data

x1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60))
x2 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55))
x3 <- rbind(c(-125,0), c(0,60), c(40,5), c(15,-45))

Use raster::spLines

library(raster)
a <- spLines(x1, x2, x3, crs="+proj=longlat")
a
#class       : SpatialLines 
#features    : 3 
#extent      : -180, 160, -60, 60  (xmin, xmax, ymin, ymax)
#crs         : +proj=longlat 
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63