forked from ChelseaKR/nearmiss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembed.js
More file actions
207 lines (193 loc) · 7.18 KB
/
Copy pathembed.js
File metadata and controls
207 lines (193 loc) · 7.18 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
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
/* nearmiss embeddable hotspot widget — self-contained, framework-free (vendored
* Leaflet only). Drop it into any advocacy site via an <iframe> (see
* web/nearmiss-embed.js for a one-line script-tag loader).
*
* It renders ONE map: exposure-normalized hazard rates, with statistically
* significant Getis-Ord hotspots dashed and labeled — the honest "where the
* danger actually is" view. Magnitude is encoded by line thickness and
* significance by a dashed pattern AND text, never by color alone, and a text
* list of the significant hotspots is rendered as the non-visual equivalent.
*
* An explicit source-fixture allowlist accepts ?city=<slug> or
* ?data=../data/published/<slug>.geojson. Query input cannot choose another
* origin or directory. The provenance line is driven by the dataset's own
* embedded metadata; the local full-demo links preserve the allowlisted fixture.
*/
(function () {
"use strict";
function hasEncodedDatasetSelector() {
return window.location.search
.replace(/^\?/, "")
.split("&")
.some(function (part) {
var separator = part.indexOf("=");
var rawName = separator === -1 ? part : part.slice(0, separator);
var rawValue = separator === -1 ? "" : part.slice(separator + 1);
var name;
try {
name = decodeURIComponent(rawName.replace(/\+/g, " "));
} catch (_error) {
return false;
}
return (name === "data" || name === "city") && /%/.test(rawName + rawValue);
});
}
function resolveDatasetSlug() {
try {
var params = new URLSearchParams(window.location.search);
var dataValues = params.getAll("data");
var cityValues = params.getAll("city");
if (
dataValues.length > 1 ||
cityValues.length > 1 ||
(dataValues.length && cityValues.length) ||
hasEncodedDatasetSelector()
) {
return "davis";
}
var data = dataValues[0] || "";
var match = /^(?:\.\.\/|\/)?data\/published\/([a-z0-9][a-z0-9_-]*)\.geojson$/i.exec(data);
if (match) return match[1].toLowerCase();
var city = cityValues[0] || "";
if (/^[a-z0-9][a-z0-9_-]*$/i.test(city)) return city.toLowerCase();
} catch (e) {
/* old browser — use the default */
}
return "davis";
}
function datasetUrlForSlug(slug) {
return slug === "riverside"
? "../data/published/riverside.geojson"
: "../data/published/davis.geojson";
}
function demoUrlForSlug(slug) {
return slug === "riverside"
? "/web/davis-demo.html?city=riverside"
: "/web/davis-demo.html?city=davis";
}
var DATA_SLUG = resolveDatasetSlug();
var DATA_URL = datasetUrlForSlug(DATA_SLUG);
var DEMO_URL = demoUrlForSlug(DATA_SLUG);
var mapEl = document.getElementById("embed-map");
var captionEl = document.getElementById("embed-caption");
var listEl = document.getElementById("embed-hotspots");
var sourceEl = document.getElementById("embed-source");
var brandLinkEl = document.getElementById("embed-brand-link");
var fullLinkEl = document.getElementById("embed-fulllink");
if (brandLinkEl) brandLinkEl.setAttribute("href", DEMO_URL);
if (fullLinkEl) fullLinkEl.setAttribute("href", DEMO_URL);
function widthFor(value, max, lo, hi) {
if (!max || max <= 0) return lo;
return lo + (hi - lo) * Math.sqrt(Math.max(0, value) / max);
}
function featureLatLngs(geometry) {
// GeoJSON is [lon, lat]; Leaflet wants [lat, lon].
return (geometry.coordinates || []).map(function (c) {
return [c[1], c[0]];
});
}
function render(geojson) {
var features = (geojson.features || []).filter(function (f) {
return f.geometry && f.geometry.type === "LineString";
});
var meta = geojson.metadata || {};
if (sourceEl) {
var unit = meta.exposure_unit ? " per " + meta.exposure_unit : "";
sourceEl.textContent =
(meta.city ? meta.city + " · " : "") +
"exposure-normalized hazard rate" +
unit +
" · synthetic fixture";
}
var maxRate = features.reduce(function (m, f) {
return Math.max(m, f.properties.rate || 0);
}, 0);
if (!window.L) {
captionEl.textContent = "Map library unavailable; see the hotspot list below.";
} else {
var map = window.L.map(mapEl, { scrollWheelZoom: false, attributionControl: true });
window.L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
maxZoom: 19,
attribution:
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}).addTo(map);
var bounds = window.L.latLngBounds([]);
features.forEach(function (f) {
var p = f.properties;
var sig = !!p.getis_ord_significant;
var latlngs = featureLatLngs(f.geometry);
var line = window.L.polyline(latlngs, {
color: sig ? "#8a1c1c" : "#0b4f9c",
weight: widthFor(p.rate || 0, maxRate, sig ? 3 : 2, 11),
opacity: 0.9,
dashArray: sig ? "6 4" : null,
lineCap: "round",
}).addTo(map);
var ci =
p.rate_ci_low != null && p.rate_ci_high != null
? " (95% CI " + p.rate_ci_low + "–" + p.rate_ci_high + ")"
: "";
var tooltipContent = document.createElement("span");
tooltipContent.textContent =
(p.name || p.segment_id) +
(sig ? " — ★ significant hotspot" : "") +
"\nrate " +
(p.rate == null ? "n/a" : p.rate) +
ci +
", n=" +
p.n;
line.bindTooltip(tooltipContent, { sticky: true });
latlngs.forEach(function (ll) {
bounds.extend(ll);
});
});
if (bounds.isValid()) map.fitBounds(bounds, { padding: [14, 14] });
}
// Text equivalent: the significant hotspots, in words.
var hotspots = features
.filter(function (f) {
return f.properties.getis_ord_significant;
})
.sort(function (a, b) {
return (b.properties.rate || 0) - (a.properties.rate || 0);
});
listEl.textContent = "";
if (hotspots.length) {
hotspots.forEach(function (f) {
var p = f.properties;
var li = document.createElement("li");
li.textContent =
"★ " +
(p.name || p.segment_id) +
" — rate " +
(p.rate == null ? "n/a" : p.rate) +
" (95% CI " +
p.rate_ci_low +
"–" +
p.rate_ci_high +
", n=" +
p.n +
")";
listEl.appendChild(li);
});
captionEl.textContent =
hotspots.length +
" statistically significant hotspot(s) — hotter than exposure and chance explain.";
} else {
var li = document.createElement("li");
li.textContent = "No statistically significant hotspots in this dataset.";
listEl.appendChild(li);
captionEl.textContent =
"No statistically significant hotspots: no street is hotter than exposure and chance explain.";
}
}
fetch(DATA_URL)
.then(function (r) {
if (!r.ok) throw new Error("HTTP " + r.status);
return r.json();
})
.then(render)
.catch(function () {
captionEl.textContent = "Could not load the hotspot dataset.";
});
})();