Skip to content

Commit

Permalink
more generic to play nice with terra
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Nov 5, 2018
1 parent a199b09 commit 058e3b1
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 89 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.gitignore
LICENSE
README.md
TODO
^revdep$
^.*\.Rproj$
^\.Rproj\.user$
Expand Down
16 changes: 4 additions & 12 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
--- to do
focal for multi-layer objects
step-size option for focal
add equivalent to "subsWithNA" to reclassify
add AMSR-E ice products and other raw binary products
add orientation fixes for AVISO currents and similar NetCDFs lacking metadata on the internal CRS
add VRT output to saveStack
Fix lines falling between the cracks in extract (calling rasterzeLines): https://stat.ethz.ch/pipermail/r-sig-geo/2014-May/020973.html
Better handling of NA values in 1 byte raster files

--- current version


--- 2-Nov-2018, version: 2.8-4
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())

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

Reading point values via GDAL has become slower in 2.7-15, reverted to previous function.
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.

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

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

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


--- 1-September-2012, version 2.0-12
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ Type: Package
Title: Geographic Data Analysis and Modeling
Version: 2.8-5
Date: 2018-11-02
Depends: methods, sp (>= 1.2-0), R (>= 3.0.0)
Depends: sp (>= 1.2-0), R (>= 3.0.0)
Suggests: rgdal (>= 0.9-1), rgeos (>= 0.3-8), ncdf4, igraph, tcltk, parallel, rasterVis, MASS, sf, testthat
LinkingTo: Rcpp
Imports: Rcpp
Imports: methods, Rcpp
SystemRequirements: C++11
Authors@R: c(
person("Robert J.", "Hijmans", role = c("cre", "aut"),
Expand Down
9 changes: 5 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import(sp)
import(Rcpp)
importFrom("methods", "Ops", "Math", "as", ".hasSlot", "validObject")
import(methods)
importFrom("stats", as.formula, aggregate, predict, density, quantile, update, approx, reshape)
importFrom("utils", stack, unstack, head, tail, download.file)
importFrom("graphics", "hist", "lines", "image", "rect", "filled.contour", "contour", "persp", "pairs", "text", "boxplot", "points", "abline", "legend", "title", "strheight", "strwidth")
importFrom("grDevices", "as.raster", "terrain.colors", "heat.colors", "col2rgb", "rainbow", "dev.flush", "dev.off", "dev.new", "xy.coords", "png")
importFrom(graphics, hist, lines, image, rect, filled.contour, contour, persp, pairs, plot, text, boxplot, points, abline, legend, title, strheight, strwidth)
importFrom(grDevices, as.raster, terrain.colors, heat.colors, col2rgb, rainbow, dev.flush, dev.off, dev.new, xy.coords, png)
exportClasses(Extent, BasicRaster, Raster, RasterLayer, RasterBrick, RasterStack, RasterStackBrick)
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)
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)
useDynLib(raster, .registration = TRUE)
exportPattern("^[^\\.\\_]")

13 changes: 9 additions & 4 deletions R/AAgeneric_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ if (!isGeneric("mask")) { setGeneric("mask", function(x, mask, ...) standardGene
if (!isGeneric("crop")) { setGeneric("crop", function(x, y, ...) standardGeneric("crop")) }
if (!isGeneric("trim")) { setGeneric("trim", function(x, ...) standardGeneric("trim")) }
if (!isGeneric("focal")) { setGeneric("focal", function(x, ...) standardGeneric("focal")) }
if (!isGeneric("geom")) {setGeneric("geom", function(x, ...) standardGeneric("geom")) }


if (!isGeneric("rasterize")) {setGeneric("rasterize", function(x, y, ...) standardGeneric("rasterize"))}

if (!isGeneric("as.data.frame")) { setGeneric("as.data.frame", function(x, row.names = NULL, optional = FALSE, ...) standardGeneric("as.data.frame")) }


if (!isGeneric("nrow<-")) { setGeneric("nrow<-", function(x, ..., value) standardGeneric("nrow<-")) }
if (!isGeneric("ncol<-")) { setGeneric("ncol<-", function(x, ..., value) standardGeneric("ncol<-")) }

if (!isGeneric("crs")) { setGeneric("crs", function(x, ...) standardGeneric("crs")) }
if (!isGeneric("crs<-")) { setGeneric("crs<-", function(x, ..., value) standardGeneric("crs<-")) }
if (!isGeneric("ncell")) { setGeneric("ncell", function(x) standardGeneric("ncell")) }
Expand All @@ -13,17 +22,13 @@ if (!isGeneric("yres")) { setGeneric("yres", function(x) standardGeneric("yres")
if (!isGeneric("values")) { setGeneric("values", function(x, ...) standardGeneric("values")) }
if (!isGeneric("values<-")) { setGeneric("values<-", function(x, value) standardGeneric("values<-"))}
if (!isGeneric('writeRaster')) {setGeneric('writeRaster', function(x, filename, ...) standardGeneric('writeRaster'))}


if (!isGeneric("yFromRow")) { setGeneric("yFromRow", function(object, row) standardGeneric("yFromRow")) }
if (!isGeneric("xFromCol")) { setGeneric("xFromCol", function(object, col) standardGeneric("xFromCol")) }
if (!isGeneric("colFromX")) { setGeneric("colFromX", function(object, x) standardGeneric("colFromX")) }
if (!isGeneric("rowFromY")) { setGeneric("rowFromY", function(object, y) standardGeneric("rowFromY")) }
if (!isGeneric("cellFromXY")) { setGeneric("cellFromXY", function(object, xy) standardGeneric("cellFromXY")) }
if (!isGeneric("cellFromRowCol")) { setGeneric("cellFromRowCol", function(object, row, col) standardGeneric("cellFromRowCol")) }

if (!isGeneric("xyFromCell")) { setGeneric("xyFromCell", function(object, cell, ...) standardGeneric("xyFromCell")) }

if (!isGeneric("yFromCell")) { setGeneric("yFromCell", function(object, cell) standardGeneric("yFromCell")) }
if (!isGeneric("xFromCell")) { setGeneric("xFromCell", function(object, cell) standardGeneric("xFromCell")) }
if (!isGeneric("rowColFromCell")) { setGeneric("rowColFromCell", function(object, cell) standardGeneric("rowColFromCell")) }
Expand Down
4 changes: 0 additions & 4 deletions R/as.data.frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
# Version 1.0
# Licence GPL v3

if (!isGeneric("as.data.frame")) {
setGeneric("as.data.frame", function(x, row.names = NULL, optional = FALSE, ...)
standardGeneric("as.data.frame"))
}


.insertColsInDF <- function(x, y, col, combinenames=TRUE) {
Expand Down
96 changes: 68 additions & 28 deletions R/cellRowCol.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
# Licence GPL v3



rowFromCell <- function(object, cell) {
object <- raster(object)
cell <- round(cell)
cell[cell < 1 | cell > ncell(object)] <- NA
trunc((cell-1)/ncol(object)) + 1
}
setMethod(rowFromCell, signature(object="BasicRaster", cell="numeric"),
function(object, cell) {
object <- raster(object)
cell <- round(cell)
cell[cell < 1 | cell > ncell(object)] <- NA
trunc((cell-1)/ncol(object)) + 1
}
)

#rowFromCell <- function(object, cell) {
# object <- raster(object)
# cell <- round(cell)
# cell[cell < 1 | cell > ncell(object)] <- NA
# trunc((cell-1)/ncol(object)) + 1
#}



.rowFromCell <- function(object, cell) {
trunc((cell-1)/ncol(object)) + 1
Expand Down Expand Up @@ -57,35 +67,65 @@ cellFromRowColCombine <- function(object, rownr, colnr) {
as.vector(t(cols))
}


colFromCell <- function(object, cell) {
object <- raster(object)
cell <- round(cell)
cell[cell < 1 | cell > ncell(object)] <- NA
rownr <- trunc((cell-1)/object@ncols) + 1
as.integer(cell - ((rownr-1) * object@ncols))
}
setMethod(colFromCell, signature(object="BasicRaster", cell="numeric"),
function(object, cell) {
object <- raster(object)
cell <- round(cell)
cell[cell < 1 | cell > ncell(object)] <- NA
rownr <- trunc((cell-1)/object@ncols) + 1
as.integer(cell - ((rownr-1) * object@ncols))
}
)

#colFromCell <- function(object, cell) {
# object <- raster(object)
# cell <- round(cell)
# cell[cell < 1 | cell > ncell(object)] <- NA
# rownr <- trunc((cell-1)/object@ncols) + 1
# as.integer(cell - ((rownr-1) * object@ncols))
#}

.colFromCell <- function(object, cell) {
nc <- object@ncols
rownr <- trunc((cell-1)/nc) + 1
cell - ((rownr-1) * nc)
}

rowColFromCell <- function(object, cell) {
object <- raster(object)
cell <- round(cell)
cell[cell < 1 | cell > ncell(object)] <- NA
row <- as.integer(trunc((cell-1)/object@ncols) + 1)
col <- as.integer(cell - ((row-1) * object@ncols))
return(cbind(row, col))
}

cellFromRowCol <- function(object, rownr, colnr) {
rows <- object@nrows
cols <- object@ncols
.doCellFromRowCol(rows, cols, rownr, colnr)
}
setMethod(rowColFromCell, signature(object="BasicRaster", cell="numeric"),
function(object, cell) {
object <- raster(object)
cell <- round(cell)
cell[cell < 1 | cell > ncell(object)] <- NA
row <- as.integer(trunc((cell-1)/object@ncols) + 1)
col <- as.integer(cell - ((row-1) * object@ncols))
return(cbind(row, col))
}
)

#rowColFromCell <- function(object, cell) {
# object <- raster(object)
# cell <- round(cell)
# cell[cell < 1 | cell > ncell(object)] <- NA
# row <- as.integer(trunc((cell-1)/object@ncols) + 1)
# col <- as.integer(cell - ((row-1) * object@ncols))
# return(cbind(row, col))
#}


setMethod(cellFromRowCol, signature(object="BasicRaster", row="numeric", col="numeric"),
function(object, row, col) {
rows <- object@nrows
cols <- object@ncols
.doCellFromRowCol(rows, cols, row, col)
}
)

#cellFromRowCol <- function(object, rownr, colnr) {
# rows <- object@nrows
# cols <- object@ncols
# .doCellFromRowCol(rows, cols, rownr, colnr)
#}



Expand Down
4 changes: 0 additions & 4 deletions R/geom.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@

if (!isGeneric("geom")) {
setGeneric("geom", function(x, ...)
standardGeneric("geom"))
}

setMethod('geom', signature(x='SpatialPolygons'),
function(x, sepNA=FALSE, ...) {
Expand Down
21 changes: 17 additions & 4 deletions R/projection.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,24 @@ setMethod("crs", signature('ANY'),
)


'crs<-' <- function(x, value) {
projection(x) <- value
x
}
#'crs<-' <- function(x, value) {
# projection(x) <- value
# x
#}

setMethod("crs<-", signature('BasicRaster', 'ANY'),
function(x, ..., value) {
projection(x) <- value
x
}
)

setMethod("crs<-", signature('Spatial', 'ANY'),
function(x, ..., value) {
projection(x) <- value
x
}
)

setMethod('is.na', signature(x='CRS'),
function(x) {
Expand Down
29 changes: 21 additions & 8 deletions R/replaceProperties.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,29 @@
# Licence GPL v3


setMethod("ncol<-", signature('BasicRaster', 'numeric'),
function(x, ..., value) {
dim(x) <- c(nrow(x), value)
return(x)
}
)

'ncol<-' <- function(x, value) {
dim(x) <- c(nrow(x), value)
return(x)
}
setMethod("nrow<-", signature('BasicRaster', 'numeric'),
function(x, ..., value) {
dim(x) <- c(value, ncol(x))
return(x)
}
)

'nrow<-' <- function(x, value) {
dim(x) <- c(value, ncol(x))
return(x)
}
#'ncol<-' <- function(x, value) {
# dim(x) <- c(nrow(x), value)
# return(x)
#}

#'nrow<-' <- function(x, value) {
# dim(x) <- c(value, ncol(x))
# return(x)
#}


'xmin<-' <- function(x, value) {
Expand Down
18 changes: 10 additions & 8 deletions R/xyCell.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ setMethod("cellFromXY", signature(object="BasicRaster", xy="ANY"),
}
)

setMethod("colFromX", signature(object="Raster", x="numeric"),
setMethod("colFromX", signature(object="BasicRaster", x="numeric"),
function ( object, x ) {
if (inherits(x, 'Spatial')) {
x <- x@coords[,1]
}
# from pre-generic
# if (inherits(x, 'Spatial')) {
# x <- x@coords[,1]
# }
if (rotated(object)) {
stop('this function is not supported for rotated rasters')
}
Expand All @@ -108,11 +109,12 @@ setMethod("colFromX", signature(object="Raster", x="numeric"),
)


setMethod("rowFromY", signature(object="Raster", y="numeric"),
setMethod("rowFromY", signature(object="BasicRaster", y="numeric"),
function(object, y) {
if (inherits(y, 'Spatial')) {
y <- y@coords[,2]
}
# from pre-generic
# if (inherits(y, 'Spatial')) {
# y <- y@coords[,2]
# }
if (rotated(object)) {
stop('this function is not supported for rotated rasters')
}
Expand Down
9 changes: 9 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--- to do
focal for multi-layer objects
step-size option for focal
add equivalent to "subsWithNA" to reclassify
add AMSR-E ice products and other raw binary products
add orientation fixes for AVISO currents and similar NetCDFs lacking metadata on the internal CRS
add VRT output to saveStack
Fix lines falling between the cracks in extract (calling rasterzeLines): https://stat.ethz.ch/pipermail/r-sig-geo/2014-May/020973.html
Better handling of NA values in 1 byte raster files
10 changes: 6 additions & 4 deletions man/cellFrom.Rd
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
\name{cellFrom}

\alias{cellFromRowCol}
\alias{cellFromRowCol,Raster,numeric,numeric-method}
\alias{cellFromRowCol,BasicRaster,numeric,numeric-method}
\alias{colFromX}
\alias{colFromX,Raster,numeric-method}
\alias{colFromX,BasicRaster,numeric-method}
\alias{rowFromY}
\alias{rowFromY,Raster,numeric-method}
\alias{rowFromY,BasicRaster,numeric-method}
\alias{cellFromXY}
\alias{cellFromXY,BasicRaster-method}

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

\usage{
cellFromRowCol(object, rownr, colnr)
cellFromRowCol(object, row, col)
cellFromRowColCombine(object, rownr, colnr)
cellFromRow(object, rownr)
cellFromCol(object, colnr)
Expand All @@ -43,6 +43,8 @@ fourCellsFromXY(object, xy, duplicates=TRUE)
\item{object}{Raster* object (or a SpatialPixels* or SpatialGrid* object)}
\item{colnr}{column number; or vector of column numbers}
\item{rownr}{row number; or vector of row numbers}
\item{col}{column number; or vector of column numbers}
\item{row}{row number; or vector of row numbers}
\item{x}{x coordinate(s)}
\item{y}{y coordinate(s)}
\item{xy}{matrix of x and y coordinates, or a SpatialPoints or SpatialPointsDataFrame object}
Expand Down
Loading

0 comments on commit 058e3b1

Please sign in to comment.