-
Notifications
You must be signed in to change notification settings - Fork 0
/
camp_dash.R
559 lines (421 loc) · 23.3 KB
/
camp_dash.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)
library(sf)
library(plotly)
library(raster)
library(rjson)
library(lubridate)
library(viridis)
library(colorspace)
#### Read in SHP, convert to MULTISTRING, extract geometry ####
temp = list.files(path = "SHP/",pattern="*.shp") #list of all SHP files
x <- seq(1, length(temp),1) #sequence as long as number of CSVs, used for loop functions
shapes <- list() #empty list to fill
for (i in x){
thing <- read_sf(paste0("SHP/",temp[[i]])) #reads in each CSV from temp list
shapes[[i]] <- thing #adds data to data list
}
for (i in x){
shapes[[i]] <- st_cast(shapes[[i]], "MULTILINESTRING") #converts polygons to multilinestrings
}
#### Read in geoJSON data into lists ####
temp <- list.files(path = "JSON/", pattern = "*.geojson") #list of all geoJSON files
json <- list() #empy list to fill
for (i in x){
thing <- fromJSON(file = paste0("JSON/",temp[[i]])) #reads in each geoJSON file from temp list
json[[i]] <- thing #adds data to geo list
}
#### Read in CSV for Dates ####
temp = list.files(path = "CSV/",pattern="*.csv") #list of all CSV files
csv <- list() #empty list to fill
alldates <- NULL
alldatelabs <- list()
for (i in x){
thing <- read.csv(file = paste0("CSV/",temp[[i]]), stringsAsFactors = F) #reads in each CSV from temp list
csv[[i]] <- thing #adds data to dates list
}
sf <- stamp("Jan 13", order = "Obd") #create date stamp for labeling
for (i in x){
csv[[i]]$Labels <- csv[[i]]$Date #create new labal column & fill with dates
csv[[i]]$Labels <- sf(mdy(csv[[i]]$Labels)) #updates label dates with stamp dates
alldatelabs[[i]] <- csv[[i]]$Labels[1]
alldates[[i]] <- csv[[i]]$Date[1]
}
encampments <- c() #empty list to fill
for (i in x){ #cylces through each date
y <- seq(1, length(csv[[i]]$Camp),1) #gets number of camps for the date
for (j in y){ #runs through each camp for the date
if (csv[[i]]$Camp[j] %in% encampments) { #if the camp is already in encampments skip
} else {
encampments <- c(encampments, csv[[i]]$Camp[j]) #add camp to encampments if it isn't already
}
}
}
counts <- list() #empty list to fill with camp/tent dataframes
for (i in x){ #loops through each CSV
c <- c(csv[[i]]$Camp) #creates list of camps
t <- c(csv[[i]]$Tents) #creates list of tent counts
counts[[i]] <- data.frame(c,t) #turns camps/tents into data frame and adds to list
}
encampments.df <- as.data.frame(encampments) #sets top row of df as encampment names
for (i in x) { #loops through each CSV
z <- seq(1, length(encampments), 1) #makes sequence equal to number of encampments
q <- c() #empty list to fill with tent counts per encampment
for (j in z){ #loops through each encampment name
if(encampments[[j]] %in% counts[[i]]$c){ #checks if encampment is in current data set
l <- which(counts[[i]] == encampments[[j]], arr.ind = T) #gives df row & column for encampment
q <- append(q, counts[[i]]$t[l[1]]) #finds tent count associated with encampment and adds it df
}else{
q <- append(q, NA) #if the encampment isn't in the data set to 0
}
}
encampments.df <- cbind(encampments.df, q) #adds all encampment tent counts for date to df
}
row.names(encampments.df) <- encampments #name rows encampments
encampments.df[1] <- NULL #remove excess 'encampments column
colnames(encampments.df) <- alldatelabs #name columns from dates
encampments.df <- t(encampments.df) #transpose df, turns it into a matrix
encampments.df <- as.data.frame(encampments.df) #convert back to final df
large.tent <- max(encampments.df, na.rm = T) #defines largest tent count
pal2 <- magma(large.tent) #creates color palette in magma from 1-largest tent count
encampments.df.total <- cbind(encampments.df, "Total Tents" = rowSums(encampments.df, na.rm = T)) #add total tents count summed row
encampments.df.dates <- cbind(encampments.df, "Dates" = as.data.frame(alldates)) #adds dates column
encampments.df.total.dates <- cbind(encampments.df.total, "Dates" = as.data.frame(alldates)) #both dates and total tents added
#### Loop to create Geo Ref Dataframe ####
encampments.df.geo <- encampments.df #new DF to hold geometries
for ( i in seq(1, nrow(encampments.df.geo),1)){ #loop through each date
for ( j in seq(1, (ncol(encampments.df.geo)),1)){ #loop through each camp in the date
if (is.na(encampments.df.geo[alldatelabs[[i]],encampments[[j]]])){ #if the cell in the DF is NA keep it so
} else {
encampments.df.geo[i,j] <- paste0("shapes[[",i,"]]$geometry[[",match(encampments[[j]], csv[[i]]$Camp),"]][[1]]") #fill occupied cells with geometry
}
}
}
#### Camp Plot ####
abatements <- read.csv("Abatements.csv", stringsAsFactors = F)
plot.camps <- plot_ly(height = 700) #empty plot to fil
for (i in seq(1, ncol(encampments.df.total),1)){
if ( i == ncol(encampments.df.total)){ #if last column (ie Total Tents)
plot.camps <- plot.camps %>% add_trace(encampments.df.total,
x = as.Date(encampments.df.dates[,ncol(encampments.df.dates)], format = "%m/%d/%Y"),
y = encampments.df.total[,i],
visible = T,
type = 'scatter',
mode = 'lines+markers',
name = "Total Tents",
line = list(color = 'rgba(0,0,0)'), #specify black color
marker = list(size = 8, #larger marker
color = 'rgba(0,0,0)')) #specify black color
}else{
plot.camps <- plot.camps %>% add_trace(encampments.df.total, #other encampments get generic plotly color
x = as.Date(encampments.df.dates[,ncol(encampments.df.dates)], format = "%m/%d/%Y"),
y = encampments.df.total[,i],
visible = T,
type = 'scatter',
mode = 'lines+markers',
name = encampments[i],
marker = list(size = 7))
}
}
### Create Abatement Lines & Annotations
line <- list(
type = "line",
line = list(color = "red"),
xref = "x",
yref = "y"
)
lines <- list()
for (i in seq(1, nrow(abatements),1)){
if ((i %% 2) == 0){
line[["y0"]] <- 0
line[["y1"]] <- large.tent
line[["x0"]] <- as.Date(abatements[i,1], format = "%m/%d/%Y")
line[["x1"]] <- as.Date.character(abatements[i,1], format = "%m/%d/%Y")
lines <- c(lines, list(line))
}else{
line[["y0"]] <- 0
line[["y1"]] <- (large.tent - 20)
line[["x0"]] <- as.Date(abatements[i,1], format = "%m/%d/%Y")
line[["x1"]] <- as.Date.character(abatements[i,1], format = "%m/%d/%Y")
lines <- c(lines, list(line))
}
}
line.height <- as.data.frame(rep(max(encampments.df[,1:(ncol(encampments.df))], na.rm = T), nrow(abatements)))
for (i in seq(1, nrow(line.height), 1)){
if ((i %% 2) == 0){
line.height[i, 1] = (line.height[i, 1] + 3)
}else{
line.height[i, 1] = ((large.tent - 20) + 3)
}
}
abatements <- cbind(abatements, line.height)
annotations <- list()
for ( i in seq_len(nrow(abatements))){
annotation <- list( x = as.Date(abatements[i,1], format = "%m/%d/%Y"),
y = abatements[i,3],
text = abatements[i,2],
showarrow = F)
annotations[[i]] <- annotation
}
abate.button <- rep(TRUE, ncol(encampments.df.total))
abate.button[ncol(encampments.df.total)+1] <- FALSE
button <- list(list(args = list(list(), list(annotations = NULL, shapes = NULL)),
args2 = list(list(), list(annotations = annotations, shapes = lines)),
method = 'update',
label = "Abatements")) #labels each map with its associated date
### Create Layout for Plot ###
plot.camps <- plot.camps %>% layout(title = "Encampment Stats", #adds title
showlegend = T,
paper_bgcolor = 'rgb(250,250,250)',
plot_bgcolor = 'rgb(240,240,240)',
legend = list(
itemsizeing = "constant",
itemwidth = 60,
bgcolor = 'rgb(220,220,220)',
bordercolor = 'rgb(0,0,0)',
borderwidth = 1,
yanchor = "top",
y = .92
),
yaxis = list(title = "Tents"), #labels y-axis
xaxis = list(
rangeselector = list(
buttons = list(
list(
count = 3,
label = "3 mo",
step = "month",
stepmode = "backward"),
list(
count = 6,
label = "6 mo",
step = "month",
stepmode = "backward"),
list(
count = 1,
label = "1 yr",
step = "year",
stepmode = "backward"),
list(
count = 1,
label = "YTD",
step = "year",
stepmode = "todate"),
list(step = "all"))),
rangeslider = list(type = "date")),
updatemenus = list(
list(
type = 'buttons',
buttons = button,
xanchor = 'left',
x = 1.02
)
)) #addes range slider
plot.camps
#### Loop for creating maps and associated slider steps ####
colorscale <- as.data.frame(c(0,1))
colorscale2 <- as.data.frame(c('rgb(235, 219, 47)', 'rgb(235, 219, 47)'))
colorscale <- cbind(colorscale, colorscale2)
steps <- list() #empty list to hold slider details
map.camp <- plot_ly(height = 700) #new empty figure for plotly
for (i in x) { #loops through each geoJSON & CSV to create maps, and sliders
camp <- c(paste0("Camp: ", csv[[i]]$Camp,""))
tents <- c(paste0("<br>","Tents: ", csv[[i]]$Tents))
date <- c(paste0("<br>", "Date: ", alldatelabs[[i]]))#counts tents for hover text
hover <- c(paste0(camp, date, tents)) #tents, area, and density are put into an array for hover text
if ( i == length(x)){
map.camp <- map.camp %>% add_trace( #add polygons/data to map in multiple layers
type = "choroplethmapbox",
geojson = json[[i]], #area info
locations = csv[[i]]$OBJECTID, #links geoJSON and CSV data
z = 1, #CSV data used for choropleth
colorscale = colorscale,
featureidkey = "properties.OBJECTID",
showscale = F,
marker=list(line=list( #removes boarders from polys
width=1, color = 'rgb(173, 0, 113)')),
hovertext = hover, #sets hover text to created array
hoverinfo = "text",
visible = T#only show array dada (no location values)
)
} else {
map.camp <- map.camp %>% add_trace( #add polygons/data to map in multiple layers
type = "choroplethmapbox",
geojson = json[[i]], #area info
locations = csv[[i]]$OBJECTID, #links geoJSON and CSV data
z = 1, #CSV data used for choropleth
colorscale = colorscale,
featureidkey = "properties.OBJECTID",
showscale = F,
marker=list(line=list( #removes boarders from polys
width=1, color = 'rgb(173, 0, 113)')),
hovertext = hover, #sets hover text to created array
hoverinfo = "text",
visible = F#only show array dada (no location values)
)
}
step <- list(args = list('visible', rep(FALSE, length(json))), #creates a list for step values set to FALSE for each map
method = 'restyle',
label = print(csv[[i]]$Labels[[1]])) #labels each map with its associated date
step$args[[2]][i] = TRUE #turns on the single map selected, set to true, all other maps left invisible
steps[[i]] = step #adds on/off list to steps list
}
#### Create Layout for Map ####
map.camp <- map.camp %>% layout( #sets look of map, title, sliders, etc...
mapbox = list(
style = "white-bg", #map style set
center = list(lon = -111.898, lat = 40.755), #center corrdinates of map set
zoom = 13.5,
layers = list(list(
opacity = .5,
below = 'traces',
sourcetype = "raster",
source = list("https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}")))), #zoom of map on cords set
margin = list(
r = 75
),
sliders = list(list(active = length(steps)-1, #turns sliders on
bgcolor = "#838289", #stylize sliders
bordercolor = "#33303f", #stylize sliders
activebgcolor = "#292929", #stylize sliders
pad = list( #stylize sliders
b = 10
),
currentvalue = list( #use current value feature
prefix = "Date: ", #call it date and pull label (ie date)
offset = 20,
font = list(
size = 15,
color = "#33303f"
)
),
font = list(
color = "#33303f"
),
steps = steps, #use steps data
transition = list(
duration = 0
))),
clickmode = "select",
paper_bgcolor = 'rgb(250,250,250)'
)
#### Set Mapbox Token, Output Map, Save as HTML ####
map.camp <- map.camp %>% #key for html mapbox pages
config(mapboxAccessToken = Sys.getenv("pk.eyJ1IjoiZ2thc3NpZGF5IiwiYSI6ImNrbGJqOHVnbDJ3Y3AzMnFtMWpxMTJnZTAifQ.gpfgCnyTuXAsEutgOWY0mg"))
map.camp #view map in RStudio
#### Dash Text ####
explanation_text <- "
## The City
Salt Lake City, like most urban areas in the United States, is home to an assortment of homeless encampments.
These spaces are seen by the city and state as hinderances to growing downtown rejuvenation projects as they are
considered dangerous, unsightly, and criminal. One way in which the city tracks the activities of the homeless is
through the app *SLC Mobile*, which offers users the ability to request cleanup services.
> SLC Mobile is a real time, free, civic engagement platform that allows the public to conveniently communicate
non-emergency civic issues directly to city government from their smart devices. Download this app to report community
issues or problems; you can even submit photos.
These cleanup service calls are aggregated by the city and presented on their [Homeless Services Dashboard](https://www.slc.gov/hand/homeless-services-dashboard/)
as a [map of homelessness throughout Salt Lake City](https://slcgov.maps.arcgis.com/apps/webappviewer/index.html?id=41d87239080d4963aa2c64ea60994c21). Of course
this map does not represent the true geography of homelessness in SLC. Rather it reveals the areas in which the
homeless are most likely to be seen as unsightly and in need of removal.
## The Maps
The map and statistics presented below attempt to illuminate the geography of homelessness in Salt Lake City through in situ data collection.
Every three days the city is canvased by car and camps are documented by video. Using these recordings the spatial extent and number
of tents present on the streets can be mapped in fine spatial and temporal detail. Camps here are defined as any public
space with two or more tents visible from the road. The outlined camps represent the spatial extent between these tents.
While more intensive than the city's mapping, this methodology is not a perfect record of SLC Homeless Encampments.
Limited by funding and time, these maps do not record any encampent not visible from the road. This means that large
encampents, sometimes over 50 tents, which exist is green spaces along the Jordan River or up mountain trails are excluded.
Relying on the knowledge of the homeless and local activists to locate camps, there are certainly blind spots in the canvasing
. Many encampments are found only once they have grown large enough to 'draw attention.'
## The Purpose
Acknowledging the limitiations of these maps, they should not be read as an exhaustive catalogue of homeless encampments in Salt Lake City.
Rather, they reveal the ways in which camps grow, shrink, adapt, and respond to stresses overtime. Since the beginning of this project the
Salt Lake City Health Department and Police Department have kept up their ongoing raids, sweeps, and abatements of these camps. Sometimes
warned, sometimes not, the inhabitants of these communities must pick up their belongings, when allowed, and move along. The relatively
constant number of tents on the streets throughout time clearly demonstrates the futility of these activities to 'eliminate homelessness.'
Critically examining how the homeless maneuver around these sweeps, adapting to new locations and rebuilding on old sites, provides insight
into these communities.
In contrast to the images of criminality and danger presented by the city, these encampents are communities of resiliance.
***
"
map.title_text <- "Encampments Map"
map.exp_text <- "
Use this map to investigate how camps move, grow, and adapt overtime. The date slider below the map can be used to
interactively explore the extent of camps for a given date, or moved quickly to see how a specific location has changed.
Hovering over an encampment gives its name, the specific date being viewed, and how many tents were documented at that time.
"
plot.title_text <- "Encampment Statistics"
plot.exp_text <- "
This plot visualizes how the total number of tents in each camp, and city wide, has changed over time. The legend on the right
can be used to turn off specific camps, or double clicked to isolate a single camp. Removing the total camps plot line gives a
more detailed view of the camps. The range slider along the bottom can be used to investigate specific time periods, or
select the buttons in the upper left for time periods of general interest. Hovering over a line marker will indicate the date of
the recording, the number of tents present, and the name of the encampment. Also make sure to select the 'Abatements' button in
the upper right. This will reveal when sweeps accured and to which encampments.
"
#### Dash ####
pageTitle <- htmlH1(
'Salt Lake City Homeless Encampments Dataset',
style = list(
textAlign = 'center'
)
)
pageSubTitle <- htmlH2(
paste0(csv[[1]][["Date"]][1], " - ", csv[[length(csv)]][["Date"]][1]),
style = list(
textAlign = 'center'
)
)
graph.plot <- dccGraph(
id = 'plot',
figure = plot.camps
)
graph.map <- dccGraph(
id = 'map',
figure = map.camp
)
explanation <- htmlDiv(
list(
dccMarkdown(children = explanation_text)
)
)
map.title <- htmlH3(
map.title_text,
style = list(
textAlign = 'center'
)
)
map.exp <- htmlDiv(
list(
dccMarkdown(children = map.exp_text)
)
)
plot.title <- htmlH3(
plot.title_text,
style = list(
textAlign = 'center'
)
)
plot.exp <- htmlDiv(
list(
dccMarkdown(children = plot.exp_text)
)
)
app <- Dash$new(external_stylesheets = "https://codepen.io/chriddyp/pen/bWLwgP.css")
app$layout(
htmlDiv(
list(
pageTitle,
pageSubTitle,
explanation,
map.title,
map.exp,
graph.map,
plot.title,
plot.exp,
graph.plot
)
)
)
app$run_server(host='0.0.0.0', port=8050)