R/rectHull.predict.R

# Author: Robert J. Hijmans
# Date : June 2016
# Version 1
# Licence GPL v3



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

setMethod('predict', signature(object='RectangularHull'), 
	function(object, x, ext=NULL, mask=FALSE, filename='',  ...) {
	
		nc <- nrow(object@polygons@data)
		if ( extends(class(x), 'Raster'))  {
			if (! mask) {
				x <- raster(x)
			}
			if (! is.null(ext)) { 
				x <- crop(x, ext) 
			}
			
			xx <- rasterize(object@polygons, raster(x), field=1, fun='sum', background=0, mask=FALSE, update=FALSE, updateValue="NA", getCover=FALSE, silent=TRUE, ...)
			if (mask) {
				xx <- mask(xx, x, ...)
			}
			
			fun <- function(x){x / nc }
			xx <- calc(xx, fun=fun, filename=filename, ...)
			return(xx)
			
		} else {
		
			if (! inherits(x, 'SpatialPoints') )  {
				x <- data.frame(x[,1:2])
				colnames(x) <- c('x', 'y')
				coordinates(x) <- ~ x + y
			}
			v <- .pointsInPolygons(x, object@polygons, sum)
			return(v)
			
		}
	}
)

Try the dismo package in your browser

Any scripts or data that you put into this service are public.

dismo documentation built on May 2, 2019, 6:07 p.m.