R/read_intermediate_region.R

Defines functions read_intermediate_region

Documented in read_intermediate_region

#' Download spatial data of Brazil's Intermediate Geographic Areas
#'
#' @description
#' The intermediate Geographic Areas are part of the geographic division of
#' Brazil created after 2017 by IBGE. These regions were created to replace the
#' "Meso Regions" division. Data at scale 1:250,000.
#'
#' @template year
#' @param code_intermediate 4-digit code of an intermediate region. If the
#'        two-digit code or a two-letter uppercase abbreviation of a state is
#'        passed, (e.g. 33 or "RJ") the function will load all intermediate
#'        regions of that state. If `code_intermediate="all"` (Default), the
#'        function downloads all intermediate regions of the country.
#' @template simplified
#' @template output
#' @template showProgress
#' @template cache
#' @template verbose
#'
#' @return An `"sf" "data.frame"` OR an `ArrowObject`
#'
#' @export
#'
#' @examplesIf identical(tolower(Sys.getenv("NOT_CRAN")), "true")
#'
#' # Read an specific intermediate region
#' inter <- read_intermediate_region(code_intermediate = 1202, year = 2024)
#'
#' # Read intermediate regions of a state
#' inter <- read_intermediate_region(code_intermediate = "AM", year = 2024)
#' inter <- read_intermediate_region(code_intermediate = 12, year = 2024)
#'
#' # Read all intermediate regions of the country
#' inter <- read_intermediate_region(code_intermediate = "all", year = 2024)
#'
read_intermediate_region <- function(year,
                                     code_intermediate = "all",
                                     simplified = TRUE,
                                     output = "sf",
                                     showProgress = TRUE,
                                     cache = TRUE,
                                     verbose = TRUE){

  # Get metadata with data url addresses
  temp_meta <- select_metadata(
    geography="intermediateregions",
    year = year,
    simplified = simplified,
    verbose = verbose
  )

  # check if metadata download failed
  if (is.null(temp_meta)) { return(invisible(NULL)) }

  # download files
  temp_arrw <- download_parquet(
    filename_to_download = temp_meta$file_name,
    showProgress,
    cache
  )

  # check if download failed
  if (is.null(temp_arrw)) { return(invisible(NULL)) }

  # FILTER
  temp_arrw <- filter_arrw(temp_arrw, code = code_intermediate)

  # convert to sf
  temp <- convert_output(temp_arrw, output)

  return(temp)


}

Try the geobr package in your browser

Any scripts or data that you put into this service are public.

geobr documentation built on June 23, 2026, 5:06 p.m.