-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path01_vector_gpx.Rmd
More file actions
108 lines (69 loc) · 3.02 KB
/
01_vector_gpx.Rmd
File metadata and controls
108 lines (69 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
---
pagetitle: "gpx"
editor_options:
chunk_output_type: console
---
<br>
## Working with `gpx` files
There 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:**
```{r loadlibsHidden, echo=TRUE, eval=FALSE, warning=FALSE}
library(sf)
library(dplyr)
library(lubridate)
library(mapview)
library(here)
mapviewOptions(fgb = FALSE)
```
```{r loadlibsOut, eval=TRUE, echo=FALSE, warning=FALSE}
suppressPackageStartupMessages({
library(sf);
library(dplyr);
library(lubridate);
library(mapview);
library(here)
})
mapviewOptions(fgb = FALSE)
```
### Read in a Track from `gpx` File
Let's use an example file that lives [here on github](https://raw.githubusercontent.com/ryanpeek/mapping-in-R-workshop/main/data/NFA.GPX) 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.
```{r gpxFile, echo=T, eval=T}
# a locally downloaded gpx file
file1 <- "NFA.GPX"
# read just the tracks:
trax <- st_read(here::here("data", file1), layer = "tracks")
# check the names of the tracks:
## here these represent dates for individual tracks
trax$name
# here we can see all the data inside the data frame
str(trax)
```
### Read points from a `gpx` from a URL
Now let's do the same thing, but let's read in `points` instead, and use a weblink instead of a local file.
```{r gpxURL, echo=TRUE, eval=TRUE}
# read straight from the interwebs
file1url <- "https://raw.githubusercontent.com/ryanpeek/mapping-in-R-workshop/main/data/NFA.GPX"
# read just the tracks:
pts <- st_read(file1url, layer = "waypoints")
# check the names of attributes
names(pts)
```
Great!
### Pull a Single Track and Subset Waypoints
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).
```{r pullSingleTrack, echo=TRUE, eval=TRUE}
# pull a single track
trx1 <- trax %>% dplyr::filter(name=="2017-05-05-NFA")
# get points and exclude a site called "NFARRAV"
pts <- st_read(here("data/",file1), layer="waypoints") %>%
filter(!grepl("NFARRAV", x = name)) %>%
mutate(YYYY = year(time)) %>%
filter(YYYY == 2018)
```
### Mapview!
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.
```{r mapview, eval=T, echo=T}
mapview(pts, zcol="ele") + mapview(trx1, color="orange")
```