@@ -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
0 commit comments