-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathentrypoint.R
More file actions
executable file
·70 lines (55 loc) · 1.73 KB
/
entrypoint.R
File metadata and controls
executable file
·70 lines (55 loc) · 1.73 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
#!/usr/local/bin/Rscript
dht::greeting()
## load libraries without messages or warnings
withr::with_message_sink("/dev/null", library(dplyr))
withr::with_message_sink("/dev/null", library(tidyr))
withr::with_message_sink("/dev/null", library(sf))
doc <- "
Usage:
entrypoint.R <filename> <site>
"
opt <- docopt::docopt(doc)
## for interactive testing
## opt <- docopt::docopt(doc, args = c('my_address_file_geocoded.csv', 'cchmc'))
centers <- readr::read_csv('/app/center_addresses.csv')
selected_site <- opt$site
if (!selected_site %in% centers$abbreviation) {
stop(
'site argument is invalid or missing; please consult documentation for details',
call. = FALSE
)
}
message("reading input file...")
d <- dht::read_lat_lon_csv(
opt$filename,
nest_df = T,
sf = T,
project_to_crs = 5072
)
dht::check_for_column(d$raw_data, "lat", d$raw_data$lat)
dht::check_for_column(d$raw_data, "lon", d$raw_data$lon)
message('loading isochrone shape file...')
isochrones <- readRDS(glue::glue(
"/app/isochrones/{selected_site}_isochrones.rds"
)) # 5072 projection
## add code here to calculate geomarkers
message('finding drive time for each point...')
d$d <- suppressWarnings(st_join(d$d, isochrones, largest = TRUE)) %>%
mutate(
drive_time = ifelse(!is.na(drive_time), as.character(drive_time), "> 60")
)
message('finding distance (m) for each point...')
centers <- centers %>%
filter(abbreviation == selected_site) %>%
st_as_sf(coords = c('lon', 'lat'), crs = 4326) %>%
st_transform(5072)
d$d$distance <-
st_distance(centers, d$d) |>
as.numeric()
## merge back on .row after unnesting .rows into .row
dht::write_geomarker_file(
d = d$d,
raw_data = d$raw_data,
filename = opt$filename,
argument = opt$site
)