-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.R
141 lines (115 loc) · 5.73 KB
/
functions.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
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
###########################################################
# #
# functions.R #
# Define common functions for Chicago VZ Dashboard #
# #
###########################################################
updateCrashes <- function() {
# Data Portal API Key
# Use .Renviron in this directory or Sys.setenv("APP_TOKEN" = "YOUR SOCRATA TOKEN")
readRenviron(".Renviron")
# Download new Chicago Data Portal crashes
url <- "https://data.cityofchicago.org/resource/85ca-t3if.json?$"
start_date <- '2022-01-01T00:00:00'
end_date <- Sys.Date()
date_query <- paste0("crash_date between ","'",start_date,"'"," and ","'",end_date,"'")
injury_query <- paste0("most_severe_injury != 'NO INDICATION OF INJURY'") # removes ~85% of crashes (property damage only)
query <- paste0(url,"where=",date_query," AND ",injury_query)
crashes <- read.socrata(query, app_token = Sys.getenv("APP_TOKEN"))
# Expected columns
expected_cols <- c("crash_record_id", "rd_no", "crash_date", "posted_speed_limit",
"traffic_control_device", "device_condition", "weather_condition",
"lighting_condition", "first_crash_type", "trafficway_type",
"lane_cnt", "alignment", "roadway_surface_cond", "road_defect",
"report_type", "crash_type", "hit_and_run_i", "damage", "date_police_notified",
"prim_contributory_cause", "sec_contributory_cause", "street_no",
"street_direction", "street_name", "beat_of_occurrence", "num_units",
"most_severe_injury", "injuries_total", "injuries_fatal", "injuries_incapacitating",
"injuries_non_incapacitating", "injuries_reported_not_evident",
"injuries_no_indication", "injuries_unknown", "crash_hour", "crash_day_of_week",
"crash_month", "location.type", "crash_date_est_i", "intersection_related_i",
"photos_taken_i", "statements_taken_i", "private_property_i",
"dooring_i", "work_zone_i", "work_zone_type", "workers_present_i",
"latitude", "longitude")
for(col in expected_cols) {
if(!col %in% names(crashes)){
crashes[, col] <- NA # set missing columns NA
}
}
# (When getting recent crashes from the portal, some of the police report fields will be missing. Annoyingly, the JSON response just omits these fields. added 2022-01-03)
# Fix Lat/Long
crashes$latitude = as.numeric(crashes$latitude)
crashes$longitude = as.numeric(crashes$longitude)
# Eliminate non-Chicago or missing lat/long points
crashes <- crashes %>%
select(-c(location.coordinates)) %>%
filter(latitude > 35 & longitude < -85)
# Reorder injury severity levels
crashes$most_severe_injury <- factor(as.character(crashes$most_severe_injury),
levels = c("FATAL","INCAPACITATING INJURY","NONINCAPACITATING INJURY","REPORTED, NOT EVIDENT","NO INDICATION OF INJURY"))
# Convert numbers saved as text to integers
crashes$injuries_total = as.integer(crashes$injuries_total)
crashes$injuries_fatal = as.integer(crashes$injuries_fatal)
crashes$injuries_incapacitating = as.integer(crashes$injuries_incapacitating)
crashes$injuries_non_incapacitating = as.integer(crashes$injuries_non_incapacitating)
crashes$injuries_reported_not_evident = as.integer(crashes$injuries_reported_not_evident)
crashes$year = format(crashes$crash_date,"%Y")
return(crashes)
}
loadCrashData <- function() {
# NOTE this is a ton of data, load into memory once
crashes = future(readRDS("./crash_summaries/Crashes_2009_present_IDOT_and_Chicago.rds"))
return(crashes)
}
getCrashes <- function(crashes, yearFrom = NULL, yearTo = NULL, hourFrom = NULL, hourTo = NULL, boundingBox = NULL, crashTypes = NULL, injTypes = NULL) {
# Argument formats:
# boundingBox = list containing north, south (degrees lat), east, west (degrees lon)
# crashTypes, injTypes = string or character vector, e.g. c("PEDESTRIAN","PEDALCYCLIST")
# Query the crash database for selected crashes
filteredCrashes = crashes
if(!is.null(yearFrom)){
filteredCrashes = filteredCrashes %>%
filter(year >= yearFrom)
}
if(!is.null(yearTo)){
filteredCrashes = filteredCrashes %>%
filter(year <= yearTo)
}
if(!is.null(hourFrom)){
filteredCrashes = filteredCrashes %>%
filter(crash_hour >= hourFrom)
}
if(!is.null(hourTo)){
filteredCrashes = filteredCrashes %>%
filter(crash_hour <= hourTo)
}
if(!is.null(boundingBox)){
filteredCrashes = filteredCrashes %>%
filter(latitude > boundingBox$south & latitude < boundingBox$north & longitude < boundingBox$east & longitude > boundingBox$west)
}
if(!is.null(crashTypes)){
filteredCrashes = filteredCrashes %>%
filter(first_crash_type %in% crashTypes)
}
if(!is.null(injTypes)){
filteredCrashes = filteredCrashes %>%
filter(most_severe_injury %in% injTypes)
}
return(filteredCrashes)
}
updateCrashTable <- function(crashes_filtered,bounds) {
table = crashes_filtered %>%
filter(latitude > bounds$south & latitude < bounds$north & longitude < bounds$east & longitude > bounds$west)
return(table)
}
getDrawnRectangle <- function(new_feature) {
if("geometry" %in% names(new_feature)) {
feature <- unlist(new_feature$geometry$coordinates)
lat <- feature[seq(0, length(feature), 2)]
lon <- feature[seq(1, length(feature), 2)]
bounds = list(north = max(lat), south = min(lat), east = max(lon), west = min(lon))
return(bounds)
} else {
return(NULL)
}
}