RStoolbox icon indicating copy to clipboard operation
RStoolbox copied to clipboard

coregisterImages seq.default error

Open John-Polo opened this issue 1 year ago • 1 comments

I tried to co-register imagery from Planet. When I use coregisterImages(), I get an error.

library(terra)
#> terra 1.7.71
library(RStoolbox)
#> Warning: package 'RStoolbox' was built under R version 4.3.3
#> The legacy packages maptools, rgdal, and rgeos, underpinning this package
#> will retire shortly. Please refer to R-spatial evolution reports on
#> https://r-spatial.org/r/2023/05/15/evolution4.html for details.
#> This package is now running under evolution status 0
#> This is version 1.0.0 of RStoolbox

basedir <- "data/MRS_20222023_8b_harm_psscene_analytic_8b_sr_udm2/PSScene/"

l1 <- list.files(basedir, full.names = T, recursive = T, pattern = "harmonized.*tif$")

r1 <- rast(l1[1])
r1
#> class       : SpatRaster 
#> dimensions  : 22, 12, 8  (nrow, ncol, nlyr)
#> resolution  : 3, 3  (x, y)
#> extent      : 321744, 321780, 3928893, 3928959  (xmin, xmax, ymin, ymax)
#> coord. ref. : WGS 84 / UTM zone 17N (EPSG:32617) 
#> source      : 20220807_154955_15_2499_3B_AnalyticMS_SR_8b_harmonized_clip.tif 
#> names       : coastal_blue, blue, green_i, green, yellow,  red, ... 
#> min values  :          405,  474,     679,   757,    906,  770, ... 
#> max values  :          568,  585,     819,   903,   1204, 1132, ...
res(r1)
#> [1] 3 3

r2 <- rast(l1[2])
r2
#> class       : SpatRaster 
#> dimensions  : 22, 12, 8  (nrow, ncol, nlyr)
#> resolution  : 3, 3  (x, y)
#> extent      : 321744, 321780, 3928893, 3928959  (xmin, xmax, ymin, ymax)
#> coord. ref. : WGS 84 / UTM zone 17N (EPSG:32617) 
#> source      : 20220807_154957_43_2499_3B_AnalyticMS_SR_8b_harmonized_clip.tif 
#> names       : coastal_blue, blue, green_i, green, yellow,  red, ... 
#> min values  :          385,  454,     681,   766,    899,  740, ... 
#> max values  :          571,  562,     819,   901,   1191, 1111, ...
res(r2)
#> [1] 3 3

coregisterImages(r2, r1, shift = 2, shiftInc = 1, reportStats = T)
#> Error in seq.default(mmin, mmax, by = (mmax - mmin)/nBins): 'from' must be a finite number
#> 
#> Do it all manually
del1 <- seq(0, 2, 1)
del1
#> [1] 0 1 2
del2 <- c(-rev(del1), del1[-1])
del2
#> [1] -2 -1  0  1  2
expand.grid(del2*3, del2*3)
#>    Var1 Var2
#> 1    -6   -6
#> 2    -3   -6
#> 3     0   -6
#> 4     3   -6
#> 5     6   -6
#> 6    -6   -3
#> 7    -3   -3
#> 8     0   -3
#> 9     3   -3
#> 10    6   -3
#> 11   -6    0
#> 12   -3    0
#> 13    0    0
#> 14    3    0
#> 15    6    0
#> 16   -6    3
#> 17   -3    3
#> 18    0    3
#> 19    3    3
#> 20    6    3
#> 21   -6    6
#> 22   -3    6
#> 23    0    6
#> 24    3    6
#> 25    6    6

sessionInfo()
#> R version 4.3.2 (2023-10-31 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19045)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=English_United States.utf8 
#> [2] LC_CTYPE=English_United States.utf8   
#> [3] LC_MONETARY=English_United States.utf8
#> [4] LC_NUMERIC=C                          
#> [5] LC_TIME=English_United States.utf8    
#> 
#> time zone: America/New_York
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> loaded via a namespace (and not attached):
#>  [1] digest_0.6.34   fastmap_1.1.1   xfun_0.39       glue_1.7.0     
#>  [5] knitr_1.43      htmltools_0.5.5 rmarkdown_2.22  lifecycle_1.0.4
#>  [9] cli_3.6.2       reprex_2.1.1    withr_3.0.1     compiler_4.3.2 
#> [13] rstudioapi_0.14 tools_4.3.2     evaluate_0.21   yaml_2.3.7     
#> [17] rlang_1.1.3     fs_1.6.2
Created on 2024-09-26 with reprex v2.1.1

There is an unrelated post on StackOverflow about the error. The answer had to do with a custom function that was allowing Inf values, but from what I can see in the code for coregisterImages(), it's not the same situation.

John-Polo avatar Sep 26 '24 19:09 John-Polo

I also get the same error message when coregistering two multiband rasters. I've checked and I don't have any NA, NaN or Inf values.

joaocarr avatar Oct 11 '24 13:10 joaocarr

Sorry for the late response: Hmmm, unfortunately, I cannot reproduce the error... However, I would suggest it has something to do with NA or NaN values and the nBins argument, have you tried modifying this parameter yet?

Here is my attempt to recreate the error:

r1 <- terra::rast(
  nrows=22, ncols=12, nlyrs=8,
  xmin=321744, xmax=321780, ymin=3928893, ymax=3928959,
  crs=crs("EPSG:32617")
)
values(r1) <- rnorm(22 * 12 * 8)
plot(r1)

r2 <- terra::rast(
  nrows=22, ncols=12, nlyrs=8,
  xmin=321744, xmax=321780, ymin=3928893, ymax=3928959,
  crs=crs("EPSG:32617")
)
values(r2) <- rnorm(22 * 12 * 8)
plot(r2)

coregisterImages(r2, r1, nSamples = 10, shift = 2, shiftInc = 1)

Could you maybe send over some data samples so I can reproduce it?

KonstiDE avatar Feb 02 '25 10:02 KonstiDE

I get a different error now, so I don't know if you want me to close this and open a new issue. Same function, similar data, but it's complaining about the 'from' value now.

I also don't know how to send/share data. I have some small multiband rasters from Planet. They're about 12 KB each.

John-Polo avatar Feb 05 '25 17:02 John-Polo

Just attach the data in a GitHub comment here or upload it at transferxl.com or wetransfer or somewhere... But let's stay in that issue here

KonstiDE avatar Feb 05 '25 17:02 KonstiDE

Okay. I've attached a zip with two rasters.

PSScene.zip

This is the code as I've currently tried.


> library(terra)
terra 1.8.5
> library(RStoolbox)
This is version 1.0.2.1 of RStoolbox

> r1 <- rast(l1[1])
> r2 <- rast(l1[2])
> r1
class       : SpatRaster 
dimensions  : 23, 22, 8  (nrow, ncol, nlyr)
resolution  : 3, 3  (x, y)
extent      : 321723, 321789, 3928911, 3928980  (xmin, xmax, ymin, ymax)
coord. ref. : WGS 84 / UTM zone 17N (EPSG:32617) 
source      : 20220809_151909_65_2465_3B_AnalyticMS_SR_8b_harmonized_clip.tif 
names       : coastal_blue, blue, green_i, green, yellow,  red, ... 
min values  :          463,  242,     594,   705,    719,  594, ... 
max values  :          839,  640,     978,  1220,   1471, 1650, ... 
> r2
class       : SpatRaster 
dimensions  : 23, 22, 8  (nrow, ncol, nlyr)
resolution  : 3, 3  (x, y)
extent      : 321723, 321789, 3928911, 3928980  (xmin, xmax, ymin, ymax)
coord. ref. : WGS 84 / UTM zone 17N (EPSG:32617) 
source      : 20220824_155028_62_2488_3B_AnalyticMS_SR_8b_harmonized_clip.tif 
names       : coastal_blue, blue, green_i, green, yellow, red, ... 
> coregisterImages(r2, r1, shiftInc = 1, reportStats = T)
Error in seq.default(mmin, mmax, by = (mmax - mmin)/nBins) : 
  'from' must be a finite number

> sessionInfo()
R version 4.4.2 (2024-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 10 x64 (build 19045)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: America/New_York
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] RStoolbox_1.0.2.1 terra_1.8-5      

loaded via a namespace (and not attached):
 [1] tidyselect_1.2.1     viridisLite_0.4.2    timeDate_4041.110    dplyr_1.1.4         
 [5] farver_2.1.2         fields_16.3          pROC_1.18.5          caret_7.0-1         
 [9] XML_3.99-0.18        digest_0.6.37        rpart_4.1.23         dotCall64_1.2       
[13] timechange_0.3.0     lifecycle_1.0.4      sf_1.0-19            survival_3.7-0      
[17] exactextractr_0.10.0 magrittr_2.0.3       kernlab_0.9-33       compiler_4.4.2      
[21] rlang_1.1.4          tools_4.4.2          data.table_1.16.4    knitr_1.49          
[25] fitdistrplus_1.2-2   sp_2.1-4             classInt_0.4-11      plyr_1.8.9          
[29] KernSmooth_2.23-24   withr_3.0.2          purrr_1.0.2          stats4_4.4.2        
[33] nnet_7.3-19          grid_4.4.2           e1071_1.7-16         colorspace_2.1-1    
[37] future_1.34.0        ggplot2_3.5.1        globals_0.16.3       scales_1.3.0        
[41] gtools_3.9.5         iterators_1.0.14     MASS_7.3-61          cli_3.6.3           
[45] generics_0.1.3       rstudioapi_0.17.1    future.apply_1.11.3  reshape2_1.4.4      
[49] DBI_1.2.3            proxy_0.4-27         stringr_1.5.1        splines_4.4.2       
[53] maps_3.4.2.1         parallel_4.4.2       vctrs_0.6.5          hardhat_1.4.0       
[57] Matrix_1.7-1         patchwork_1.3.0      listenv_0.9.1        spThin_0.2.0        
[61] foreach_1.5.2        tidyr_1.3.1          gower_1.0.2          units_0.8-5         
[65] recipes_1.1.0        glue_1.8.0           parallelly_1.41.0    spam_2.11-0         
[69] codetools_0.2-20     stringi_1.8.4        lubridate_1.9.4      gtable_0.3.6        
[73] raster_3.6-30        munsell_0.5.1        tibble_3.2.1         pillar_1.10.1       
[77] ipred_0.9-15         randomForest_4.7-1.2 lava_1.8.1           gbm_2.2.2           
[81] R6_2.5.1             doParallel_1.0.17    maxnet_0.1.4         evaluate_1.0.3      
[85] lattice_0.22-6       class_7.3-22         Rcpp_1.0.13-1        nlme_3.1-166        
[89] prodlim_2024.06.25   flexsdm_1.3.6        mgcv_1.9-1           xfun_0.50           
[93] ModelMetrics_1.2.2.2 pkgconfig_2.0.3     

John-Polo avatar Feb 05 '25 19:02 John-Polo

Evening, the issue was with several values() calls within the function in where I forgot to pass na.rm=T, since your rasters had NA values, mmin and co. where NA, which is not allowed.

You should be good to go now installing from the git using devtools::install_github("bleutner/RStoolbox")

Here's the code:

setwd("path/to/your/files/")

r1 <- rast("20220809_151909_65_2465_3B_AnalyticMS_SR_8b_harmonized_clip.tif")
r2 <- rast("20220824_155028_62_2488_3B_AnalyticMS_SR_8b_harmonized_clip.tif")

coregisterImages(r1, r2)

# output is:
class       : SpatRaster 
dimensions  : 23, 22, 8  (nrow, ncol, nlyr)
resolution  : 3, 3  (x, y)
extent      : 321720, 321786, 3928911, 3928980  (xmin, xmax, ymin, ymax)
coord. ref. : WGS 84 / UTM zone 17N (EPSG:32617) 
source      : 20220824_155028_62_2488_3B_AnalyticMS_SR_8b_harmonized_clip.tif 
names       : coastal_blue, blue, green_i, green, yellow, red, ... 

Reopen the issue if the error still persists :)

KonstiDE avatar Feb 05 '25 22:02 KonstiDE