0

I have an issue that is driving me crazy that I just can't figure out for the life of me. I'm processing large Sentinel-2 tiles and attempting to use terra::mosaic() to put them together. I start with two SpatRasters, notice how the min-max values differ for each band:

> sent1
class       : SpatRaster 
dimensions  : 10980, 10980, 7  (nrow, ncol, nlyr)
resolution  : 10, 10  (x, y)
extent      : 399960, 509760, 6290220, 6400020  (xmin, xmax, ymin, ymax)
coord. ref. : WGS 84 / UTM zone 9N (EPSG:32609) 
source      : T09VVD_2022_mean.tif 
names       :   B02,  B03,   B04,        B05,        B06,       B07, ... 
min values  :     0,    0,    29,   379.8125,   523.3125,   385.875, ... 
max values  : 10456, 9464, 15768, 13930.6875, 15912.1250, 16714.938, ... 

> sent2
class       : SpatRaster 
dimensions  : 10980, 10980, 7  (nrow, ncol, nlyr)
resolution  : 10, 10  (x, y)
extent      : 399960, 509760, 6390240, 6500040  (xmin, xmax, ymin, ymax)
coord. ref. : WGS 84 / UTM zone 9N (EPSG:32609) 
source      : T09VVE_2022_mean.tif 
names       :  B02,  B03,   B04,       B05,        B06,      B07, ... 
min values  :    0,  211,     1,   379.125,   331.3125,   342.75, ... 
max values  : 9104, 8448, 10480, 12210.500, 16052.9375, 16814.38, ... 

Now see how all bands have the same values:

sent3 <- mosaic(sent1, sent2)

> show(sent3)
class       : SpatRaster 
dimensions  : 20982, 10980, 7  (nrow, ncol, nlyr)
resolution  : 10, 10  (x, y)
extent      : 399960, 509760, 6290220, 6500040  (xmin, xmax, ymin, ymax)
coord. ref. : WGS 84 / UTM zone 9N (EPSG:32609) 
source      : spat_fubTXtzTOH0dho5_70406.tif 
names       :        B02,        B03,        B04,        B05,        B06,        B07, ... 
min values  :   355.6161,   355.6161,   355.6161,   355.6161,   355.6161,   355.6161, ... 
max values  : 12130.0361, 12130.0361, 12130.0361, 12130.0361, 12130.0361, 12130.0361, ... 

When I try to plot it in R or QGIS all the bands look the same which is obviously not the desired output.

I have also tried to add the files to a SpatRasterCollection and mosaic that but it had the same results:

sent.sprc <- sprc(sent1, sent2)

sent4 <- mosaic(sent.sprc)

> show(sent4)
class       : SpatRaster 
dimensions  : 20982, 10980, 7  (nrow, ncol, nlyr)
resolution  : 10, 10  (x, y)
extent      : 399960, 509760, 6290220, 6500040  (xmin, xmax, ymin, ymax)
coord. ref. : WGS 84 / UTM zone 9N (EPSG:32609) 
source      : spat_hDQlRCrwBUOApYB_49105.tif 
names       :        B02,        B03,        B04,        B05,        B06,        B07, ... 
min values  :   355.6161,   355.6161,   355.6161,   355.6161,   355.6161,   355.6161, ... 
max values  : 12130.0361, 12130.0361, 12130.0361, 12130.0361, 12130.0361, 12130.0361, ... 

However, merge() works and produces the desired output:

sent5 <- merge(sent1, sent2)

> show(sent5)
class       : SpatRaster 
dimensions  : 20982, 10980, 7  (nrow, ncol, nlyr)
resolution  : 10, 10  (x, y)
extent      : 399960, 509760, 6290220, 6500040  (xmin, xmax, ymin, ymax)
coord. ref. : WGS 84 / UTM zone 9N (EPSG:32609) 
source      : spat_fqpzxaTDk0yKNGb_50974.tif 
names       :   B02,  B03,   B04,       B05,        B06,      B07, ... 
min values  :     0,    0,     1,   379.125,   331.3125,   342.75, ... 
max values  : 10456, 9464, 15768, 13930.688, 16052.9375, 16814.38, ... 

Overall I am working with several files that are all quite large so I'm running on an HPC. I transferred two of the files to my local machine to test it out and it's the same problem. I have the current development version of Terra installed and only have 'terra' and 'here' packages loaded. I tried to make a reproducible example but couldn't figure out how to generate dummy data for the multiband raster. If you have any suggestions please let me know! I can supply two of the sentinel-files if that helps but they are >3gb each.

Meirzev
  • 1
  • 2

1 Answers1

0

You have not provided the data. So, I cannot test my code. You can try the following code:

library(terra)

setwd("...") #Provide the directory containg the the tif files

files <- list.files(".\\", pattern='tif$',
                    full.names = TRUE) 
x <- sprc(files) 
m <- mosaic(x)
UseR10085
  • 7,120
  • 3
  • 24
  • 54
  • Thank you. I have tried mosiac() on an sprc as well but still get the same result. I am updating my original post to show this. As I mentioned in my post, the example files are 3gb each and I was not able to figure out how to make reproducible example rasters. If you have tips on how to do that I would appreciate it! – Meirzev Aug 16 '23 at 16:42