Skip to content

Commit 058e3b1

Browse files
committed
more generic to play nice with terra
1 parent a199b09 commit 058e3b1

16 files changed

+164
-89
lines changed

.Rbuildignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.gitignore
22
LICENSE
33
README.md
4+
TODO
45
^revdep$
56
^.*\.Rproj$
67
^\.Rproj\.user$

ChangeLog

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1-
--- to do
2-
focal for multi-layer objects
3-
step-size option for focal
4-
add equivalent to "subsWithNA" to reclassify
5-
add AMSR-E ice products and other raw binary products
6-
add orientation fixes for AVISO currents and similar NetCDFs lacking metadata on the internal CRS
7-
add VRT output to saveStack
8-
Fix lines falling between the cracks in extract (calling rasterzeLines): https://stat.ethz.ch/pipermail/r-sig-geo/2014-May/020973.html
9-
Better handling of NA values in 1 byte raster files
101

112
--- current version
123

4+
135
--- 2-Nov-2018, version: 2.8-4
146
Improved estimation of available RAM with contributions by Lorenzo Busetto. RAM available is now computed on windows, linux and mac. No more that 60% of available RAM is used (if the estimated RAM needed is not too low). Not more than raster:::.maxmemory() is used; but it should now be safe to set this to Inf. If canProcessInMemory() returns FALSE, chunksize is set 25% of available RAM, or raster:::.chunksize(), whichever is lower. (set options via rasterOptions())
157

168
testthat unit tests introduced by Mike Sumner and Jakub Nowosad
179
as.character() for Raster objects to create the R code to re-create the Raster skeleton in examples.
1810

19-
Reading point values via GDAL has become slower in 2.7-15, reverted to previous function.
11+
Reading point values via GDAL has become slower in 2.7-15, reverted to previous function. Thanks to Andrew Brown and Dylan Beaudette for identifying the problem.
2012

2113
Bug fixes:
2214
fixed the link for GADM countries download. Bug reported by Loic Dutrieux
@@ -26,7 +18,7 @@ fixed problem with subs reported by Andy Craig https://github.com/rspatial/raste
2618
Previous version had a maxmemory value that was too high (reported by Lorenzo Busetto)
2719

2820
--- 16-Oct-2018, version: 2.7-15
29-
faster extraction for points via GDAL
21+
faster extraction for points via GDAL (reverted to previous code in version 2.8-4)
3022
faster rasterization of polygons
3123

3224
Bug fixes:
@@ -284,7 +276,7 @@ Fixed bug with RAT tables in native format (reported by Joseph Steward)
284276
Fixed corner case bug with extract/polygons df=TRUE (reported by Jon Olav Skoien)
285277
Added big.matrix as a "driver" (file format). This is experimental (and not documented).
286278
raster now attempts to interpret the CRS from netcdf files
287-
added support for the 360 day calander in netcdf files
279+
added support for the 360 day calendar in netcdf files
288280

289281

290282
--- 1-September-2012, version 2.0-12

DESCRIPTION

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ Type: Package
33
Title: Geographic Data Analysis and Modeling
44
Version: 2.8-5
55
Date: 2018-11-02
6-
Depends: methods, sp (>= 1.2-0), R (>= 3.0.0)
6+
Depends: sp (>= 1.2-0), R (>= 3.0.0)
77
Suggests: rgdal (>= 0.9-1), rgeos (>= 0.3-8), ncdf4, igraph, tcltk, parallel, rasterVis, MASS, sf, testthat
88
LinkingTo: Rcpp
9-
Imports: Rcpp
9+
Imports: methods, Rcpp
1010
SystemRequirements: C++11
1111
Authors@R: c(
1212
person("Robert J.", "Hijmans", role = c("cre", "aut"),

NAMESPACE

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import(sp)
22
import(Rcpp)
3-
importFrom("methods", "Ops", "Math", "as", ".hasSlot", "validObject")
3+
import(methods)
44
importFrom("stats", as.formula, aggregate, predict, density, quantile, update, approx, reshape)
55
importFrom("utils", stack, unstack, head, tail, download.file)
6-
importFrom("graphics", "hist", "lines", "image", "rect", "filled.contour", "contour", "persp", "pairs", "text", "boxplot", "points", "abline", "legend", "title", "strheight", "strwidth")
7-
importFrom("grDevices", "as.raster", "terrain.colors", "heat.colors", "col2rgb", "rainbow", "dev.flush", "dev.off", "dev.new", "xy.coords", "png")
6+
importFrom(graphics, hist, lines, image, rect, filled.contour, contour, persp, pairs, plot, text, boxplot, points, abline, legend, title, strheight, strwidth)
7+
importFrom(grDevices, as.raster, terrain.colors, heat.colors, col2rgb, rainbow, dev.flush, dev.off, dev.new, xy.coords, png)
88
exportClasses(Extent, BasicRaster, Raster, RasterLayer, RasterBrick, RasterStack, RasterStackBrick)
9-
exportMethods("[", "==", '!=', '!', approxNA, as.array, as.character, as.vector, as.matrix, area, bandnr, barplot, brick, calc, clump, crop, disaggregate, distance, erase, extent, extract, extend, flip, head, intersect, is.factor, merge, mosaic, ncell, nlayers, overlay, plot, plotRGB, raster, reclassify, resample, rotate, sampleRandom, stackSelect, shift, subs, t, tail, trim, unique, unstack, union, xmin, xmax, ymin, ymax, zonal)
9+
exportMethods("[", "==", '!=', '!', approxNA, as.array, as.character, as.vector, as.matrix, area, bandnr, barplot, brick, calc, clump, crop, crs, "crs<-", disaggregate, distance, erase, extent, extract, extend, flip, focal, geom, head, intersect, is.factor, mask, merge, mosaic, ncell, nlayers, "nrow<-", "ncol<-", overlay, plot, plotRGB, raster, rasterize, reclassify, res, resample, rotate, sampleRandom, stackSelect, shift, subs, t, tail, trim, unique, unstack, union, xmin, xmax, xres, ymin, ymax, yres, zonal, yFromRow, xFromCol,colFromX, rowFromY, cellFromXY, cellFromRowCol, xyFromCell, yFromCell, xFromCell, rowColFromCell,rowFromCell, colFromCell, readStart, readStop, values, "values<-", writeStart, writeStop, writeValues, writeRaster)
1010
useDynLib(raster, .registration = TRUE)
1111
exportPattern("^[^\\.\\_]")
12+

R/AAgeneric_functions.R

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@ if (!isGeneric("mask")) { setGeneric("mask", function(x, mask, ...) standardGene
22
if (!isGeneric("crop")) { setGeneric("crop", function(x, y, ...) standardGeneric("crop")) }
33
if (!isGeneric("trim")) { setGeneric("trim", function(x, ...) standardGeneric("trim")) }
44
if (!isGeneric("focal")) { setGeneric("focal", function(x, ...) standardGeneric("focal")) }
5+
if (!isGeneric("geom")) {setGeneric("geom", function(x, ...) standardGeneric("geom")) }
6+
7+
58
if (!isGeneric("rasterize")) {setGeneric("rasterize", function(x, y, ...) standardGeneric("rasterize"))}
69

10+
if (!isGeneric("as.data.frame")) { setGeneric("as.data.frame", function(x, row.names = NULL, optional = FALSE, ...) standardGeneric("as.data.frame")) }
11+
12+
13+
if (!isGeneric("nrow<-")) { setGeneric("nrow<-", function(x, ..., value) standardGeneric("nrow<-")) }
14+
if (!isGeneric("ncol<-")) { setGeneric("ncol<-", function(x, ..., value) standardGeneric("ncol<-")) }
15+
716
if (!isGeneric("crs")) { setGeneric("crs", function(x, ...) standardGeneric("crs")) }
817
if (!isGeneric("crs<-")) { setGeneric("crs<-", function(x, ..., value) standardGeneric("crs<-")) }
918
if (!isGeneric("ncell")) { setGeneric("ncell", function(x) standardGeneric("ncell")) }
@@ -13,17 +22,13 @@ if (!isGeneric("yres")) { setGeneric("yres", function(x) standardGeneric("yres")
1322
if (!isGeneric("values")) { setGeneric("values", function(x, ...) standardGeneric("values")) }
1423
if (!isGeneric("values<-")) { setGeneric("values<-", function(x, value) standardGeneric("values<-"))}
1524
if (!isGeneric('writeRaster')) {setGeneric('writeRaster', function(x, filename, ...) standardGeneric('writeRaster'))}
16-
17-
1825
if (!isGeneric("yFromRow")) { setGeneric("yFromRow", function(object, row) standardGeneric("yFromRow")) }
1926
if (!isGeneric("xFromCol")) { setGeneric("xFromCol", function(object, col) standardGeneric("xFromCol")) }
2027
if (!isGeneric("colFromX")) { setGeneric("colFromX", function(object, x) standardGeneric("colFromX")) }
2128
if (!isGeneric("rowFromY")) { setGeneric("rowFromY", function(object, y) standardGeneric("rowFromY")) }
2229
if (!isGeneric("cellFromXY")) { setGeneric("cellFromXY", function(object, xy) standardGeneric("cellFromXY")) }
2330
if (!isGeneric("cellFromRowCol")) { setGeneric("cellFromRowCol", function(object, row, col) standardGeneric("cellFromRowCol")) }
24-
2531
if (!isGeneric("xyFromCell")) { setGeneric("xyFromCell", function(object, cell, ...) standardGeneric("xyFromCell")) }
26-
2732
if (!isGeneric("yFromCell")) { setGeneric("yFromCell", function(object, cell) standardGeneric("yFromCell")) }
2833
if (!isGeneric("xFromCell")) { setGeneric("xFromCell", function(object, cell) standardGeneric("xFromCell")) }
2934
if (!isGeneric("rowColFromCell")) { setGeneric("rowColFromCell", function(object, cell) standardGeneric("rowColFromCell")) }

R/as.data.frame.R

-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
# Version 1.0
44
# Licence GPL v3
55

6-
if (!isGeneric("as.data.frame")) {
7-
setGeneric("as.data.frame", function(x, row.names = NULL, optional = FALSE, ...)
8-
standardGeneric("as.data.frame"))
9-
}
106

117

128
.insertColsInDF <- function(x, y, col, combinenames=TRUE) {

R/cellRowCol.R

+68-28
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@
44
# Licence GPL v3
55

66

7-
8-
rowFromCell <- function(object, cell) {
9-
object <- raster(object)
10-
cell <- round(cell)
11-
cell[cell < 1 | cell > ncell(object)] <- NA
12-
trunc((cell-1)/ncol(object)) + 1
13-
}
7+
setMethod(rowFromCell, signature(object="BasicRaster", cell="numeric"),
8+
function(object, cell) {
9+
object <- raster(object)
10+
cell <- round(cell)
11+
cell[cell < 1 | cell > ncell(object)] <- NA
12+
trunc((cell-1)/ncol(object)) + 1
13+
}
14+
)
15+
16+
#rowFromCell <- function(object, cell) {
17+
# object <- raster(object)
18+
# cell <- round(cell)
19+
# cell[cell < 1 | cell > ncell(object)] <- NA
20+
# trunc((cell-1)/ncol(object)) + 1
21+
#}
22+
23+
1424

1525
.rowFromCell <- function(object, cell) {
1626
trunc((cell-1)/ncol(object)) + 1
@@ -57,35 +67,65 @@ cellFromRowColCombine <- function(object, rownr, colnr) {
5767
as.vector(t(cols))
5868
}
5969

60-
61-
colFromCell <- function(object, cell) {
62-
object <- raster(object)
63-
cell <- round(cell)
64-
cell[cell < 1 | cell > ncell(object)] <- NA
65-
rownr <- trunc((cell-1)/object@ncols) + 1
66-
as.integer(cell - ((rownr-1) * object@ncols))
67-
}
70+
setMethod(colFromCell, signature(object="BasicRaster", cell="numeric"),
71+
function(object, cell) {
72+
object <- raster(object)
73+
cell <- round(cell)
74+
cell[cell < 1 | cell > ncell(object)] <- NA
75+
rownr <- trunc((cell-1)/object@ncols) + 1
76+
as.integer(cell - ((rownr-1) * object@ncols))
77+
}
78+
)
79+
80+
#colFromCell <- function(object, cell) {
81+
# object <- raster(object)
82+
# cell <- round(cell)
83+
# cell[cell < 1 | cell > ncell(object)] <- NA
84+
# rownr <- trunc((cell-1)/object@ncols) + 1
85+
# as.integer(cell - ((rownr-1) * object@ncols))
86+
#}
6887

6988
.colFromCell <- function(object, cell) {
7089
nc <- object@ncols
7190
rownr <- trunc((cell-1)/nc) + 1
7291
cell - ((rownr-1) * nc)
7392
}
7493

75-
rowColFromCell <- function(object, cell) {
76-
object <- raster(object)
77-
cell <- round(cell)
78-
cell[cell < 1 | cell > ncell(object)] <- NA
79-
row <- as.integer(trunc((cell-1)/object@ncols) + 1)
80-
col <- as.integer(cell - ((row-1) * object@ncols))
81-
return(cbind(row, col))
82-
}
8394

84-
cellFromRowCol <- function(object, rownr, colnr) {
85-
rows <- object@nrows
86-
cols <- object@ncols
87-
.doCellFromRowCol(rows, cols, rownr, colnr)
88-
}
95+
setMethod(rowColFromCell, signature(object="BasicRaster", cell="numeric"),
96+
function(object, cell) {
97+
object <- raster(object)
98+
cell <- round(cell)
99+
cell[cell < 1 | cell > ncell(object)] <- NA
100+
row <- as.integer(trunc((cell-1)/object@ncols) + 1)
101+
col <- as.integer(cell - ((row-1) * object@ncols))
102+
return(cbind(row, col))
103+
}
104+
)
105+
106+
#rowColFromCell <- function(object, cell) {
107+
# object <- raster(object)
108+
# cell <- round(cell)
109+
# cell[cell < 1 | cell > ncell(object)] <- NA
110+
# row <- as.integer(trunc((cell-1)/object@ncols) + 1)
111+
# col <- as.integer(cell - ((row-1) * object@ncols))
112+
# return(cbind(row, col))
113+
#}
114+
115+
116+
setMethod(cellFromRowCol, signature(object="BasicRaster", row="numeric", col="numeric"),
117+
function(object, row, col) {
118+
rows <- object@nrows
119+
cols <- object@ncols
120+
.doCellFromRowCol(rows, cols, row, col)
121+
}
122+
)
123+
124+
#cellFromRowCol <- function(object, rownr, colnr) {
125+
# rows <- object@nrows
126+
# cols <- object@ncols
127+
# .doCellFromRowCol(rows, cols, rownr, colnr)
128+
#}
89129

90130

91131

R/geom.R

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11

2-
if (!isGeneric("geom")) {
3-
setGeneric("geom", function(x, ...)
4-
standardGeneric("geom"))
5-
}
62

73
setMethod('geom', signature(x='SpatialPolygons'),
84
function(x, sepNA=FALSE, ...) {

R/projection.R

+17-4
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,24 @@ setMethod("crs", signature('ANY'),
1313
)
1414

1515

16-
'crs<-' <- function(x, value) {
17-
projection(x) <- value
18-
x
19-
}
16+
#'crs<-' <- function(x, value) {
17+
# projection(x) <- value
18+
# x
19+
#}
20+
21+
setMethod("crs<-", signature('BasicRaster', 'ANY'),
22+
function(x, ..., value) {
23+
projection(x) <- value
24+
x
25+
}
26+
)
2027

28+
setMethod("crs<-", signature('Spatial', 'ANY'),
29+
function(x, ..., value) {
30+
projection(x) <- value
31+
x
32+
}
33+
)
2134

2235
setMethod('is.na', signature(x='CRS'),
2336
function(x) {

R/replaceProperties.R

+21-8
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,29 @@
44
# Licence GPL v3
55

66

7+
setMethod("ncol<-", signature('BasicRaster', 'numeric'),
8+
function(x, ..., value) {
9+
dim(x) <- c(nrow(x), value)
10+
return(x)
11+
}
12+
)
713

8-
'ncol<-' <- function(x, value) {
9-
dim(x) <- c(nrow(x), value)
10-
return(x)
11-
}
14+
setMethod("nrow<-", signature('BasicRaster', 'numeric'),
15+
function(x, ..., value) {
16+
dim(x) <- c(value, ncol(x))
17+
return(x)
18+
}
19+
)
1220

13-
'nrow<-' <- function(x, value) {
14-
dim(x) <- c(value, ncol(x))
15-
return(x)
16-
}
21+
#'ncol<-' <- function(x, value) {
22+
# dim(x) <- c(nrow(x), value)
23+
# return(x)
24+
#}
25+
26+
#'nrow<-' <- function(x, value) {
27+
# dim(x) <- c(value, ncol(x))
28+
# return(x)
29+
#}
1730

1831

1932
'xmin<-' <- function(x, value) {

R/xyCell.R

+10-8
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,12 @@ setMethod("cellFromXY", signature(object="BasicRaster", xy="ANY"),
9292
}
9393
)
9494

95-
setMethod("colFromX", signature(object="Raster", x="numeric"),
95+
setMethod("colFromX", signature(object="BasicRaster", x="numeric"),
9696
function ( object, x ) {
97-
if (inherits(x, 'Spatial')) {
98-
x <- x@coords[,1]
99-
}
97+
# from pre-generic
98+
# if (inherits(x, 'Spatial')) {
99+
# x <- x@coords[,1]
100+
# }
100101
if (rotated(object)) {
101102
stop('this function is not supported for rotated rasters')
102103
}
@@ -108,11 +109,12 @@ setMethod("colFromX", signature(object="Raster", x="numeric"),
108109
)
109110

110111

111-
setMethod("rowFromY", signature(object="Raster", y="numeric"),
112+
setMethod("rowFromY", signature(object="BasicRaster", y="numeric"),
112113
function(object, y) {
113-
if (inherits(y, 'Spatial')) {
114-
y <- y@coords[,2]
115-
}
114+
# from pre-generic
115+
# if (inherits(y, 'Spatial')) {
116+
# y <- y@coords[,2]
117+
# }
116118
if (rotated(object)) {
117119
stop('this function is not supported for rotated rasters')
118120
}

TODO

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--- to do
2+
focal for multi-layer objects
3+
step-size option for focal
4+
add equivalent to "subsWithNA" to reclassify
5+
add AMSR-E ice products and other raw binary products
6+
add orientation fixes for AVISO currents and similar NetCDFs lacking metadata on the internal CRS
7+
add VRT output to saveStack
8+
Fix lines falling between the cracks in extract (calling rasterzeLines): https://stat.ethz.ch/pipermail/r-sig-geo/2014-May/020973.html
9+
Better handling of NA values in 1 byte raster files

man/cellFrom.Rd

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
\name{cellFrom}
22

33
\alias{cellFromRowCol}
4-
\alias{cellFromRowCol,Raster,numeric,numeric-method}
4+
\alias{cellFromRowCol,BasicRaster,numeric,numeric-method}
55
\alias{colFromX}
6-
\alias{colFromX,Raster,numeric-method}
6+
\alias{colFromX,BasicRaster,numeric-method}
77
\alias{rowFromY}
8-
\alias{rowFromY,Raster,numeric-method}
8+
\alias{rowFromY,BasicRaster,numeric-method}
99
\alias{cellFromXY}
1010
\alias{cellFromXY,BasicRaster-method}
1111

@@ -27,7 +27,7 @@ The last cell number equals the number of cells of the Raster* object.
2727
}
2828

2929
\usage{
30-
cellFromRowCol(object, rownr, colnr)
30+
cellFromRowCol(object, row, col)
3131
cellFromRowColCombine(object, rownr, colnr)
3232
cellFromRow(object, rownr)
3333
cellFromCol(object, colnr)
@@ -43,6 +43,8 @@ fourCellsFromXY(object, xy, duplicates=TRUE)
4343
\item{object}{Raster* object (or a SpatialPixels* or SpatialGrid* object)}
4444
\item{colnr}{column number; or vector of column numbers}
4545
\item{rownr}{row number; or vector of row numbers}
46+
\item{col}{column number; or vector of column numbers}
47+
\item{row}{row number; or vector of row numbers}
4648
\item{x}{x coordinate(s)}
4749
\item{y}{y coordinate(s)}
4850
\item{xy}{matrix of x and y coordinates, or a SpatialPoints or SpatialPointsDataFrame object}

0 commit comments

Comments
 (0)