[R-sig-Geo] extract cell numbers from raster along a line

Robert Hijmans r.hijmans at gmail.com
Sat Mar 19 19:11:58 CET 2011


> 
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.



More information about the R-sig-Geo mailing list