-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.js
More file actions
449 lines (343 loc) · 11.5 KB
/
map.js
File metadata and controls
449 lines (343 loc) · 11.5 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
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
var canvas, context;
var _dragging = false;
var _startingCoord = {};
function onCanvasClick(event) {
var coord = getMouseCoordFromEvent(event);
var canvasCoord = { x: coord.x + (canvas.width / 3),
y: coord.y + (canvas.height /3 ) };
var dontDraw = false;
if (event.type == "dblclick") {
zoomInAtPx(canvasCoord);
}
else if (event.type == "mousedown") {
// This handles the right click case.
if (event.which == 3 || event.button == 2) {
zoomOutAtPx(canvasCoord);
}
else {
_dragging = true;
_startingCoord = coord;
dontDraw = true;
}
}
else if (event.type == "mouseup") {
if (_dragging) {
_dragging = false;
panMap({ x: coord.x - _startingCoord.x,
y: coord.y - _startingCoord.y });
centerCanvas();
}
}
else if (event.type == "mousemove") {
if (_dragging) {
var diffX = coord.x - _startingCoord.x;
var diffY = coord.y - _startingCoord.y;
diffX += canvas.width / -3;
diffY += canvas.height / -3;
translateCanvas(diffX, diffY);
}
dontDraw = true;
}
else {
dump(event.type);
}
if (!dontDraw)
testRender();
}
function translateCanvas(translateX, translateY) {
var translateStr = "translate(" + translateX + "px, " + translateY + "px)";
canvas.style.webkitTransform = translateStr;
canvas.style.MozTransform = translateStr;
}
function centerCanvas() {
var translateX = canvas.width / -3;
var translateY = canvas.height / -3;
translateCanvas(translateX, translateY);
}
function zoomInAtPx(coord) {
zoomAtPx(coord, true);
}
function zoomOutAtPx(coord) {
zoomAtPx(coord, false);
}
function zoomAtPx(coord, zoomIn) {
var oldScaleX = mapScaleX;
var oldScaleY = mapScaleY;
var translateRatio = zoomIn ? (1/2) : -1;
var scaleRatio = zoomIn ? (1/2) : 2;
var bounds = getBounds();
bounds.x += (coord.x * translateRatio);
bounds.y += (coord.y * translateRatio);
bounds.width *= Math.abs(scaleRatio);
bounds.height *= Math.abs(scaleRatio);
setBounds(bounds);
}
function panMap(coord) {
var bounds = getBounds();
bounds.x -= coord.x;
bounds.y -= coord.y;
setBounds(bounds);
// XXX: Disabled grabbing new data while the server is down.
//getDataForBounds(window.bounds);
}
function onLoad() {
canvas = document.getElementById("main");
context = canvas.getContext("2d");
canvas.addEventListener("mousedown", onCanvasClick);
canvas.addEventListener("mouseup", onCanvasClick);
canvas.addEventListener("mousemove", onCanvasClick);
canvas.addEventListener("dblclick", onCanvasClick);
// Try and prevent a context menu from showing up when the user right clicks
// on the canvas.
canvas.addEventListener("contextmenu", function(e) { e.preventDefault(); });
centerCanvas();
var request = new XMLHttpRequest();
request.open("GET", "map.osm", true);
request.overrideMimeType("text/xml");
request.onreadystatechange = function(evt) {
if ((request.readyState == 4) && (request.status == 200)) {
var start = +new Date();
parseXML(request.responseXML);
var end = +new Date();
dump("Took ", (end - start), "ms to load data.");
testRender();
// We handle this data specifically by moving the map to a "desirable"
// viewing position.
panMap({ x: 200, y: 400 });
testRender();
}
};
request.onprogress = function(evt) {
if (evt.lengthComputable) {
drawProgress(evt.loaded / evt.total);
}
};
request.send(null);
}
function getDataForBounds(bounds) {
var str = JSON.stringify({ bbox: bounds });
var request = new XMLHttpRequest();
request.open("POST", "bbox", true);
request.setRequestHeader("Content-type", "application/json");
request.setRequestHeader("Content-length", str.length);
request.onreadystatechange = function(evt) {
if ((request.readyState == 4) && (request.status == 200)) {
var start = +new Date();
var data = JSON.parse(request.responseText);
var end = +new Date();
dump ("Took ", (end - start), "ms to load data from JSON.");
nodes = data.nodes;
ways = data.ways;
testRender();
}
};
request.send(str);
}
var _data;
// nodes: hash from id to point object. Each point is of the form:
// lat: float, lon: float
var nodes = {};
// ways: hash from id to path object. Each path is of the form:
// pts: array of ids, corresponding to nodes, highway: string from OSM data.
var ways = {};
var bounds = {};
// bounds: object of the form:
// minlat: float, maxlat: float, minlon: float, maxlon: float
function parseXML(osmData) {
_data = osmData;
var data = osmData.firstChild.childNodes;
for (var i = 0; i < data.length; i++) {
var node = data[i];
if (node.nodeType != Node.ELEMENT_NODE)
continue;
switch (node.tagName) {
case "node":
var id = node.getAttribute("id");
if (typeof(nodes[id]) != "undefined")
break;
nodes[id] = { lat: parseFloat(node.getAttribute("lat")),
lon: parseFloat(node.getAttribute("lon")) };
break;
case "bounds":
bounds = { minlat: parseFloat(node.getAttribute("minlat")),
minlon: parseFloat(node.getAttribute("minlon")),
maxlat: parseFloat(node.getAttribute("maxlat")),
maxlon: parseFloat(node.getAttribute("maxlon")) };
break;
case "way":
var id = node.getAttribute("id");
if (typeof(ways[id]) != "undefined")
break;
ways[id] = extractWay(node);
break;
}
}
}
function extractWay(node) {
var toRet = {};
var pts = [];
for (var i = 0; i < node.childNodes.length; i++) {
var elem = node.childNodes[i];
if (elem.nodeType != Node.ELEMENT_NODE)
continue;
if (elem.tagName == "nd") {
pts.push(elem.getAttribute("ref"));
}
else if (elem.tagName == "tag") {
var key = elem.getAttribute("k");
if (key == "highway")
toRet.highway = elem.getAttribute("v");
}
}
toRet.pts = pts;
return toRet;
}
function dump(txt) {
var console = document.getElementById("console");
console.textContent += (arguments.length > 1 ? Array.prototype.join.call(arguments, "") : txt) + "\n";
}
// zoomFactor: Factor used when converting points to pixels and vice-versa.
// It is rooted in Google Maps' zoom factor.
var zoomFactor = 4;
function calculateZoomFactor() {
// In order to calculate the zoom factor, we scale from where the bounds
// would be mapped to with a zoom factor of 1.
var minPx = ptToPx({ lon: bounds.minlon, lat: bounds.minlat }, true);
var maxPx = ptToPx({ lon: bounds.maxlon, lat: bounds.maxlon }, true);
var xDiff = Math.abs(maxPx.x - minPx.x);
var yDiff = Math.abs(maxPx.y - minPx.y);
var xScale = Math.floor(Math.log((canvas.width / xDiff) / 256) / Math.log(2));
var yScale = Math.floor(Math.log((canvas.height / yDiff) / 256) / Math.log(2));
// Usually we would take the minimum of |xScale| and |yScale| so that we show
// as much data as possible. Sadly our data is usually skewed towards having
// too much content, so we take the maximum.
zoomFactor = Math.max(xScale, yScale);
}
// ptToPx: converts a point object (lat, long) to a pixel object (x, y) using
// a Mercator projection. The second parameter, |dontScale|, specifies whether
// to apply any sort of scale. (It is usually true when we want to determine
// what scale to provide for a set of points.)
function ptToPx(pt, dontScale) {
var R = dontScale ? 1 : Math.pow(2, zoomFactor) * 256;
var longOffset = 0;
var latRad = pt.lat * Math.PI / 180;
var lonRad = pt.lon * Math.PI / 180;
var x = (lonRad - longOffset) * R;
// Because we draw the points on a canvas, we negate the y position to allow
// drawing where the |y| coordinate grows as you move down.
var y = -1 * Math.log((Math.sin(latRad) + 1) / Math.cos(latRad)) * R;
return { x: x, y: y };
}
// pxToPt: converts a pixel object to a point object using a Mercator
// projection. This should be the inverse of |ptToPx|.
function pxToPt(px) {
var R = Math.pow(2, zoomFactor) * 256;
var longOffset = 0;
// Just like in |ptToPx|, we must invert the y position to account for drawing
// on a canvas.
var y = -1 * px.y;
var latRad = 2 * Math.atan(Math.exp(y / R)) - (Math.PI / 2);
var lonRad = px.x / R + longOffset;
var lat = latRad * 180 / Math.PI;
var lon = lonRad * 180 / Math.PI;
return { lat: lat, lon: lon };
}
function drawWay(way) {
context.beginPath();
var pts = way.pts.map(function(x) { return nodes[x]; });
// Sanity check: make sure that we have data on all the points in the path.
for (var i = 0; i < pts.length; i++) {
if (typeof(pts[i]) == "undefined") {
dump("Missing data on ", way.pts[i]);
return;
}
}
var firstPx = ptToPx(pts[0]);
context.moveTo(firstPx.x, firstPx.y);
for (var i = 1; i < pts.length; i++) {
var px = ptToPx(pts[i]);
context.lineTo(px.x, px.y);
}
context.stroke();
}
function getBounds() {
var minBound = ptToPx({ lat: bounds.minlat, lon: bounds.minlon });
var maxBound = ptToPx({ lat: bounds.maxlat, lon: bounds.maxlon });
var x = Math.min(minBound.x, maxBound.x);
var y = Math.min(minBound.y, maxBound.y);
var width = Math.abs(minBound.x - maxBound.x);
var height = Math.abs(minBound.y - maxBound.y);
return { x: x, y: y, width: width, height: height };
}
function setBounds(aBounds) {
var p1 = pxToPt({ x: aBounds.x, y: aBounds.y });
var p2 = pxToPt({ x: aBounds.x + aBounds.width,
y: aBounds.y + aBounds.height });
var minLat = Math.min(p1.lat, p2.lat);
var minLon = Math.min(p1.lon, p2.lon);
var maxLat = Math.max(p1.lat, p2.lat);
var maxLon = Math.max(p1.lon, p2.lon);
bounds.minlat = minLat;
bounds.minlon = minLon;
bounds.maxlat = maxLat;
bounds.maxlon = maxLon;
}
var mapScaleX = 1;
var mapScaleY = 1;
var mapTranslateX;
var mapTranslateY;
function setupRenderBounds() {
calculateZoomFactor();
var bounds = getBounds();
mapTranslateX = -1 * bounds.x;
mapTranslateY = -1 * bounds.y;
}
function drawProgress(percent) {
var percentTxt = Math.floor(percent * 100);
context.clearRect(0, 0, canvas.width, canvas.height);
context.font = "16px sans-serif";
context.fillText(percentTxt + "% loaded..", canvas.width / 2 - 50, canvas.height / 2);
}
function testRender() {
setupRenderBounds();
var start = +new Date();
var bounds = getBounds();
context.clearRect(0, 0, canvas.width, canvas.height);
context.save();
context.scale(mapScaleX, mapScaleY);
context.translate(mapTranslateX, mapTranslateY);
var maxWays = 100000;
var waysSlice = [];
var i = 0;
for (var x in ways) {
if (typeof(ways[x].highway) == "undefined")
continue;
i++;
if (i >= maxWays)
break;
waysSlice.push(ways[x]);
}
for (var i = 0; i < waysSlice.length; i++) {
drawWay(waysSlice[i]);
}
context.restore();
var end = +new Date();
dump("Took ", (end - start), "ms to render map.");
}
function getMouseCoordFromEvent(event) {
var x = 0;
var y = 0;
// We purposely calculate the |event.offsetX/Y|-like values because they do
// not take CSS transforms into account, which are useful if the transform
// is changed during the mouse event. (e.g. if the transform is changed on
// mousedown.)
var element = event.target;
do {
x += element.offsetLeft;
y += element.offsetTop;
} while (element = element.offsetParent);
x = (window.pageXOffset + event.clientX) - x;
y = (window.pageYOffset + event.clientY) - y;
return { x: x, y: y };
}
window.onload = onLoad;