gpx
filesThere are many file formats for spatial data. One of the more common formats used for many hand held devices (GPS, smart watches, etc.), .gpx
are an interesting file type. They can hold points, as well as lines (or “tracks”). These usually have additional information, and include timestamps, elevation, and other metadata.
Let’s load packages first:
library(sf)
library(dplyr)
library(lubridate)
library(mapview)
library(here)
mapviewOptions(fgb = FALSE)
gpx
FileLet’s use an example file that lives here on github on github. Download the file, and save it into a data
folder.
Now we can read it in! Notice there’s lots of additional information in there. However, this is a still a data.frame
and an sf
object, which means its easy to work with.
# a locally downloaded gpx file
<- "NFA.GPX"
file1
# read just the tracks:
<- st_read(here::here("data", file1), layer = "tracks") trax
## Reading layer `tracks' from data source `/Users/rapeek/Documents/github/mapping-in-R-workshop/data/NFA.GPX' using driver `GPX'
## Simple feature collection with 4 features and 14 fields
## Geometry type: MULTILINESTRING
## Dimension: XY
## Bounding box: xmin: -120.9267 ymin: 39.10202 xmax: -120.9186 ymax: 39.10835
## Geodetic CRS: WGS 84
# check the names of the tracks:
## here these represent dates for individual tracks
$name trax
## [1] "2017-05-05-NFA" "2018-05-02-NFA" "2020-05-06-NFA" "2020-06-03-NFA"
# here we can see all the data inside the data frame
str(trax)
## Classes 'sf' and 'data.frame': 4 obs. of 15 variables:
## $ name : chr "2017-05-05-NFA" "2018-05-02-NFA" "2020-05-06-NFA" "2020-06-03-NFA"
## $ cmt : chr NA NA NA NA
## $ desc : chr NA NA NA NA
## $ src : chr NA NA NA NA
## $ link1_href : chr NA NA NA NA
## $ link1_text : chr NA NA NA NA
## $ link1_type : chr NA NA NA NA
## $ link2_href : chr NA NA NA NA
## $ link2_text : chr NA NA NA NA
## $ link2_type : chr NA NA NA NA
## $ number : int NA NA NA NA
## $ type : chr NA NA NA NA
## $ gpxx_TrackExtension : chr " <gpxx:DisplayColor>Cyan</gpxx:DisplayColor> " " <gpxx:DisplayColor>Cyan</gpxx:DisplayColor> " " <gpxx:DisplayColor>Yellow</gpxx:DisplayColor> " " <gpxx:DisplayColor>Magenta</gpxx:DisplayColor> "
## $ gpxtrkx_TrackStatsExtension: chr " <gpxtrkx:Distance>4470</gpxtrkx:Distance> <gpxtrkx:TimerTime>13209</gpxtrkx:TimerTime> <g"| __truncated__ " <gpxtrkx:Distance>3171</gpxtrkx:Distance> <gpxtrkx:TimerTime>10017</gpxtrkx:TimerTime> <g"| __truncated__ NA " <gpxtrkx:Distance>1659</gpxtrkx:Distance> <gpxtrkx:TimerTime>6197</gpxtrkx:TimerTime> <gp"| __truncated__
## $ geometry :sfc_MULTILINESTRING of length 4; first list element: List of 1
## ..$ : num [1:529, 1:2] -121 -121 -121 -121 -121 ...
## ..- attr(*, "class")= chr [1:3] "XY" "MULTILINESTRING" "sfg"
## - attr(*, "sf_column")= chr "geometry"
## - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
## ..- attr(*, "names")= chr [1:14] "name" "cmt" "desc" "src" ...
gpx
from a URLNow let’s do the same thing, but let’s read in points
instead, and use a weblink instead of a local file.
# read straight from the interwebs
<- "https://raw.githubusercontent.com/ryanpeek/mapping-in-R-workshop/main/data/NFA.GPX"
file1url
# read just the tracks:
<- st_read(file1url, layer = "waypoints") pts
## Reading layer `waypoints' from data source `https://raw.githubusercontent.com/ryanpeek/mapping-in-R-workshop/main/data/NFA.GPX' using driver `GPX'
## Simple feature collection with 167 features and 26 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: -120.9266 ymin: 39.1043 xmax: -120.9182 ymax: 39.10817
## Geodetic CRS: WGS 84
# check the names of attributes
names(pts)
## [1] "ele" "time" "magvar" "geoidheight" "name"
## [6] "cmt" "desc" "src" "link1_href" "link1_text"
## [11] "link1_type" "link2_href" "link2_text" "link2_type" "sym"
## [16] "type" "fix" "sat" "hdop" "vdop"
## [21] "pdop" "ageofdgpsdata" "dgpsid" "gpxx_WaypointExtension" "wptx1_WaypointExtension"
## [26] "ctx_CreationTimeExtension" "geometry"
Great!
If we want to pull a single track or a single set of points, we can leverage {dplyr}
and filter or subset our data in whatever way we want.
Let’s grab a single track, and only pull waypoints from a selected set of locations and specific year (need the {lubridate}
package).
# pull a single track
<- trax %>% dplyr::filter(name=="2017-05-05-NFA")
trx1
# get points and exclude a site called "NFARRAV"
<- st_read(here("data/",file1), layer="waypoints") %>%
pts filter(!grepl("NFARRAV", x = name)) %>%
mutate(YYYY = year(time)) %>%
filter(YYYY == 2018)
## Reading layer `waypoints' from data source `/Users/rapeek/Documents/github/mapping-in-R-workshop/data/NFA.GPX' using driver `GPX'
## Simple feature collection with 167 features and 26 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: -120.9266 ymin: 39.1043 xmax: -120.9182 ymax: 39.10817
## Geodetic CRS: WGS 84
Now that we have our filtered and selected pieces, we can make a quick map with the excellent {mapview}
package. We’ll color points by elevation, just because. This information isn’t particularly relevant, more to show how you can make fun mapview maps.
mapview(pts, zcol="ele") + mapview(trx1, color="orange")