| 1 |
Spatial <- function(bbox, proj4string = CRS(as.character(NA))) { |
| 2 |
new("Spatial", bbox=bbox, proj4string=proj4string) |
| 3 |
} |
| 4 |
|
| 5 |
if (!isGeneric("addAttrToGeom")) |
| 6 |
setGeneric("addAttrToGeom", function(x, y, match.ID, ...) |
| 7 |
standardGeneric("addAttrToGeom")) |
| 8 |
if (!isGeneric("bbox")) |
| 9 |
setGeneric("bbox", function(obj) |
| 10 |
standardGeneric("bbox")) |
| 11 |
if (!isGeneric("coordinates")) |
| 12 |
setGeneric("coordinates", function(obj, ...) |
| 13 |
standardGeneric("coordinates")) |
| 14 |
if (!isGeneric("coordinates<-")) |
| 15 |
setGeneric("coordinates<-", function(object, value) |
| 16 |
standardGeneric("coordinates<-")) |
| 17 |
if (!isGeneric("coordnames")) |
| 18 |
setGeneric("coordnames", function(x) |
| 19 |
standardGeneric("coordnames")) |
| 20 |
if (!isGeneric("coordnames<-")) |
| 21 |
setGeneric("coordnames<-", function(x,value) |
| 22 |
standardGeneric("coordnames<-")) |
| 23 |
if (!isGeneric("dimensions")) |
| 24 |
setGeneric("dimensions", function(obj) |
| 25 |
standardGeneric("dimensions")) |
| 26 |
if (!isGeneric("fullgrid")) |
| 27 |
setGeneric("fullgrid", function(obj) |
| 28 |
standardGeneric("fullgrid")) |
| 29 |
if (!isGeneric("fullgrid<-")) |
| 30 |
setGeneric("fullgrid<-", function(obj, value) |
| 31 |
standardGeneric("fullgrid<-")) |
| 32 |
if (!isGeneric("geometry")) |
| 33 |
setGeneric("geometry", function(obj) |
| 34 |
standardGeneric("geometry")) |
| 35 |
if (!isGeneric("geometry<-")) |
| 36 |
setGeneric("geometry<-", function(obj, value) |
| 37 |
standardGeneric("geometry<-")) |
| 38 |
if (!isGeneric("gridded")) |
| 39 |
setGeneric("gridded", function(obj) |
| 40 |
standardGeneric("gridded")) |
| 41 |
if (!isGeneric("gridded<-")) |
| 42 |
setGeneric("gridded<-", function(obj, value) |
| 43 |
standardGeneric("gridded<-")) |
| 44 |
if (!isGeneric("is.projected")) |
| 45 |
setGeneric("is.projected", function(obj) |
| 46 |
standardGeneric("is.projected")) |
| 47 |
#if (!isGeneric("overlay")) |
| 48 |
# setGeneric("overlay", function(x, y, ...) |
| 49 |
# standardGeneric("overlay")) |
| 50 |
if (!isGeneric("over")) |
| 51 |
setGeneric("over", function(x, y, returnList = FALSE, fn = NULL, ...) |
| 52 |
standardGeneric("over")) |
| 53 |
if (!isGeneric("plot")) |
| 54 |
setGeneric("plot", function(x, y, ...) |
| 55 |
standardGeneric("plot")) |
| 56 |
if (!isGeneric("polygons")) |
| 57 |
setGeneric("polygons", function(obj) |
| 58 |
standardGeneric("polygons")) |
| 59 |
if (!isGeneric("polygons<-")) |
| 60 |
setGeneric("polygons<-", function(object, value) |
| 61 |
standardGeneric("polygons<-")) |
| 62 |
if (!isGeneric("proj4string")) |
| 63 |
setGeneric("proj4string", function(obj) |
| 64 |
standardGeneric("proj4string")) |
| 65 |
if (!isGeneric("proj4string<-")) |
| 66 |
setGeneric("proj4string<-", function(obj, value) |
| 67 |
standardGeneric("proj4string<-")) |
| 68 |
if (!isGeneric("sppanel")) |
| 69 |
setGeneric("sppanel", function(obj, ...) |
| 70 |
standardGeneric("sppanel")) |
| 71 |
if (!isGeneric("spplot")) |
| 72 |
setGeneric("spplot", function(obj, ...) |
| 73 |
standardGeneric("spplot")) |
| 74 |
if (!isGeneric("spsample")) |
| 75 |
setGeneric("spsample", function(x, n, type, ...) |
| 76 |
standardGeneric("spsample")) |
| 77 |
if (!isGeneric("summary")) |
| 78 |
setGeneric("summary", function(object, ...) |
| 79 |
standardGeneric("summary")) |
| 80 |
if (!isGeneric("spChFIDs")) |
| 81 |
setGeneric("spChFIDs", function(obj, x) |
| 82 |
standardGeneric("spChFIDs")) |
| 83 |
if (!isGeneric("spChFIDs<-")) |
| 84 |
setGeneric("spChFIDs<-", function(obj, value) |
| 85 |
standardGeneric("spChFIDs<-")) |
| 86 |
if (!isGeneric("surfaceArea")) |
| 87 |
setGeneric("surfaceArea", function(m, ...) |
| 88 |
standardGeneric("surfaceArea")) |
| 89 |
if (!isGeneric("split")) |
| 90 |
setGeneric("split", function(x, f, drop = FALSE, ...) |
| 91 |
standardGeneric("split")) |
| 92 |
if (!isGeneric("spTransform")) |
| 93 |
setGeneric("spTransform", function(x, CRSobj, ...) |
| 94 |
standardGeneric("spTransform")) |
| 95 |
setMethod("spTransform", signature("Spatial", "CRS"), |
| 96 |
function(x, CRSobj, ...) { |
| 97 |
if (!requireNamespace("sf", quietly = TRUE)) |
| 98 |
stop("sf required") |
| 99 |
as(sf::st_transform(sf::st_as_sf(x), sf::st_crs(CRSobj)), "Spatial") |
| 100 |
|
| 101 |
}) |
| 102 |
setMethod("spTransform", signature("Spatial", "character"), |
| 103 |
function(x, CRSobj, ...) spTransform(x, CRS(CRSobj), ...) |
| 104 |
) |
| 105 |
setMethod("spTransform", signature("Spatial", "ANY"), |
| 106 |
function(x, CRSobj, ...) stop("second argument needs to be of class CRS") |
| 107 |
) |
| 108 |
|
| 109 |
|
| 110 |
bbox.default <- function(obj) { |
| 111 |
is_points <- function(obj) { |
| 112 |
is <- FALSE |
| 113 |
if(is.array(obj)) |
| 114 |
if(length(dim(obj))==2) |
| 115 |
if(dim(obj)[2]>=2) is <- TRUE |
| 116 |
is |
| 117 |
} |
| 118 |
if(!is_points(obj))stop('object not a >= 2-column array') |
| 119 |
xr <- range(obj[,1],na.rm=TRUE) |
| 120 |
yr <- range(obj[,2],na.rm=TRUE) |
| 121 |
res <- rbind(x=xr, y=yr) |
| 122 |
colnames(res) <- c("min","max") |
| 123 |
res |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
setMethod("bbox", "ANY", bbox.default) |
| 128 |
|
| 129 |
setMethod("bbox", "Spatial", function(obj) obj@bbox) |
| 130 |
|
| 131 |
setMethod("dimensions", "Spatial", function(obj) nrow(bbox(obj))) |
| 132 |
|
| 133 |
setMethod("polygons", "Spatial", function(obj) { |
| 134 |
if (is(obj, "SpatialPolygons")) |
| 135 |
as(obj, "SpatialPolygons") |
| 136 |
else |
| 137 |
stop("polygons method only available for objects of class or deriving from SpatialPolygons") |
| 138 |
} |
| 139 |
) |
| 140 |
|
| 141 |
summary.Spatial = function(object, ...) { |
| 142 |
obj = list() |
| 143 |
obj[["class"]] = class(object) |
| 144 |
obj[["bbox"]] = bbox(object) |
| 145 |
obj[["is.projected"]] = is.projected(object) |
| 146 |
obj[["proj4string"]] = object@proj4string@projargs |
| 147 |
if (is(object, "SpatialPoints")) |
| 148 |
obj[["npoints"]] = nrow(object@coords) |
| 149 |
if (is(object, "SpatialGrid") || is(object, "SpatialPixels")) |
| 150 |
obj[["grid"]] = gridparameters(object) |
| 151 |
if ("data" %in% slotNames(object) && ncol(object@data) > 0) |
| 152 |
obj[["data"]] = summary(object@data) |
| 153 |
class(obj) = "summary.Spatial" |
| 154 |
obj |
| 155 |
} |
| 156 |
setMethod("summary", "Spatial", summary.Spatial) |
| 157 |
|
| 158 |
print.summary.Spatial = function(x, ...) { |
| 159 |
cat(paste("Object of class ", x[["class"]], "\n", sep = "")) |
| 160 |
cat("Coordinates:\n") |
| 161 |
print(x[["bbox"]], ...) |
| 162 |
cat(paste("Is projected:", x[["is.projected"]], "\n")) |
| 163 |
# cat(paste("proj4string : [", x[["proj4string"]], "]\n", sep="")) |
| 164 |
pst <- paste(strwrap(x[["proj4string"]]), collapse="\n") |
| 165 |
if (nchar(pst) < 40) cat(paste("proj4string : [", pst, "]\n", sep="")) |
| 166 |
else cat(paste("proj4string :\n[", pst, "]\n", sep="")) |
| 167 |
if (!is.null(x$npoints)) { |
| 168 |
cat("Number of points: ") |
| 169 |
cat(x$npoints) |
| 170 |
cat("\n") |
| 171 |
} |
| 172 |
if (!is.null(x$n.polygons)) { |
| 173 |
cat("Number of polygons: ") |
| 174 |
cat(x$n.polygons) |
| 175 |
cat("\n") |
| 176 |
} |
| 177 |
if (!is.null(x$grid)) { |
| 178 |
cat("Grid attributes:\n") |
| 179 |
print(x$grid, ...) |
| 180 |
} |
| 181 |
if (!is.null(x$data)) { |
| 182 |
cat("Data attributes:\n") |
| 183 |
print(x$data, ...) |
| 184 |
} |
| 185 |
invisible(x) |
| 186 |
} |
| 187 |
|
| 188 |
# sp.axes = FALSE |
| 189 |
|
| 190 |
#asp <- function(x, ylim) { |
| 191 |
# if (is.na(proj4string(x)) || is.projected(x)) |
| 192 |
# return(1.0) |
| 193 |
# else |
| 194 |
# return(1/cos((mean(ylim) * pi)/180)) |
| 195 |
#} |
| 196 |
|
| 197 |
bb2merc = function(x, cls = "ggmap") { # return bbox in the appropriate "web mercator" CRS |
| 198 |
WGS84 = CRS("+init=epsg:4326") |
| 199 |
# merc = CRS("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs") |
| 200 |
merc = CRS("+init=epsg:3857") # http://wiki.openstreetmap.org/wiki/EPSG:3857 |
| 201 |
if (cls == "ggmap") { |
| 202 |
b = sapply(attr(x, "bb"), c) |
| 203 |
pts = cbind(c(b[2],b[4]),c(b[1],b[3])) |
| 204 |
} else if (cls == "RgoogleMaps") |
| 205 |
pts = rbind(x$BBOX$ll, x$BBOX$ur)[,2:1] |
| 206 |
else |
| 207 |
stop("unknown cls") |
| 208 |
bbox(spTransform(SpatialPoints(pts, WGS84), merc)) |
| 209 |
} |
| 210 |
|
| 211 |
plot.Spatial <- function(x, xlim = NULL, ylim = NULL, |
| 212 |
asp = NA, axes = FALSE, bg = par("bg"), ..., |
| 213 |
xaxs, yaxs, lab, setParUsrBB = FALSE, bgMap = NULL, expandBB = c(0,0,0,0)) { |
| 214 |
|
| 215 |
# expandBB: 1=below, 2=left, 3=above and 4=right. |
| 216 |
bbox <- bbox(x) |
| 217 |
expBB = function(lim, expand) c(lim[1] - expand[1] * diff(lim), lim[2] + expand[2] * diff(lim)) |
| 218 |
if (is.null(xlim)) |
| 219 |
xlim <- expBB(bbox[1,], expandBB[c(2,4)]) |
| 220 |
if (is.null(ylim)) |
| 221 |
ylim <- expBB(bbox[2,], expandBB[c(1,3)]) |
| 222 |
if (is.na(asp)) |
| 223 |
asp <- ifelse(is.na(slot(slot(x, "proj4string"), "projargs")) || is.projected(x), 1.0, |
| 224 |
1/cos((mean(ylim) * pi)/180)) |
| 225 |
|
| 226 |
plot.new() |
| 227 |
|
| 228 |
args = list(xlim = xlim, ylim = ylim, asp = asp) |
| 229 |
if (!missing(xaxs)) args$xaxs = xaxs |
| 230 |
if (!missing(yaxs)) args$yaxs = yaxs |
| 231 |
if (!missing(lab)) args$lab = lab |
| 232 |
do.call(plot.window, args) |
| 233 |
|
| 234 |
if (setParUsrBB) |
| 235 |
par(usr=c(xlim, ylim)) |
| 236 |
pl_reg <- par("usr") |
| 237 |
rect(xleft=pl_reg[1], ybottom=pl_reg[3], xright=pl_reg[2], |
| 238 |
ytop=pl_reg[4], col=bg, border=FALSE) |
| 239 |
if (axes) { # set up default axes system & box: |
| 240 |
box() |
| 241 |
if (identical(is.projected(x), FALSE)) { |
| 242 |
degAxis(1, ...) |
| 243 |
degAxis(2, ...) |
| 244 |
} else { |
| 245 |
axis(1, ...) |
| 246 |
axis(2, ...) |
| 247 |
} |
| 248 |
# axis(3, labels = FALSE, ...) |
| 249 |
# axis(4, labels = FALSE, ...) |
| 250 |
} |
| 251 |
localTitle <- function(..., col, bg, pch, cex, lty, lwd) title(...) |
| 252 |
localTitle(...) |
| 253 |
if (!is.null(bgMap)) { |
| 254 |
is3875 = function(x) length(grep("+init=epsg:3857", x@proj4string@projargs)) > 0 |
| 255 |
mercator = FALSE |
| 256 |
if (is(bgMap, "ggmap")) { |
| 257 |
bb = bb2merc(bgMap, "ggmap") |
| 258 |
mercator = TRUE |
| 259 |
} else if (all(c("lat.center","lon.center","zoom","myTile","BBOX") %in% names(bgMap))) { |
| 260 |
# an object returned by RgoogleMaps::GetMap |
| 261 |
bb = bb2merc(bgMap, "RgoogleMaps") |
| 262 |
bgMap = bgMap$myTile |
| 263 |
mercator = TRUE |
| 264 |
} else |
| 265 |
bb = rbind(xlim, ylim) # can be any CRS! |
| 266 |
if (mercator && !is3875(x)) |
| 267 |
warning(paste('CRS of plotting object differs from that of bgMap, which is assumed to be CRS("+init=epsg:3857")')) |
| 268 |
rasterImage(bgMap, bb[1,1], bb[2,1], bb[1,2], bb[2,2], interpolate = FALSE) |
| 269 |
} |
| 270 |
} |
| 271 |
setMethod("plot", signature(x = "Spatial", y = "missing"), |
| 272 |
function(x,y,...) plot.Spatial(x,...)) |
| 273 |
|
| 274 |
degAxis = function (side, at, labels, ...) { |
| 275 |
if (missing(at)) |
| 276 |
at = axTicks(side) |
| 277 |
if (missing(labels)) { |
| 278 |
labels = FALSE |
| 279 |
if (side == 1 || side == 3) |
| 280 |
labels = parse(text = degreeLabelsEW(at)) |
| 281 |
else if (side == 2 || side == 4) |
| 282 |
labels = parse(text = degreeLabelsNS(at)) |
| 283 |
} |
| 284 |
axis(side, at = at, labels = labels, ...) |
| 285 |
} |
| 286 |
|
| 287 |
setReplaceMethod("spChFIDs", signature(obj = "Spatial", value = "ANY"), |
| 288 |
function(obj, value) { spChFIDs(obj, as.character(value)) } |
| 289 |
) |
| 290 |
|
| 291 |
setReplaceMethod("coordinates", signature(object = "Spatial", value = "ANY"), |
| 292 |
function(object, value) |
| 293 |
stop("setting coordinates cannot be done on Spatial objects, where they have already been set") |
| 294 |
) |
| 295 |
|
| 296 |
setMethod("[[", c("Spatial", "ANY", "missing"), |
| 297 |
function(x, i, j, ...) { |
| 298 |
if (!("data" %in% slotNames(x))) |
| 299 |
stop("no [[ method for object without attributes") |
| 300 |
x@data[[i]] |
| 301 |
} |
| 302 |
) |
| 303 |
|
| 304 |
setReplaceMethod("[[", c("Spatial", "ANY", "missing", "ANY"), |
| 305 |
function(x, i, j, value) { |
| 306 |
if (!("data" %in% slotNames(x))) |
| 307 |
stop("no [[ method for object without attributes") |
| 308 |
if (is.character(i) && any(!is.na(match(i, dimnames(coordinates(x))[[2]])))) |
| 309 |
stop(paste(i, "is already present as a coordinate name!")) |
| 310 |
x@data[[i]] <- value |
| 311 |
x |
| 312 |
} |
| 313 |
) |
| 314 |
|
| 315 |
setMethod("$", "Spatial", |
| 316 |
function(x, name) { |
| 317 |
if (!("data" %in% slotNames(x))) |
| 318 |
stop("no $ method for object without attributes") |
| 319 |
x@data[[name]] |
| 320 |
} |
| 321 |
) |
| 322 |
|
| 323 |
setReplaceMethod("$", "Spatial", |
| 324 |
function(x, name, value) { |
| 325 |
if (name %in% coordnames(x)) |
| 326 |
stop(paste(name, |
| 327 |
"is a coordinate name, please choose another name")) |
| 328 |
if (!("data" %in% slotNames(x))) { |
| 329 |
df = list(value); names(df) = name |
| 330 |
return(addAttrToGeom(x, data.frame(df), match.ID = FALSE)) |
| 331 |
# stop("no $<- method for object without attributes") |
| 332 |
} |
| 333 |
#if (is.list(value)) |
| 334 |
# warning("assigning list or data.frame to attribute vector") |
| 335 |
x@data[[name]] = value |
| 336 |
x |
| 337 |
} |
| 338 |
) |
| 339 |
|
| 340 |
setMethod("geometry", "Spatial", |
| 341 |
function(obj) { |
| 342 |
if ("data" %in% slotNames(obj)) |
| 343 |
stop(paste("geometry method missing for class", class(obj))) |
| 344 |
obj |
| 345 |
} |
| 346 |
) |
| 347 |
|
| 348 |
setReplaceMethod("geometry", c("data.frame", "Spatial"), |
| 349 |
function(obj, value) addAttrToGeom(value, obj) |
| 350 |
) |
| 351 |
|
| 352 |
setReplaceMethod("[", c("Spatial", "ANY", "ANY", "ANY"), |
| 353 |
function(x, i, j, value) { |
| 354 |
if (!("data" %in% slotNames(x))) |
| 355 |
stop("no [ method for object without attributes") |
| 356 |
if (is.character(i) && any(!is.na(match(i, dimnames(coordinates(x))[[2]])))) |
| 357 |
stop(paste(i, "is already present as a coordinate name!")) |
| 358 |
x@data[i,j] <- value |
| 359 |
x |
| 360 |
} |
| 361 |
) |
| 362 |
|
| 363 |
setMethod("rebuild_CRS", signature(obj = "Spatial"), |
| 364 |
function(obj) { |
| 365 |
slot(obj, "proj4string") <- rebuild_CRS(slot(obj, "proj4string")) |
| 366 |
obj |
| 367 |
} |
| 368 |
) |
| 369 |
|
| 370 |
# Don MacQueen provided head & tail: |
| 371 |
#head.Spatial <- function(x, n=6L, ...) { |
| 372 |
# ix <- sign(n)*seq(abs(n)) |
| 373 |
# x[ ix , , drop=FALSE] |
| 374 |
#} |
| 375 |
# revision from Finn Lindgren |
| 376 |
head.Spatial <- function (x, n = 6L, ...) |
| 377 |
{ |
| 378 |
if (n > 0L) { |
| 379 |
n <- min(n, nrow(x)) |
| 380 |
ix <- seq_len(n) |
| 381 |
} else if (n < 0L) { |
| 382 |
n <- min(abs(n), nrow(x)) |
| 383 |
ix <- seq_len(nrow(x) - n) |
| 384 |
} else { |
| 385 |
ix <- seq_len(0) |
| 386 |
} |
| 387 |
x[ix, , drop = FALSE] |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
tail.Spatial <- function(x, n=6L, ...) { |
| 392 |
ix <- sign(n)*rev(seq(nrow(x), by=-1L, len=abs(n))) |
| 393 |
x[ ix , , drop=FALSE] |
| 394 |
} |