File: SP2owin.R

package info (click to toggle)
r-cran-maptools 1%3A0.8-30-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,896 kB
  • ctags: 230
  • sloc: ansic: 3,007; makefile: 3
file content (44 lines) | stat: -rw-r--r-- 1,414 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
.SP2owin <- function(SP) {
    require(spatstat)
    pls <- slot(SP, "polygons")
    nParts <- sapply(pls, function(x) length(slot(x, "Polygons")))
    nOwin <- sum(nParts)
    if (nOwin == 1) {
        pl <- slot(pls[[1]], "Polygons")
        crds <- slot(pl[[1]], "coords")
	colnames(crds) <- c("x", "y")
	rD <- pl[[1]]@ringDir
	if (rD == 1) crds <- crds[nrow(crds):1,]
	crds <- crds[-nrow(crds),]
	res <- owin(poly=list(x=crds[,1], y=crds[,2]))
    } else if (nOwin > 1) {
        opls <- vector(mode="list", length=nOwin)
        io <- 1
        for (i in seq(along=pls)) {
            pl <- slot(pls[[i]], "Polygons")
            for (j in 1:nParts[i]) {
                crds <- slot(pl[[j]], "coords")
	        colnames(crds) <- c("x", "y")
	        rD <- slot(pl[[j]], "ringDir") # sp:::.spFindCG(crds)$rD
		hole <- slot(pl[[j]], "hole")

	        if (rD == -1 && hole) crds <- crds[nrow(crds):1,]
                else if (rD == 1 && !hole) crds <- crds[nrow(crds):1,]

	        crds <- crds[-nrow(crds),]

                opls[[io]] <- list(x=crds[,1], y=crds[,2])
                io <- io+1
            }
        }
#	if (exists(".spatstat_check") && !.spatstat_check) 
        if (!spatstat.options("checkpolygons")) 
        	res <- owin(bbox(SP)[1,], bbox(SP)[2,], poly = opls,
			check=FALSE)
# 070718 added check avoidance
	else res <- owin(poly=opls)
    } else stop("no valid polygons")
    res
}