Skip to content

Commit 96d9944

Browse files
Merge pull request #1487 from rolfsimoes/dev
Fix #1486
2 parents 9702634 + 6b804fa commit 96d9944

3 files changed

Lines changed: 97 additions & 10 deletions

File tree

R/api_csv.R

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,14 @@
6767
#'
6868
.csv_get_lat_lon <- function(csv_file) {
6969
# read sample information from CSV file and put it in a tibble
70-
tibble::as_tibble(
70+
data <- tibble::as_tibble(
7171
utils::read.csv(
7272
file = csv_file,
7373
stringsAsFactors = FALSE
7474
)
75-
) |>
76-
# select valid columns
77-
dplyr::select(
78-
c("longitude", "latitude")
79-
)
75+
)
76+
# select valid columns
77+
.csv_metadata_from_samples(data)
8078
}
8179
#' @title Get samples metadata as CSV
8280
#' @name .csv_metadata_from_samples

src/sampling_window.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ DataFrame C_max_sampling(const NumericVector& x, int nrows,
1818
for (int j = 0; j < ncols; j += window_size) {
1919
int max_wi = std::min(nrows, i + window_size);
2020
int max_wj = std::min(ncols, j + window_size);
21-
int cell = i * ncols + j;
22-
int max_value = x(cell);
21+
int cell = i * ncols + j + 1; // Convert to 1-based indexing
22+
int max_value = x(cell - 1); // x is 0-based
2323
for (int wi = i; wi < max_wi; ++wi) {
2424
for (int wj = j; wj < max_wj; ++wj) {
2525
if (x(wi * ncols + wj) > max_value) {
26-
cell = wi * ncols + wj;
27-
max_value = x(cell);
26+
cell = wi * ncols + wj + 1; // Convert to 1-based indexing
27+
max_value = x(cell - 1); // x is 0-based
2828
}
2929
}
3030
}

tests/testthat/test-active_learning.R

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)