Skip to content

Commit

Permalink
add Beatriz Milz as ctb
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Dec 1, 2023
1 parent dec6f34 commit 9857fc1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 20 deletions.
75 changes: 57 additions & 18 deletions data_prep/R/health_facilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@
#> Source: Cadastro Nacional de Estabelecimentos de Saúde - CNES


#' source:
#' https://dados.gov.br/dados/conjuntos-dados/cnes-cadastro-nacional-de-estabelecimentos-de-saude
file_url = 'https://s3.sa-east-1.amazonaws.com/ckan.saude.gov.br/CNES/cnes_estabelecimentos.zip'


update_health_facilities <- function(){

current_date <- Sys.Date()
dir.create(path = temp_dir, recursive = TRUE, showWarnings = FALSE)
#' source:
#' https://dados.gov.br/dados/conjuntos-dados/cnes-cadastro-nacional-de-estabelecimentos-de-saude
file_url = 'https://s3.sa-east-1.amazonaws.com/ckan.saude.gov.br/CNES/cnes_estabelecimentos.zip'

# determine date of last update
caminho_api <- "https://dados.gov.br/api/publico/conjuntos-dados/cnes-cadastro-nacional-de-estabelecimentos-de-saude"

meta <- jsonlite::read_json(caminho_api, simplifyVector = TRUE) |>
purrr::pluck("resources") |>
tibble::as_tibble() |>
dplyr::filter(format == "ZIP")

meta$created
date_update <- as.Date(meta$created) |> as.character()
date_update <- gsub("-", "", date_update)
year_update <- substring(date_update, 1, 4)

# wodnload file to tempdir
temp_local_file <- download_file(file_url = file_url)
Expand All @@ -27,30 +37,51 @@ update_health_facilities <- function(){
file_name <- utils::unzip(temp_local_file, list = TRUE)$Name
file_full_name <- paste0(temp_local_dir,'/', file_name)

file.info(file_url)

# read file stored locally
dt <- data.table::fread( file_full_name )
head(dt)


# rename columns
# rename columns
names(dt) <- tolower(names(dt))
dt <- dplyr::rename(dt,
code_cnes = 'co_cnes',
code_state = 'co_uf',
code_muni6 = 'co_ibge',
cep = 'co_cep',
lat = 'nu_latitude',
lon = 'nu_longitude')

# add state and region
# fix code_cnes to 7 digits
dt[, code_cnes := sprintf("%07d", code_cnes)]

# fix code_muni to 7 digits
muni <- geobr::read_municipality(code_muni = 'all', year = as.numeric(year_update) - 1)
code7 <- data.table(code_muni = muni$code_muni)
code7[, code_muni6 := as.numeric(substring(code_muni, 1, 6))]

dt[code7, on = 'code_muni6', code_muni := i.code_muni]
dt[, code_muni6 := NULL]

# add state and region
dt <- add_state_info(temp_sf = dt, column = 'code_state')
dt <- add_region_info(temp_sf = dt, column = 'code_state')

# add update date columns
dt[, date_update := as.character(date_update)]
dt[, year_update := as.character(year_update)]

# dt[is.na(lat) | is.na(lon),]
# dt[lat==0,]
# reorder columns
data.table::setcolorder(dt,
c('code_cnes',
'code_muni',
'code_state', 'abbrev_state', 'name_state',
'code_region', 'name_region',
'date_update', 'year_update'))



head(dt)
# dt[is.na(lat) | is.na(lon),]
# dt[lat==0,]

# replace NAs with 0
data.table::setnafill(dt,
Expand All @@ -70,12 +101,20 @@ update_health_facilities <- function(){
# Change CRS to SIRGAS Geodetic reference system "SIRGAS2000" , CRS(4674).
temp_sf <- harmonize_projection(temp_sf)

# Save raw file in sf format
sf::st_write(cnes_sf, dsn= paste0("./health_facilities/shapes_in_sf_all_years_cleaned/",most_freq_year,"/cnes_sf_",most_freq_year,".gpkg"))

sf::st_write(temp2_simplified, i,
overwrite = TRUE, append = FALSE,
delete_dsn = T, delete_layer = T, quiet = T)
# create folder to save the data
dest_dir <- paste0('./data/health_facilities/')
dir.create(path = dest_dir, recursive = TRUE, showWarnings = FALSE)


# Save raw file in sf format
sf::st_write(cnes_sf,
dsn= paste0(dest_dir, 'cnes_', date_update,".gpkg"),
overwrite = TRUE,
append = FALSE,
delete_dsn = T,
delete_layer = T,
quiet = T
)

}
5 changes: 3 additions & 2 deletions r-package/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: geobr
Title: Download Official Spatial Data Sets of Brazil
Version: 1.8.1
Version: 1.8.1999
Authors@R: c(person(given="Rafael H. M.", family="Pereira", email="[email protected]", role=c("aut", "cre"), comment = c(ORCID = "0000-0003-2125-7465")),
person(given="Caio Nogueira", family="Goncalves", role=c("aut")),
person(given="Paulo Henrique Fernandes de", family="Araujo", role=c("ctb")),
Expand All @@ -17,7 +17,8 @@ Authors@R: c(person(given="Rafael H. M.", family="Pereira", email="rafa.pereira.
person(given="Alessandro", family="Samuel-Rosa", role="ctb"),
person(given="Daniel", family="Ferreira", role="ctb"),
person(given="Marcus", family="Saraiva", role="ctb"),
person("Ipea - Institue for Applied Economic Research", role = c("cph", "fnd")))
person(given="Beatriz", family="Milz", , role = "ctb", comment = c(ORCID = "0000-0002-3064-4486")),
person("Ipea - Institue for Applied Economic Research", role = c("cph", "fnd")))
URL: https://github.com/ipeaGIT/geobr
BugReports: https://github.com/ipeaGIT/geobr/issues
Description: Easy access to official spatial data sets of Brazil as 'sf' objects
Expand Down

0 comments on commit 9857fc1

Please sign in to comment.