Debian Codegraph @

R/chfids.R

raw
1 chFIDsSpatialLines <- function(obj, x) {
2 nl <- length(slot(obj, "lines"))
3 if (length(x) != nl) stop("lengths differ")
4 if (length(x) > length(unique(x))) stop("duplicate IDs")
5 # for (i in 1:nl) slot(slot(obj, "lines")[[i]], "ID") <- x[i]
6 lns <- slot(obj, "lines")
7 lns1 <- vector(mode="list", length=nl)
8 for (i in 1:nl) {
9 lni <- lns[[i]]
10 slot(lni, "ID") <- x[i]
11 lns1[[i]] <- lni
12 }
13 slot(obj, "lines") <- lns1
14 obj
15 }
16
17 setMethod("spChFIDs", signature(obj="SpatialLines", x="character"),
18 chFIDsSpatialLines)
19
20 chFIDsSpatialLinesDataFrame <- function(obj, x) {
21 SL <- as(obj, "SpatialLines")
22 SLx <- spChFIDs(SL, x)
23 df <- as(obj, "data.frame")
24 row.names(df) <- sapply(slot(SLx, "lines"), function(x) slot(x, "ID"))
25 SpatialLinesDataFrame(SLx, data=df)
26 }
27
28 setMethod("spChFIDs", signature(obj="SpatialLinesDataFrame", x="character"),
29 chFIDsSpatialLinesDataFrame)
30
31 chFIDsSpatialPolygons <- function(obj, x) {
32 np <- length(slot(obj, "polygons"))
33 if (length(x) != np) stop("lengths differ")
34 if (isTRUE(anyDuplicated(x))) stop("duplicate IDs")
35 # for (i in 1:np) slot(slot(obj, "polygons")[[i]], "ID") <- x[i]
36 pls <- slot(obj, "polygons")
37 pls1 <- vector(mode="list", length=np)
38 for (i in 1:np) {
39 pli <- pls[[i]]
40 slot(pli, "ID") <- x[i]
41 pls1[[i]] <- pli
42 }
43 slot(obj, "polygons") <- pls1
44 obj
45 }
46
47 setMethod("spChFIDs", signature(obj="SpatialPolygons", x="character"),
48 chFIDsSpatialPolygons)
49
50 chFIDsSpatialPolygonsDataFrame <- function(obj, x) {
51 SP <- as(obj, "SpatialPolygons")
52 SPx <- spChFIDs(SP, x)
53 df <- as(obj, "data.frame")
54 row.names(df) <- .Call(SpatialPolygons_getIDs_c, SPx)
55 SpatialPolygonsDataFrame(SPx, data=df)
56 }
57
58 setMethod("spChFIDs", signature(obj="SpatialPolygonsDataFrame", x="character"),
59 chFIDsSpatialPolygonsDataFrame)