R/GenerateStateDict.R

Defines functions GenerateStateDict

Documented in GenerateStateDict

#' Generating the State Dictionary
#'
#' A function demonstrating how I derived the associative lists found in
#' \code{\link{StateSubregionDict}}.
#'
#' @param LGAobject Either of the Big or Simple LGA objects. The ABS includes
#'     a field (STE_NAME16) that provides the state in which each LGA is found.
#'     The POAs are derived from the Wikipedia page (see Source).
#'
#' @source https://en.wikipedia.org/wiki/Postcodes_in_Australia
#'
#' @export
GenerateStateDict <- function(LGAobject){

  `%>%` <- magrittr::`%>%`

  State.Dicts <- list()

  # Using the data provided in the shapefile from the ABS,
  # assemble the state-wise associative lists of LGA names.
  State.Dicts$LGAs <- (function(){

    states <- unique(LGAobject$STE_NAME16) %>% as.character() %>% sort()

    # This bit was manual
    state.abbreviations <- dplyr::case_when(

      states == "Australian Capital Territory" ~ "ACT",
      states == "New South Wales"              ~ "NSW",
      states == "Northern Territory"           ~ "NT",
      states == "Other Territories"            ~ "OTH",
      states == "Queensland"                   ~ "QLD",
      states == "South Australia"              ~ "SA",
      states == "Tasmania"                     ~ "TAS",
      states == "Victoria"                     ~ "VIC",
      states == "Western Australia"            ~ "WA"

    )

    purrr::map(states, function(x){

      as.character(LGAobject$LGA_NAME16)[which(as.character(LGAobject$STE_NAME16) == x)]

    }) %>%

      stats::setNames(state.abbreviations) %>%

      return()

  })()

  # Now the state-wise associate lists of postcodes:
  State.Dicts$POAs <- (function(){

    # Courtesy of the Wikipedia page on Australian postcodes
    # https://en.wikipedia.org/wiki/Postcodes_in_Australia

    all.states <- list(
      "VIC" = c(3000:3999, 8000:8999),
      "NSW" = c(1000:1999, 2000:2599, 2619:2899, 2921:2999),
      "TAS" = c(7000:7799, 7800:7999),
      "ACT" = c(0200:0299, 2600:2618, 2900:2920),
      "QLD" = c(4000:4999, 9000:9999),
      "NT"  = c(0800:0899, 0900:0999),
      "SA"  = c(5000:5799, 5800:5999),
      "WA"  = c(6000:6797, 6800:6999)
    )

    # And then this is a bit of a hack -
    # just lump every number that's NOT in one of those ranges into the "OTH" category.
    # This is NOT to say that all/any of these exist.
    all.states$OTH <- dplyr::setdiff(0:9999, unlist(just.states))

    # And don't forget to convert to a 4-digit-wide (leading zeroes!) string:
    return(purrr::map(all.states, ~formatC(., width = 4, flag = "0")))

  })()

}
mjkerrison/OzMap documentation built on Dec. 27, 2019, 2:18 a.m.