checkRasters: Check that rasters confrom to a specified template

Description Usage Arguments Value See Also Examples

Description

Check that the raster(s) rasters conforms to a raster template, specified by template. By default the extent, projection and number of cells are checked. If celllbycell = TRUE cell values in rasters and template are individually compared to ensure that all NA values in template are NA in rasters and vice versa - but this can be very slow with big rasters.

If any of the tests are failed checkRasters throws an error message (which should hopefully be informative) and stops, if they are passed rasters is returned. The function should therefore be used with care in scripts since calls of the form ras <- checkRasters(ras, template) could throw an error but allow the script to continue. Consider wrapping the workflow up in a function, using tryCatch or assigning to a new object, as detailed in the examples.

Usage

1
checkRasters(rasters, template, cellbycell = FALSE)

Arguments

rasters

A RasterLayer, RasterBrick orRasterStack object containing the raster or rasters to check.

template

A RasterLayer object giving the template raster to compare rasters to.

cellbycell

Whether to individually check cell values between rasters and template to ensure that NA values (masked values) line up. This can be very slow for large rasters!

Value

If all checks are passed rasters is returned, otherwise checkRasters throws an error.

See Also

checkOccurrence, tryCatch

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
## Not run:
# (not running example when checking package as
#   error messages would cause it to fail)

# load the example template raster
data(template)

# make a load of copys
raster_proj <- raster_extent <- raster_ncell <- raster_values <- template

# and break them!
# wrong projection
projection(raster_proj) <- wgs84()

# wrong extent
extent(raster_extent) <- extent(as.vector(extent(raster_extent)) / 2)

# wrong number of cells
raster_ncell <- aggregate(raster_ncell, 2)

# and missing cell values
raster_values[sample(1:nrow(template), 20), sample(1:ncol(template), 20)] <- NA
## Not run: 
# now test them
ans <- checkRasters(raster_proj, template)
ans <- checkRasters(raster_extent, template)
ans <- checkRasters(raster_ncell, template)
ans <- checkRasters(raster_values, template, cellbycell = TRUE)

# so nothing has been assigned to ans
ans

# note that using checkRasters to assign back to the same object is a bad idea
# in a script:
raster_proj <- checkRasters(raster_proj, template)
# since it returns an error message, but the script continues with the original
# dodgy raster as raster_proj wasn't overwritten by anything

# instead you could wrap up the whole analysis in a function like this:
run <- function (raster, template) {
  raster <- checkRasters(raster, template)
  plot(raster)
}

# since the function will fail with the error message and stop
run(raster_proj, template)

# (this is what it does with no error)
run(template, template)

# alternatively you could use tryCatch to overwrite with NULL
# if there's an error
raster_proj <- tryCatch(checkRasters(raster_proj, template),
                        error = function(error) NULL)
raster_proj

# An easier solution would be to assign the answer to a new object,
# causing the rest of the script to fail if the checks aren't completed
raster_clean <- checkRasters(raster_extent, template)

plot(raster_clean)
# doesn't exist so throws an error

## End(Not run)
## End(Not run)

SEEG-Oxford/seegSDM documentation built on May 9, 2019, 11:08 a.m.