R/read_gps_visualizer.R

Defines functions read_gps_visualizer

Documented in read_gps_visualizer

#' Function to read tab delimited text files generated by 
#' \code{http://www.gpsvisualizer.com/draw} as spatial polygons. 
#' 
#' @param file File name. 
#' 
#' @param type Type of spatial object to generate. 
#' 
#' @author Stuart K. Grange
#' 
#' @return Spatial polygons. 
#' 
#' @export
read_gps_visualizer <- function(file, type = "polygon") {
  
  stopifnot(type == "polygon")
  
  # Load
  suppressWarnings(
    df <- readr::read_delim(
      file, delim = "\t", show_col_types = FALSE, progress = FALSE
    )
  )
  
  # When there are multiple units in the file
  if (class(df$latitude[1]) == "character") {
    
    df <- df %>% 
      filter(type != "type") %>% 
      mutate(latitude = as.numeric(latitude),
             longitude = as.numeric(longitude)) %>% 
      tidyr::fill(desc)
    
    id <- "desc"
    
  } else {
    id <- NA
  }
  
  # Promote to spatial object
  sp <- gissr::sp_from_data_frame(df, type = type, id = id)
  
  return(sp)
  
}
skgrange/gissr documentation built on Feb. 24, 2024, 2:55 p.m.