-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
247 lines (213 loc) · 7.33 KB
/
app.js
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
const map = L.map('map').setView([35.8, -78.65], 11)
const streets = L.esri.basemapLayer('Topographic').addTo(map)
// LAYERS
function lapColor(score) {
return score >= 90 ? '#a50026' :
score >= 80 ? '#d73027' :
score >= 70 ? '#f46d43' :
score >= 60 ? '#fdae61' :
score >= 50 ? '#fee090' :
score >= 40 ? '#e0f3f8' :
score >= 30 ? '#abd9e9' :
score >= 20 ? '#74add1' :
score >= 10 ? '#4575b4' :
'#313695';
}
map.createPane('equity')
map.getPane('equity').style.zIndex = 403
let equity = L.esri.featureLayer({
url: 'https://services.arcgis.com/v400IkDOw1ad7Yad/arcgis/rest/services/Raleigh_Census_Block_Groups_2024_Park_Equity_Analysis/FeatureServer', // Actual date is from 20210727 but I made a typo creating the service
layers: [0],
pane: 'equity',
})
//map.createPane('lap')
//map.getPane('lap').style.zIndex = 403
//let lap = L.esri.featureLayer({
//url: 'https://services.arcgis.com/v400IkDOw1ad7Yad/arcgis/rest/services/spa_20210721/FeatureServer/0', // Actual date is from 20210727 but I made a typo creating the service
//where: "etj = 1",
//pane: 'lap',
//style: feature => {
//return {
//fillColor: lapColor(feature.properties.lap_score),
//weight: 0,
//fillOpacity: 1
//}
//}
//})
map.createPane('parcels')
map.getPane('parcels').style.zIndex = 405
let parcels = L.esri.dynamicMapLayer({
url: 'https://maps.wakegov.com/arcgis/rest/services/Property/Parcels/MapServer',
layers: [0],
pane: 'parcels'
}).addTo(map)
map.createPane('recap')
map.getPane('recap').style.zIndex = 409
let recap = L.esri.featureLayer({
url: 'https://services.arcgis.com/VTyQ9soqVukalItT/arcgis/rest/services/Racially_or_Ethnically_Concentrated_Areas_of_Poverty/FeatureServer/0',
where: "STUSAB='NC' AND COUNTY_NAME='Wake' AND rcap_current=1",
style: {
weight: 0,
fillOpacity: 0.5,
fillColor: '#607D8B'
},
pane: 'recap'
}).addTo(map)
//map.createPane('parks')
//map.getPane('parks').style.zIndex = 411
//let parks = L.esri.featureLayer({
//url: 'https://services.arcgis.com/v400IkDOw1ad7Yad/arcgis/rest/services/Parks/FeatureServer/0',
//pane: 'parks'
map.createPane('blockgroups')
map.getPane('blockgroups').style.zIndex = 413
let blockgroups = L.featureGroup({pane: 'blockgroups'}).addTo(map)
map.createPane('isoline')
map.getPane('isoline').style.zIndex = 417
let isoline = L.featureGroup({pane: 'isoline'}).addTo(map)
map.createPane('mapClickPoint')
map.getPane('mapClickPoint').style.zIndex = 421
let mapClickPoint = L.featureGroup({pane: 'mapClickPoint'}).addTo(map)
let overlays = {
'Selected Location': mapClickPoint,
'Service Area': isoline,
'Selected Block Groups': blockgroups,
'Racially or Ethnically Concentrated Areas of Poverty': recap,
'Wake County Parcels': parcels,
'Equity': equity
}
let baseLayers = {
'Streets': streets
}
L.control.layers(baseLayers, overlays).addTo(map)
map.on('click', e => {
// Clear previous analysis layers
blockgroups.clearLayers()
isoline.clearLayers()
mapClickPoint.clearLayers()
// Get parameters
let inputXY = [e.latlng.lat, e.latlng.lng]
let mode = getCheckedRadioValue('mode')
let minutes = rangeSlider.value
// Run Analysis
// KEEP THIS FOR TESTING
// let bufferDistance;
// if (mode == '1') {
// bufferDistance = parseFloat(minutes) * 0.583
// } else {
// bufferDistance = parseFloat(minutes) * 0.08
// }
// let saPolygonJSON = turf.featureCollection([
// turf.buffer(
// turf.flip(turf.point(inputXY)),
// bufferDistance,
// { unit: 'miles' }
// )
// ])
let saURL = `https://utility.arcgis.com/usrsvcs/appservices/cD6s6VKuje0IvTZF/rest/services/World/ServiceAreas/NAServer/ServiceArea_World/solveServiceArea?facilities=${e.latlng.lng},${e.latlng.lat}&f=json&travelMode=${mode}&defaultBreaks=${minutes}f=geojson`
fetch(saURL)
.then(result => {
return result.json()
})
.then(json => {
saPolygonJSON = L.esri.Util.arcgisToGeoJSON(json.saPolygons)
// Query service and calculate values
let query = L.esri.query({
url: 'https://services.arcgis.com/v400IkDOw1ad7Yad/arcgis/rest/services/Raleigh_Census_Block_Groups_2024_Park_Equity_Analysis/FeatureServer/0' // Actual date is from 20210727 but I made a typo creating the service
})
query.intersects(saPolygonJSON.features[0])
query.fields(["HistInequity", "HealthWellness", "EnvironJust", "WalkDemand"])
console.log(query)
query.run((err, results, raw) => {
let features = results.features
if (features.length === 0) {
console.warn("No features returned from the query.");
return;
}
blockgroups.addLayer(L.geoJSON(turf.featureCollection(features), {
style: {
weight: 0,
color: '#673AB7'
},
pane: 'blockgroups'
}))
map.fitBounds(blockgroups.getBounds())
// Historic Inequity
let inequityMean = ss.sum(features.map(f => f.properties.HistInequity))
document.getElementById("inequity-stat").innerText = inequityMean.toFixed(2);
// Health and Wellness
let healthwellnessMean = Math.round(ss.mean(features.map(f => f.properties.HealthWellness)))
document.getElementById("healthwellness-stat").innerText = healthwellnessMean.toFixed(2);
// Environmental Justice
let environMean = Math.round(ss.mean(features.map(f => f.properties.EnvironJust)))
document.getElementById("environ-stat").innerText = environMean.toFixed(2);
// Walkability Demand
let walkdemandMean = ss.mean(features.map(f => f.properties.WalkDemand))
document.getElementById("walkdemand-stat").innerText = walkdemandMean.toFixed(2);
})
// Add Layers
mapClickPoint.addLayer(L.circleMarker(inputXY, {
radius: 12,
weight: 0,
color: "#121212",
opacity: 0.25,
fillOpacity: 0.5,
pane: 'mapClickPoint'
}))
isoline.addLayer(L.geoJSON(saPolygonJSON, {
style: {
fillOpacity: 0,
weight: 2,
color: '#121212',
dashArray: "5 5"
},
pane: 'isoline'
}))
})
.catch(err => {
console.log(err)
})
})
function losScoreToGrade(score) {
return score >= 17 ? 'A' :
score >= 13 ? 'B' :
score >= 9 ? 'C' :
score >= 5 ? 'D' :
score === 4 ? 'F' :
'';
}
function priorityLevel(val) {
return val >= 80 ? 'Very High' :
val >= 60 ? 'High' :
val >= 40 ? 'Medium' :
val >= 20 ? 'Low' :
val >= 0 ? 'Very Low' :
'🤷';
}
// UI
// Checked Radio Button
function getCheckedRadioValue(name) {
const radios = document.querySelectorAll(`input[name=${name}]`)
let selectedValue;
for (const radio of radios) {
if (radio.checked) {
selectedValue = radio.value
break
}
}
return selectedValue
}
// Clear everything
document.getElementById("clear-button").addEventListener("click", resetResults)
function resetResults() {
blocks.clearLayers()
isoline.clearLayers()
mapClickPoint.clearLayers()
for (stat of ["inequity-stat", "healthwellness-stat", "environ-stat", "walkdemand-stat"]) {
document.getElementById(stat).innerText = "-"
}
}
// Range slider
const rangeSlider = document.getElementById("minutes");
rangeSlider.addEventListener("input", () => {
document.getElementById("minutes-slider-value").innerText = rangeSlider.value
})