Combines rasters into all unique combinations of inputs

combine(x, rnames = NULL, sp = FALSE)

Arguments

x

raster stack/brick or SpatialPixelsDataFrame object

rnames

Column names to combine in raster stack or sp object

sp

(FALSE/TRUE) output SpatialPixelsDataFrame

Value

A ratified rasterLayer or a list containing a SpatialPixelsDataFrame and a data.frame of unique combinations.

Details

Please note that this is not a memory safe function that utilizes rasters out of memory in the manner that the raster package does.

If sp = TRUE the object will be a list with "combine", containing the SpatialPixelsDataFrame with the value attribute containing the unique combinations, and "summary" with the summary table of collapsed combinations and associated attributes.

If sp = FALSE the a single ratified rasterLayer class object is returned with the summary table as the raster attribute table, this is most similar to the ESRI format resulting from their combine function.

Author

Jeffrey S. Evans <jeffrey_evans@tnc.org>

Examples

library(raster) r1 <- raster(nrows=100, ncol=100) r1[] <- round(runif(ncell(r1), 1,4),0) r2 <- raster(nrows=100, ncol=100) r2[] <- round(runif(ncell(r2), 2,6),0) r3 <- raster(nrows=100, ncol=100) r3[] <- round(runif(ncell(r3), 2,6),0) r <- stack(r1,r2,r3) names(r) <- c("LC1","LC2","LC3") # Combine rasters in stack ( cr <- combine(r) )
#> class : RasterLayer #> dimensions : 100, 100, 10000 (nrow, ncol, ncell) #> resolution : 3.6, 1.8 (x, y) #> extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) #> crs : +proj=longlat +datum=WGS84 +no_defs #> source : memory #> names : value #> values : 1, 100 (min, max) #> attributes : #> ID LC1 LC2 LC3 Freq #> from: 1 1 2 2 25 #> to : 100 4 6 6 19 #>
levels(cr)
#> [[1]] #> ID LC1 LC2 LC3 Freq #> 1 1 1 2 2 25 #> 126 26 2 2 2 48 #> 251 51 3 2 2 41 #> 376 76 4 2 2 33 #> 406 6 1 3 2 49 #> 531 31 2 3 2 103 #> 656 56 3 3 2 108 #> 781 81 4 3 2 65 #> 811 11 1 4 2 42 #> 936 36 2 4 2 101 #> 1061 61 3 4 2 96 #> 1186 86 4 4 2 54 #> 1216 16 1 5 2 57 #> 1341 41 2 5 2 108 #> 1466 66 3 5 2 116 #> 1591 91 4 5 2 40 #> 1621 21 1 6 2 25 #> 1746 46 2 6 2 58 #> 1871 71 3 6 2 46 #> 1996 96 4 6 2 19 #> 2002 2 1 2 3 46 #> 2127 27 2 2 3 128 #> 2252 52 3 2 3 100 #> 2377 77 4 2 3 53 #> 2407 7 1 3 3 116 #> 2532 32 2 3 3 203 #> 2657 57 3 3 3 239 #> 2782 82 4 3 3 104 #> 2812 12 1 4 3 104 #> 2937 37 2 4 3 227 #> 3062 62 3 4 3 168 #> 3187 87 4 4 3 104 #> 3217 17 1 5 3 99 #> 3342 42 2 5 3 243 #> 3467 67 3 5 3 217 #> 3592 92 4 5 3 113 #> 3622 22 1 6 3 67 #> 3747 47 2 6 3 93 #> 3872 72 3 6 3 93 #> 3997 97 4 6 3 60 #> 4003 3 1 2 4 61 #> 4128 28 2 2 4 103 #> 4253 53 3 2 4 92 #> 4378 78 4 2 4 48 #> 4408 8 1 3 4 104 #> 4533 33 2 3 4 201 #> 4658 58 3 3 4 221 #> 4783 83 4 3 4 95 #> 4813 13 1 4 4 101 #> 4938 38 2 4 4 216 #> 5063 63 3 4 4 208 #> 5188 88 4 4 4 84 #> 5218 18 1 5 4 92 #> 5343 43 2 5 4 194 #> 5468 68 3 5 4 204 #> 5593 93 4 5 4 112 #> 5623 23 1 6 4 54 #> 5748 48 2 6 4 89 #> 5873 73 3 6 4 110 #> 5998 98 4 6 4 52 #> 6004 4 1 2 5 53 #> 6129 29 2 2 5 113 #> 6254 54 3 2 5 90 #> 6379 79 4 2 5 49 #> 6409 9 1 3 5 126 #> 6534 34 2 3 5 187 #> 6659 59 3 3 5 199 #> 6784 84 4 3 5 111 #> 6814 14 1 4 5 113 #> 6939 39 2 4 5 218 #> 7064 64 3 4 5 185 #> 7189 89 4 4 5 98 #> 7219 19 1 5 5 114 #> 7344 44 2 5 5 209 #> 7469 69 3 5 5 198 #> 7594 94 4 5 5 104 #> 7624 24 1 6 5 51 #> 7749 49 2 6 5 101 #> 7874 74 3 6 5 102 #> 7999 99 4 6 5 65 #> 8005 5 1 2 6 25 #> 8130 30 2 2 6 57 #> 8255 55 3 2 6 45 #> 8380 80 4 2 6 22 #> 8410 10 1 3 6 49 #> 8535 35 2 3 6 108 #> 8660 60 3 3 6 97 #> 8785 85 4 3 6 46 #> 8815 15 1 4 6 48 #> 8940 40 2 4 6 119 #> 9065 65 3 4 6 94 #> 9190 90 4 4 6 35 #> 9220 20 1 5 6 50 #> 9345 45 2 5 6 145 #> 9470 70 3 5 6 119 #> 9595 95 4 5 6 58 #> 9625 25 1 6 6 22 #> 9750 50 2 6 6 54 #> 9875 75 3 6 6 50 #> 10000 100 4 6 6 19 #>
# Combine rasters in stack, using specific rasters ( cr <- combine(r, rnames=c("LC1","LC3")) )
#> class : RasterLayer #> dimensions : 100, 100, 10000 (nrow, ncol, ncell) #> resolution : 3.6, 1.8 (x, y) #> extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) #> crs : +proj=longlat +datum=WGS84 +no_defs #> source : memory #> names : value #> values : 1, 20 (min, max) #> attributes : #> ID LC1 LC3 Freq #> from: 1 1 2 198 #> to : 20 4 6 180 #>
# Combine rasters in stack, output SpatialPixelsDataFrame cr.sp <- combine(r, sp = TRUE) head(cr.sp$summary)
#> value LC1 LC2 LC3 Freq #> 1 1 1 2 2 25 #> 126 26 2 2 2 48 #> 251 51 3 2 2 41 #> 376 76 4 2 2 33 #> 406 6 1 3 2 49 #> 531 31 2 3 2 103
class(cr.sp$combine)
#> [1] "SpatialPixelsDataFrame" #> attr(,"package") #> [1] "sp"
# Input SpatialPixelsDataFrame r.sp <- as(r, "SpatialPixelsDataFrame") cr.sp <- combine(r.sp, sp = TRUE)