How to use a custom function in terra::mosaic()?

94 views Asked by At

I have thousands of adjacent and (partly) overlapping geotiffs, which I would like to mosaic into a single geotiff. I need to use 'mode' to calculate values of the new geotiff in case of overlapping pixels.

I have found the mosaic() function in the R's terra package, but 'mode' is not on a list of 'fun' parameters. Is there a way to provide a custom function?

By the way, I have concerns about whether I can use this function to process such large data (i.e. 6000 images of 512x512 pixels), which after mosaicking will be approx. 10000 x 10000 pixels. So, if anyone come up with an alternative solution (e.g. gdal_calc_py?), it would be much appreciated.

1

There are 1 answers

0
Robert Hijmans On

You cannot (yet) use custom functions but with "terra" version 1.7.57 you can now use fun="modal".

This is currently the development version. You can install this version of terra from the R-Universe with

install.packages('terra', repos='https://rspatial.r-universe.dev')

Your problem does not seem to be especially large, and "terra" is designed to work with big data.