Skip to content
Prev 7521 / 29559 Next

Overlay spatial lines on raster

Hi Agus, thank you. You were right about the cause of the problem ---
I patched it in version 0.9.9-5, now on R-Forge (and should be
available for automatic install within 24 hours); the function could
use some optimization for speed but at least it works now for
SpatialLines* with a larger extent then the target RasterLayer.

# simple example
library(raster)
library(maptools)
data(wrld_simpl)
r = raster(xmn=-10, xmx=20, ymn=37, ymx=43, ncol=360, nrow=72)
r = linesToRaster(wrld_simpl, r, progress='text')
plot(r)
plot(wrld_simpl, add=T)


# example using zonal as I proposed in your original question, but
here with polygonsToRaster
# What is (roughly) the mean latitude of each country?

x = raster()
# res(x) = 0.1    # higher res, slower, but more small countries are included
x = polygonsToRaster(wrld_simpl, x, progress='text')
y = raster(x)
y[] = rep(yFromRow(y, 1:nrow(y)), each=ncol(y))
z = zonal(y, x, mean)
res= data.frame(wrld_simpl[z[,'zone'], 'NAME'], lat=z[,2])
res[order(res[,2]), ]

Robert
On Sat, Jan 30, 2010 at 5:03 AM, Agustin Lobo <alobolistas at gmail.com> wrote: