Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with sits_mosaic() after operations and classification #1221

Open
paulacopelli opened this issue Oct 8, 2024 · 2 comments
Open

Error with sits_mosaic() after operations and classification #1221

paulacopelli opened this issue Oct 8, 2024 · 2 comments
Labels

Comments

@paulacopelli
Copy link

Describe the bug
I encountered an issue with sits_mosaic() when performing mosaicking after operations and classification. Based on the documentation, it's possible to apply the mosaic either to the entire data cube or to already processed layers. My study area spans 4 tiles, so mosaicking is essential.

The code I used was:

itata_probs <- sits_cube(
  source = "MPC",
  collection = "SENTINEL-2-L2A",
  data_dir = "data/out/classify_itata/itata_probs1000",
  bands = "probs",
  labels = labels,
  parse_info = c(
    "satellite", "sensor", "tile",
    "start_date", "end_date", "band", "version"
  )
)

# 05. Mosaics
roi_itata <- read_sf(paste0(raiz,"/in/ROI/Subcuenca_Itata_Bajo_UTM.shp"))
plot(roi_itata)

# First step: clipping
mosaic_cube <- sits_mosaic(
  cube = itata_probs,
  roi = roi_itata,
  crs = "EPSG:32718",
  multicores = 2,
  output_dir = "C:/Users/paulu/Documents"
)

# Second step: merging
mosaic_cube <- sits_mosaic(
  cube = itata_probs,
  roi = roi_itata,
  crs = "EPSG:32718",
  multicores = 1,
  output_dir = "C:/Users/paulu/Documents"
)

I applied this to both the regularized data cube and the classified layers, but the resulting mosaic had incorrect clipping. For example, my study area is divided into 4 equal parts by each tile, and only 1 tile displayed the classification correctly, while the rest had values of 1 or NULL. This prevented the proper visualization of the entire study area.

The only solution I found was to perform the mosaicking before all image calculations and classification, as only the raw bands were clipped correctly. However, this also caused issues because while sits_mosaic() worked with the regularized cube, the output filenames were in a different format:

SENTINEL-2_MSI_MOSAIC_2020-03-04_2020-03-04_B06_v2

They should have followed the parse_info format:

SENTINEL-2_MSI_MOSAIC_2020-03-04_v2

Additionally, I had to apply sits_mosaic() first with 2 cores, and then with 1 core, as I described in a previous bug report.

To Reproduce
The issue can be reproduced using the code snippet provided above.

If reporting a change from previous versions
Version: sits 1.5.1

@gilbertocamara
Copy link
Contributor

Dear @paulacopelli, many thanks for pointing out the bug when running sits_mosaic(). We are working to solve the matter as soon as possible.

@Pedrobrito99
Copy link

Dear @paulacopelli,

Thank you for your contribution and for the descriptive code.
We did not succeed in reproducing this error. For better understand, could you kindly share the shapefile, source and collection of the region that you used?

Besides, it would be helpful to receive the sessionInfo() from the session where the error occurred, so that we can check the versions of the libraries used.

We tried to reproduce your error using the following code:

# 1. Load library and create tempdir:
#
library(sits)
library(tidyr)

output_dir <- tempdir()

#
# 2. Set the seed:
#
set.seed(42)

#
# 3. Generate the data cube:
#
sentinel_cube <- sits_cube(
    source = "MPC",
    collection = "SENTINEL-2-L2A",
    tiles = c("23LMF","23LME", "23LLF", "23LLE"),
    bands = c("B02", "B8A", "B11", "CLOUD"),
    start_date = "2019-07-01",
    end_date = "2019-07-28"
)

#
# 4. Regularize the cube:
#
reg_cube <- sits_regularize(
    cube = sentinel_cube,
    output_dir = output_dir,
    res = 200,
    period = "P16D",
    multicores = 12
)

#
# 5.Land Use and Land Cover Samples:
#
samples <- "https://brazildatacube.dpi.inpe.br/geo-knowledge-hub/bdc-article/training-samples/training-samples.csv"

#
# 6. Obtain the time series:
#
obtained_series <- sits_get_data(
    cube = reg_cube,
    samples = samples,
    multicores = 12
)

#
# 7. Train the classifier:
#
random_forest_training <- sits_train(
    samples = obtained_series,
    ml_method = sits_rfor(num_trees = 100)
)

#
# 8. Generate the probability cube:
#
random_forest_probability_cube <- sits_classify(
    data = reg_cube,
    ml_model = random_forest_training,
    output_dir = output_dir,
    version = "v1",
    multicores = 12,
    memsize = 11,
    verbose = FALSE,
    progress = TRUE
)

#
#.9 Define the classes of the probability cube:
#
labels <- c("1"="Crop", "2"="Natural", "3"="Pasture")

#
# 10. Read the probability cube:
#
probs_cube <- sits_cube(
    source = "MPC",
    collection = "SENTINEL-2-L2A",
    data_dir = output_dir,
    bands = "probs",
    labels = labels,
    parse_info = c(
        "satellite", "sensor", "tile",
        "start_date", "end_date", "band", "version"
    )
)

#
# 11. Read the RIO of the selected region:
#
roi <- c(
    lon_min = -46.7139, lat_min = -14.2005,
    lon_max = -45.3955, lat_max = -12.9189
)

#
# 12. Generate a mosaic of tiles using 'multicores = 1':
#
mosaic_cube <- sits_mosaic(
    cube = probs_cube,
    roi = roi,
    crs = "EPSG:32718",
    multicores = 1,
    version = "v1",
    output_dir = output_dir
)

#
# 13. Generate a mosaic of tiles using 'multicores = 2':
#
mosaic_cube <- sits_mosaic(
    cube = probs_cube,
    roi = roi,
    crs = "EPSG:32718",
    multicores = 2,
    version = "v2",
    output_dir = output_dir
)

I am running this code using the following versions:

sessionInfo()

loaded via a namespace (and not attached):
 [1] randomForest_4.7-1.1 jsonlite_1.8.7      
 [3] dplyr_1.1.3          compiler_4.3.0      
 [5] crayon_1.5.2         tidyselect_1.2.0    
 [7] Rcpp_1.0.11          parallel_4.3.0      
 [9] yaml_2.3.7           R6_2.5.1            
[11] generics_0.1.3       warp_0.2.1          
[13] curl_5.1.0           classInt_0.4-10     
[15] s2_1.1.4             httr2_0.2.3         
[17] sf_1.0-14            gdalUtilities_1.2.5 
[19] rstac_0.9.2-5        tibble_3.2.1        
[21] units_0.8-4          lubridate_1.9.3     
[23] DBI_1.1.3            pillar_1.9.0        
[25] rlang_1.1.1          utf8_1.2.4          
[27] terra_1.7-55         gdalcubes_0.6.4     
[29] timechange_0.2.0     cli_3.6.1           
[31] withr_2.5.2          magrittr_2.0.3      
[33] wk_0.9.0             class_7.3-21        
[35] digest_0.6.33        grid_4.3.0          
[37] ncdf4_1.21           rstudioapi_0.15.0   
[39] slider_0.3.1         rappdirs_0.3.3      
[41] lifecycle_1.0.3      vctrs_0.6.4         
[43] KernSmooth_2.23-20   proxy_0.4-27        
[45] glue_1.6.2           codetools_0.2-19    
[47] fansi_1.0.5          e1071_1.7-13        
[49] purrr_1.0.2          httr_1.4.7          
[51] tools_4.3.0          pkgconfig_2.0.3     

Thank you very much!

Pedro Brito.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants