R/load_prepare_data.R

Defines functions loadPrepareShpFile divideNChunks

Documented in divideNChunks loadPrepareShpFile

#' Load And Prepare Flood Shapefile
#'
#' @param shapefile_path Path to the Shapefile containing flood risk
#'
#' @return A \code{\link{SpatialPolygonsDataFrame-class}} object
#' @export
#'
#' @importFrom rgdal readOGR
#' @importFrom sp spTransform
#' @examples
#' \dontrun{
#' my_flood_shapefile <- loadPrepareShpFile("C:\\Users\\Mat\\Desktop\\CV\\RoFRS_London.shp")
#'}
loadPrepareShpFile <- function(shapefile_path) {
  spdf <- readOGR(shapefile_path)
  spdf <- spTransform(spdf,
                      CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
  )
  return(spdf)
}


#' Divide flood shapefile into N smaller parts
#'
#' @param shapefile Flood shapefile from loadPrepareShpFile function
#' @param shapefile_bounds Shapefile containing teritorial division (i.e. administrative)
#' @param region_col_name Name of column containing region name from shapefileBounds
#' to check which column contains information about name of region)
#'
#' @return A \code{\link{list}} with N smaller shapefiles
#' @export
#'
#' @note
#' Note - For the identification of column name containing region name for region_col_name argument,
#' You should run View(head(shapefile_bounds@data))
#'
#' @examples
#' shapefile_divided <- divideNChunks(shapefile = shapefile,
#' shapefile_bounds = shapefile_bounds, region_col_name = "NAME")
#'

divideNChunks <- function(shapefile, shapefile_bounds, region_col_name){

  divided <- list()
  chunks_names <- as.character(shapefile_bounds@data[[region_col_name]])

  for (i in 1:length(chunks_names)){
    single_name <- as.character(shapefile_bounds@data[i,region_col_name])
    single_name_polygon <- shapefile_bounds[which(shapefile_bounds[[region_col_name]]==single_name),]

    divided[[chunks_names[i]]] <- shapefile[single_name_polygon,]
  }

  return(divided)
}
mur4mat/floodshp documentation built on Dec. 16, 2019, 12:37 a.m.