An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20110318/c6855357/attachment.pl>
extract cell numbers from raster along a line
5 messages · Etienne Bellemare, Robert J. Hijmans
I'd like to extract celle numbers from a raster using a line, but it seems the cellnumbers=TRUE option isn't working with lines in raster::extract.
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
r[] = 1:ncell(r) extract(r, l)
[[1]] [1] 4 5 8 9 12 13 16 17
I'm also not sure I get the order in which the values are returned, but it seems it is not from the begining of the line to the end or reverse.
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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20110318/a8660eda/attachment.pl>
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).
The values are first in row order, and then in column order. Not in the order of the coordinates of the line(s).
I'm trying to think of a way to order these. Do you see a simple solution ? I'm finally doing a profile from a raster using a line.
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20110321/24e13d92/attachment.pl>