Convert DataFrame of Coordinates to GPX Format in Four Steps


Often, one may have a data frame of locations with attributes such as species names and may wish to create a GPX file from this data to use with a navigation software such as a GPS device. This can be achieved in four steps as follows:

Step 1: Load R Library and DataFrame

# Load R library and dataframe
library(terra)

x <- data.frame(NAME=c("Species1", "Species2", "Species3"),
                LAT=c(70.11, 70.032145, 68.619535),
                LON=c(-148.52, -148.640821, -149.556137))
head(x)
    

Step 2: Create a Spatial Vector from the DataFrame

# Create a spatial vector from the dataframe
v <- vect(x, geom=c("LON", "LAT"), crs="+proj=longlat +datum=WGS84")
    

Step 3: Write the Spatial Vector to File in GPX Format

# Write the spatial vector to file in GPX format
writeVector(v, "~/GPS_waypoints.gpx", filetype="GPX", layer="waypoints", 
            overwrite=TRUE, options="GPX_USE_EXTENSIONS=yes")
    

Step 4: Upload File to GPS Device

Connect your GPS device to a computer, and drag and drop the new file in the GPX folder of your GPS device. That's it!