Merging Lots of Rasters in R Using do.call: A Comprehensive Guide to Avoiding Numerical Overflows and Underflows

Merging Lots of Rasters in R Using do.call

Introduction

The R programming language is a popular choice for data analysis and manipulation. One common task when working with raster data is merging multiple rasters into a single image. In this article, we will explore how to use the do.call function to merge lots of rasters in R.

Background

Raster data is a common format for storing geospatial data, such as satellite images or map data. Rasters can be represented as 2D arrays, where each pixel has a value associated with it. When merging multiple rasters, we want to combine the values at each pixel location into a single value.

What is do.call?

The do.call function in R is used to apply a function to multiple arguments in an expression. It’s a shorthand way of calling a function with multiple arguments without having to specify them individually.

In the context of raster data, do.call can be used to merge multiple rasters by applying the merge function from the raster package to a list of raster objects.

Merging Rasters

To merge lots of rasters using do.call, we first need to create a list of raster objects. We can do this using the ListRasters function, which takes a list of file names as input and returns a list of raster objects.

Here is an example:

tifname1 <- c("fpar_h29v09.tif", "fpar_h29v10.tif", "fpar_h29v11.tif", "fpar_h29v12.tif", "fpar_h29v13.tif")
mergetifList(tifname1)

This will create a list of raster objects, where each object corresponds to one of the files in the tifname1 vector.

Next, we can use do.call to apply the merge function to this list of raster objects. Here is an example:

raster.list <- sapply(tifList, FUN = ListRasters)
names(raster.list) <- NULL
mergeRas <- do.call(raster::merge, raster.list)

This will merge all the rasters in the tifname1 vector into a single image.

The Problem with Merging Lots of Rasters

However, when merging lots of rasters using do.call, we can encounter some issues. Specifically, if the list of files is very long, the values at each pixel location may become incorrect due to numerical overflows or underflows.

This is because the merge function uses a simple arithmetic operation to combine the values at each pixel location. If the values are too large or small, this can cause the result to overflow or underflow, resulting in incorrect values.

Resolving the Issue

To resolve this issue, we need to modify the ListRasters function to handle numerical overflows and underflows when merging lots of rasters.

Here is an updated version of the ListRasters function:

ListRasters <- function(list_names) {
  raster_list <- list() # initialise the list of rasters
  for (i in 1:(length(list_names))) {
    tif_name <- list_names[i] # list_names contains all the names of the images in .tif format
    raster_file <- raster(tif_name)
    raster_file[raster_file > 1] <- NA # set values greater than 1 to NA
    raster_list <- append(raster_list, raster_file) # update raster_list at each iteration
  }
  return(raster_list)
}

This updated function sets all values greater than 1 in the raster_file object to NA, which prevents numerical overflows and underflows when merging lots of rasters.

Conclusion

Merging lots of rasters in R using do.call requires careful consideration of numerical overflows and underflows. By modifying the ListRasters function to handle these issues, we can ensure that our merged raster images are accurate and reliable.

In conclusion, this article has demonstrated how to use do.call to merge lots of rasters in R. We have explored the potential issues that arise when merging large numbers of rasters and provided a modified version of the ListRasters function that resolves these issues.


Last modified on 2024-05-31