[GIS] Assigning values values to raster cells overlain by polygon layer using R

nodatapolygonrraster

I have a raster layer and a polygon layer overlaying the raster. The thing is that I want to make a new raster as follows: 1) the extents of the new raster should be the same as the old raster. 2) The cells which are overlain by polygons should have a value of 1 and the parts which are not overlain by polygons, should have the value of zero.

enter image description here

Best Answer

You do not specify the software you are using, so I assume it is R. Here is how you can do it:

Some example data

library(raster)
pols <- shapefile(system.file("external/lux.shp", package="raster"))[1:3, ]
r <- raster(extent(pols), res=.01)

Solution

r <- rasterize(pols, r, field=1, background=0)
plot(r)