1\name{checkPolygonsHoles}
2\alias{checkPolygonsHoles}
3\alias{rgeosStatus}
4\alias{gpclibPermit}
5\alias{gpclibPermitStatus}
6%- Also NEED an '\alias' for EACH other topic documented here.
7\title{Check holes in Polygons objects}
8\description{
9  The function checks holes in Polygons objects. Use of the rgeos package functions is prefered, and if rgeos is available, they will be used automatically. In this case, member Polygon objects are checked against each other for containment, and the returned Polygons object has component hole slots set appropriately. In addition, the output Polygons object may be provided with a comment string, encoding the external and internal rings. For gpclib use, see details below.
10}
11\usage{
12checkPolygonsHoles(x, properly=TRUE, avoidGEOS=FALSE, useSTRtree=FALSE)
13gpclibPermitStatus()
14gpclibPermit()
15rgeosStatus()
16}
17%- maybe also 'usage' for other objects documented here.
18\arguments{
19  \item{x}{An Polygons object as defined in package sp}
20  \item{properly}{default TRUE, use \code{\link[rgeos]{gContainsProperly}} rather than \code{\link[rgeos]{gContains}}}
21  \item{avoidGEOS}{default FALSE; if TRUE force use of \pkg{gpclib} even when \pkg{rgeos} is available}
22  \item{useSTRtree}{default FALSE, if TRUE, use \pkg{rgeos} STRtree in checking holes, which is much faster, but uses a lot of memory and does not release it on completion (work in progress)}
23}
24\details{
25If the gpclib package is used, an intersection between a gpc.poly object with one or more polygon contours and its bounding box is used to set the hole flag. The function will set single polygon contours to hole=FALSE, and if multiple polygon contours are holes, will set them TRUE. The \code{gpclibPermit} function is used to choose to permit the use of gpclib if installed, and \code{gpclibPermitStatus} reports its status. The licence for gpclib is not Free or Open Source and explicitly forbids commercial use. See \code{library(help=gpclib)}.}
26
27\value{
28  An Polygons object re-created from the input object.
29}
30
31\author{Roger Bivand}
32%\seealso{\code{\link[rgeos]{createPolygonsComment}}, \code{\link[rgeos]{gIsValid}}, \code{\link[rgeos]{gEquals}}, \code{\link[rgeos]{gContainsProperly}}}
33\examples{
34if (rgeosStatus()) {
35nc1 <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1],
36 proj4string=CRS("+proj=longlat +ellps=clrk66"))
37pl <- slot(nc1, "polygons")
38sapply(slot(pl[[4]], "Polygons"), function(x) slot(x, "hole"))
39pl[[4]] <- Polygons(list(slot(pl[[4]], "Polygons")[[1]],
40 Polygon(slot(slot(pl[[4]], "Polygons")[[2]], "coords"), hole=TRUE),
41 slot(pl[[4]], "Polygons")[[3]]), slot(pl[[4]], "ID"))
42sapply(slot(pl[[4]], "Polygons"), function(x) slot(x, "hole"))
43pl_new <- lapply(pl, checkPolygonsHoles)
44sapply(slot(pl_new[[4]], "Polygons"), function(x) slot(x, "hole"))
45srs <- slot(slot(pl[[1]], "Polygons")[[1]], "coords")
46hle2 <- structure(c(-81.64093, -81.38380, -81.34165, -81.66833, -81.64093,
47 36.57865, 36.57234, 36.47603, 36.47894, 36.57865), .Dim = as.integer(c(5, 2)))
48hle3 <- structure(c(-81.47759, -81.39118, -81.38486, -81.46705, -81.47759,
49 36.56289, 36.55659, 36.49907, 36.50380, 36.56289), .Dim = as.integer(c(5, 2)))
50x <- Polygons(list(Polygon(srs), Polygon(hle2), Polygon(hle3)),
51 ID=slot(pl[[1]], "ID"))
52sapply(slot(x, "Polygons"), function(x) slot(x, "hole"))
53res <- checkPolygonsHoles(x)
54sapply(slot(res, "Polygons"), function(x) slot(x, "hole"))
55\dontrun{
56opar <- par(mfrow=c(1,2))
57SPx <- SpatialPolygons(list(x))
58plot(SPx)
59text(t(sapply(slot(x, "Polygons"), function(i) slot(i, "labpt"))),
60 labels=sapply(slot(x, "Polygons"), function(i) slot(i, "hole")), cex=0.6)
61title(xlab="Hole slot values before checking")
62SPres <- SpatialPolygons(list(res))
63plot(SPres)
64text(t(sapply(slot(res, "Polygons"), function(i) slot(i, "labpt"))),
65 labels=sapply(slot(res, "Polygons"), function(i) slot(i, "hole")), cex=0.6)
66title(xlab="Hole slot values after checking")
67par(opar)
68p1 <- Polygon(cbind(x=c(0, 0, 10, 10, 0), y=c(0, 10, 10, 0, 0))) # I
69p2 <- Polygon(cbind(x=c(3, 3, 7, 7, 3), y=c(3, 7, 7, 3, 3))) # H
70p8 <- Polygon(cbind(x=c(1, 1, 2, 2, 1), y=c(1, 2, 2, 1, 1))) # H
71p9 <- Polygon(cbind(x=c(1, 1, 2, 2, 1), y=c(5, 6, 6, 5, 5))) # H
72p3 <- Polygon(cbind(x=c(20, 20, 30, 30, 20), y=c(20, 30, 30, 20, 20))) # I
73p4 <- Polygon(cbind(x=c(21, 21, 29, 29, 21), y=c(21, 29, 29, 21, 21))) # H
74p14 <- Polygon(cbind(x=c(21, 21, 29, 29, 21), y=c(21, 29, 29, 21, 21))) # H
75p5 <- Polygon(cbind(x=c(22, 22, 28, 28, 22), y=c(22, 28, 28, 22, 22))) # I
76p15 <- Polygon(cbind(x=c(22, 22, 28, 28, 22), y=c(22, 28, 28, 22, 22))) # I
77p6 <- Polygon(cbind(x=c(23, 23, 27, 27, 23), y=c(23, 27, 27, 23, 23))) # H
78p7 <- Polygon(cbind(x=c(13, 13, 17, 17, 13), y=c(13, 17, 17, 13, 13))) # I
79p10 <- Polygon(cbind(x=c(24, 24, 26, 26, 24), y=c(24, 26, 26, 24, 24))) # I
80p11 <- Polygon(cbind(x=c(24.25, 24.25, 25.75, 25.75, 24.25),
81 y=c(24.25, 25.75, 25.75, 24.25, 24.25))) # H
82p12 <- Polygon(cbind(x=c(24.5, 24.5, 25.5, 25.5, 24.5),
83 y=c(24.5, 25.5, 25.5, 24.5, 24.5))) # I
84p13 <- Polygon(cbind(x=c(24.75, 24.75, 25.25, 25.25, 24.75),
85 y=c(24.75, 25.25, 25.25, 24.75, 24.75))) # H
86lp <- list(p1, p2, p13, p7, p6, p5, p4, p3, p8, p11, p12, p9, p10, p14, p15)
87#           1   2    3   4   5   6   7   8   9   10   11  12   13   14   15
88#           0   1   11   0   6   0   8   0   1   13    0   1    0  (7)  (6)
89#           I   H    H   I   H   I   H   I   H    H    I   H    I   ?    ?
90pls <- Polygons(lp, ID="1")
91comment(pls)
92pls1 <- checkPolygonsHoles(pls)
93comment(pls1)
94opar <- par(mfrow=c(1,2))
95plot(SpatialPolygons(list(pls)), col="magenta", pbg="cyan", usePolypath=FALSE)
96title(xlab="Hole slot values before checking")
97plot(SpatialPolygons(list(pls1)), col="magenta", pbg="cyan", usePolypath=FALSE)
98title(xlab="Hole slot values after checking")
99par(opar)
100}
101}
102}
103\keyword{spatial}
104
105