-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheatmap.js
596 lines (462 loc) · 14.3 KB
/
heatmap.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
// **********************************************************************************
// Author: Sunit Raut github.com/SunitRaut
// **********************************************************************************
// License
// **********************************************************************************
// This program is free software; you can redistribute it
// and/or modify it under the terms of the GNU General
// Public License as published by the Free Software
// Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public
// License for more details.
//
// Licence can be viewed at
// http://www.gnu.org/licenses/gpl-3.0.txt
//
// Please maintain this license information along with authorship
// and copyright notices in any redistribution of this code
// **********************************************************************************
class HeatMap {
/*
Parameters:
sensing_ellipse_param
sensing_ellipse_radius
sensing_circle_radius
*/
constructor()
{
//this.locations = locations;
//this.data = data;
//Setting default values for Parameters
this.sensing_circle_radius = 10;
this.sensing_ellipse_radius = 22;
this.sensing_ellipse_param = 0.8;
this.wind_speed=0;
this.wind_dir=0;
this.colorMax=[255,0,0]; //red
this.colorMin=[0,255,0]; //green
this.colorCase=[1,-1,0];
this.resolution = 50;
this.spread = 0.8;
//can be changed later using class methods
}
setMaxMin(cMax,cMin)
{
this.colorMax=cMax;
this.colorMin=cMin;
this.calculateColorCase();
}
calculateColorCase()
{
for(var i=0;i<3;i++)
{
if(this.colorMax[i]==this.colorMin[i])
{this.colorCase[i]=0;}
else if(this.colorMax[i]>this.colorMin[i])
{this.colorCase[i]=1;}
else
{this.colorCase[i]=-1;}
}//end of for
}//end of calculate ColorCase()
postSomething()
{
console.log("Posted Something");
//console.log(this.locations);
//console.log(this.data);
//document.write("Hi");
}
//set values from 0 to 100
setResolution(res)
{
this.resolution = res;
}
defineRegion(locations)
{
this.locations = locations;
this.pxPerLength = 40000;
var LatList=[];
var LngList=[];
for(var a=0;a<((this.locations).length);a++)
{
LatList[a] = Number(this.locations[a][0]);
LngList[a] = Number(this.locations[a][1]);
}//end of for
console.log("Hello");
//console.log(LatList);
//console.log(LngList);
//determine region parameters
var avg=0;var nCount=0;
for(var a=0;a<((this.locations).length-1);a++)
{
for(var b=a+1;b<((this.locations).length);b++)
{
var f1= Math.abs(LatList[a]-LatList[b]);
var f2= Math.abs(LngList[a]-LngList[b]);
var f = f1*f1+f2*f2;
var df = Math.sqrt(f);
//document.getElementById("demo").innerHTML+="<br>"+df;
avg+=df;
nCount++;
}//end of for b
}//end of for a
avg = avg / nCount;
console.log(avg);
this.avgdis = avg;
this.sensing_circle_radius = this.resolution/2;
this.sensing_ellipse_radius = this.spread*this.resolution;
this.sensing_ellipse_param = 0.8;
//document.getElementById("demo").innerHTML+=LatList+"<br>"+LngList;
this.vNorth = max(LatList)+2*this.avgdis;
this.vSouth = min(LatList)-2*this.avgdis;
this.vEast = max(LngList)+2*this.avgdis;
this.vWest = min(LngList)-2*this.avgdis;
this.LatPx = [];
this.LngPx = [];
for(var a=0;a<((this.locations).length);a++)
{
this.LatPx[a]=Math.ceil((this.vNorth-LatList[a])*(this.resolution/this.avgdis));
this.LngPx[a]=Math.floor((LngList[a]-this.vWest)*(this.resolution/this.avgdis));
}//end of for
//document.getElementById("demo").innerHTML+="<br>"+vNorth+"<br>"+vSouth+"<br>"+vEast+"<br>"+vWest;
this.vHeight=Math.ceil((this.vNorth-this.vSouth)*(this.resolution/this.avgdis)); this.vWidth=Math.ceil((this.vEast-this.vWest)*(this.resolution/this.avgdis));
//document.getElementById("demo").innerHTML+="<br>"+vHeight+"px<br>"+vWidth+"px<br>";
//document.getElementById("demo").innerHTML+="<br>"+LatPx+"<br>"+LngPx;
var dfCount=0;
var ellipseSet1 = [];
var ellipseSet2 = [];
var dfLength = [];
for(var a=0;a<((this.locations).length-1);a++)
{
for(var b=a+1;b<((this.locations).length);b++)
{
var f1= Math.abs(this.LatPx[a]-this.LatPx[b]);
var f2= Math.abs(this.LngPx[a]-this.LngPx[b]);
var f = f1*f1+f2*f2;
var df = Math.sqrt(f);
console.log(2*this.resolution+","+df);
//document.getElementById("demo").innerHTML+="<br>"+df;
if(df<=(2*this.resolution)) {ellipseSet1[dfCount]=a;ellipseSet2[dfCount]=b;dfLength[dfCount]=df;dfCount++;}
}//end of for b
}//end of for a
//document.getElementById("demo").innerHTML+="<br>"+dfLength+"<br>"+ellipseSet1+"<br>"+ellipseSet2;
var imageBounds = {
north : this.vNorth,
south : this.vSouth,
east : this.vEast,
west : this.vWest
};
/*
north: 19.083550,
south: 19.080016,
east: 72.835064,
west: 72.821016
north: 19.087174,
south: 19.084099,
east: 72.835255,
west: 72.832180
*/
//vWidth=10;
this.iMap = [];
for(let i = 0; i < this.vWidth; i++) {
this.iMap[i] = [];
for(let j = 0; j < this.vHeight; j++) {
this.iMap[i][j] = 0;
}
}
//document.getElementById("demo").innerHTML+="<br>"+ellipseSet1.length;
//p[0] = new PNGlib(vWidth, vHeight, 512); // construcor takes height, weight and color-depth
//var background = p[0].color(0, 0, 0, 0); // set the background transparent
for (var i = 0; i < this.vWidth; i++) {
for (var j = 0; j < this.vHeight; j++) {
//p.buffer[p.index(i , j)] = p.color(255, 255, 255);
this.iMap[i][j]=0;
var dCount=0;
for(var a=0;a<(dfLength).length;a++)
{
var ellipseMid1 = (this.LatPx[ellipseSet1[a]] + this.LatPx[ellipseSet2[a]])/2 ;
var ellipseMid2 = (this.LngPx[ellipseSet1[a]] + this.LngPx[ellipseSet2[a]])/2 ;
var e1= Math.abs(this.LatPx[ellipseSet1[a]]-j);
var e2= Math.abs(this.LngPx[ellipseSet1[a]]-i);
var e3= Math.abs(this.LatPx[ellipseSet2[a]]-j);
var e4= Math.abs(this.LngPx[ellipseSet2[a]]-i);
var e5= Math.abs(ellipseMid1-j);
var e6= Math.abs(ellipseMid2-i);
var e8 = e1*e1+e2*e2;
var e9 = e3*e3+e4*e4;
var e10 = e5*e5+e6*e6;
var d1 = Math.sqrt(e8);
if(d1>2*this.resolution) continue;
var d2 = Math.sqrt(e9);
if(d2>2*this.resolution) continue;
var d3 = Math.sqrt(e10);
//if ((Math.abs(d1+d2-d3))<=(300/dfLength[a])) {dCount++;continue;}
//if ((Math.abs(d1+d2-d3))<=(50*(20/dfLength[a]))) {dCount++;continue;}
if ((Math.abs(d1+d2-(this.sensing_ellipse_param*d3)))<=((2-this.sensing_ellipse_param)*(this.sensing_ellipse_radius))) {dCount++;break;}
//if (Math.abs(d1+d2)<=(45)) {dCount++;break;}
//if ((d1+d2)<=55) dCount++;
//d = e1+e2;
//if(d<=30) dCount++;
}
for(var a=0;a<((this.locations).length);a++)
{
var e1= Math.abs(this.LatPx[a]-j);
var e2= Math.abs(this.LngPx[a]-i);
var e5 = e1*e1+e2*e2;
var d = Math.sqrt(e5);
if (d<=this.sensing_circle_radius) dCount++;
if (d<=1) this.iMap[i][j]=2;
}
if((dCount>0)&&(this.iMap[i][j]!=2))
{
//var opa=0;
//if(dCount>1)opa=255;
//else if(dCount==2)opa=255;
//else if(dCount==1)opa=155;
//p[0].buffer[p[0].index(i , j)] = p[0].color(255, 0, 0);
this.iMap[i][j]=1;
}//end of if
}
}
//imgOverLay = "data:image/png;base64,"+p[0].getBase64();
return this.iMap;
} //end of shadeRegion
createHeatMap(data)
{
this.data = data;
var weight=0;
//document.getElementById("demo").innerHTML+="<br>"+p.buffer[p.index(0 , 0)];
//document.getElementById("demo").innerHTML+="<br>"+aqi[1]+"<br>";
var computation=0;
this.wMap=[];
for(let i = 0; i < this.vWidth; i++) {
this.wMap[i] = [];
//oMap[i]=[];
for(let j = 0; j < this.vHeight; j++) {
this.wMap[i][j] = 0;
//oMap[i][j]=0;
}
}
//console.log(vWidth*vHeight);
for (var i = 0; i < this.vWidth; i++) {
for (var j = 0; j < this.vHeight; j++) {
//p[0].buffer[p[0].index(i , j)] = p[0].color(255, 255, 255);
var dCount=0;
var wei=[];var dis=[];var distances=[];var wCount=0;var mul=1;var sum=0; var dSum=0;
if(this.iMap[i][j]!=0)
{
//for(var a=0;a<(nodeMeta.length-1);a++)
for(var a=0;a<((this.locations).length);a++)
{
var ed1= Math.abs(this.LatPx[a]-j); //y axis
var ed2= Math.abs(this.LngPx[a]-i);//x axis
var ed8 = ed1*ed1+ed2*ed2;
var dd1 = Math.sqrt(ed8);//Distance between each node and sample point
var ddd1 = dd1;
if(this.wind_speed>0)
{
var x=i;
var y=-1*j;
var x1=this.LngPx[a];
var y1=-1*this.LatPx[a];
var slope=(y1-y)/(x1-x);
var angle = Math.atan(slope)*(180/3.14);
var theta=0;var heading=0;
if((x>x1)&&(y>y1)) theta=0+angle; //first quadrant
if((x<x1)&&(y>y1)) theta=180+angle; //second quadrant
if((x<x1)&&(y<y1)) theta=180+angle; //third quadrabt
if((x>x1)&&(y<y1)) theta=360+angle; //fourth quadrant
heading = (450-theta)%360;
var diff=0;
if(heading-this.wind_dir<=180)
diff = Math.abs(heading - this.wind_dir);
else if (heading-this.wind_dir>=180)
diff = Math.abs(360-(heading-this.wind_dir))%180;
//var ddd1 = Math.pow(((diff)/180),1)*dd1;
ddd1 = (0.1)*(this.wind_speed)*Math.pow(((diff)/180),2)+dd1;
}
if((dd1)<(2*this.resolution)) {distances[wCount]=dd1;wei[wCount]=this.data[a];dis[wCount]=ddd1;wCount++;}
}//end of for a
sum=0;
for(var bi=0;bi<dis.length;bi++)
{
mul=wei[bi];
for(var bj=0;bj<dis.length;bj++)
{ if(bj!=bi) mul*=Math.pow(dis[bj],2);}
// *Math.pow(diffe[bj],2)
sum+=mul;
}
dSum=0;
for(var bi=0;bi<dis.length;bi++)
{
mul=1;
for(var bj=0;bj<dis.length;bj++)
{ if(bj!=bi) mul*=Math.pow(dis[bj],2);}
dSum+=mul;}
weight=0;
weight = sum/dSum;
this.wMap[i][j]=weight;
}//end of if
}//end of for j
}//end of for i
var filter=[[1,1,1],[1,1,1],[1,1,1]];
//var filter=[[0,0,0],[0,1,0],[0,0,0]];
for (var i = 0; i < this.vWidth-3; i++) {
for (var j = 0; j < this.vHeight-3; j++) {
if(this.iMap[i][j]!=0)
{
var sum=0;var fsum=0;
for(var a=0;a<9;a++)
{
var aa=Math.floor(a/3);var ab=a%3;
if(this.iMap[i+aa][j+ab]!=0)
{
sum+=this.wMap[i+aa][j+ab];
fsum+=1;
//filter[aa][ab]
}
}//end of for a
sum=sum/fsum;
var w=sum;
if (w>100) {w=100;}
//Decide colour coding here
this.wMap[i][j]=[];
this.wMap[i][j]['red'] = 0;
this.wMap[i][j]['green'] = 0;
this.wMap[i][j]['blue'] = 0;
var red,green,blue=0;
//red
if(this.colorCase[0]==1)
{
if(w<=50)
{
red = ((this.colorMax[0]-this.colorMin[0])*2*(w)/100) + this.colorMin[0];
}
else
{
red = this.colorMax[0];
}
}
else if(this.colorCase[0]==-1)
{
if(w<=50)
{
red = this.colorMin[0];
}
else
{
red = ((this.colorMin[0]-this.colorMax[0])*(100-w))/100 + this.colorMax[0];
}
}
else
{
red = this.colorMin[0];
}
//green
if(this.colorCase[1]==1)
{
if(w<=50)
{
green = ((this.colorMax[1]-this.colorMin[1])*2*(w)/100) + this.colorMin[1];
}
else
{
green = this.colorMax[1];
}
}
else if(this.colorCase[1]==-1)
{
if(w<=50)
{
green = this.colorMin[1];
}
else
{
green = ((this.colorMin[1]-this.colorMax[1])*(100-w))/100 + this.colorMax[1];
}
}
else
{
green = this.colorMin[1];
}
//blue
if(this.colorCase[2]==1)
{
if(w<=50)
{
blue = ((this.colorMax[2]-this.colorMin[2])*2*(w)/100) + this.colorMin[2];
}
else
{
blue = this.colorMax[2];
}
}
else if(this.colorCase[2]==-1)
{
if(w<=50)
{
blue = this.colorMin[2];
}
else
{
blue = ((this.colorMin[2]-this.colorMax[2])*(100-w))/100 + this.colorMax[2];
}
}
else
{
blue = this.colorMin[2];
}
//green
//green = ((this.colorMin[1]-this.colorMax[1])*(100-w))/100 + this.colorMax[1];
//if((this.green>255)) console.log("Here");
//blue
if(red>255) red = 255;
if(green>255) green = 255;
if(blue>255) blue = 255;
//if(red<0) red = 0;
//if(green<0) green = 0;
//if(blue<0) blue = 0;
//red=0;blue=0;
//Rounding off
red = Math.floor(red);
green = Math.floor(green);
blue = Math.floor(blue);
//3x Reduction in colour quantization
red = red - red%4;
green = green - green%4;
blue = blue - blue%4;
this.wMap[i][j]['red'] = red;
this.wMap[i][j]['green'] = green;
this.wMap[i][j]['blue'] = blue;
//opacity=255;
//w=Math.floor(oMap[i][j]);
//w=opacity;
//if((j>vWidth/2)&&(i>vHeight/2)) opacity=180;
//if(opacity>255)opacity=255;
//if(opacity<0) opacity=0;
//if(w>200)console.log("200");
//w=255;
//opacity=255;
//p[iBufferCount].buffer[p[iBufferCount].index(i , j)] = p[iBufferCount].color(p_red, p_green, p_blue,opacity);
}//end of if iMap!=0
}//end of i
}//end of j
return this.wMap;
//.............
}//end of colourCodeRegion
}//End of HeatMap Class
//Auxilliary Functions
function max(input) {
if (toString.call(input) !== "[object Array]")
return false;
return Math.max.apply(null, input);
}
function min(input) {
if (toString.call(input) !== "[object Array]")
return false;
return Math.min.apply(null, input);
}