-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparticlemap.js
491 lines (395 loc) · 14.4 KB
/
particlemap.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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
var ParticleMap = function(geojson, options) {
if (geojson === undefined) {
throw 'ParticleMap needs a geojson file!';
}
this.geojson = geojson;
this.options = options || {};
if (!this.options.pixelResolution) this.options.pixelResolution = 10;
if (!this.options.drawOptions) {
this.options.drawOptions = {
arcSize: this.options.pixelResolution / 4,
color: '#ffffff',
opacity: 1
};
}
if (!this.options.foregroundColor) {
this.options.foregroundColor = '#333333';
}
if (!this.options.backgroundColor) {
this.options.backgroundColor = '#eeeeee';
}
var canvas = this.canvas = this.options.canvas;
if (canvas && !this.options.width) {
this.options.width = canvas.width;
}
if (canvas && !this.options.height) {
this.options.height = canvas.height;
}
if (canvas) {
this.setCanvasRatio(canvas);
}
this.grid = [];
// Magic numbers to do geojson -> screen coords mapping
this._transformX = null;
this._transformXOffset = null;
this._transformY = null;
this._transformYOffset = null;
this._polygons = [];
if (this.options.autostart !== false) {
var data = this.parse(geojson);
this.drawMap(data);
}
};
ParticleMap.prototype = {
pixelStatusEnum: {
NOTVISITED: 0,
INSIDE: 1,
OUTSIDE: 2,
EDGE: 3
},
_flatten: function(a, b) {
return a.concat(b);
},
/*
* Utility function to merge options from a and b.
* Items in b take precedence. Does a shallow copy.
* @param {object} a
* @param {object} b
*/
_mergeOptions: function(a, b) {
var tmpObject = {};
var attrname;
for (attrname in a) {tmpObject[attrname] = a[attrname];}
for (attrname in b) {tmpObject[attrname] = b[attrname];}
return tmpObject;
},
_recursiveFindPolygons: function(object) {
if (object.type == 'Polygon') {
return this._polygons.push(object);
}
else if (object.type == 'MultiPolygon') {
// how the hell is the geojson spec so fucking shitty.
object.coordinates.forEach(function(polygonArray) {
var tmpPolygon = {};
tmpPolygon.coordinates = polygonArray;
this._polygons.push(tmpPolygon);
}, this);
return;
}
else if (object.type == 'GeometryCollection') {
return object.geometries.forEach(this._recursiveFindPolygons, this);
}
else if (object.type == 'Feature') {
return this._recursiveFindPolygons(object.geometry);
}
else if (object.type == 'FeatureCollection') {
return object.features.forEach(this._recursiveFindPolygons, this);
}
else {
console.warn('ParticleMap.js: Could not find valid type in geojson object', object);
}
},
/**
* Either returns or finds all the polygon objects in this._geojson
*/
recursiveFindPolygons: function() {
if (this._polygons && this._polygons.length > 0) {
return this._polygons;
}
this._recursiveFindPolygons(this._geojson);
return this._polygons;
},
/*
* Returns the outermost ring of coordinates from a polygon
* @param {object} polygon GeoJSON Polygon object
*/
getCoordsFromPolygon: function(polygon) {
var outerRing;
if (polygon.coordinates) {
outerRing = polygon.coordinates[0];
} else {
throw 'Cannot find coordinates in polygon ' + polygon;
}
return outerRing;
},
/**
* Gets all polygons and finds all their coordinates
*/
recursiveFindPolygonCoords: function() {
var polygons = this.recursiveFindPolygons();
var coords = polygons.map(this.getCoordsFromPolygon);
coords = coords.reduce(this._flatten);
return coords;
},
/**
* Takes a geojson object and determines max and min points
* Effectively the entry method for ParticleMap.js
* @param {object} geojson GeoJSON object
*/
parse: function(geojson) {
this._geojson = geojson;
// Find max and min points in geojson file
var minx;
var miny;
var maxx;
var maxy;
var getMaxMin = function(coord) {
var t = {};
t.x = coord[0];
t.y = coord[1];
if (maxx === undefined) {
maxx = t.x;
minx = t.x;
maxy = t.y;
miny = t.y;
}
if (t.x > maxx) {
maxx = t.x;
}
if (t.x < minx) {
minx = t.x;
}
if (t.y > maxy) {
maxy = t.y;
}
if (t.y < miny) {
miny = t.y;
}
};
// Determine all the coordinates in file
var coords = this.recursiveFindPolygonCoords(geojson);
coords.forEach(getMaxMin, this);
var data = {
max: {
x: maxx,
y: maxy
},
min: {
x: minx,
y: miny
},
orig_data: coords
};
this._data = data;
return data;
},
/*
* Creates the canvas and determines offsets, calls into makeGrid and drawGrid
* @param {object} data Data object from parse that has min and max values in.
*/
drawMap: function(data) {
// Create the canvas
var canvas;
if (this.canvas) {
canvas = this.canvas;
} else if (this.options.canvas) {
canvas = this.options.canvas;
this.canvas = canvas;
} /* else {
canvas = document.createElement('canvas');
var width = document.body.scrollWidth;
width = width * 0.8;
var height = document.body.scrollHeight;
height = height * 0.8;
canvas.height = height;
canvas.width = width;
document.body.appendChild(canvas);
this.canvas = canvas;
} */
this.determineOffsets(data);
var context = canvas.getContext('2d');
context.clearRect(0, 0, this.options.width, this.options.height);
this.makeGrid();
this.drawGrid();
},
/* Creates the offsets and sets them on the ParticleGrid object - used later one
* @param {object} data Data object from parse that has min and max values in.
*/
determineOffsets: function(data) {
// Determine offsets:
this._transformX = Math.abs(this.options.width / (data.max.x - data.min.x));
this._transformY = Math.abs(this.options.height / (data.max.y - data.min.y));
if (!this.options.stretch) {
var diff = Math.abs(this._transformY - this._transformX);
diff /= 2;
if (this._transformY > this._transformX) {
this._transformYOffset = (this.options.height / this._transformY) * diff;
this._transformY = this._transformX;
} else {
this._transformXOffset = (this.options.width / this._transformX) * diff;
this._transformX = this._transformY;
}
}
},
/* Converts from screen co-ordinates to map co-ordinates
* Screen co-ords relative to the canvas element
* @param {array} coord Co-ordinate array object
*/
getScreenCoordFromMapCoord: function(coord) {
var x = coord[0];
x = this._data.min.x - x;
x = x * this._transformX;
x = x * -1;
if (this._transformXOffset) {
x += this._transformXOffset;
}
var y = coord[1];
y = y - this._data.min.y;
y = y * this._transformY;
if (this._transformYOffset) {
y += this._transformYOffset;
}
return [x, y];
},
getMapCoordFromScreenCoord: function(coord) {
var x = coord[0];
if (this._transformXOffset) {
x -= this._transformXOffset;
}
x = x / this._transformX;
x = x + this._data.min.x;
var y = coord[1];
if (this._transformYOffset) {
y -= this._transformYOffset;
}
y = y / this._transformY;
y = this._data.max.y - y;
return [x, y];
},
/* Readies the grid for being drawn upon - creates and populates the grid array */
makeGrid: function() {
this.grid = [];
var xiters = Math.ceil(this.options.height / this.options.pixelResolution);
var yiters = Math.ceil(this.options.width / this.options.pixelResolution);
var rowLen = Math.ceil(this.options.width / this.options.pixelResolution);
for (var i = 0; i < xiters; i++) {
for (var j = 0; j < yiters; j++) {
var idx = (i * rowLen) + j;
this.grid[idx] = ParticleMap.prototype.pixelStatusEnum.NOTVISITED;
}
}
},
/* Determines what each point on the grid should be (inside, outside etc) */
determineGrid: function() {
var idx, point;
for (idx = 0; idx < this.grid.length; idx++) {
point = this.grid[idx];
if (point == ParticleMap.prototype.pixelStatusEnum.NOTVISITED) {
var pointStatus = this.isPointInPolygon(idx);
this.grid[idx] = pointStatus;
}
}
},
paintGrid: function() {
if (!this.canvas) {
throw 'ParticleMap.js: You attempted to paint the grid with no canvas element set!';
}
var idx, point;
for (idx = 0; idx < this.grid.length; idx++) {
point = this.grid[idx];
var coords = this.gridIndexToScreenCoord(idx);
this.drawPoint(coords, idx, point);
}
},
drawGrid: function() {
this.determineGrid();
this.paintGrid();
},
drawPoint: function(coords, idx, status) {
// javascript what. really. this is the best solution?!
var drawOptions = JSON.parse(JSON.stringify(this.options.drawOptions));
if (this.options.foregroundColor &&
status === ParticleMap.prototype.pixelStatusEnum.INSIDE) {
drawOptions.color = this.options.foregroundColor;
}
if (this.options.backgroundColor &&
status === ParticleMap.prototype.pixelStatusEnum.OUTSIDE) {
drawOptions.color = this.options.backgroundColor;
}
if (this.options.drawPointFunc) {
// If it returns true or false, don't draw, client will take care of it
// If it returns an object, assume that is the drawOptions object, still draw
// If it returns or null, still draw the object
var retVal = this.options.drawPointFunc(coords, idx, status, this.canvas);
if (retVal === true || retVal === false) {
return;
} else if (retVal && typeof retVal == 'object') {
drawOptions = this._mergeOptions(this.options.drawOptions, retVal);
}
}
this.drawCircle(coords, drawOptions, this.canvas);
},
drawCircle: function(coords, drawOptions, canvas) {
var ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.arc(coords[0], coords[1], drawOptions.arcSize, 0, 2 * Math.PI, false);
ctx.globalAlpha = drawOptions.opacity;
ctx.fillStyle = drawOptions.color;
ctx.fill();
ctx.closePath();
},
/*
posToGridIndex: function(coord) {
// FIXME
var rowLen = Math.ceil(this.canvas.width / this.options.pixelResolution);
var x = Math.floor(coord[0] / this.options.pixelResolution);
var y = Math.floor(coord[1] / this.options.pixelResolution);
return x + (y * rowLen);
},
*/
gridIndexToScreenCoord: function(idx) {
// returns the coordinates to draw the item on the *screen*
var xlen = Math.ceil(this.options.width / this.options.pixelResolution);
var x = idx % xlen;
var y = Math.floor(idx / xlen);
var xcoord = (x * this.options.pixelResolution) + (this.options.pixelResolution * 0.5);
var ycoord = (y * this.options.pixelResolution) + (this.options.pixelResolution * 0.5);
return [xcoord, ycoord];
},
gridIndexToMapCoord: function(idx) {
var coord = this.gridIndexToScreenCoord(idx);
var point = this.getMapCoordFromScreenCoord(coord);
return point;
},
isPointInPolygon: function(gridIndex) {
var coord = this.gridIndexToMapCoord(gridIndex);
var x = coord[0];
var y = coord[1];
var inPoint = false;
var polygons = this.recursiveFindPolygons();
for (var i = 0; i < polygons.length; i++) {
var polygon = this.getCoordsFromPolygon(polygons[i]);
for (var j = 0; j < polygon.length - 1; j++) {
var point1 = polygon[j];
var point2 = polygon[j + 1];
if (point1[1] < y && point2[1] >= y || point2[1] < y && point1[1] >= y) {
if (point1[0] + (y - point1[1]) / (point2[1] - point1[1]) * (point2[0] - point1[0]) < x) {
inPoint = !inPoint;
}
}
}
}
if (inPoint) return ParticleMap.prototype.pixelStatusEnum.INSIDE;
else return ParticleMap.prototype.pixelStatusEnum.OUTSIDE;
},
setCanvasRatio: function(canvas) {
var context = canvas.getContext('2d');
var devicePixelRatio = window.devicePixelRatio || 1;
var backingStoreRatio = context.webkitBackingStorePixelRatio ||
context.mozBackingStorePixelRatio ||
context.msBackingStorePixelRatio ||
context.oBackingStorePixelRatio ||
context.backingStorePixelRatio || 1;
var ratio = devicePixelRatio / backingStoreRatio;
if (devicePixelRatio !== backingStoreRatio) {
var oldWidth = canvas.width;
var oldHeight = canvas.height;
canvas.width = oldWidth * ratio;
canvas.height = oldHeight * ratio;
canvas.style.width = oldWidth + 'px';
canvas.style.height = oldHeight + 'px';
context.scale(ratio, ratio);
}
return canvas;
}
};