-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
141 lines (98 loc) · 2.78 KB
/
README.Rmd
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
---
output: github_document
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
## What is gpx?
Very simple package which converts [GPX](https://www.topografix.com/GPX/1/1/) files to `sf` objects.
```{r}
library(gpx)
library(sf) ## for print methods of sf objects
gpx <- system.file("gpx/city_trail.gpx", package = "gpx")
sf <- gpx::gpx_sf( gpx )
sf
```
And that's it.
However, the real benefit, and the reason I built this package, is so we can plot them in mapdeck's trip layer
```{r, eval = FALSE, echo = FALSE}
library(mapdeck)
set_token( read.dcf("~/Documents/.googleAPI",fields = "MAPBOX"))
sf <- gpxsf::gpx_sf( gpx, time = "counter" )
mapdeck(
style = mapdeck_style("dark")
, location = c(145., -37.8)
, zoom = 10
) %>%
add_trips(
data = sf
)
```
## How are timestamps handled?
The timestamp can specified as one of
- datetime (default) - the numeric representation of POSIX dates
- counter - every track starts from 0 and counts to the maximum time
- normalise - every track's time is rescaled to be [0,1]
Here are some examples
```{r}
sf_time <- gpx_sf( gpx, time = "datetime" )
sf_count <- gpx_sf( gpx, time = "counter" )
sf_norm <- gpx_sf( gpx, time = "normalise" )
d <- getOption("digits")
options(digits = 12)
track_time <- unclass( sf_time[, "geometry"][[1]][[1]] )
head( track_time )
summary( track_time[, 4] )
track_count <- unclass( sf_count[, "geometry"][[1]][[1]] )
head( track_count )
summary( track_count[, 4] )
track_norm <- unclass( sf_norm[, "geometry"][[1]][[1]] )
head( track_norm )
summary( track_norm[, 4] )
options(digits = d)
```
### Surely there's something else?
Oh yes, it's quite quick too!
This example compares reading 364 gpx files using this library, compared to reading a single gpx file using `library(plotKML)`
```{r, eval = FALSE}
library(plotKML)
library(microbenchmark)
fp <- path.expand("../../Data/Strava/Bethan/activities/")
fp <- path.expand("~/Downloads/activities/")
l <- list.files(fp, patter = "gpx$")
gpx <- paste0(fp, l)
length( gpx )
# 364
sf <- gpx::gpx_sf( gpx, time = "counter" )
mapdeck(
style = mapdeck_style("dark")
#, location = c(145., -37.8)
, zoom = 10
) %>%
add_trips(
data = sf
, trail_length = 4000
, animation_speed = 800
, stroke_width = 25
, stroke_colour = "#FDE725BB"
)
microbenchmark::microbenchmark(
gpx = {
sf <- gpx::gpx_sf( gpx )
},
kml = {
#kml <- plotKML::readGPX( gpx[2]( )
lst <- lapply( gpx[1:5], plotKML::readGPX )
},
times = 1
)
# Unit: seconds
# expr min lq mean median uq max neval
# gpx 3.486148 3.486148 3.486148 3.486148 3.486148 3.486148 1
# kml 3.926912 3.926912 3.926912 3.926912 3.926912 3.926912 1
```