[R-sig-Geo] Holes in polygons disappear after performing checkPolygonsHoles from maptools

Roger Bivand Roger.Bivand at nhh.no
Thu Apr 2 22:24:22 CEST 2015


On Thu, 2 Apr 2015, Roger Bivand wrote:

> On Thu, 2 Apr 2015, Edzer Pebesma wrote:
>
>> 
>>
>>  On 04/01/2015 08:03 PM, Grigory Alexandrovich wrote:
>> >  Hello all,
>> > 
>> >  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 at polygons <- lapply(shape_with_hole at 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?
>> > 
>>
>>  It looks as if it is related to rgeos, as
>>
>>  avoidGEOS = FALSE
>>  library(sp)
>>  library(maptools)
>>  data(wrld_simpl)
>>  x = wrld_simpl
>>
>>  shape_with_hole <- wrld_simpl[5,]
>>
>>  if (avoidGEOS)
>>  	gpclibPermit()
>>  shape_with_hole at polygons <- lapply(shape_with_hole at polygons,
>>  checkPolygonsHoles,
>>   avoidGEOS = avoidGEOS)
>>
>>  data(wrld_simpl)
>>  all.equal(x, wrld_simpl)
>
> No, but:
>
> library(sp)
> library(maptools)
> data(wrld_simpl)
> x = wrld_simpl
> shape_with_hole <- wrld_simpl[5,]
> pls <- slot(shape_with_hole, "polygons")
> pls1 <- lapply(pls, checkPolygonsHoles, avoidGEOS = FALSE)
> isTRUE(all.equal(pls, pls1))
> # TRUE
> gpclibPermit()
> pls1 <- lapply(pls, checkPolygonsHoles, avoidGEOS = TRUE)
> isTRUE(all.equal(pls, pls1))
> # FALSE
>
> where the Polygon objects are re-ordered and plotted out of order, so the 
> deprecated gpclib code may be broken here. However, this isn't the problem,

The gpclib code is OK, it re-orders the Polygon objects, but the plot 
order is correct. The problem is as described below.

Roger

> but rather (perhaps) graphics::polypath. If it is used (by default it is), 
> and with the default path fill mode:
>
> plot(x[5,], col="blue", usePolypath=TRUE, rule="winding")
>
> does not whiten the holes. However:
>
> plot(x[5,], col="blue", usePolypath=TRUE, rule="evenodd")
>
> does. Using:
>
> plot(x[5,], col="blue", pbg="white", usePolypath=FALSE)
>
> also works by overpainting the island with the holes in white.
>
> Please summarise the your SO thread.
>
> By the way, in R it is unusual for changes in one object to propagate in an 
> unintended way to another object, and this is not happening here. The visual 
> impression is being driven by polypath doing different things depending on 
> its rule setting. Look at ?polypath for details.
>
> Well, this doesn't clarify, but it explains what is happeing, I hope ...
>
> Roger
>
>>
>>  returns a difference, but TRUE if avoidGEOS = TRUE.
>> 
>
>

-- 
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 91 00
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list