-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathREADME.Rmd
199 lines (151 loc) · 6.4 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/"
)
```
<!-- badges: start -->
[](https://github.com/ateucher/lutz/actions/workflows/R-CMD-check.yaml)
[](https://app.codecov.io/github/ateucher/lutz?branch=master)
[](https://cran.r-project.org/package=lutz)
[](https://cran.r-project.org/package=lutz)
<!-- badges: end -->
# lutz (look up time zones)
## Lookup the time zone of coordinates
Input latitude and longitude values or an `sf/sfc` POINT object and get back
the time zone in which they exist. Two methods are implemented. One is very
fast and uses Rcpp in conjunction with source data from (https://github.com/darkskyapp/tz-lookup-oss/).
However, speed comes at the cost of accuracy - near time zone borders away from
populated centres there is a chance that it will return the incorrect time zone.
The other method is slower but more accurate - it uses the sf package to
intersect points with a detailed map of time zones from
[here](https://github.com/evansiroky/timezone-boundary-builder).
## time zone utility functions
**lutz** also contains several utility functions for helping to understand and
visualize time zones, such as listing of world time zones,including information about daylight savings times and their offsets from UTC. You can also plot a time zone to visualize the UTC offset over a year and when daylight savings times are in effect.
## Installation
You can install lutz from CRAN with:
```{r eval=FALSE}
install.packages("lutz")
```
Or you can install the development version from github with:
```{r gh-installation, eval = FALSE}
# install.packages("devtools")
devtools::install_github("ateucher/lutz")
```
## Examples
There are two functions in this package for looking up the time zones of coordinates: `tz_lookup()` which works with both `sf/sfc` and `SpatialPoints` objects, and `tz_lookup_coords` for looking up lat/long pairs. Use the `method` argument to choose the `"fast"` or `"accurate"`
method.
### With coordinates. They must be lat/long in decimal degrees:
```{r warning=FALSE}
library(lutz)
tz_lookup_coords(49.5, -123.5, method = "fast")
tz_lookup_coords(49.5, -123.5, method = "accurate")
tz_lookup_coords(lat = c(48.9, 38.5, 63.1, -25), lon = c(-123.5, -110.2, -95.0, 130))
```
### With `sf` objects:
```{r warning=FALSE, message=FALSE}
library(sf)
library(ggplot2) # this requires the devlopment version of ggplot2
# Create an sf object out of the included state.center dataset:
pts <- lapply(seq_along(state.center$x), function(i) {
st_point(c(state.center$x[i], state.center$y[i]))
})
state_centers_sf <- st_sf(st_sfc(pts))
# Use tz_lookup_sf to find the time zones
state_centers_sf$tz <- tz_lookup(state_centers_sf)
state_centers_sf$tz <- tz_lookup(state_centers_sf, method = "accurate")
ggplot() +
geom_sf(data = state_centers_sf, aes(colour = tz)) +
theme_minimal() +
coord_sf(datum = NA)
```
### With `SpatialPoints` objects:
```{r warning=FALSE}
library(sp)
state_centers_sp <- as(state_centers_sf, "Spatial")
state_centers_sp$tz <- tz_lookup(state_centers_sp)
ggplot(cbind(as.data.frame(coordinates(state_centers_sp)), tz = state_centers_sp$tz),
aes(x = coords.x1, y = coords.x2, colour = tz)) +
geom_point() +
coord_fixed() +
theme_minimal()
```
Note that there are some regions in the world where a single point can land in
two different overlapping time zones. The `"accurate"` method [includes these](https://github.com/evansiroky/timezone-boundary-builder/releases/tag/2018g),
however the method used in the `"fast"` does not include overlapping time zones
([at least for now](https://github.com/darkskyapp/tz-lookup-oss/issues/34)).
We can compare the accuracy of both methods to the high-resolution time zone map
provided by https://github.com/evansiroky/timezone-boundary-builder. This is the
map that is used by `lutz` for the `"accurate"` method, but in `lutz` it is
simplified by about 80% to be small enough to fit in the package.
```{r eval=FALSE}
## Get the full time zone geojson from https://github.com/evansiroky/timezone-boundary-builder
download.file("https://github.com/evansiroky/timezone-boundary-builder/releases/download/2019a/timezones-with-oceans.geojson.zip",
destfile = "tz.zip")
unzip("tz.zip", exdir = "data-raw/dist/")
```
```{r eval=file.exists("data-raw/dist/combined-with-oceans.json")}
library(lutz)
library(sf)
library(purrr)
library(dplyr)
tz_full <- read_sf("data-raw/dist/combined-with-oceans.json")
# Create a data frame of 500000 lat/long pairs:
set.seed(1)
n <- 500000
ll <- data.frame(id = seq(n), lat = runif(n, -90, 90), lon = runif(n, -180, 180))
ll_sf <- st_as_sf(ll, coords = c("lon", "lat"), crs = 4326)
# Overlay those points with the full high-resolution time zone map:
ref_ll_tz <- sf::st_join(ll_sf, tz_full)
# Combine those that had overlapping time zones
ref_ll_tz <- ref_ll_tz %>%
st_set_geometry(NULL) %>%
group_by(id) %>%
summarize(tzid = paste(tzid, collapse = "; "))
# run tz_lookup with both `"fast"` and `"accurate"` methods and compare with
# the time zones looked up with the high-resolution map:
tests <- map_df(c("fast", "accurate"), ~ {
time <- system.time(test_ll_tz <- tz_lookup(ll_sf, method = .x, warn = FALSE))
comp <- ref_ll_tz$tzid == test_ll_tz
matches <- sum(comp, na.rm = TRUE)
mismatches <- sum(!comp, na.rm = TRUE)
list(
method = .x,
time = time["elapsed"],
matches = matches,
mismatches = mismatches,
accuracy = matches / (matches + mismatches),
ref_nas = sum(is.na(ref_ll_tz$tzid)),
fun_nas = sum(is.na(test_ll_tz))
)
})
```
```{r results="asis", eval=file.exists("data-raw/dist/combined-with-oceans.json")}
knitr::kable(tests)
```
## time zone utility functions
### `tz_plot()`
```{r}
tz_plot("America/Vancouver")
```
### `tz_offset()`
```{r}
# A Date object
tz_offset(Sys.Date(), "Africa/Algiers")
# A Date-like character string
tz_offset("2017-03-01", tz = "Singapore")
# A POSIXct date-time object
tz_offset(Sys.time())
```
### `tz_list()`
```{r}
tz_list() %>%
head(20) %>%
knitr::kable()
```