| 1 |
Line <- function(coords) { |
| 2 |
coords <- coordinates(coords) |
| 3 |
if (ncol(coords) != 2) |
| 4 |
stop("coords must be a two-column matrix") |
| 5 |
new("Line", coords = coords) |
| 6 |
} |
| 7 |
|
| 8 |
Lines <- function(slinelist, ID) { |
| 9 |
if (is(slinelist, "Line")) |
| 10 |
slinelist = list(slinelist) |
| 11 |
if (any(sapply(slinelist, function(x) !is(x, "Line")))) |
| 12 |
stop("slinelist not a list of Line objects") |
| 13 |
if (missing(ID)) stop("Single ID required") |
| 14 |
if (length(ID) != 1) stop("Single ID required") |
| 15 |
ID <- as.character(ID) |
| 16 |
stopifnot(nzchar(ID)) |
| 17 |
new("Lines", Lines = slinelist, ID=ID) |
| 18 |
} |
| 19 |
|
| 20 |
SpatialLines <- function(LinesList, proj4string=CRS(as.character(NA))) { |
| 21 |
if (any(sapply(LinesList, function(x) !is(x, "Lines")))) |
| 22 |
stop("lines list not exclusively filled with Lines objects") |
| 23 |
res <- new("SpatialLines", bbox = .bboxSls(LinesList), proj4string=proj4string, |
| 24 |
lines=LinesList) |
| 25 |
res |
| 26 |
} |
| 27 |
|
| 28 |
LineLength = function(cc, longlat=FALSE, sum=TRUE) { |
| 29 |
if (is(cc, "Line")) |
| 30 |
cc = coordinates(cc) |
| 31 |
|
| 32 |
if (!is.matrix(cc)) stop("cc must be a matrix") |
| 33 |
if (ncol(cc) != 2) stop("cc must have two columns") |
| 34 |
if (!is.numeric(cc)) stop("cc must be numeric") |
| 35 |
x <- as.double(cc[,1]) |
| 36 |
y <- as.double(cc[,2]) |
| 37 |
n <- as.integer(length(x)) |
| 38 |
if (n == 1) return(0) |
| 39 |
lengths <- vector(mode="double", length=(n-1)) |
| 40 |
lonlat <- as.integer(longlat) |
| 41 |
res <- .C("sp_lengths", x, y, n, lengths, lonlat, PACKAGE = "sp")[[4]] |
| 42 |
if (any(!is.finite(res))) stop("non-finite line lengths") |
| 43 |
if (sum) |
| 44 |
sum(res) |
| 45 |
else |
| 46 |
res |
| 47 |
|
| 48 |
# dxy = matrix(apply(cc, 2, diff), ncol = 2) |
| 49 |
# sum(sqrt(apply(dxy, 1, function(x) sum(x ** 2)))) |
| 50 |
} |
| 51 |
|
| 52 |
bbox.Lines <- function(obj) { |
| 53 |
rx=range(lapply(obj@Lines, function(x) range(x@coords[,1]))) |
| 54 |
ry=range(lapply(obj@Lines, function(x) range(x@coords[,2]))) |
| 55 |
res=rbind(x=rx,y=ry) |
| 56 |
dimnames(res)[[2]] <- c("min", "max") |
| 57 |
res |
| 58 |
} |
| 59 |
|
| 60 |
setMethod("bbox", "Lines", bbox.Lines) |
| 61 |
|
| 62 |
bbox.Line <- function(obj) { |
| 63 |
rx <- range(obj@coords[,1]) |
| 64 |
ry <- range(obj@coords[,2]) |
| 65 |
res = rbind(x = rx, y = ry) |
| 66 |
dimnames(res)[[2]] <- c("min", "max") |
| 67 |
res |
| 68 |
} |
| 69 |
|
| 70 |
setMethod("bbox", "Line", bbox.Line) |
| 71 |
|
| 72 |
.bboxSls <- function(lst) { |
| 73 |
bb = sapply(lst, bbox) |
| 74 |
res = matrix(c(min(bb[1,]), min(bb[2,]), max(bb[3,]), max(bb[4,])), 2, 2) |
| 75 |
dimnames(res) = list(c("x", "y"), c("min", "max")) |
| 76 |
res |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
## plotSpatialLines <- function(SL, xlim = NULL, ylim = NULL, |
| 81 |
## col = 1, lwd = 1, lty=1, add = FALSE, axes = FALSE, ..., |
| 82 |
## setParUsrBB=FALSE) |
| 83 |
## { |
| 84 |
## # frame() |
| 85 |
## # plot.window(xlim = xlim, ylim = ylim, asp = asp) |
| 86 |
## if (! add) |
| 87 |
## plot(as(SL, "Spatial"), xlim = xlim, ylim = ylim, |
| 88 |
## axes = axes, ..., setParUsrBB=setParUsrBB) |
| 89 |
## lst <- SL@lines |
| 90 |
## for (i in seq(along=lst)) { |
| 91 |
## sllst = lst[[i]]@Lines |
| 92 |
## for (j in seq(along=sllst)) { |
| 93 |
## crds <- coordinates(sllst[[j]]) |
| 94 |
## if (length(col) != length(lst)) |
| 95 |
## # Michael Sumner, Jul 6 2010; |
| 96 |
## # col <- rep(col[1], length(lst)) |
| 97 |
## col <- rep(col, length = length(lst)) |
| 98 |
## if (length(lwd) != length(lst)) |
| 99 |
## lwd <- rep(lwd[1], length(lst)) |
| 100 |
## if (length(lty) != length(lst)) |
| 101 |
## lty <- rep(lty[1], length(lst)) |
| 102 |
## lines(crds, col = col[i], lwd = lwd[i], |
| 103 |
## lty = lty[i], ...) |
| 104 |
## } |
| 105 |
## } |
| 106 |
## } |
| 107 |
|
| 108 |
## MDS 2010-07-07 new handling for col, lwd, lty, and new lend, ljoin, lmitre |
| 109 |
plotSpatialLines <- function(SL, xlim = NULL, ylim = NULL, |
| 110 |
col = 1, lwd = 1, lty=1, add = FALSE, axes = FALSE, |
| 111 |
lend = 0, ljoin = 0, lmitre = 10, ..., |
| 112 |
setParUsrBB=FALSE) { |
| 113 |
|
| 114 |
if (! add) |
| 115 |
plot(as(SL, "Spatial"), xlim = xlim, ylim = ylim, |
| 116 |
axes = axes, ..., setParUsrBB=setParUsrBB) |
| 117 |
lst <- SL@lines |
| 118 |
|
| 119 |
if (length(col) != length(lst)) col <- rep(col, length = length(lst)) |
| 120 |
if (length(lwd) != length(lst)) lwd <- rep(lwd, length = length(lst)) |
| 121 |
if (length(lty) != length(lst)) lty <- rep(lty, length = length(lst)) |
| 122 |
if (length(lend) != length(lst)) lend <- rep(lend, length = length(lst)) |
| 123 |
if (length(ljoin) != length(lst)) ljoin <- rep(ljoin, length = length(lst)) |
| 124 |
if (length(lmitre) != length(lst)) lmitre <- rep(lmitre, length = length(lst)) |
| 125 |
|
| 126 |
for (i in seq_along(lst)) { |
| 127 |
sllst = lst[[i]]@Lines |
| 128 |
for (j in seq_along(sllst)) { |
| 129 |
crds <- coordinates(sllst[[j]]) |
| 130 |
lines(crds, col = col[i], lwd = lwd[i], lty = lty[i], |
| 131 |
lend = lend[i], ljoin = ljoin[i], lmitre = lmitre[i]) |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
setMethod("plot", signature(x = "SpatialLines", y = "missing"), |
| 137 |
function(x, y, ...) plotSpatialLines(x, ...)) |
| 138 |
|
| 139 |
setMethod("coordinates", "Line", function(obj) obj@coords) |
| 140 |
setMethod("coordinates", "Lines", function(obj) lapply(obj@Lines, coordinates)) |
| 141 |
setMethod("coordinates", "SpatialLines", function(obj) lapply(obj@lines, coordinates)) |
| 142 |
|
| 143 |
lines.Line = function(x, y = NULL, ...) invisible(lines(coordinates(x), ...)) |
| 144 |
lines.Lines = function(x, y = NULL, ...) invisible(lapply(x@Lines, |
| 145 |
function(x, ...) lines(x, ...), ...)) |
| 146 |
lines.SpatialLines = function(x, y = NULL, ...) invisible(lapply(x@lines, |
| 147 |
function(x, ...) lines(x, ...), ...)) |
| 148 |
|
| 149 |
row.names.SpatialLines <- function(x) { |
| 150 |
sapply(slot(x, "lines"), slot, "ID") |
| 151 |
} |
| 152 |
|
| 153 |
"row.names<-.SpatialLines" <- function(x, value) { |
| 154 |
spChFIDs(x, value) |
| 155 |
} |
| 156 |
|
| 157 |
#"[.SpatialLines" = function(x, i, j, ..., drop = T) { |
| 158 |
setMethod("[", "SpatialLines", function(x, i, j, ..., drop = TRUE) { |
| 159 |
if (is(i, "Spatial")) |
| 160 |
i = !is.na(over(x, geometry(i))) |
| 161 |
if (is.logical(i)) { |
| 162 |
if (length(i) == 1 && i) |
| 163 |
i = 1:length(x@lines) |
| 164 |
else |
| 165 |
i <- which(i) |
| 166 |
} else if (is.character(i)) |
| 167 |
i <- match(i, row.names(x)) |
| 168 |
if (any(is.na(i))) |
| 169 |
stop("NAs not permitted in row index") |
| 170 |
#SpatialLines(x@lines[i], CRS(proj4string(x))) |
| 171 |
x@lines = x@lines[i] |
| 172 |
if (length(x@lines) > 0) |
| 173 |
x@bbox = .bboxSls(x@lines) |
| 174 |
x |
| 175 |
}) |
| 176 |
|
| 177 |
setMethod("coordnames", signature(x = "SpatialLines"), |
| 178 |
function(x) coordnames(x@lines[[1]]) |
| 179 |
) |
| 180 |
setMethod("coordnames", signature(x = "Lines"), |
| 181 |
function(x) coordnames(x@Lines[[1]]) |
| 182 |
) |
| 183 |
setMethod("coordnames", signature(x = "Line"), |
| 184 |
function(x) dimnames(coordinates(x))[[2]] |
| 185 |
) |
| 186 |
setReplaceMethod("coordnames", |
| 187 |
signature(x = "SpatialLines", value = "character"), |
| 188 |
function(x, value) { |
| 189 |
dimnames(x@bbox)[[1]] = value |
| 190 |
for (i in seq_along(x@lines)) |
| 191 |
coordnames(x@lines[[i]]) = value |
| 192 |
x |
| 193 |
} |
| 194 |
) |
| 195 |
setReplaceMethod("coordnames", |
| 196 |
signature(x = "Lines", value = "character"), |
| 197 |
function(x, value) { |
| 198 |
for (i in seq_along(x@Lines)) |
| 199 |
coordnames(x@Lines[[i]]) = value |
| 200 |
x |
| 201 |
} |
| 202 |
) |
| 203 |
setReplaceMethod("coordnames", |
| 204 |
signature(x = "Line", value = "character"), |
| 205 |
function(x, value) { |
| 206 |
dimnames(x@coords)[[2]] = value |
| 207 |
x |
| 208 |
} |
| 209 |
) |
| 210 |
|
| 211 |
getSpatialLinesMidPoints = function(SL) { |
| 212 |
ret = lapply(SL@lines, |
| 213 |
function(x) sapply(x@Lines, |
| 214 |
function(X) apply(X@coords, 2, mean) |
| 215 |
) |
| 216 |
) |
| 217 |
ret = t(sapply(ret, function(x) apply(x, 1, mean))) |
| 218 |
SpatialPoints(ret, slot(SL, "proj4string")) |
| 219 |
} |
| 220 |
|
| 221 |
LinesLength = function(Ls, longlat=FALSE) sum(sapply(Ls@Lines, LineLength, longlat)) |
| 222 |
|
| 223 |
SpatialLinesLengths = function(SL, longlat) { |
| 224 |
if (missing(longlat)) { |
| 225 |
proj <- is.projected(SL) |
| 226 |
if (is.na(proj)) { |
| 227 |
longlat <- FALSE |
| 228 |
} else { |
| 229 |
longlat <- !proj |
| 230 |
} |
| 231 |
} |
| 232 |
if (!is.logical(longlat)) stop("longlat should be logical") |
| 233 |
sapply(SL@lines, LinesLength, longlat=longlat) |
| 234 |
} |
| 235 |
|
| 236 |
setAs("Lines", "SpatialPoints", function(from) { |
| 237 |
cc = do.call(rbind, coordinates(from)) |
| 238 |
if (!is.null(rownames(cc))) |
| 239 |
rownames(cc) = make.unique(rownames(cc)) |
| 240 |
SpatialPoints(cc) |
| 241 |
} |
| 242 |
) |
| 243 |
setAs("SpatialLines", "SpatialPoints", function(from) { |
| 244 |
cc = do.call(rbind, |
| 245 |
lapply(from@lines, function(x) coordinates(as(x, "SpatialPoints")))) |
| 246 |
if (!is.null(rownames(cc))) |
| 247 |
rownames(cc) = make.unique(rownames(cc)) |
| 248 |
SpatialPoints(cc, rebuild_CRS(slot(from, "proj4string"))) |
| 249 |
} |
| 250 |
) |
| 251 |
setAs("Lines", "SpatialMultiPoints", function(from) { |
| 252 |
SpatialMultiPoints(coordinates(from)) |
| 253 |
} |
| 254 |
) |
| 255 |
setAs("SpatialLines", "SpatialMultiPoints", function(from) { |
| 256 |
l = lapply(from@lines, function(x) do.call(rbind, coordinates(x))) |
| 257 |
names(l) = sapply(from@lines, function(x) x@ID) |
| 258 |
SpatialMultiPoints(l, rebuild_CRS(slot(from, "proj4string"))) |
| 259 |
} |
| 260 |
) |
| 261 |
SpatialLines2SpatialPointsDataFrame = function(from) { |
| 262 |
spp = as(as(from, "SpatialLines"), "SpatialPoints") |
| 263 |
L = lapply(from@lines, function(x) {rep(1:length(x@Lines), |
| 264 |
times = sapply(x@Lines, function(x) nrow(x@coords)))}) |
| 265 |
IDs = sapply(from@lines, function(x) x@ID) |
| 266 |
L2 = rep(IDs, times = sapply(L, length)) |
| 267 |
L3 = rep(1:length(from@lines), times = sapply(L, length)) |
| 268 |
L = unlist(L) |
| 269 |
SpatialPointsDataFrame(spp, data.frame(Lines.NR = L3, Lines.ID = factor(L2), |
| 270 |
Line.NR=L)) |
| 271 |
} |
| 272 |
setAs("SpatialLines", "SpatialPointsDataFrame", function(from) |
| 273 |
SpatialLines2SpatialPointsDataFrame(from) |
| 274 |
) |
| 275 |
|
| 276 |
setAs("SpatialPoints", "Line", function(from) |
| 277 |
Line(coordinates(from)) |
| 278 |
) |
| 279 |
|
| 280 |
setAs("SpatialPoints", "Lines", function(from) |
| 281 |
Lines(as(from, "Line"), "ID") |
| 282 |
) |
| 283 |
|
| 284 |
setAs("SpatialPoints", "SpatialLines", function(from) |
| 285 |
SpatialLines(list(as(from, "Lines")), from@proj4string) |
| 286 |
) |
| 287 |
|
| 288 |
asWKTSpatialLines = function(x, digits = getOption("digits")) { |
| 289 |
ids = sapply(x@lines, function(x)slot(x,"ID")) |
| 290 |
df = data.frame(geometry = paste("MULTILINESTRING((", |
| 291 |
apply( |
| 292 |
signif(sapply(coordinates(x), function(x) x[[1]][1,]),digits=digits), |
| 293 |
2, paste, collapse=" ")," ...))",sep="")) |
| 294 |
row.names(df) = ids |
| 295 |
df |
| 296 |
} |
| 297 |
print.SpatialLines = function(x, ..., digits = getOption("digits"), |
| 298 |
asWKT=FALSE) { |
| 299 |
cat("SpatialLines:\n") |
| 300 |
if (asWKT) |
| 301 |
print(asWKTSpatialLines(x, digits)) |
| 302 |
else |
| 303 |
show(x) |
| 304 |
pst <- paste(strwrap(paste( |
| 305 |
"Coordinate Reference System (CRS) arguments:", |
| 306 |
proj4string(x))), collapse="\n") |
| 307 |
cat(pst, "\n") |
| 308 |
} |
| 309 |
#setMethod("show", "SpatialLines", function(object) print.SpatialLines(object)) |
| 310 |
length.SpatialLines = function(x) { length(x@lines) } |
| 311 |
|
| 312 |
names.SpatialLines = function(x) { |
| 313 |
unlist(lapply(x@lines, function(X) X@ID)) |
| 314 |
} |
| 315 |
|
| 316 |
labels.SpatialLines = function(object, labelCRS, side = 1:2, ...) { |
| 317 |
# 1=below, 2=left, 3=above and 4=right. |
| 318 |
if (! identical(names(object), c("EW", "NS"))) |
| 319 |
warning("this labels method is meant to operate on SpatialLines created with sp::gridlines") |
| 320 |
if (missing(labelCRS) && !is.na(slot(slot(object, "proj4string"), "projargs"))) |
| 321 |
labelCRS = object@proj4string |
| 322 |
|
| 323 |
cc = coordinates(object) |
| 324 |
pts = append( |
| 325 |
lapply(cc, function(x) do.call(rbind, lapply(x, function(y) head(y, 1)))), |
| 326 |
lapply(cc, function(x) do.call(rbind, lapply(x, function(y) tail(y, 1)))) |
| 327 |
) |
| 328 |
d = SpatialPoints(do.call(rbind, lapply(pts, function(x) { row.names(x) = NULL; x}))) |
| 329 |
d$pos = rep(c(2,1,4,3), times = rep(sapply(cc, length), 2)) |
| 330 |
ang = append( |
| 331 |
lapply(cc, function(x) apply(do.call(rbind, lapply(x, function(y) apply(head(y, 2), 2, diff))), 1, |
| 332 |
function(x) atan2(x[2], x[1])*180/pi)), |
| 333 |
lapply(cc, function(x) apply(do.call(rbind, lapply(x, function(y) apply(tail(y, 2), 2, diff))), 1, |
| 334 |
function(x) atan2(x[2], x[1])*180/pi)) |
| 335 |
) |
| 336 |
d$srt = c(ang[[1]], ang[[2]] - 90, ang[[3]], ang[[4]] - 90) |
| 337 |
|
| 338 |
# get the labels: |
| 339 |
if (! missing(labelCRS)) |
| 340 |
object = spTransform(object, labelCRS) # may do nothing |
| 341 |
if (is.na(slot(slot(object, "proj4string"), "projargs"))) |
| 342 |
is.p = TRUE |
| 343 |
else |
| 344 |
is.p = is.projected(object) |
| 345 |
|
| 346 |
pts = lapply(coordinates(object), function(x) do.call(rbind, lapply(x, function(y) y[1,]))) |
| 347 |
lat = pts[[1]][,2] |
| 348 |
long = pts[[2]][,1] |
| 349 |
if (is.p) |
| 350 |
d$labels = as.character(rep(c(lat, long), 2)) |
| 351 |
else |
| 352 |
d$labels = rep(c(degreeLabelsNS(lat), degreeLabelsEW(long)), 2) |
| 353 |
|
| 354 |
d[d$pos %in% side, ] |
| 355 |
} |