Skip to content

extract cell numbers from raster along a line

5 messages · Etienne Bellemare, Robert J. Hijmans

#
Etienne, 

It is not working because "cellnumbers" is currently only an argument for
extracting values from a Raster* for polygons. That's what the docs say. But
I'll also implement if for lines.  

In your example it is easy to extract cell numbers for the line
[[1]]
[1]  4  5  8  9 12 13 16 17
The values are first in row order, and then in column order.  Not in the
order of the coordinates of the line(s).

Robert

--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/extract-cell-numbers-from-raster-along-a-line-tp6185643p6186374.html
Sent from the R-sig-geo mailing list archive at Nabble.com.
#
I did notice that, but there was no ==line== section in the doc I have. Is 
my package outdated ?
No, there were no additional arguments for lines specifically. There is one
now (cellnumbers) in the R-Forge version (1.8-6).
Below I show how -- I think -- you can order the values such that they
follow the line.


library(raster)
r <- raster(matrix(1:20, nrow=4)) 
l <- SpatialLines(list(Lines(list(Line(list(x=c(0, 1), y=c(0, 1)))), ID=1))) 
plot(r, asp=1); lines(l) 

a <- extract(r, l, cellnumbers=T)[[1]] 
rc <- rowColFromCell(r, a[,1])
b <- cbind(rc, value=a[,2])

#order top to bottom
b[order(-b[,1], b[,2]), 3]
#order bottom to top
b[order(b[,1], -b[,2]), 3]
# or
rev( b[order(-b[,1], b[,2]), 3] )


--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/extract-cell-numbers-from-raster-along-a-line-tp6185643p6187936.html
Sent from the R-sig-geo mailing list archive at Nabble.com.
2 days later