Permalink
Browse files

Merge pull request #61 from ianmseddy/development

gdalwarp fix
  • Loading branch information...
eliotmcintire committed Oct 11, 2018
2 parents d2aea22 + 563ccd0 commit 242dbf03e9e138f140c0e67cdbda2ffc2d15a1b3
Showing with 20 additions and 6 deletions.
  1. +1 −0 NAMESPACE
  2. +2 −2 R/gis.R
  3. +17 −4 R/postProcess.R
View
@@ -221,6 +221,7 @@ importFrom(raster,projectExtent)
importFrom(raster,projectRaster)
importFrom(raster,raster)
importFrom(raster,res)
importFrom(raster,sampleRandom)
importFrom(raster,shapefile)
importFrom(raster,stack)
importFrom(raster,writeRaster)
View
@@ -148,9 +148,9 @@ fastMask <- function(x, y) {
paste0(paste0(getOption("gdalUtils_gdalPath")[[1]]$path, "gdalwarp", exe, " "),
"-multi ",
"-ot ",
dType, " ",
"-crop_to_cutline ",
"-cutline ", tempSrcShape, " ",
dType,
"-cutline ", "\"", tempSrcShape,"\"", " ",
" -overwrite ",
"-tr ", paste(tr, collapse = " "), " ",
"\"", tempSrcRaster, "\"", " ",
View
@@ -313,7 +313,14 @@ cropInputs.spatialObjects <- function(x, studyArea = NULL, rasterToMatch = NULL,
message(" cropping ...")
dots <- list(...)
dots[.formalsNotInCurrentDots("crop", ...)] <- NULL
if (canProcessInMemory(x, 4)) {
x <- do.call(raster::crop, args = append(list(x = x, y = cropExtent), dots))
} else {
x <- do.call(raster::crop, args = append(list(x = x,
y = cropExtent,
filename = paste0(tempfile(), ".tif")),
dots))
}
if (is.null(x)) {
message(" polygons do not intersect.")
}
@@ -437,6 +444,7 @@ projectInputs.Raster <- function(x, targetCRS = NULL, rasterToMatch = NULL, ...)
!identical(res(x), res(rasterToMatch)) |
!identical(extent(x), extent(rasterToMatch))) {
message(" reprojecting ...")
if (canProcessInMemory(x, 4)) {
tempRas <- projectExtent(object = rasterToMatch, crs = targetCRS) ## make a template RTM, with targetCRS
warn <- capture_warnings(x <- projectRaster(from = x, to = tempRas, ...))
@@ -448,6 +456,7 @@ projectInputs.Raster <- function(x, targetCRS = NULL, rasterToMatch = NULL, ...)
## the different projections (e.g. degree based and meter based). This should be fine
if (identical(crs(x), crs(rasterToMatch)) &
any(res(x) != res(rasterToMatch))) {
if (all(res(x) %==% res(rasterToMatch))) {
res(x) <- res(rasterToMatch)
} else {
@@ -456,6 +465,7 @@ projectInputs.Raster <- function(x, targetCRS = NULL, rasterToMatch = NULL, ...)
}
}
} else {
message(" large raster: reprojecting after writing to temp drive...")
#rasters need to go to same file so it can be unlinked at end without losing other temp files
@@ -473,7 +483,6 @@ projectInputs.Raster <- function(x, targetCRS = NULL, rasterToMatch = NULL, ...)
tempSrcRaster <- x@file@name #Keep original raster
}
#Will use Nearest Neighbour - fastest, safest, but worst interpolation for continuous
res(rasterToMatch) <- c(30,30)
tr <- res(rasterToMatch)
gdalUtils::gdal_setInstallation()
@@ -854,7 +863,11 @@ assessDataType <- function(ras) {
#' @rdname assessDataType
assessDataType.Raster <- function(ras) {
## using ras@data@... is faster, but won't work for @values in large rasters
rasVals <- getValues(ras)
if (ncell(ras) > 100000) {
rasVals <- raster::sampleRandom(x = ras, size = 100000)
} else {
rasVals <- raster::getValues(ras)
}
minVal <- ras@data@min
maxVal <- ras@data@max
signVal <- minVal < 0
@@ -912,7 +925,7 @@ assessDataType.default <- function(ras) {
#' @author Tati Micheletti
#' @export
#' @rdname assessDataTypeGDAL
#' @importFrom raster getValues ncell
#' @importFrom raster getValues ncell sampleRandom
#' @example inst/examples/example_assessDataTypeGDAL.R
#' @return The appropriate data type for the range of values in \code{ras} for using gdal. See \code{\link[raster]{dataType}} for details.
assessDataTypeGDAL <- function(ras) {
@@ -939,7 +952,7 @@ assessDataTypeGDAL <- function(ras) {
} else {
if (ncell(ras) > 100000) {
rasVals <- raster::sampleRandom(x = ras, size = 100000) #assumes 100,000 pixels in raster
rasVals <- raster::sampleRandom(x = ras, size = 100000)
} else {
rasVals <- raster::getValues(ras)
}

0 comments on commit 242dbf0

Please sign in to comment.