| 1 |
SpatialLinesDataFrame = function(sl, data, match.ID = TRUE) { |
| 2 |
if (is.character(match.ID)) { |
| 3 |
row.names(data) = data[, match.ID[1]] |
| 4 |
match.ID = TRUE |
| 5 |
} |
| 6 |
if (match.ID) { |
| 7 |
Sl_IDs <- sapply(slot(sl, "lines"), function(x) slot(x, "ID")) |
| 8 |
data_IDs <- row.names(data) |
| 9 |
mtch <- match(Sl_IDs, data_IDs) |
| 10 |
if (any(is.na(mtch))) |
| 11 |
stop("row.names of data and Lines IDs do not match") |
| 12 |
if (length(unique(mtch)) != length(Sl_IDs)) |
| 13 |
stop("row.names of data and Lines IDs do not match") |
| 14 |
data <- data[mtch, , drop=FALSE] |
| 15 |
} |
| 16 |
if (nrow(data) != length(sl@lines)) |
| 17 |
stop("length of data.frame does not match number of Lines elements") |
| 18 |
new("SpatialLinesDataFrame", sl, data = data) |
| 19 |
} |
| 20 |
|
| 21 |
names.SpatialLinesDataFrame = function(x) names(x@data) |
| 22 |
"names<-.SpatialLinesDataFrame" = function(x,value) { checkNames(value); names(x@data)<-value; x } |
| 23 |
|
| 24 |
as.data.frame.SpatialLinesDataFrame = function(x, row.names, optional, ...) x@data |
| 25 |
|
| 26 |
setMethod("addAttrToGeom", signature(x = "SpatialLines", y = "data.frame"), |
| 27 |
function(x, y, match.ID, ...) |
| 28 |
SpatialLinesDataFrame(x, y, match.ID = match.ID, ...) |
| 29 |
) |
| 30 |
|
| 31 |
setAs("SpatialLinesDataFrame", "SpatialMultiPointsDataFrame", |
| 32 |
function(from) |
| 33 |
SpatialMultiPointsDataFrame(as(geometry(from), "SpatialMultiPoints"), from@data) |
| 34 |
) |
| 35 |
setAs("SpatialLinesDataFrame", "data.frame", function(from) |
| 36 |
as.data.frame.SpatialLinesDataFrame(from)) |
| 37 |
|
| 38 |
row.names.SpatialLinesDataFrame <- function(x) { |
| 39 |
sapply(slot(x, "lines"), slot, "ID") |
| 40 |
} |
| 41 |
|
| 42 |
"row.names<-.SpatialLinesDataFrame" <- function(x, value) { |
| 43 |
spChFIDs(x, value) |
| 44 |
} |
| 45 |
|
| 46 |
setMethod("[", c("SpatialLinesDataFrame", "ANY", "ANY"), function(x, i, j, ... , drop = TRUE) { |
| 47 |
missing.i = missing(i) |
| 48 |
missing.j = missing(j) |
| 49 |
nargs = nargs() # e.g., a[3,] gives 2 for nargs, a[3] gives 1. |
| 50 |
if (missing.i && missing.j) { |
| 51 |
i = TRUE |
| 52 |
j = TRUE |
| 53 |
} else if (missing.j && !missing.i) { |
| 54 |
if (nargs == 2) { |
| 55 |
j = i |
| 56 |
i = TRUE |
| 57 |
} else { |
| 58 |
j = TRUE |
| 59 |
} |
| 60 |
} else if (missing.i && !missing.j) |
| 61 |
i = TRUE |
| 62 |
|
| 63 |
if (is.matrix(i)) |
| 64 |
stop("matrix argument not supported in SpatialLinesDataFrame selection") |
| 65 |
if (is(i, "Spatial")) |
| 66 |
i = !is.na(over(x, geometry(i))) |
| 67 |
if (is.logical(i)) { |
| 68 |
if (length(i) == 1 && i) |
| 69 |
i = 1:length(x@lines) |
| 70 |
else |
| 71 |
i <- which(i) |
| 72 |
} else if (is.character(i)) { |
| 73 |
i <- match(i, row.names(x)) |
| 74 |
} |
| 75 |
if (any(is.na(i))) |
| 76 |
stop("NAs not permitted in row index") |
| 77 |
#SpatialLinesDataFrame(as(x, "SpatialLines")[i, , drop = FALSE], |
| 78 |
# data = x@data[i, j, drop = FALSE], match.ID = FALSE) |
| 79 |
x@lines = x@lines[i] |
| 80 |
x@data = x@data[i, j, ..., drop = FALSE] |
| 81 |
# RSB 081003 |
| 82 |
if (length(x@lines) > 0) # EJP, 151218 |
| 83 |
x@bbox = .bboxSls(x@lines) |
| 84 |
x |
| 85 |
}) |
| 86 |
|
| 87 |
lines.SpatialLinesDataFrame = function(x, y = NULL, ...) |
| 88 |
lines(as(x, "SpatialLines"), ...) |
| 89 |
|
| 90 |
setAs("SpatialLinesDataFrame", "SpatialPointsDataFrame", function(from) { |
| 91 |
spp = as(as(from, "SpatialLines"), "SpatialPointsDataFrame") |
| 92 |
dfl = from@data[spp$Lines.NR, , drop = FALSE] |
| 93 |
spp@data = cbind(dfl, spp@data) |
| 94 |
spp |
| 95 |
} |
| 96 |
) |
| 97 |
|
| 98 |
dim.SpatialLinesDataFrame = function(x) dim(x@data) |
| 99 |
|
| 100 |
setMethod("split", "SpatialLinesDataFrame", split.data.frame) |
| 101 |
|
| 102 |
print.SpatialLinesDataFrame = function(x, ..., digits = getOption("digits"), |
| 103 |
asWKT = .asWKT) |
| 104 |
print(data.frame(asWKTSpatialLines(x, digits), x@data),..., digits = digits) |
| 105 |
|
| 106 |
setMethod("geometry", "SpatialLinesDataFrame", |
| 107 |
function(obj) as(obj, "SpatialLines")) |
| 108 |
|
| 109 |
length.SpatialLinesDataFrame = function(x) { length(x@lines) } |