Skip to content

Instantly share code, notes, and snippets.

@mdmadhu
Last active August 17, 2022 23:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mdmadhu/e92ee7a2fc595b3941c596c35662f1a8 to your computer and use it in GitHub Desktop.
Save mdmadhu/e92ee7a2fc595b3941c596c35662f1a8 to your computer and use it in GitHub Desktop.
R Script to pull together GPS waypoints from multiple GPX files into a single CSV file
# MD Madhusudan, mdm@ncf-india.org, Nature Conservation Foundation, 2016-04-10
# Code to import waypoints from multiple GPX files in a folder,
# extract coordinates from each file into a single object, and
# export to CSV or some other format
#
# - initialisation steps
library(plotKML)
library(dplyr)
# Set working directory to folder where all the GPX Waypoint files are present
setwd("~/Desktop/")
# - List all filenames in folder starting with "Waypoint"
# Works for waypoint files with names like "Waypoints_22-FEB-16.gpx"
files <- list.files(pattern = "\\bWaypoints_")
# Initialise empty data frame
wpfull <- NULL
for (i in 1:length(files)) {
# - Select first file from the list and import data into R object
wplist <- readGPX(files[i], way=T)
# extract latitude, longituDe, elevation, time, name and comments and apppend to R dataframe
wpdf<- wplist$waypoints
# append dataframe from last index to a full waypoint object
wpfull <- bind_rows(wpfull, wpdf)
}
# export object with all waypoints to csv file
write.csv(wpfull, "finalwp.csv")
@ginskr
Copy link

ginskr commented Feb 10, 2020

Hi, what if my data is not contained under waypoints, but under tracks - how to expand that part into a dataframe?

@ginskr
Copy link

ginskr commented Feb 14, 2020

To add to above, I get an error message: Error: Argument 1 can't be a list containing data frames

@mdmadhu
Copy link
Author

mdmadhu commented Feb 14, 2020 via email

@eihwood
Copy link

eihwood commented Jun 23, 2020

To add to above, I get an error message: Error: Argument 1 can't be a list containing data frames

Hi, what if my data is not contained under waypoints, but under tracks - how to expand that part into a dataframe?

The dataframe containing the consecutive points of your trackline is nested within the wplist as a list in a list in a list. I was able to access mine using the following:

tracklinedf <- wplist$tracks[[1]][[1]]

@vjjan91
Copy link

vjjan91 commented May 31, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment