File: recenter.R

package info (click to toggle)
r-cran-sp 1%3A1.4-5-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,328 kB
  • sloc: ansic: 1,108; sh: 14; makefile: 2
file content (81 lines) | stat: -rw-r--r-- 1,816 bytes parent folder | download | duplicates (3)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
if (!isGeneric("recenter"))
	setGeneric("recenter", function(obj)
		standardGeneric("recenter"))

recenter.SpatialPolygons <- function(obj) {
	proj <- is.projected(obj)
	if (is.na(proj)) stop("unknown coordinate reference system")
	if (proj) stop("cannot recenter projected coordinate reference system")
	projargs <- slot(obj, "proj4string")
	pls <- slot(obj, "polygons")
	Srl <- lapply(pls, recenter.Polygons)
	res <- SpatialPolygons(Srl, proj4string=projargs)
	res
}

setMethod("recenter", "SpatialPolygons", recenter.SpatialPolygons)

recenter.Polygons <- function(obj) {
	ID <- slot(obj, "ID")
	rings <- slot(obj, "Polygons")
	srl <- lapply(rings, recenter.Polygon)
	res <- Polygons(srl, ID=ID)
	res
}


recenter.Polygon <- function(obj) {
	crds <- slot(obj, "coords")
	hole <- slot(obj, "hole")
	inout <- (crds[,1] < 0)
	if (all(inout)) {
		crds[,1] <- crds[,1]+360
	} else {
		if (any(inout)) {
			crds[,1] <- ifelse(inout, crds[,1]+360, crds[,1])
		}
	}
	res <- Polygon(crds, hole)
	res
}



recenter.SpatialLines <- function(obj) {
	proj <- is.projected(obj)
	if (is.na(proj)) stop("unknown coordinate reference system")
	if (proj) stop("cannot recenter projected coordinate reference system")
	projargs <- slot(obj, "proj4string")
	lns <- slot(obj, "lines")
	Sll <- lapply(lns, recenter.Lines)
	res <- SpatialLines(Sll, projargs)
	res
}

setMethod("recenter", "SpatialLines", recenter.SpatialLines)


recenter.Lines <- function(obj) {
	ID <- slot(obj, "ID")
	lines <- slot(obj, "Lines")
	sll <- lapply(lines, recenter.Line)
	res <- Lines(sll, ID=ID)
	res
}


recenter.Line <- function(obj) {
	crds <- coordinates(obj)
	inout <- (crds[,1] < 0)
	if (all(inout)) {
		crds[,1] <- crds[,1]+360
	} else {
		if (any(inout)) {
			crds[,1] <- ifelse(inout, crds[,1]+360, crds[,1])
		}
	}
	res <- Line(crds)
	res
}