ipeaGIT / geobr

Easy access to official spatial data sets of Brazil in R and Python
https://ipeagit.github.io/geobr/
789 stars 118 forks source link

gpkg corrupted? #224

Closed NilsDroste closed 3 years ago

NilsDroste commented 3 years ago

currently, there seems to be an issue with the one of the downloaded gpkg as geobr::read_municipality() fails to read.

Steps to reproduce:

muns <- geobr::read_municipality(code_muni="all", simplified = F, year=2020)

Error: Cannot open "..\Temp\RtmpsBQxDW\21MU.gpkg"; The source could be corrupt or not supported. See st_drivers() for a list of supported formats.

sf::st_drivers() does indicate the existence of a corresponding driver

                         name                                                   long_name write  copy is_raster is_vector   vsi
GPKG                     GPKG                                                  GeoPackage  TRUE  TRUE      TRUE      TRUE  TRUE
sessionInfo()
R version 4.0.4 (2021-02-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_Sweden.1252  LC_CTYPE=English_Sweden.1252    LC_MONETARY=English_Sweden.1252 LC_NUMERIC=C                   
[5] LC_TIME=English_Sweden.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] sf_0.9-8         ggpubr_0.4.0     eventStudy_0.1.0 here_1.0.1       forcats_0.5.1    stringr_1.4.0    dplyr_1.0.5     
 [8] purrr_0.3.4      readr_1.4.0      tidyr_1.1.3      tibble_3.1.0     ggplot2_3.3.3    tidyverse_1.3.0 
rafapereirabr commented 3 years ago

Hi @NilsDroste. I believe our server is a bit unstable at the moment. Could you please restart you R session and try it again in an hour or so?

henrique1008 commented 3 years ago

Olá @rafapereirabr , estou com o mesmo problema citado por NilsDroste

image

Olhando mais a fundo, fiz o código para ver se havia algum problema com um estado específico

No resultado de temp_sf vem uma lista, onde o índice que estiver NULL representa o estado com problema

Adicionei um Sys.sleep() entre o download de um estado e outro, para garantir não sobrecarregar o servidor

library(tidyverse)

temp_meta <- geobr::select_metadata(
   geography = "state", 
   year = 2020,
   simplified = FALSE
   )

file_url <- temp_meta$download_path

download_gpkg_sleep <- function(url){

   dados <- geobr::download_gpkg(file_url = url,progress_bar = TRUE)

   Sys.sleep(3)

   return(dados)

 }

maybe <- purrr::possibly(download_gpkg_sleep,otherwise = NULL)

temp_sf <- file_url %>% 
   purrr::map(.,maybe)

Rodei algumas vezes, para ver se o erro vem sempre no mesmo estado, porém não é o que acontece, a cada vez, da problema em um estado diferente

Realmente deve ser problema no servidor dos arquivos, e como sempre vem algum dos estados com problema, da pau na hora de consolidar o arquivo final da saída da função

Dados esse problema, um possível esquema de solução para ser incorporado na função do pacote

library(tidyverse)

# Baixa os metadados
temp_meta <- select_metadata(
   geography = "state", 
   year = 2020,
   simplified = FALSE
   )

# Baixa os links dos estados
file_url <- temp_meta$download_path

# Deixa a função de download flexível ao erro
maybe_download <- purrr::possibly(geobr::download_gpkg,otherwise = NULL)

# Função para download, caso de erro, continua tentando ate funcionar
download_gpkg_2 <- function(url){

   dados <- NULL

   while(is.null(dados)){

     dados <- maybe_download(file_url = url,progress_bar = TRUE)

   }

   return(dados)

 }

# Baixa todos os estados
temp_sf <- file_url %>% 
   purrr::map(.,download_gpkg_2)

# Cria o arquivo final
estados <- dplyr::bind_rows(temp_sf)

Desse jeito, quando a função geobr::download_gpkg der problema devido ao servidor dos arquivos, retornará NULL, e tentará baixar outra vez o mesmo estado

rafapereirabr commented 3 years ago

Thank you for your suggesiton, @henrique1008. This might help, but it does not entirely solve the problem, which seems to involve making changes to Ipea's server infrastructure. I'm checking this with our IT team. I'll keep you posted here.

rafapereirabr commented 3 years ago

Hi all. We've mande some changes to our servers and this problem seems to be fixed now. Thank you for the heads up! Closing this issue for now, but please let us know if you encounter this problem again.

rzendonadi commented 2 years ago

Hey team, I've been receiving the message: " 'temp.gpkg' not recognized as a supported file format" when using the following code: census = read_census_tract(code_tract="all", year=2020). Am I missing something?

rafapereirabr commented 2 years ago

Hi @rzendonadi . It seems be working fine on the other machines I've tried on now. Please restart your R session and try again. Let me know if it works

rzendonadi commented 2 years ago

Hey, @rafapereirabr. Apparently only for the state of Amapá returns that message when using this python code: geobr.read_census_tract(code_tract = 'AP', year=2020, zone="urban"). All the other states that I've tried are running well. Can you double check, please?

rafapereirabr commented 2 years ago

I just checked and the data file itself is fine. Could you try the code without the zone="urban" bit? This parameter is only really necessary when year=2000.

geobr.read_census_tract(code_tract = 'AP', year=2020)

matheushenriquebarboza commented 6 months ago

Estou tendo o mesmo problema aqui, como mostra a figura seguinte. Tentei rodar algumas vezes 3 dias atrás e agora mais algumas vezes, em outro computador, dando sempre esse mesmo problema. Até tentei mudar o ano do layer, mas não adiantou image

rafapereirabr commented 6 months ago

Oi @matheushenriquebarboza . O codigo roda normalmente aqui. Isso pode ocorrer se houve algum problema de conexão durante o download e o arquivo baixado ficou corrompido. Nesse caso, sugiro reiniciar a sessão do R e tentar o download novamente.

df <- geobr::read_census_tract(code_tract = 3550308, year = 2000)
head(df)