@@ -172,3 +172,92 @@ test_that("Increased samples have high confidence, low entropy", {
172172 )))
173173 unlink(probs_cube $ file_info [[1 ]]$ path )
174174})
175+
176+ test_that(" uncertainty sampling returns correct values at sample coordinates" , {
177+ # Test that the bug with 0-based vs 1-based indexing is fixed
178+ # Create a small raster with known values
179+ r <- terra :: rast(
180+ nrows = 100 ,
181+ ncols = 100 ,
182+ xmin = - 6073798 ,
183+ xmax = - 6014726 ,
184+ ymin = - 1312333 ,
185+ ymax = - 1278280 ,
186+ crs = " EPSG:6933"
187+ )
188+ terra :: values(r ) <- 1 : 10000
189+
190+ # Save the raster
191+ temp_dir <- tempdir()
192+ raster_path <- file.path(temp_dir , " test_raster.tif" )
193+ terra :: writeRaster(r , raster_path , overwrite = TRUE )
194+
195+ # Create uncertainty cube
196+ uncert_cube <- tibble :: tibble(
197+ source = " BDC" ,
198+ collection = " MOD13Q1-6.1" ,
199+ satellite = " TERRA" ,
200+ sensor = " MODIS" ,
201+ tile = " test_tile" ,
202+ xmin = terra :: xmin(r ),
203+ xmax = terra :: xmax(r ),
204+ ymin = terra :: ymin(r ),
205+ ymax = terra :: ymax(r ),
206+ crs = terra :: crs(r ),
207+ file_info = list (tibble :: tibble(
208+ band = " entropy" ,
209+ start_date = as.Date(" 2023-01-01" ),
210+ end_date = as.Date(" 2023-12-31" ),
211+ ncols = terra :: ncol(r ),
212+ nrows = terra :: nrow(r ),
213+ xres = terra :: xres(r ),
214+ yres = terra :: yres(r ),
215+ xmin = terra :: xmin(r ),
216+ xmax = terra :: xmax(r ),
217+ ymin = terra :: ymin(r ),
218+ ymax = terra :: ymax(r ),
219+ path = raster_path
220+ )),
221+ labels = list (c(" 1" , " 2" , " 3" , " 4" ))
222+ )
223+ class(uncert_cube ) <- c(" uncertainty_cube" , " derived_cube" , " raster_cube" ,
224+ " tbl_df" , " tbl" , " data.frame" )
225+
226+ # Get samples
227+ samples_df <- sits_uncertainty_sampling(
228+ uncert_cube ,
229+ n = 5 ,
230+ min_uncert = 0.0 ,
231+ max_uncert = 1.0 ,
232+ progress = FALSE
233+ )
234+
235+ # Verify that uncertainty values match the raster values at the sample coordinates
236+ all_match <- all(sapply(1 : nrow(samples_df ), function (i ) {
237+ lon <- samples_df $ longitude [i ]
238+ lat <- samples_df $ latitude [i ]
239+ uncert <- samples_df $ uncertainty [i ]
240+
241+ # Convert lat/lon (WGS84) to raster CRS (EPSG:6933)
242+ pts_wgs84 <- terra :: vect(matrix (c(lon , lat ), ncol = 2 ), type = " points" , crs = " EPSG:4326" )
243+ pts_proj <- terra :: project(pts_wgs84 , terra :: crs(r ))
244+ coords <- terra :: geom(pts_proj )[, c(" x" , " y" )]
245+
246+ # Get cell from coordinates
247+ cell_idx <- terra :: cellFromXY(r , matrix (coords , ncol = 2 ))
248+
249+ # Get raster value at that cell
250+ raster_value <- terra :: values(r )[cell_idx ]
251+
252+ # Expected uncertainty = raster_value / 10000
253+ expected_uncert <- raster_value / 10000
254+
255+ # Check if they match (with small tolerance for floating point)
256+ abs(uncert - expected_uncert ) < 0.001
257+ }))
258+
259+ expect_true(all_match )
260+
261+ # Clean up
262+ unlink(raster_path )
263+ })
0 commit comments