-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprojectmap.html
More file actions
270 lines (240 loc) · 11 KB
/
projectmap.html
File metadata and controls
270 lines (240 loc) · 11 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CommunityScale projects</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v2.11.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.11.0/mapbox-gl.js"></script>
<script src="https://unpkg.com/papaparse@5.4.1/papaparse.min.js"></script>
<link href="https://fonts.googleapis.com/css2?family=Roboto&family=Roboto+Condensed:wght@700&display=swap" rel="stylesheet">
<style>
html, body, #map { height: 100%; margin: 0; }
.legend { position:absolute; right:10px; bottom:30px; background:#fff; padding:10px; font:12px/20px Roboto,sans-serif; border-radius:6px; box-shadow:0 1px 2px rgba(0,0,0,.1) }
.legend div span { display:inline-block; width:10px; height:10px; border-radius:50%; margin-right:6px; }
.mapboxgl-popup-close-button { display: none; }
.reset-btn { position:absolute; top:10px; right:10px; background:#fff; padding:8px 12px; font:12px Roboto,sans-serif; border:none; border-radius:4px; box-shadow:0 1px 2px rgba(0,0,0,.1); cursor:pointer; }
.reset-btn:hover { background:#f5f5f5; }
.zoom-controls { position:absolute; top:50px; right:10px; display:flex; flex-direction:column; gap:4px; }
.zoom-btn { background:#fff; width:32px; height:32px; font:18px Roboto,sans-serif; border:none; border-radius:4px; box-shadow:0 1px 2px rgba(0,0,0,.1); cursor:pointer; display:flex; align-items:center; justify-content:center; }
.zoom-btn:hover { background:#f5f5f5; }
</style>
</head>
<body>
<div id="map"></div>
<button class="reset-btn" onclick="resetView()">Reset map</button>
<div class="zoom-controls">
<button class="zoom-btn" onclick="map.zoomIn()">+</button>
<button class="zoom-btn" onclick="map.zoomOut()">−</button>
</div>
<div class="legend" id="state-legend">
<div><span style="background-color:#1b2f5a"></span>Housing Plan</div>
<div><span style="background-color:#82AC21"></span>Zoning</div>
<div><span style="background-color:#A34997"></span>Other project type/service</div>
</div>
<script>
// 1) ACCESS TOKEN + MAP INIT (this was missing)
mapboxgl.accessToken = 'pk.eyJ1Ijoic2FyYWJyZW50IiwiYSI6ImNtMHpsb2Q4NTAwemoybHExbnB6eHVvZ2kifQ.nH0hUSv3IGzX6MkYB_cSmA';
const initialCenter = [-86, 39];
const initialZoom = 3.4;
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v11',
center: initialCenter, zoom: initialZoom, projection: 'mercator'
});
// Reset view function
window.resetView = function() {
// Close any open popups
const popups = document.getElementsByClassName('mapboxgl-popup');
if (popups.length) {
for (let i = 0; i < popups.length; i++) {
popups[i].remove();
}
}
map.flyTo({
center: initialCenter,
zoom: initialZoom,
essential: true
});
};
// 2) classification helper
function classifyService(serviceRaw) {
const s = (serviceRaw || '').toLowerCase();
if (s.includes('zoning')) return 'Zoning';
if (s.includes('housing')) return 'Housing Plan';
return 'Other project type/service';
}
// 3) CSV -> GeoJSON (skip blanks/#REF!, add category)
function csvToGeoJSON(csv) {
const parsed = Papa.parse(csv, { header: true, skipEmptyLines: true });
const features = [];
for (const row of parsed.data) {
const latRaw = (row.lat || '').toString().trim();
const lonRaw = (row.long || '').toString().trim();
if (!latRaw || !lonRaw) continue;
if (latRaw.toUpperCase().includes('REF') || lonRaw.toUpperCase().includes('REF')) continue;
const lat = Number(latRaw), lon = Number(lonRaw);
if (!Number.isFinite(lat) || !Number.isFinite(lon)) continue;
features.push({
type: 'Feature',
geometry: { type: 'Point', coordinates: [lon, lat] },
properties: { ...row, category: classifyService(row.service) }
});
}
return { type: 'FeatureCollection', features };
}
map.on('load', async () => {
map.setFog({});
try {
// 4) fetch from live Google Sheets CSV
const resp = await fetch('https://docs.google.com/spreadsheets/d/e/2PACX-1vSn5ksUiVITRYgWt-E3maCu2jwE94QZSxG_80oWM4pCRVx51K-Y6B649BpnAQ5axaFu91Pw5j35wWi7/pub?gid=2091918674&single=true&output=csv');
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
const csvText = await resp.text();
const geojson = csvToGeoJSON(csvText);
console.log('Loaded features:', geojson.features.length);
// 5) add source + layer using the live data
map.addSource('projects', { type: 'geojson', data: geojson });
map.addLayer({
id: 'points',
type: 'circle',
source: 'projects',
paint: {
'circle-radius': [
'interpolate',
['linear'],
['zoom'],
4, 5,
8, 6,
12, 9
],
'circle-stroke-width': 1,
'circle-stroke-color': 'white',
'circle-opacity': 0.9,
// use normalized category for exact color mapping
'circle-color': [
'case',
// If Service contains both 'housing' and 'zoning', use housing plan color
['all',
['in', 'housing', ['downcase', ['coalesce', ['get', 'Service'], '']]],
['in', 'zoning', ['downcase', ['coalesce', ['get', 'Service'], '']]]
],
'#1b2f5a',
// If Service contains 'housing', use housing plan color
['in', 'housing', ['downcase', ['coalesce', ['get', 'Service'], '']]],
'#1b2f5a',
// If Service contains 'zoning', use zoning color
['in', 'zoning', ['downcase', ['coalesce', ['get', 'Service'], '']]],
'#82AC21',
// Default: other project type/service
'#A34997'
]
}
});
// 6) add labels layer
map.addLayer({
id: 'labels',
type: 'symbol',
source: 'projects',
minzoom: 6,
maxzoom: 12,
layout: {
'text-field': ['get', 'NameLocation'],
'text-font': ['Roboto Condensed', 'Arial Unicode MS Regular'],
'text-size': 12,
'text-offset': [0, 1],
'text-anchor': 'top'
},
paint: {
'text-color': '#000000',
'text-halo-color': '#ffffff',
'text-halo-width': 1
}
});
// 7) popups with multi-feature handling
map.on('click', 'points', (e) => {
const allFeatures = map.queryRenderedFeatures(e.point, { layers: ['points'] });
// Deduplicate features based on Project name (in case same feature is rendered multiple times)
const seen = new Set();
const deduped = allFeatures.filter(f => {
const key = f.properties.Project;
if (seen.has(key)) return false;
seen.add(key);
return true;
});
// Get the CentroidPlace of the first clicked feature
const clickedPlace = deduped[0]?.properties.CentroidPlace;
// Filter to only include features from the same CentroidPlace
const features = deduped.filter(f => f.properties.CentroidPlace === clickedPlace);
// Sort features alphabetically by Project name
features.sort((a, b) => {
const nameA = (a.properties.Project || '').toLowerCase();
const nameB = (b.properties.Project || '').toLowerCase();
return nameA.localeCompare(nameB);
});
// If multiple features at same location, show list
if (features.length > 1) {
let html = '<div style="font-family:Roboto,sans-serif;font-size:12px;line-height:1.4">';
features.forEach((feature, i) => {
const p = feature.properties;
let description = '';
if (p['Resume blurb']) {
const parts = p['Resume blurb'].split(': ');
description = parts.length > 1 ? parts.slice(1).join(': ').trim() : '';
}
const blogLink = p['CS blog link'] && p['CS blog link'].toLowerCase() !== 'not_applicable' ? p['CS blog link'] : '';
html += `<div style="padding:8px 0;border-top:1px solid #eee">
<div style="font-family:'Roboto Condensed',sans-serif;font-weight:700;font-size:14px;margin-bottom:4px">${p.Project || 'Untitled'}</div>
${p.Service ? `<div style="margin-top:4px"><strong>Service:</strong> ${p.Service}</div>` : ''}
${description ? `<div style="margin-top:4px"><strong>Description:</strong> ${description}</div>` : ''}
${blogLink ? `<div style="margin-top:6px"><a href="${blogLink}" target="_blank" style="color:#1b2f5a;text-decoration:none">Learn more →</a></div>` : ''}
</div>`;
});
html += '</div>';
new mapboxgl.Popup().setLngLat(e.lngLat).setHTML(html).addTo(map);
} else {
// Single feature - show full popup
const p = features[0].properties;
let description = '';
if (p['Resume blurb']) {
const parts = p['Resume blurb'].split(': ');
description = parts.length > 1 ? parts.slice(1).join(': ').trim() : '';
}
const blogLink = p['CS blog link'] && p['CS blog link'].toLowerCase() !== 'not_applicable' ? p['CS blog link'] : '';
const html = `
<div style="font-family:Roboto,sans-serif;font-size:12px;line-height:1.4">
<div style="font-family:'Roboto Condensed',sans-serif;font-weight:700;font-size:14px;margin-bottom:4px">${p.Project || 'Untitled'}</div>
${p.Service ? `<div style="margin-top:4px"><strong>Service:</strong> ${p.Service}</div>` : ''}
${description ? `<div style="margin-top:4px"><strong>Description:</strong> ${description}</div>` : ''}
${blogLink ? `<div style="margin-top:6px"><a href="${blogLink}" target="_blank" style="color:#1b2f5a;text-decoration:none">Learn more →</a></div>` : ''}
</div>`;
new mapboxgl.Popup().setLngLat(e.lngLat).setHTML(html).addTo(map);
}
});
// Helper function for showing single popup from list
window.showSinglePopup = function(index, features, lngLat) {
const p = features[index];
let description = '';
if (p['Resume blurb']) {
const parts = p['Resume blurb'].split(': ');
description = parts.length > 1 ? parts.slice(1).join(': ').trim() : '';
}
const blogLink = p['CS blog link'] && p['CS blog link'].toLowerCase() !== 'not_applicable' ? p['CS blog link'] : '';
const html = `
<div style="font-family:Roboto,sans-serif;font-size:12px;line-height:1.4">
<div style="font-family:'Roboto Condensed',sans-serif;font-weight:700;font-size:14px;margin-bottom:4px">${p.Project || 'Untitled'}</div>
${p.Service ? `<div style="margin-top:4px"><strong>Service:</strong> ${p.Service}</div>` : ''}
${description ? `<div style="margin-top:4px"><strong>Description:</strong> ${description}</div>` : ''}
${blogLink ? `<div style="margin-top:6px"><a href="${blogLink}" target="_blank" style="color:#1b2f5a;text-decoration:none">Learn more →</a></div>` : ''}
<div style="margin-top:8px;padding-top:6px;border-top:1px solid #eee;font-size:11px;color:#666;cursor:pointer" onclick="history.back()">← Back to list</div>
</div>`;
new mapboxgl.Popup().setLngLat(lngLat).setHTML(html).addTo(map);
};
map.on('mouseenter', 'points', () => map.getCanvas().style.cursor = 'pointer');
map.on('mouseleave', 'points', () => map.getCanvas().style.cursor = '');
} catch (e) {
console.error('CSV load error:', e);
}
});
</script>
</body>
</html>