forked from Picturedigits/housing-code-cases-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
155 lines (120 loc) · 5.09 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Housing Code Cases Heatmap</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tachyons/4.11.1/tachyons.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.heat/0.2.0/leaflet-heat.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/papaparse@5.3.0/papaparse.min.js"></script>
</head>
<body class="helvetica">
<div id="map" class="vh-100 vw-100 bg-near-white"></div>
<div id="legend" class="bg-white fixed top-0 left-0 pa2 mt2 ml2 br1 o-90" style="z-index: 999; width: 380px">
<h1 class="f3 mt0">Housing Code Cases Heatmap</h1>
<p class="measure mb1">
Zoom in for details on Housing Code Cases reported by Hartford CT from Jan 2020 to today. <a href="https://github.com/HandsOnDataViz/housing-code-cases-map">Learn more about up-to-date data and code.</a>
</p>
</div>
<script>
var map = L.map('map', {
zoomControl: false, // Add zoom control separately below
center: [41.76, -72.685], // Initial map center
zoom: 14, // Initial zoom level
attributionControl: false, // Instead of default attribution, we add custom at the bottom of script
scrollWheelZoom: true
});
let heatLayer = null;
let pointsLayer = null;
let noise = function(x) {
return x + (Math.random() * 0.0001) - 0.00005;
}
Papa.parse('hartford-parcels/hartford-parcels.csv', {
download: true,
header: true,
complete: function(rows) {
let parcel2coords = {}
for (var i = 0; i < rows.data.length; i++) {
let r = rows.data[i]
parcel2coords[ parseInt(r.parcel_id)] = {
lng: parseFloat(r.lng),
lat: parseFloat(r.lat)
}
}
$.getJSON(
'https://data.hartford.gov/resource/86ax-cfey.json?'
+ '$select=case_number,parcel_id,location,complaintviolation,reported'
+ '&$limit=99999999&$where=reported>="2020-01-01"',
function(cases) {
// Add lat/lng to cases where possible
let casesGeocoded = cases.map(function(x) {
let pid = parseInt(x.parcel_id);
if (parcel2coords[pid]) {
x.lat = noise(parseFloat(parcel2coords[pid].lat));
x.lng = noise(parseFloat(parcel2coords[pid].lng));
return x;
}
return null;
}).filter(function(x) { return x !== null; })
// Create a heatmap layer
let heatCoords = casesGeocoded.map(function(x) {
return [ x.lat, x.lng]
});
heatLayer = L.heatLayer(heatCoords, { radius: 20 }).addTo(map);
// Create a point layer
let points = casesGeocoded.map(function(x) {
return L.circleMarker([x.lat, x.lng], {
radius: 5,
stroke: false,
fillOpacity: 0.5,
fillColor: 'blue'
}).bindTooltip(
[x.location, x.case_number, x.complaintviolation, x.reported.split('T')[0]].join('<br>')
)
})
pointLayer = L.layerGroup(points);
}
)
}
});
// Toggle between heat & point layers
map.on('zoomend', function() {
if (map.getZoom() > 16) {
map.removeLayer(heatLayer);
if (!map.hasLayer(pointLayer)) {
pointLayer.addTo(map)
}
}
else {
map.removeLayer(pointLayer);
if (!map.hasLayer(heatLayer)) {
heatLayer.addTo(map)
}
}
})
// Add zoom in/out buttons to the top-right
L.control.zoom({position: 'topright'}).addTo(map)
// Add baselayer
L.tileLayer('https://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}{r}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>',
subdomains: 'abcd',
maxZoom: 19
}).addTo(map)
// Add geographical labels only layer on top of baselayer
var labels = L.tileLayer('https://{s}.basemaps.cartocdn.com/light_only_labels/{z}/{x}/{y}{r}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>',
subdomains: 'abcd',
maxZoom: 19,
pane: 'shadowPane' // always display on top
}).addTo(map)
// Add custom attribution
L.control.attribution({
prefix: '<a href="https://github.com/HandsOnDataViz/housing-code-cases-map">View data and code</a> \
by <a href="https://handsondataviz.org" target="_blank">HandsOnDataViz</a>'
}).addTo(map)
</script>
</body>
</html>