Skip to content

Commit ba135a8

Browse files
authored
fix v1 data download fails (#196)
* Update version to 0.2.5.9000, enhance NEWS.md, and improve data handling in available-data and download_data functions * Enhance metadata mapping and improve error handling in data retrieval functions * Remove unnecessary package loading in test-regression-v1-meta.R * Refactor metadata path retrieval and enhance tests for V1 data handling
1 parent 0a5b12f commit ba135a8

12 files changed

Lines changed: 237 additions & 50 deletions

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: spanishoddata
22
Title: Get Spanish Origin-Destination Data
3-
Version: 0.2.5
3+
Version: 0.2.5.9000
44
Authors@R: c(
55
person("Egor", "Kotov", , "kotov.egor@gmail.com", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0001-6690-5345")),

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# spanishoddata (development version)
2+
3+
## Bug fixes
4+
5+
* Improved Version 1 (2020-2021) metadata mapping to ensure redirected district files are correctly identified and added a check to alert the user if no data is found for the requested criteria.
6+
17
# spanishoddata 0.2.5
28

39
## Bug fixes

R/available-data.R

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,33 @@ spod_available_data_v1 <- function(
227227
))
228228
# order by pub_ts
229229
files_table <- files_table[order(files_table$pub_ts, decreasing = TRUE), ]
230+
231+
# replace all municipal data download links with districts links
232+
# this is to address the bugs described in detail in:
233+
# https://www.ekotov.pro/mitma-data-issues/issues/011-v1-tpp-mismatch-zone-ids-in-table-and-spatial-data.html
234+
# https://www.ekotov.pro/mitma-data-issues/issues/012-v1-tpp-district-files-in-municipality-folders.html
235+
# the decision was to use distrcit data and aggregate it to replicate municipal data
236+
# this substitution MUST happen before local_path generation to avoid mismatches
237+
# that lead to spod_download skipping files
238+
239+
# create a flag to identify rows that were originally municipality entries
240+
# this is needed to correctly handle file sizes later
241+
files_table$is_redirected_muni <- grepl(
242+
"mitma-municipios",
243+
files_table$target_url
244+
)
245+
246+
files_table$target_url <- gsub(
247+
"mitma-municipios",
248+
"mitma-distritos",
249+
files_table$target_url
250+
)
251+
files_table$target_url <- gsub(
252+
"mitma_municipio",
253+
"mitma_distrito",
254+
files_table$target_url
255+
)
256+
230257
files_table$local_path <- fs::path(
231258
data_dir,
232259
spod_subfolder_raw_data_cache(ver = 1),
@@ -278,22 +305,6 @@ spod_available_data_v1 <- function(
278305
files_table$local_path
279306
)
280307

281-
# replace all municipal data download links with districts links
282-
# this is to address the bugs described in detail in:
283-
# https://www.ekotov.pro/mitma-data-issues/issues/011-v1-tpp-mismatch-zone-ids-in-table-and-spatial-data.html
284-
# https://www.ekotov.pro/mitma-data-issues/issues/012-v1-tpp-district-files-in-municipality-folders.html
285-
# the decision was to use distrcit data and aggregate it to replicate municipal data
286-
files_table$target_url <- gsub(
287-
"mitma-municipios",
288-
"mitma-distritos",
289-
files_table$target_url
290-
)
291-
files_table$target_url <- gsub(
292-
"mitma_municipio",
293-
"mitma_distrito",
294-
files_table$target_url
295-
)
296-
297308
files_table <- files_table |>
298309
dplyr::mutate(
299310
study = factor(
@@ -337,6 +348,7 @@ spod_available_data_v1 <- function(
337348

338349
zones = factor(
339350
dplyr::case_when(
351+
.data$is_redirected_muni ~ "municipalities",
340352
grepl("distrito", .data$target_url) ~ "districts",
341353
grepl("municipio", .data$target_url) ~ "municipalities",
342354
TRUE ~ NA_character_
@@ -348,15 +360,19 @@ spod_available_data_v1 <- function(
348360
# add known file sizes from cached data
349361
if (s3_successful) {
350362
# replace remote file sizes for v1
363+
# this is to handle cases where we redirected a municipality URL to a district URL
364+
# we need the size of the district file, not the municipality one
351365
replacement_file_sizes_distr <- files_table |>
352-
dplyr::filter(grepl("mitma-distr", .data$local_path)) |>
366+
dplyr::filter(!.data$is_redirected_muni) |>
353367
dplyr::select("target_url", "file_size_bytes")
368+
354369
replaced_file_sizes_municip <- files_table |>
355-
dplyr::filter(grepl("mitma-municip", .data$local_path)) |>
370+
dplyr::filter(.data$is_redirected_muni) |>
356371
dplyr::select(-"file_size_bytes") |>
357372
dplyr::left_join(replacement_file_sizes_distr, by = "target_url")
373+
358374
files_table_replaced_file_sizes <- files_table |>
359-
dplyr::filter(!grepl("mitma-municip", .data$local_path)) |>
375+
dplyr::filter(!.data$is_redirected_muni) |>
360376
dplyr::bind_rows(replaced_file_sizes_municip) |>
361377
dplyr::arrange(dplyr::desc(.data$pub_ts))
362378
files_table <- files_table_replaced_file_sizes
@@ -366,13 +382,19 @@ spod_available_data_v1 <- function(
366382
2
367383
)
368384

369-
file_sizes <- readRDS(
370-
system.file(
371-
"extdata",
372-
"available_data_v1.rds",
373-
package = "spanishoddata"
385+
file_sizes_path <- spod_get_v1_meta_path()
386+
if (file.exists(file_sizes_path)) {
387+
file_sizes <- readRDS(file_sizes_path)
388+
} else {
389+
# Fallback to empty if not found
390+
file_sizes <- tibble::tibble(
391+
target_url = character(0),
392+
etag = character(0),
393+
true_etag = character(0),
394+
true_remote_file_size_bytes = numeric(0)
374395
)
375-
)
396+
}
397+
376398
files_table <- dplyr::left_join(
377399
files_table |> dplyr::select(-"file_size_bytes"),
378400
file_sizes |>
@@ -394,13 +416,19 @@ spod_available_data_v1 <- function(
394416
) |>
395417
dplyr::select(-"true_etag")
396418
} else {
397-
file_sizes <- readRDS(
398-
system.file(
399-
"extdata",
400-
"available_data_v1.rds",
401-
package = "spanishoddata"
419+
file_sizes_path <- spod_get_v1_meta_path()
420+
if (file.exists(file_sizes_path)) {
421+
file_sizes <- readRDS(file_sizes_path)
422+
} else {
423+
# Fallback to empty if not found
424+
file_sizes <- tibble::tibble(
425+
target_url = character(0),
426+
etag = character(0),
427+
true_etag = character(0),
428+
true_remote_file_size_bytes = numeric(0)
402429
)
403-
)
430+
}
431+
404432
if ("file_size_bytes" %in% colnames(files_table)) {
405433
files_table_no_size <- files_table |> dplyr::select(-"file_size_bytes")
406434
} else {
@@ -497,6 +525,8 @@ spod_available_data_v1 <- function(
497525
)
498526
}
499527

528+
files_table$is_redirected_muni <- NULL
529+
500530
return(files_table)
501531
}
502532

R/download_data.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ spod_download <- function(
180180
]
181181
}
182182

183+
if (nrow(requested_files) == 0 && !isTRUE(ignore_missing_dates)) {
184+
stop(
185+
"No data files found for the requested criteria (type, zones, and dates) in the metadata.\n",
186+
"Please check if the data for these dates is available in the selected format."
187+
)
188+
}
189+
183190
# compare file sizes
184191
requested_files <- requested_files |>
185192
dplyr::mutate(

R/internal-utils.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,3 +592,14 @@ spod_assert_package <- function(...) {
592592

593593
rlang::is_installed(...)
594594
}
595+
596+
#' Get the path to the V1 metadata RDS file
597+
#' @return A `character` string with the path to the RDS file, or `""` if not found.
598+
#' @keywords internal
599+
spod_get_v1_meta_path <- function() {
600+
system.file(
601+
"extdata",
602+
"available_data_v1.rds",
603+
package = "spanishoddata"
604+
)
605+
}

tests/testthat/test-check-files.R

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
library(testthat)
2-
library(spanishoddata)
3-
41
# Tests using real fixtures to verify ETag computation and consistency checking logic
52

63
test_that("spod_check_files detects inconsistency in fixtures", {

tests/testthat/test-cite.R

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# Load testthat
2-
library(testthat)
3-
41
# --- Tests for spod_cite function ---
52

63
test_that("spod_cite with default arguments prints all citations in all formats", {

tests/testthat/test-download.R

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
library(testthat)
2-
library(spanishoddata)
3-
41
# Use real data directory fixtures
52
# Tests integration of metadata cache -> decision logic -> download call
63

tests/testthat/test-duckdb-helpers-coverage.R

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
library(testthat)
2-
library(spanishoddata)
3-
41
# Use setup_test_data_dir helper instead of mocks
52
# This ensures we test against the real file structure and logic
63

7-
test_that("spod_duckdb_od validates version", {
4+
test_that("spod_duckdb_od creates a valid DuckDB view", {
85
expect_error(spod_duckdb_od(ver = 3), "Invalid version number")
96
})
107

tests/testthat/test-live-integration.R

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
library(testthat)
2-
library(spanishoddata)
3-
41
# This test file is designed for scheduled live integration tests on CI.
52
# It hits real APIs and S3 buckets to ensure the data provider infrastructure
63
# and the package's parsing logic remain compatible.

0 commit comments

Comments
 (0)