Skip to content

Instantly share code, notes, and snippets.

@simbamangu
Created December 1, 2022 08:40
Show Gist options
  • Save simbamangu/1e6da21ef197a30ad5870ca8ec2dca47 to your computer and use it in GitHub Desktop.
Save simbamangu/1e6da21ef197a30ad5870ca8ec2dca47 to your computer and use it in GitHub Desktop.
Read a directory of GPX files and summarise by data type
# Read a directory of GPX files and summarise by data type
require(sf)
require(dplyr)
# Read all the GPX filenames/locations in target, recursive
gpxs <-
dir(
path = "../_data/tracklogs/",
recursive = T,
full.names = T,
pattern = "*.gpx"
)
# Create an empty tibble
gpx_count <- length(gpxs)
gpxs_summary <- tibble(
sourcefile = character(gpx_count),
waypoints = numeric(gpx_count),
routes = numeric(gpx_count),
tracks = numeric(gpx_count),
route_points = numeric(gpx_count),
track_points = numeric(gpx_count)
)
# Update each row with filename and number of features
for (n in 1:gpx_count) {
gpxs_summary[n, 1] <- basename(gpxs[n])
gpxs_summary[n, 2:6] <- as.list(st_layers(gpxs[n])$features)
}
# Save it with YYYYMMDDTHHMMSS stamp
write_csv(gpxs_summary, file = paste0(
"GPXfiles_summary_",
format(Sys.time(), '%Y%m%dT%H%M%S'),
".csv"
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment