Debian Codegraph @

R/rbind.R

raw
1 makeUniqueIDs <- function(lst) {
2 ids = sapply(lst, function(i) slot(i, "ID"))
3 if (any(duplicated(ids))) {
4 ids <- make.unique(as.character(unlist(ids)), sep = "")
5 for (i in seq_along(ids))
6 lst[[i]]@ID = ids[i]
7 }
8 lst
9 }
10
11 rbind.SpatialPoints <- function(...) {
12 dots = list(...)
13 names(dots) <- NULL
14 stopifnot(identicalCRS(dots))
15 dropRowNames = is.null(dimnames(dots[[1]]@coords)[[1]]) # or check each of them?
16 coordinates.strip = function(x) {
17 x = coordinates(x)
18 row.names(x) = NULL
19 x
20 }
21 ret = SpatialPoints(do.call(rbind, lapply(dots, coordinates.strip)),
22 rebuild_CRS(slot(dots[[1]], "proj4string")))
23 if (!dropRowNames)
24 row.names(ret) = make.unique(do.call(c, lapply(dots, row.names)))
25 ret
26 }
27
28 rbind.SpatialPointsDataFrame <- function(...) {
29 dots = list(...)
30 names(dots) <- NULL # bugfix Clement Calenge 100417
31 sp = do.call(rbind, lapply(dots, function(x) as(x, "SpatialPoints")))
32 df = do.call(rbind, lapply(dots, function(x) x@data))
33 SpatialPointsDataFrame(sp, df, coords.nrs = dots[[1]]@coords.nrs)
34 }
35
36
37 # contributed by Kent Johnson, r-sig-geo, Dec 5, 2015:
38 rbind.SpatialMultiPoints <- function(...) {
39 dots = list(...)
40 names(dots) <- NULL
41 stopifnot(identicalCRS(dots))
42 SpatialMultiPoints(do.call(c, lapply(dots, slot, name="coords")),
43 rebuild_CRS(slot(dots[[1]], "proj4string")))
44 }
45
46 rbind.SpatialMultiPointsDataFrame <- function(...) {
47 dots = list(...)
48 names(dots) <- NULL
49 sp = do.call(rbind, lapply(dots, function(x) as(x, "SpatialMultiPoints")))
50 df = do.call(rbind, lapply(dots, function(x) x@data))
51 SpatialMultiPointsDataFrame(sp, df)
52 }
53
54
55 rbind.SpatialPixels = function(...) {
56 dots = list(...)
57 names(dots) <- NULL
58 sp = do.call(rbind, lapply(dots, function(x) as(x, "SpatialPoints")))
59 gridded(sp) = T
60 sp
61 }
62
63 rbind.SpatialPixelsDataFrame = function(...) {
64 dots = list(...)
65 names(dots) <- NULL
66 sp = do.call(rbind, lapply(dots, function(x) as(x, "SpatialPointsDataFrame")))
67 gridded(sp) = T
68 sp
69 }
70
71 rbind.SpatialPolygons = function(..., makeUniqueIDs = FALSE) {
72 dots = list(...)
73 names(dots) <- NULL
74 stopifnot(identicalCRS(dots))
75 # checkIDSclash(dots)
76 pl = do.call(c, lapply(dots, function(x) slot(x, "polygons")))
77 if (makeUniqueIDs)
78 pl = makeUniqueIDs(pl)
79 SpatialPolygons(pl, proj4string = rebuild_CRS(slot(dots[[1]], "proj4string")))
80 }
81
82 rbind.SpatialPolygonsDataFrame <- function(..., makeUniqueIDs = TRUE) {
83 dots = list(...)
84 names(dots) <- NULL # bugfix Clement Calenge 100417
85 lst = lapply(dots, function(x) as(x, "SpatialPolygons"))
86 lst$makeUniqueIDs = makeUniqueIDs
87 pl = do.call(rbind.SpatialPolygons, lst)
88 df = do.call(rbind, lapply(dots, function(x) x@data))
89 SpatialPolygonsDataFrame(pl, df, match.ID = FALSE)
90 }
91
92
93 rbind.SpatialLines = function(..., makeUniqueIDs = TRUE) {
94 dots = list(...)
95 names(dots) <- NULL
96 stopifnot(identicalCRS(dots))
97 ll = do.call(c, lapply(dots, function(x) slot(x, "lines")))
98 if (makeUniqueIDs)
99 ll = makeUniqueIDs(ll)
100 SpatialLines(ll, proj4string = rebuild_CRS(slot(dots[[1]], "proj4string")))
101 }
102
103 rbind.SpatialLinesDataFrame <- function(...) {
104 dots = list(...)
105 names(dots) <- NULL # bugfix Clement Calenge 100417
106 ll = do.call(rbind, lapply(dots, function(x) as(x, "SpatialLines")))
107 df = do.call(rbind, lapply(dots, function(x) x@data))
108 SpatialLinesDataFrame(ll, df, match.ID = FALSE)
109 }
110
111 cbind.Spatial <- function(...) {
112 dots = list(...)
113 names(dots) <- NULL
114 stopifnot(identicalCRS(dots[ which(sapply(dots, function(x) is(x, "Spatial"))) ]))
115 dfs = lapply(dots, function(x) if(is(x, "Spatial")) x@data else x)
116 d = do.call(cbind, dfs)
117 addAttrToGeom(geometry(dots[[1]]), data.frame(d), FALSE)
118 }