R/loadmeerun.R

#' loadmeerun
#'
#' @description Load gpx files and return a tidy data frame (tested with files generated by MeeRun).
#' @param folder select the folder containing the kml file(s)
#' @param layer select layer to use for reading the data
#' @return gpx tidy data frame containing loaded data
#' @export
#'
loadmeerun <- function(folder = NULL, layer = "track_points") {
	if (is.null(folder)) {
		folder <- readline(prompt = "Please select a folder! ")
	}
	files <- list.files(path = folder, full.names = TRUE, pattern = ".gpx")
	# Check whether GPS data could be loaded
	if (length(files) >= 1) {
		gpx <- lapply(files, FUN = rgdal::readOGR, layer = layer, stringsAsFactors = FALSE, verbose = FALSE)
	}
	else {
		print("No '.gpx' files found. Check your source folder.")
	}
	# Clean the data
	gpx <- lapply(gpx, data.frame)
	keeps <- c("track_seg_id", "track_seg_point_id", "ele", "time", "coords.x1", "coords.x2")
	gpx <- lapply(gpx, subset, select = keeps)
	cols <- c("segment", "segment_element", "elevation", "datetime", "longitude", "latitude")
	gpx <- lapply(gpx, FUN = stats::setNames, cols)
	gpx <- dplyr::bind_rows(gpx, .id = "track")
	# Return a tidy data frame
	return(gpx)
}
mruessler/gpxmap documentation built on May 23, 2019, 7:49 a.m.