-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathtornadoes.R
56 lines (51 loc) · 1.55 KB
/
tornadoes.R
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
#' Get NOAA tornado data.
#'
#' This function gets spatial paths of tornadoes from NOAA's National Weather
#' Service Storm Prediction Center Severe Weather GIS web page.
#'
#' @export
#' @param ... Curl options passed on to [crul::verb-GET] (optional)
#' @return A Spatial object is returned of class SpatialLinesDataFrame.
#' @references https://www.spc.noaa.gov/gis/svrgis/
#' @note See [torn_cache] for managing cached files
#' @examples \dontrun{
#' shp <- tornadoes()
#' library('sp')
#' if (interactive()) {
#' # may take 10 sec or so to render
#' plot(shp)
#' }
#' }
tornadoes <- function(...) {
check4pkg('rgdal')
url <- 'https://www.spc.noaa.gov/gis/svrgis/zipped/1950-2019-torn-aspath.zip'
tornadoes_GET(url, ...)
readshp(file.path(torn_cache$cache_path_get(), tornadoes_basename))
}
tornadoes_GET <- function(url, ...) {
bp <- torn_cache$cache_path_get()
torn_cache$mkdir()
if (!is_tornadoes(file.path(bp, tornadoes_basename))) {
fp <- file.path(bp, "tornadoes.zip")
cli <- crul::HttpClient$new(url, opts = list(...))
res <- cli$get(disk = fp)
res$raise_for_status()
unzip(fp, exdir = bp)
} else {
cache_mssg(bp)
}
}
is_tornadoes <- function(x){
if (identical(list.files(x), character(0))) {
FALSE
} else {
all(list.files(x) %in% tornadoes_files)
}
}
tornadoes_basename <- "1950-2019-torn-aspath"
readshp <- function(x) {
rgdal::readOGR(dsn = path.expand(x),
layer = tornadoes_basename, stringsAsFactors = FALSE)
}
tornadoes_files <- paste0(tornadoes_basename,
c(".dbf", ".prj", ".shp", ".shx", ".cpg"))