[R-sig-Geo] rasterize SpatialLines

Alexandre Villers villers.alexandre at gmail.com
Thu Nov 28 17:28:20 CET 2013


Dear list,

I'm trying to rasterize a SpatialLines object, which represents the path 
of an individual (created from the points locations).
I would like to know the number of times he visited a given pixel.
Following the example (slightly modified) in raster::rasterize


cds1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60))
cds2 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(50,100))
cds3 <- rbind(c(-125,0), c(0,60), c(40,5), c(15,-45))

lines <- SpatialLines(list(Lines(list(Line(cds1)), "1"),
      Lines(list(Line(cds2)), "2"), Lines(list(Line(cds3)), "3") ))
	
r <- raster(ncols=90, nrows=45)
rl <- rasterize(lines, r, fun='count')
plot(rl)
lines (lines)

It's clear from the figure that a pixel that is "visited" twice by a 
SpatialLines with the same ID isn't counted twice (which I would like) 
compared to a pixel visited twice by SpatialLines of 2 different IDs.

I guess I could build a SpatialLines object with each segment bearing a 
different ID but I suspect there is a more elegant way. However I 
couldn't figure out what function to specify (though I'm pretty sure 
once I'll see the answer, it will become obvious...).

Any hint ?

Thanks in advance

Alex



More information about the R-sig-Geo mailing list