[R-sig-Geo] vector to raster

Edzer J. Pebesma e.pebesma at geo.uu.nl
Thu Jul 5 13:56:05 CEST 2007


Agustin Lobo wrote:
> Dear sig-geos,
>
> is there any way of converting from vector
> (imported with rgdal from a shp) to raster
> within R?
>
> Thanks!
>
> Agus
>
>   
Yes, using the classes and methods in sp -- you'd first have to create a 
grid e.g. with spsample, sampling type regular or makegrid. Next, if you 
want to have the polygon values at grid cell centres, overlay the 
polygons with the grid, using the overlay method.

 > library(sp)
 > data(meuse.riv)
 > meuse.sr = SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)), "x")))
 >
 > plot(meuse.sr)
 > points(spsample(meuse.sr, n = 1000, "regular"), pch = 3)
 > class(meuse.sr)
[1] "SpatialPolygons"
attr(,"package")
[1] "sp"
 > pts = spsample(meuse.sr, n = 1000, "regular")
 > class(pts)
[1] "SpatialPoints"
attr(,"package")
[1] "sp"
 > gridded(pts)=T
 > class(pts)
[1] "SpatialPixels"
attr(,"package")
[1] "sp"

--
Edzer




More information about the R-sig-Geo mailing list