| 1 |
|
| 2 |
.shp2srs <- function(shp, nParts#, proj4string=CRS(as.character(NA)) |
| 3 |
) { |
| 4 |
Pstart <- shp$Pstart |
| 5 |
nVerts <- nrow(shp$verts) |
| 6 |
from <- integer(nParts) |
| 7 |
to <- integer(nParts) |
| 8 |
from[1] <- 1 |
| 9 |
for (j in 1:nParts) { |
| 10 |
if (j == nParts) to[j] <- nVerts |
| 11 |
else { |
| 12 |
to[j] <- Pstart[j+1] |
| 13 |
from[j+1] <- to[j]+1 |
| 14 |
} |
| 15 |
} |
| 16 |
srl <- vector(mode="list", length=nParts) |
| 17 |
for (j in 1:nParts) { |
| 18 |
srl[[j]] <- Polygon(coords=shp$verts[from[j]:to[j],]#, proj4string=proj4string |
| 19 |
) |
| 20 |
} |
| 21 |
srl |
| 22 |
} |
| 23 |
|
| 24 |
nParts.shp <- function(shp) attr(shp, "nParts") |
| 25 |
|
| 26 |
|
| 27 |
.NAmat2xyList <- function(xy) { |
| 28 |
NAs <- unclass(attr(na.omit(xy), "na.action")) |
| 29 |
if ((length(NAs) == 1) && (NAs == nrow(xy))) { |
| 30 |
xy <- xy[-nrow(xy)] |
| 31 |
NAs <- NULL |
| 32 |
} |
| 33 |
nParts <- length(NAs) + 1 |
| 34 |
res <- vector(mode="list", length=nParts) |
| 35 |
from <- integer(nParts) |
| 36 |
to <- integer(nParts) |
| 37 |
from[1] <- 1 |
| 38 |
to[nParts] <- nrow(xy) |
| 39 |
if (nParts > 1) { |
| 40 |
for (i in 2:nParts) { |
| 41 |
to[(i-1)] <- NAs[(i-1)]-1 |
| 42 |
from[i] <- NAs[(i-1)]+1 |
| 43 |
} |
| 44 |
} |
| 45 |
for (i in 1:nParts) res[[i]] <- xy[from[i]:to[i],, drop = FALSE] |
| 46 |
res |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
.ringDirxy <- function(xy) { |
| 51 |
a <- xy[,1] |
| 52 |
b <- xy[,2] |
| 53 |
nvx <- length(b) |
| 54 |
|
| 55 |
if((a[1] == a[nvx]) && (b[1] == b[nvx])) { |
| 56 |
a <- a[-nvx] |
| 57 |
b <- b[-nvx] |
| 58 |
nvx <- nvx - 1 |
| 59 |
} |
| 60 |
if (nvx < 3) return(1) |
| 61 |
|
| 62 |
tX <- 0.0 |
| 63 |
dfYMax <- max(b) |
| 64 |
ti <- 1 |
| 65 |
for (i in 1:nvx) { |
| 66 |
if (b[i] == dfYMax && a[i] > tX) ti <- i |
| 67 |
} |
| 68 |
if ( (ti > 1) & (ti < nvx) ) { |
| 69 |
dx0 = a[ti-1] - a[ti] |
| 70 |
dx1 = a[ti+1] - a[ti] |
| 71 |
dy0 = b[ti-1] - b[ti] |
| 72 |
dy1 = b[ti+1] - b[ti] |
| 73 |
} else if (ti == nvx) { |
| 74 |
dx0 = a[ti-1] - a[ti] |
| 75 |
dx1 = a[1] - a[ti] |
| 76 |
dy0 = b[ti-1] - b[ti] |
| 77 |
dy1 = b[1] - b[ti] |
| 78 |
} else { |
| 79 |
# /* if the tested vertex is at the origin then continue from 0 (1) */ |
| 80 |
dx1 = a[2] - a[1] |
| 81 |
dx0 = a[nvx] - a[1] |
| 82 |
dy1 = b[2] - b[1] |
| 83 |
dy0 = b[nvx] - b[1] |
| 84 |
} |
| 85 |
v3 = ( (dx0 * dy1) - (dx1 * dy0) ) |
| 86 |
if ( v3 > 0 ) return(as.integer(1)) |
| 87 |
else return(as.integer(-1)) |
| 88 |
} |
| 89 |
|
| 90 |
.bboxSlot <- function(x) { |
| 91 |
r1 <- range(x[,1], na.rm=TRUE) |
| 92 |
r2 <- range(x[,2], na.rm=TRUE) |
| 93 |
res <- rbind(r1, r2) |
| 94 |
dimnames(res)[[2]] <- c("min", "max") |
| 95 |
res |
| 96 |
} |
| 97 |
|
| 98 |
.bboxR4s <- function(R4s) { |
| 99 |
x <- sapply(R4s, function(x) bbox.R4(x)[1,]) |
| 100 |
y <- sapply(R4s, function(x) bbox.R4(x)[2,]) |
| 101 |
r1 <- range(x, na.rm=TRUE) |
| 102 |
r2 <- range(y, na.rm=TRUE) |
| 103 |
res <- rbind(r1, r2) |
| 104 |
dimnames(res)[[2]] <- c("min", "max") |
| 105 |
res |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
.bboxSrs <- function(R4s) { |
| 110 |
x <- sapply(R4s, function(x) bbox.R4(x)[1,]) |
| 111 |
y <- sapply(R4s, function(x) bbox.R4(x)[2,]) |
| 112 |
r1 <- range(x, na.rm=TRUE) |
| 113 |
r2 <- range(y, na.rm=TRUE) |
| 114 |
res <- rbind(r1, r2) |
| 115 |
dimnames(res)[[2]] <- c("min", "max") |
| 116 |
res |
| 117 |
|
| 118 |
} |
| 119 |
.bboxCalcR <- function(lst) { |
| 120 |
rx=NULL |
| 121 |
ry=NULL |
| 122 |
|
| 123 |
for(i in 1:length(lst)) |
| 124 |
{ |
| 125 |
x = lst[[i]] |
| 126 |
rxx=range(c(sapply(x@Polygons, |
| 127 |
function(x) range(x@coords[,1])))) |
| 128 |
ryy=range(c(sapply(x@Polygons, |
| 129 |
function(x) range(x@coords[,2])))) |
| 130 |
rx=range(c(rx,rxx)) |
| 131 |
ry=range(c(ry,ryy)) |
| 132 |
} |
| 133 |
res=rbind(r1=rx,r2=ry) |
| 134 |
dimnames(res)[[2]] <- c("min", "max") |
| 135 |
res |
| 136 |
} |
| 137 |
|
| 138 |
.bbox2SPts <- function(bb, proj4string=CRS(as.character(NA))) { |
| 139 |
x <- c(bb[1,1], bb[1,1], bb[1,2], bb[1,2]) |
| 140 |
y <- c(bb[2,1], bb[2,2], bb[2,2], bb[2,1]) |
| 141 |
SpatialPoints(cbind(x,y), proj4string=proj4string) |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
bbox.R4 <- function(x) { |
| 146 |
x@bbox |
| 147 |
} |
| 148 |
|
| 149 |
.bbox1 <- function(x) { |
| 150 |
r1 <- range(x[,1], na.rm=TRUE) |
| 151 |
r2 <- range(x[,2], na.rm=TRUE) |
| 152 |
res <- c(r1[1], r2[1], r1[2], r2[2]) |
| 153 |
res |
| 154 |
} |
| 155 |
|
| 156 |
.bbox2 <- function(x) { |
| 157 |
r1 <- range(x@coords[,1], na.rm=TRUE) |
| 158 |
r2 <- range(x@coords[,2], na.rm=TRUE) |
| 159 |
res <- c(r1[1], r2[1], r1[2], r2[2]) |
| 160 |
res |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
.saneRD <- function(rD) { |
| 165 |
if (length(rD) == 0) stop("Not a valid polygon: rD length 0") |
| 166 |
if (any(is.na(rD))) stop("Not a valid polygon: NA rD value") |
| 167 |
if (any(abs(rD) != 1)) stop("Not a valid polygon: abs(rD) != 1") |
| 168 |
invisible(NULL) |
| 169 |
} |
| 170 |
|
| 171 |
.RingCentrd_2d <- function(plmat) { |
| 172 |
nVert <- nrow(plmat) |
| 173 |
x_base <- plmat[1,1] |
| 174 |
y_base <- plmat[1,2] |
| 175 |
Cy_accum <- 0.0 |
| 176 |
Cx_accum <- 0.0 |
| 177 |
Area <- 0.0 |
| 178 |
ppx <- plmat[2,1] - x_base |
| 179 |
ppy <- plmat[2,2] - y_base |
| 180 |
for (iv in 2:(nVert-1)) { |
| 181 |
x = plmat[iv,1] - x_base |
| 182 |
y = plmat[iv,2] - y_base |
| 183 |
dx_Area <- ((x * ppy) - (y * ppx)) * 0.5 |
| 184 |
Area <- Area + dx_Area |
| 185 |
Cx_accum <- Cx_accum + ( ppx + x ) * dx_Area |
| 186 |
Cy_accum <- Cy_accum + ( ppy + y ) * dx_Area |
| 187 |
ppx <- x |
| 188 |
ppy <- y |
| 189 |
} |
| 190 |
xc <- (Cx_accum / (Area * 3)) + x_base |
| 191 |
yc <- (Cy_accum / (Area * 3)) + y_base |
| 192 |
list(xc=xc, yc=yc, area=abs(Area)) |
| 193 |
} |
| 194 |
|
| 195 |
.spFindCG <- function(xy) { |
| 196 |
a <- xy[,1] |
| 197 |
b <- xy[,2] |
| 198 |
storage.mode(a) <- "double" |
| 199 |
storage.mode(b) <- "double" |
| 200 |
n <- nrow(xy) |
| 201 |
res <- .C("spRFindCG", as.integer(n), a, b, |
| 202 |
as.double(0), as.double(0), as.double(0), PACKAGE="sp") |
| 203 |
|
| 204 |
cents <- c(res[[4]], res[[5]]) |
| 205 |
area <- abs(res[[6]]) |
| 206 |
rD <- ifelse(res[[6]] > 0, as.integer(-1), as.integer(1)) |
| 207 |
list(area=area, cents=cents, rD=rD) |
| 208 |
} |