[R-sig-Geo] Only holes in the first polygon rasterized

Van Hezewijk, Brian (NRCan/RNCan) brian.vanhezewijk at canada.ca
Mon Feb 8 23:54:49 CET 2016


I am trying to rasterize() a shapefile using the raster package and am encountering erratic behaviour.  I think I've tracked it down to rasterize only identifying holes in the first Polygons in a SpatialPolygons object.  The following modification of the example code from rasterize reproduces the behaviour.  Am I missing something here or is it possibly a bug?

Thanks, Brian.  (using R 3.1.2, raster 2.5-2, Windows 7)


#first the original example code

p1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60), c(-180,-20))
hole <- rbind(c(-150,-20), c(-100,-10), c(-110,20), c(-150,-20))
p1 <- list(p1, hole)
p2 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(-10,0))
p3 <- rbind(c(-125,0), c(0,60), c(40,5), c(15,-45), c(-125,0))

pols <- spPolygons(p1, p2, p3)

r <- raster(ncol=90, nrow=45)
r <- rasterize(pols, r, fun=sum)

## Not run:

plot(r)
plot(pols, add=T)


## Now add a hole to p2

hole2 <- rbind(c(100,0), c(120,20), c(90,30), c(100,0))
p2 <- list(p2, hole2)

pols <- spPolygons(p1, p2, p3)

r <- raster(ncol=90, nrow=45)
r <- rasterize(pols, r, fun=sum)


## Not run:

plot(r)                  #rasterize doesn't recognize the hole in p2
plot(pols, add=T)


## Make p2 the first polygon with a hole

pols <- spPolygons(p2, p1, p3)

r <- raster(ncol=90, nrow=45)
r <- rasterize(pols, r, fun=sum)


## Not run:

plot(r)                  #Now the hole in p2 is good but p1 doesn't work
plot(pols, add=T)




Dr. Brian Van Hezewijk
Research Scientist - Insect Ecology
Canadian Forest Service / Service canadien des forêts
Natural Resources Canada / Ressources naturelles Canada
506 West Burnside Road / 506 chemin Burnside ouest
Victoria, BC V8Z 1M5 / Victoria, (C.-B.) V8Z 1M5
Tel./Télé. (250) 298-2373
Fax/Télécopieur (250) 363-6004
Email/Courriel:  Brian.vanhezewijk at canada.ca




	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list