[R-sig-Geo] Vector to raster "zone" conversion...

Robert J. Hijmans r.hijmans at gmail.com
Thu Feb 4 19:01:35 CET 2010


Hi Jonathan,

Here is an example of how one could do this with 'raster':

install.packages("packagename",repos="http://R-Forge.R-project.org")
library(raster)
# Let p be a SpatialPolygons* object, e.g.
p = getData('GADM', country='BEL', level=1)
# create a RasterLayer with the same extent
r = raster(extent(p))
# set the resolution (cell size)
res(r) = 0.05
# rasterize polygons
r = polygonsToRaster(p, r)
# extract xy for zone (polygon) # 3
xy = rasterToPoints(r, fun=function(x){x==3}, asSpatialPoints=TRUE)
#plot
plot(r)
plot(p, add=T)
plot(xy, add=T, col='blue', cex=0.2)

holes are supported (if properly specified in the Sp object); and
there is also an option to look at the fraction of a cell covered by a
polygon (but then you need to process one polygon at a time).
See ?polygonsToRaster.

Robert


On Wed, Feb 3, 2010 at 6:38 PM, Jonathan Greenberg
<greenberg at ucdavis.edu> wrote:
> Rsiggeo'ers:
>
>   Given a vector file containing polygons and a raster file of the same
> projection, I would like to create raster "zones" based on the polygons --
> e.g. I would like to rasterize the vector polygon at the resolution of the
> raster.  I'm not seeing any obvious way to do this -- any ideas?  In the
> end, I want a set of x,y coordinates of all pixels which fall within a given
> polygon -- ideally I'd like donuts to be properly rasterized, and some level
> of control on how much of a pixel must fall within a polygon to "count".
>  I'm trying to port my program "starspan" over to R to provide a much needed
> training/test data import capability to complement rgdal and raster's
> capabilities.
>   Thanks!
>
> --j
>
> --
>
> Jonathan A. Greenberg, PhD
> Postdoctoral Scholar
> Center for Spatial Technologies and Remote Sensing (CSTARS)
> University of California, Davis
> One Shields Avenue
> The Barn, Room 250N
> Davis, CA 95616
> Phone: 415-763-5476
> AIM: jgrn307, MSN: jgrn307 at hotmail.com, Gchat: jgrn307
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



More information about the R-sig-Geo mailing list