[R-sig-Geo] raster directions to vectorial lines (Stream to Feature)
Gavan McGrath
gavan.mcgrath at uwa.edu.au
Fri Mar 22 05:31:14 CET 2013
Hi Mauricio,
Below is a simple pair of functions for plotting a flow network over an image which should achieve what you were after. You may want to play around with how your flow direction numbers 1 -9 match up with those in the lookupfidr function. Its a bit cluncky but gets the job done.
The function fdirPlot takes as input a matrix imData the background image, and a matrix fdirs of flow direction integers.
Good luck.
lookupfdir <-function(fdir) {
switch(paste(fdir,"",sep=""),
'1' = c(-1,-1),
'2' = c(-1,0),
'3' = c(-1,1),
'4' = c(0,1),
'5' = c(1,1),
'6' = c(1,0),
'7' = c(1,-1),
'8' = c(0,-1),
'9' = c(0,0),
c(-2,-2)
)
}
fdirPlot <- function(imData,fdirs) {
dimDat<- dim(imData)
image(imData,axes=FALSE)
for (i in 1:dimDat[1]) {
for (j in 1:dimDat[2]) {
dxdy <- lookupfdir(fdirs[i,j])
if (dxdy[1] != -2) {
lines(rbind(c(i-0.5,j-0.5)/dimDat[1],c(i-dxdy[1]-0.5,j-dxdy[2]-0.5)/dimDat[1]))
} else {
if (i==1) {
lines(rbind(c(i-0.5,j-0.5)/dimDat[1],c(i-1.5,j-0.5)/dimDat[1]))
} else {
if (i==dimDat[1]) {
lines(rbind(c(i-0.5,j-0.5)/dimDat[1],c(i+1-0.5,j-0.5)/dimDat[1]))
} else {
if (j==1) {
lines(rbind(c(i-0.5,j-0.5)/dimDat[1],c(i-0.5,j-1-0.5)/dimDat[1]))
} else {
if (j==dimDat[2]) {
lines(rbind(c(i-0.5,j-0.5)/dimDat[1],c(i-0.5,j+1-0.5)/dimDat[1]))
}
}
}
}
}
}
}
}
Gavan McGrath
More information about the R-sig-Geo
mailing list