Skip to contents

Split a raster object into a list of smaller rasters based on specified numbers of rows and columns. It can optionally save the resulting rasters to disk, plot them, or return just their extents.

Usage

SplitRaster(
  raster = NULL,
  Ncol = 4,
  Nrow = 4,
  save = FALSE,
  SplitPath = "",
  plot = FALSE,
  Extent = FALSE
)

Arguments

raster

A raster object to be split. If NULL (the default), the function will not execute.

Ncol, Nrow

Integer. The desired number of columns and rows to split the raster into. Default is 4 columns and 4 rows.

save

Logical. Indicates whether to save the split rasters to disk. Default is FALSE.

SplitPath

Character string. Specifies the directory path where the split rasters should be saved if save is TRUE. If the directory does not exist, it will be created.

plot

Logical. Indicates whether to plot the split rasters. Default is FALSE.

Extent

Logical. If TRUE, the function returns only the extents of the split rasters instead of the raster data. Default is FALSE.

Value

A list of raster objects or extents of the split rasters, depending on the value of the Extent parameter.

References

Click here and here

Author

Ahmed El-Gabbas

Examples

library(raster)
logo <- raster(system.file("external/rlogo.grd", package = "raster"))
plot(logo, axes = FALSE, legend = FALSE, bty = "n",
     box = FALSE, main = "Original raster layer")

# --------------------------------------------------

# Split into 3 rows and 3 columns
logoSplit <- SplitRaster(raster = logo, Ncol = 3, Nrow = 3, plot = TRUE)


print(logoSplit) # a list object of 9 items
#> [[1]]
#> class      : RasterLayer 
#> dimensions : 26, 34, 884  (nrow, ncol, ncell)
#> resolution : 1, 1  (x, y)
#> extent     : 0, 34, 51, 77  (xmin, xmax, ymin, ymax)
#> crs        : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
#> source     : memory
#> names      : red 
#> values     : 0, 255  (min, max)
#> 
#> 
#> [[2]]
#> class      : RasterLayer 
#> dimensions : 26, 34, 884  (nrow, ncol, ncell)
#> resolution : 1, 1  (x, y)
#> extent     : 34, 68, 51, 77  (xmin, xmax, ymin, ymax)
#> crs        : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
#> source     : memory
#> names      : red 
#> values     : 0, 255  (min, max)
#> 
#> 
#> [[3]]
#> class      : RasterLayer 
#> dimensions : 26, 33, 858  (nrow, ncol, ncell)
#> resolution : 1, 1  (x, y)
#> extent     : 68, 101, 51, 77  (xmin, xmax, ymin, ymax)
#> crs        : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
#> source     : memory
#> names      : red 
#> values     : 32, 255  (min, max)
#> 
#> 
#> [[4]]
#> class      : RasterLayer 
#> dimensions : 26, 34, 884  (nrow, ncol, ncell)
#> resolution : 1, 1  (x, y)
#> extent     : 0, 34, 25, 51  (xmin, xmax, ymin, ymax)
#> crs        : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
#> source     : memory
#> names      : red 
#> values     : 0, 255  (min, max)
#> 
#> 
#> [[5]]
#> class      : RasterLayer 
#> dimensions : 26, 34, 884  (nrow, ncol, ncell)
#> resolution : 1, 1  (x, y)
#> extent     : 34, 68, 25, 51  (xmin, xmax, ymin, ymax)
#> crs        : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
#> source     : memory
#> names      : red 
#> values     : 0, 255  (min, max)
#> 
#> 
#> [[6]]
#> class      : RasterLayer 
#> dimensions : 26, 33, 858  (nrow, ncol, ncell)
#> resolution : 1, 1  (x, y)
#> extent     : 68, 101, 25, 51  (xmin, xmax, ymin, ymax)
#> crs        : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
#> source     : memory
#> names      : red 
#> values     : 0, 255  (min, max)
#> 
#> 
#> [[7]]
#> class      : RasterLayer 
#> dimensions : 25, 34, 850  (nrow, ncol, ncell)
#> resolution : 1, 1  (x, y)
#> extent     : 0, 34, 0, 25  (xmin, xmax, ymin, ymax)
#> crs        : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
#> source     : memory
#> names      : red 
#> values     : 37, 255  (min, max)
#> 
#> 
#> [[8]]
#> class      : RasterLayer 
#> dimensions : 25, 34, 850  (nrow, ncol, ncell)
#> resolution : 1, 1  (x, y)
#> extent     : 34, 68, 0, 25  (xmin, xmax, ymin, ymax)
#> crs        : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
#> source     : memory
#> names      : red 
#> values     : 0, 255  (min, max)
#> 
#> 
#> [[9]]
#> class      : RasterLayer 
#> dimensions : 25, 33, 825  (nrow, ncol, ncell)
#> resolution : 1, 1  (x, y)
#> extent     : 68, 101, 0, 25  (xmin, xmax, ymin, ymax)
#> crs        : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
#> source     : memory
#> names      : red 
#> values     : 0, 255  (min, max)
#> 
#> 

# --------------------------------------------------

# Merging split maps again
logoSplit$fun <- mean
logoSplit$na.rm <- TRUE
logoSplit2 <- do.call(mosaic, logoSplit)
par(mfrow = c(1, 1))
plot(logoSplit2, axes = FALSE, legend = FALSE, bty = "n",
     box = FALSE, main = "Merged raster layers")


print({logoSplit2 - logo}) # No value difference!
#> class      : RasterLayer 
#> dimensions : 77, 101, 7777  (nrow, ncol, ncell)
#> resolution : 1, 1  (x, y)
#> extent     : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
#> crs        : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
#> source     : memory
#> names      : layer 
#> values     : 0, 0  (min, max)
#> 

# --------------------------------------------------

logoSplit <- SplitRaster(logo, Ncol = 3, Nrow = 3, Extent = TRUE)
print(logoSplit)
#> [[1]]
#> class      : Extent 
#> xmin       : 0 
#> xmax       : 34 
#> ymin       : 51 
#> ymax       : 77 
#> 
#> [[2]]
#> class      : Extent 
#> xmin       : 34 
#> xmax       : 68 
#> ymin       : 51 
#> ymax       : 77 
#> 
#> [[3]]
#> class      : Extent 
#> xmin       : 68 
#> xmax       : 102 
#> ymin       : 51 
#> ymax       : 77 
#> 
#> [[4]]
#> class      : Extent 
#> xmin       : 0 
#> xmax       : 34 
#> ymin       : 25 
#> ymax       : 51 
#> 
#> [[5]]
#> class      : Extent 
#> xmin       : 34 
#> xmax       : 68 
#> ymin       : 25 
#> ymax       : 51 
#> 
#> [[6]]
#> class      : Extent 
#> xmin       : 68 
#> xmax       : 102 
#> ymin       : 25 
#> ymax       : 51 
#> 
#> [[7]]
#> class      : Extent 
#> xmin       : 0 
#> xmax       : 34 
#> ymin       : -1 
#> ymax       : 25 
#> 
#> [[8]]
#> class      : Extent 
#> xmin       : 34 
#> xmax       : 68 
#> ymin       : -1 
#> ymax       : 25 
#> 
#> [[9]]
#> class      : Extent 
#> xmin       : 68 
#> xmax       : 102 
#> ymin       : -1 
#> ymax       : 25 
#>