R/ShapefileFunctions.R

Defines functions ReadOzShapefile SimplifyOzShapefile FindOzCentroids

Documented in ReadOzShapefile SimplifyOzShapefile

#' Read in ABS Shapefile
#'
#' A wrapper for rgdal's readOGR() function to demonstrate how the Big___
#' objects included in the package were created.
#'
#' @param ShapefileURL A string pointing to the top-level directory where the
#'     Australian Bureau of Statistic files (see source) were extracted. This
#'     should be the folder containing a .dbf, .prj, .shp, .shx and .xml
#' @param LGAorPOA A string, either "LGA" or "POA". This will be converted to
#'     the ABS' name for the appropriate layer, "LGA_2016_AUST" or
#'     "POA_2016_AUST" respectively.
#'
#' @return A SpatialPolygonsDataFrame: 545 LGAs (18 null geometries get
#'     dropped) or 2668 postcodes (2 null geometries dropped).
#'
#' @examples
#' \dontrun{
#' # With a file structure like:
#' #
#' # Working directory/
#' #     ABS Shapefiles/
#' #         LGAs/
#' #             LGA_2016_AUST.dbf
#' #             LGA_2016_AUST.prj
#' #             LGA_2016_AUST.shp
#' #             LGA_2016_AUST.shx
#' #             LGA_2016_AUST.xml
#'
#' ReadOzShapefile("ABS Shapefiles/LGAs", "LGA")
#'
#' }
#'
#' @export
ReadOzShapefile <- function(ShapefileURL, LGAorPOA){

  rgdal::readOGR(

    dsn = ShapefileURL,
    layer = switch(LGAorPOA,
                   "LGA" = "LGA_2016_AUST",
                   "POA" = "POA_2016_AUST")

  )

}


#' Simplify ABS Shapefile
#'
#' A wrapper for rmapshaper's ms_simplify() function to demonstrate how the
#' Simple___ objects included in the package were created. This takes some time
#' to run.
#'
#' @param ShapefileObject Either the BigLGA or BigPOA objects, i.e. the object
#'     returned by \code{\link{ReadOzShapefile}}.
#'
#' @return A much smaller SpatialPolygonsDataFrame
#'
#' @examples
#' \dontrun{
#'     SimplifyOzShapefile(BigLGA)
#'     SimplifyOzShapefile(BigPOA)
#' }
#'
#' @export
SimplifyOzShapefile <- function(ShapefileObject){

  rmapshaper::ms_simplify(ShapefileObject,

      keep = 0.005,
      keep_shapes = T,
      no_repair = F,
      snap = T,
      explode = F

    )

}


#' Find Centres of Polygons
#'
#' A very simple wrapper to sp's coordinates() function. As far as I'm aware
#' that just finds some kind of reasonable midpoint in x (longitudes) and y
#' (latitudes).
#'
#' @param BigShapefile Either the BigLGA or BigPOA objects (see also
#'     \code{\link{ReadOzShapefile}}). The 'big' versions are preferred for
#'     precision - these centroids should be more 'accurate' than those
#'     derived using the already-simplified versions.
#' @param LGAorPOA Character, "LGA" or "POA"
#'
#' @return A tibble with three columns:
#' \describe{
#'     \item{long}{The longitudinal coordinate}
#'     \item{lat}{The latitudinal coordinate}
#'     \item{Name}{The name of the polygon, i.e. the postcode or the LGA name}
#' }
#'
#' @export
FindOzCentroids <- function(BigShapefile, LGAorPOA){

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

  sp::coordinates(BigShapefile) %>%

    as.tibble %>%

    setNames(c("long", "lat")) %>%

    mutate("Name" = as.character(BigShapefile@data[[paste0(LGAorPOA, "_NAME16")]])) %>%

    return()

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