| 1 |
if (!isGeneric("recenter")) |
| 2 |
setGeneric("recenter", function(obj) |
| 3 |
standardGeneric("recenter")) |
| 4 |
|
| 5 |
recenter.SpatialPolygons <- function(obj) { |
| 6 |
proj <- is.projected(obj) |
| 7 |
if (is.na(proj)) stop("unknown coordinate reference system") |
| 8 |
if (proj) stop("cannot recenter projected coordinate reference system") |
| 9 |
projargs <- slot(obj, "proj4string") |
| 10 |
pls <- slot(obj, "polygons") |
| 11 |
Srl <- lapply(pls, recenter.Polygons) |
| 12 |
res <- SpatialPolygons(Srl, proj4string=projargs) |
| 13 |
res |
| 14 |
} |
| 15 |
|
| 16 |
setMethod("recenter", "SpatialPolygons", recenter.SpatialPolygons) |
| 17 |
|
| 18 |
recenter.Polygons <- function(obj) { |
| 19 |
ID <- slot(obj, "ID") |
| 20 |
rings <- slot(obj, "Polygons") |
| 21 |
srl <- lapply(rings, recenter.Polygon) |
| 22 |
res <- Polygons(srl, ID=ID) |
| 23 |
res |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
recenter.Polygon <- function(obj) { |
| 28 |
crds <- slot(obj, "coords") |
| 29 |
hole <- slot(obj, "hole") |
| 30 |
inout <- (crds[,1] < 0) |
| 31 |
if (all(inout)) { |
| 32 |
crds[,1] <- crds[,1]+360 |
| 33 |
} else { |
| 34 |
if (any(inout)) { |
| 35 |
crds[,1] <- ifelse(inout, crds[,1]+360, crds[,1]) |
| 36 |
} |
| 37 |
} |
| 38 |
res <- Polygon(crds, hole) |
| 39 |
res |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
recenter.SpatialLines <- function(obj) { |
| 45 |
proj <- is.projected(obj) |
| 46 |
if (is.na(proj)) stop("unknown coordinate reference system") |
| 47 |
if (proj) stop("cannot recenter projected coordinate reference system") |
| 48 |
projargs <- slot(obj, "proj4string") |
| 49 |
lns <- slot(obj, "lines") |
| 50 |
Sll <- lapply(lns, recenter.Lines) |
| 51 |
res <- SpatialLines(Sll, projargs) |
| 52 |
res |
| 53 |
} |
| 54 |
|
| 55 |
setMethod("recenter", "SpatialLines", recenter.SpatialLines) |
| 56 |
|
| 57 |
|
| 58 |
recenter.Lines <- function(obj) { |
| 59 |
ID <- slot(obj, "ID") |
| 60 |
lines <- slot(obj, "Lines") |
| 61 |
sll <- lapply(lines, recenter.Line) |
| 62 |
res <- Lines(sll, ID=ID) |
| 63 |
res |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
recenter.Line <- function(obj) { |
| 68 |
crds <- coordinates(obj) |
| 69 |
inout <- (crds[,1] < 0) |
| 70 |
if (all(inout)) { |
| 71 |
crds[,1] <- crds[,1]+360 |
| 72 |
} else { |
| 73 |
if (any(inout)) { |
| 74 |
crds[,1] <- ifelse(inout, crds[,1]+360, crds[,1]) |
| 75 |
} |
| 76 |
} |
| 77 |
res <- Line(crds) |
| 78 |
res |
| 79 |
} |
| 80 |
|
| 81 |
|