geotheory
geotheory

Reputation: 23680

Extracting raster profile in R gives warning: "number of columns of result is not a multiple of vector length"

I'm having an issue extracting the values of a raster along a line. The code below illustrates. Very grateful for assistance.

require(raster)
require(sp)

r = raster(volcano)
extent(r) = c(-2,1,50,54)
crs(r) = CRS('+proj=longlat +datum=WGS84')
plot(r, asp=T)

pts = data.frame(name=c('A','B','C','D'), lon=c(-1.5,-0.5,0,0.5), lat=c(51,52,53,53.8))
coordinates(pts) = ~ lon + lat
plot(pts, add=T)

path = list()
for(i in 2:4) path[[i-1]] = Line(pts[(i-1):i,])
path = SpatialLines(list(Lines(path, ID = 'id')), proj4string=CRS('+proj=longlat +datum=WGS84'))
plot(path, add=T)

produces..

enter image description here

And using extract() to get the values along the line:

topo_profile = extract(x=r, y=path, along=TRUE)[[1]]
plot(topo_profile, type='l')

Gives the following warnings:

Warning messages:
1: In rbind(vv, v) :
  number of columns of result is not a multiple of vector length (arg 1)
2: In rbind(vv, v) :
  number of columns of result is not a multiple of vector length (arg 1)

and produces what looks like a profile of just one segment of the polyline..

enter image description here

Any idea why extract isn't working as intended here?

Upvotes: 1

Views: 486

Answers (1)

geotheory
geotheory

Reputation: 23680

Solved - my line creation was a little unconventional. This version of path works:

path = SpatialLines(list(Lines(Line(pts[,2:3]),'id')))

enter image description here

Upvotes: 1

Related Questions