Grigory
Grigory

Reputation: 21

Holes in polygons disappear after performing checkPolygonsHoles from maptools

Note: At Edzer Pebesma's suggestion, this question has been crossposted to R-sig-geo, here, where it has received some good responses.


I encountered the following unexpected result using checkPolygonsHoles:

# attach the worldmap as SpatialPolygonsDataFrame from the package maptools
library(sp)
library(maptools)
data(wrld_simpl)

# get a polygon with a hole
shape_with_hole <- wrld_simpl[5,]

# plot it (hole is left white, surrounded by blue color)
plot(shape_with_hole, col = "blue")

# perform checkPolygonsHoles 
shape_with_hole@polygons <- lapply(shape_with_hole@polygons,   checkPolygonsHoles)

# plot again, now holes aren't recognized as such
plot(shape_with_hole, col = "blue")

# and even the original SpatialPolygonsDataFrame object is changed !?
plot(wrld_simpl[5,], col = "blue")

One irritating side effect here is that the original object wrld_simpl is also changed. This result looks to me like a bug, or have I missed something?

P.S.: the object shape_with_hole edited with checkPolygonsHoles before, continues to behave strange:

# we check which polygons are marked as holes. The flags are still set 
# properly, although the `plot` function didn't recognize them:
sapply(shape_with_hole@polygons[[1]]@Polygons, slot, "hole")

[1] FALSE  TRUE  TRUE  TRUE

# load library rgdal for reprojection
library(rgdal)

# reproject with `spTransform`, just for testing
shape_with_hole <- spTransform(shape_with_hole, 
CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))

# after reprojection all flags are set to FALSE 
sapply(shape_with_hole@polygons[[1]]@Polygons, slot, "hole")

[1] FALSE FALSE FALSE FALSE

Upvotes: 2

Views: 366

Answers (1)

Edzer Pebesma
Edzer Pebesma

Reputation: 4121

This clear report has revealed a bug in R package sp, that became only manifest in R 3.1.x See here for more detail. sp 1.1-0, available from CRAN, has fixed this.

Upvotes: 1

Related Questions