Similarly to this post
Rasterize (R package raster) fails to rasterize island polygons?
the rasterize
function in the raster
package is giving me problems with enclosed polygons. In my case, some enclosed polygons are rasterized with cell values equal to the enclosing polygons, as they were somehow 'merged'.
Here's my sample code
require(sp)
require(rgdal)
require(rgeos)
require(raster)
## set wd
wd <- 'M:/foo'
setwd(wd)
## open shapefile
sample.poly <- readOGR(dsn=wd, layer='sample')
## rasterize
resol <- 10
r <- raster(res=resol, ext=extent(sample.poly))
sample.r <- rasterize(sample.poly, r, 'Code')
## export files
writeRaster(x=sample.r, filename='sample.tif', format='GTiff')
writeOGR(obj=sample.poly, dsn=wd, layer='sample', driver='ESRI Shapefile')
Here's the structure of table of attributes
str(sample.poly@data)
'data.frame': 11 obs. of 2 variables:
$ OBJECTID: num 6249 14593 14614 15434 15683 ...
$ Code : int 33200 32110 33300 32410 32210 31210 33300 32110 32210 31210 ..
data$Code
are CORINE LCM-like codes. I already tried to set them as characters instead of integers, but the same error occurred.
Here's how the sample shapefile looks like (in QGIS)
and here's the result of rasterize
As you can see, the enclosed polygon marked with yellow arrow is properly rasterized, while the one marked with the red arrow is 'dissolved' in the enclosing polygon.
Any suggestion? Is it a bug? This is just a sample taken from a larger shapefile, with dozens of such errors.