-
Notifications
You must be signed in to change notification settings - Fork 64
/
geostats.js
executable file
·1435 lines (1071 loc) · 37.1 KB
/
geostats.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* geostats() is a tiny and standalone javascript library for classification
* Project page - https://github.com/simogeo/geostats
* Copyright (c) 2011 Simon Georget, http://www.intermezzo-coop.eu
* Licensed under the MIT license
*/
(function (definition) {
// This file will function properly as a <script> tag, or a module
// using CommonJS and NodeJS or RequireJS module formats.
// CommonJS
if (typeof exports === "object") {
module.exports = definition();
// RequireJS
} else if (typeof define === "function" && define.amd) {
define(definition);
// <script>
} else {
geostats = definition();
}
})(function () {
var isInt = function(n) {
return typeof n === 'number' && parseFloat(n) == parseInt(n, 10) && !isNaN(n);
} // 6 characters
var _t = function(str) {
return str;
};
//taking from http://stackoverflow.com/questions/18082/validate-decimal-numbers-in-javascript-isnumeric
var isNumber = function(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
//indexOf polyfill
// from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement, fromIndex) {
if ( this === undefined || this === null ) {
throw new TypeError( '"this" is null or not defined' );
}
var length = this.length >>> 0; // Hack to convert object.length to a UInt32
fromIndex = +fromIndex || 0;
if (Math.abs(fromIndex) === Infinity) {
fromIndex = 0;
}
if (fromIndex < 0) {
fromIndex += length;
if (fromIndex < 0) {
fromIndex = 0;
}
}
for (;fromIndex < length; fromIndex++) {
if (this[fromIndex] === searchElement) {
return fromIndex;
}
}
return -1;
};
}
var geostats = function(a) {
this.objectID = '';
this.separator = ' - ';
this.legendSeparator = this.separator;
this.method = '';
this.precision = 0;
this.precisionflag = 'auto';
this.roundlength = 2; // Number of decimals, round values
this.is_uniqueValues = false;
this.debug = false;
this.silent = false;
this.bounds = Array();
this.ranges = Array();
this.inner_ranges = null;
this.colors = Array();
this.counter = Array();
// statistics information
this.stat_sorted = null;
this.stat_mean = null;
this.stat_median = null;
this.stat_sum = null;
this.stat_max = null;
this.stat_min = null;
this.stat_pop = null;
this.stat_variance = null;
this.stat_stddev = null;
this.stat_cov = null;
/**
* logging method
*/
this.log = function(msg, force) {
if(this.debug == true || force != null)
console.log(this.objectID + "(object id) :: " + msg);
};
/**
* Set bounds
*/
this.setBounds = function(a) {
this.log('Setting bounds (' + a.length + ') : ' + a.join());
this.bounds = Array() // init empty array to prevent bug when calling classification after another with less items (sample getQuantile(6) and getQuantile(4))
this.bounds = a;
//this.bounds = this.decimalFormat(a);
};
/**
* Set a new serie
*/
this.setSerie = function(a) {
this.log('Setting serie (' + a.length + ') : ' + a.join());
this.serie = Array() // init empty array to prevent bug when calling classification after another with less items (sample getQuantile(6) and getQuantile(4))
this.serie = a;
//reset statistics after changing serie
this.resetStatistics();
this.setPrecision();
};
/**
* Set colors
*/
this.setColors = function(colors) {
this.log('Setting color ramp (' + colors.length + ') : ' + colors.join());
this.colors = colors;
};
/**
* Get feature count
* With bounds array(0, 0.75, 1.5, 2.25, 3);
* should populate this.counter with 5 keys
* and increment counters for each key
*/
this.doCount = function() {
if (this._nodata())
return;
var tmp = this.sorted();
this.counter = new Array();
// we init counter with 0 value
if(this.is_uniqueValues == true) {
for (var i = 0; i < this.bounds.length; i++) {
this.counter[i]= 0;
}
} else {
for (var i = 0; i < this.bounds.length -1; i++) {
this.counter[i]= 0;
}
}
for (var j=0; j < tmp.length; j++) {
// get current class for value to increment the counter
var cclass = this.getClass(tmp[j]);
this.counter[cclass]++;
}
};
/**
* Set decimal precision according to user input
* or automatcally determined according
* to the given serie.
*/
this.setPrecision = function(decimals) {
// only when called from user
if(typeof decimals !== "undefined") {
this.precisionflag = 'manual';
this.precision = decimals;
}
// we calculate the maximal decimal length on given serie
if(this.precisionflag == 'auto') {
for (var i = 0; i < this.serie.length; i++) {
// check if the given value is a number and a float
if (!isNaN((this.serie[i]+"")) && (this.serie[i]+"").toString().indexOf('.') != -1) {
var precision = (this.serie[i] + "").split(".")[1].length;
} else {
var precision = 0;
}
if(precision > this.precision) {
this.precision = precision;
}
}
}
if(this.precision > 20) {
// prevent "Uncaught RangeError: toFixed() digits argument must be between 0 and 20" bug. See https://github.com/simogeo/geostats/issues/34
this.log('this.precision value (' + this.precision + ') is greater than max value. Automatic set-up to 20 to prevent "Uncaught RangeError: toFixed()" when calling decimalFormat() method.');
this.precision = 20;
}
this.log('Calling setPrecision(). Mode : ' + this.precisionflag + ' - Decimals : '+ this.precision);
this.serie = this.decimalFormat(this.serie);
};
/**
* Format array numbers regarding to precision
*/
this.decimalFormat = function(a) {
var b = new Array();
for (var i = 0; i < a.length; i++) {
// check if the given value is a number
if (isNumber(a[i])) {
b[i] = parseFloat(parseFloat(a[i]).toFixed(this.precision));
} else {
b[i] = a[i];
}
}
return b;
}
/**
* Transform a bounds array to a range array the following array : array(0,
* 0.75, 1.5, 2.25, 3); becomes : array('0-0.75', '0.75-1.5', '1.5-2.25',
* '2.25-3');
*/
this.setRanges = function() {
this.ranges = Array(); // init empty array to prevent bug when calling classification after another with less items (sample getQuantile(6) and getQuantile(4))
for (var i = 0; i < (this.bounds.length - 1); i++) {
this.ranges[i] = this.bounds[i] + this.separator + this.bounds[i + 1];
}
};
/** return min value */
this.min = function(exclude = []) {
if (this._nodata())
return;
if(!exclude.includes(this.serie[0])) this.stat_min = this.serie[0];
else this.stat_min = 999999999999;
for (var i = 0; i < this.pop(); i++) {
if (this.serie[i] < this.stat_min && !exclude.includes(this.serie[i])) {
this.stat_min = this.serie[i];
}
}
return this.stat_min;
};
/** return max value */
this.max = function(exclude = []) {
if (this._nodata())
return;
if(!exclude.includes(this.serie[0])) this.stat_max = this.serie[0];
else this.stat_max = -999999999999;
for (var i = 0; i < this.pop(); i++) {
if (this.serie[i] > this.stat_max && !exclude.includes(this.serie[i])) {
this.stat_max = this.serie[i];
}
}
return this.stat_max;
};
/** return sum value */
this.sum = function() {
if (this._nodata())
return;
if (this.stat_sum == null) {
this.stat_sum = 0;
for (var i = 0; i < this.pop(); i++) {
this.stat_sum += parseFloat(this.serie[i]);
}
}
return this.stat_sum;
};
/** return population number */
this.pop = function() {
if (this._nodata())
return;
if (this.stat_pop == null) {
this.stat_pop = this.serie.length;
}
return this.stat_pop;
};
/** return mean value */
this.mean = function() {
if (this._nodata())
return;
if (this.stat_mean == null) {
this.stat_mean = parseFloat(this.sum() / this.pop());
}
return this.stat_mean;
};
/** return median value */
this.median = function() {
if (this._nodata())
return;
if (this.stat_median == null) {
this.stat_median = 0;
var tmp = this.sorted();
// serie pop is odd
if (tmp.length % 2) {
this.stat_median = parseFloat(tmp[(Math.ceil(tmp.length / 2) - 1)]);
// serie pop is even
} else {
this.stat_median = ( parseFloat(tmp[((tmp.length / 2) - 1)]) + parseFloat(tmp[(tmp.length / 2)]) ) / 2;
}
}
return this.stat_median;
};
/** return variance value */
this.variance = function() {
var round = (typeof round === "undefined") ? true : false;
if (this._nodata())
return;
if (this.stat_variance == null) {
var tmp = 0, serie_mean = this.mean();
for (var i = 0; i < this.pop(); i++) {
tmp += Math.pow( (this.serie[i] - serie_mean), 2 );
}
this.stat_variance = tmp / this.pop();
if(round == true) {
this.stat_variance = Math.round(this.stat_variance * Math.pow(10,this.roundlength) )/ Math.pow(10,this.roundlength);
}
}
return this.stat_variance;
};
/** return standard deviation value */
this.stddev = function(round) {
var round = (typeof round === "undefined") ? true : false;
if (this._nodata())
return;
if (this.stat_stddev == null) {
this.stat_stddev = Math.sqrt(this.variance());
if(round == true) {
this.stat_stddev = Math.round(this.stat_stddev * Math.pow(10,this.roundlength) )/ Math.pow(10,this.roundlength);
}
}
return this.stat_stddev;
};
/** coefficient of variation - measure of dispersion */
this.cov = function(round) {
var round = (typeof round === "undefined") ? true : false;
if (this._nodata())
return;
if (this.stat_cov == null) {
this.stat_cov = this.stddev() / this.mean();
if(round == true) {
this.stat_cov = Math.round(this.stat_cov * Math.pow(10,this.roundlength) )/ Math.pow(10,this.roundlength);
}
}
return this.stat_cov;
};
/** reset all attributes after setting a new serie */
this.resetStatistics = function() {
this.stat_sorted = null;
this.stat_mean = null;
this.stat_median = null;
this.stat_sum = null;
this.stat_max = null;
this.stat_min = null;
this.stat_pop = null;
this.stat_variance = null;
this.stat_stddev = null;
this.stat_cov = null;
}
/** data test */
this._nodata = function() {
if (this.serie.length == 0) {
if(this.silent) this.log("[silent mode] Error. You should first enter a serie!", true);
else throw new TypeError("Error. You should first enter a serie!");
return 1;
} else
return 0;
};
/** data test */
this._classificationCheck = function(nbClass) {
if(nbClass >= this.pop()) {
var errnum ='Error. ' + nbClass + ' classes are defined for only ' + this.pop() + ' values in serie ! For the current serie, no more than ' + (this.pop() - 1 ) + ' classes can be defined.';
if(this.silent) this.log(errnum, true);
else {
alert(errnum);
throw new TypeError(errnum);
}
}
};
/** ensure nbClass is an integer */
this._nbClassInt = function(nbClass) {
var nbclassTmp = parseInt(nbClass, 10);
if (isNaN(nbclassTmp)) {
if(this.silent) this.log("[silent mode] '" + nbclassTmp + "' is not a valid integer. Enable to set class number.", true);
else throw new TypeError("'" + nbclassTmp + "' is not a valid integer. Enable to set class number.");
} else {
return nbclassTmp;
}
};
/** check if the serie contains negative value */
this._hasNegativeValue = function() {
for (var i = 0; i < this.serie.length; i++) {
if(this.serie[i] < 0)
return true;
}
return false;
};
/** check if the serie contains zero value */
this._hasZeroValue = function() {
for (var i = 0; i < this.serie.length; i++) {
if(parseFloat(this.serie[i]) === 0)
return true;
}
return false;
};
/** return sorted values (as array) */
this.sorted = function() {
if (this.stat_sorted == null) {
if(this.is_uniqueValues == false) {
this.stat_sorted = this.serie.sort(function(a, b) {
return a - b;
});
} else {
this.stat_sorted = this.serie.sort(function(a,b){
var nameA=a.toString().toLowerCase(), nameB=b.toString().toLowerCase();
if(nameA < nameB) return -1;
if(nameA > nameB) return 1;
return 0;
})
}
}
return this.stat_sorted;
};
/** return all info */
this.info = function() {
if (this._nodata())
return;
var content = '';
content += _t('Population') + ' : ' + this.pop() + ' - [' + _t('Min')
+ ' : ' + this.min() + ' | ' + _t('Max') + ' : ' + this.max()
+ ']' + "\n";
content += _t('Mean') + ' : ' + this.mean() + ' - ' + _t('Median') + ' : ' + this.median() + "\n";
content += _t('Variance') + ' : ' + this.variance() + ' - ' + _t('Standard deviation') + ' : ' + this.stddev()
+ ' - ' + _t('Coefficient of variation') + ' : ' + this.cov() + "\n";
return content;
};
/**
* Set Manual classification Return an array with bounds : ie array(0,
* 0.75, 1.5, 2.25, 3);
* Set ranges and prepare data for displaying legend
*
*/
this.setClassManually = function(array) {
if (this._nodata())
return;
if(array[0] !== this.min() || array[array.length-1] !== this.max()) {
if(this.silent) this.log("[silent mode] " + t('Given bounds may not be correct! please check your input.\nMin value : ' + this.min() + ' / Max value : ' + this.max()), true);
else throw new TypeError(_t('Given bounds may not be correct! please check your input.\nMin value : ' + this.min() + ' / Max value : ' + this.max()));
return;
}
this.setBounds(array);
this.setRanges();
// we specify the classification method
this.method = _t('manual classification') + ' (' + (array.length -1) + ' ' + _t('classes') + ')';
return this.bounds;
};
/**
* Equal intervals classification Return an array with bounds : ie array(0,
* 0.75, 1.5, 2.25, 3);
*/
this.getClassEqInterval = function(nbClass, forceMin, forceMax) {
var nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer
if (this._nodata())
return;
var tmpMin = (typeof forceMin === "undefined") ? this.min() : forceMin;
var tmpMax = (typeof forceMax === "undefined") ? this.max() : forceMax;
var a = Array();
var val = tmpMin;
var interval = (tmpMax - tmpMin) / nbClass;
for (var i = 0; i <= nbClass; i++) {
a[i] = val;
val += interval;
}
//-> Fix last bound to Max of values
a[nbClass] = tmpMax;
this.setBounds(a);
this.setRanges();
// we specify the classification method
this.method = _t('eq. intervals') + ' (' + nbClass + ' ' + _t('classes') + ')';
return this.bounds;
};
this.getQuantiles = function(nbClass) {
var nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer
var tmp = this.sorted();
var quantiles = [];
var step = this.pop() / nbClass;
for (var i = 1; i < nbClass; i++) {
var qidx = Math.round(i*step+0.49);
quantiles.push(tmp[qidx-1]); // zero-based
}
return quantiles;
};
/**
* Quantile classification Return an array with bounds : ie array(0, 0.75,
* 1.5, 2.25, 3);
*/
this.getClassQuantile = function(nbClass) {
var nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer
if (this._nodata())
return;
this._classificationCheck(nbClass); // be sure number of classes is valid
var tmp = this.sorted();
var bounds = this.getQuantiles(nbClass);
bounds.unshift(tmp[0]);
if (bounds[tmp.length - 1] !== tmp[tmp.length - 1])
bounds.push(tmp[tmp.length - 1]);
this.setBounds(bounds);
this.setRanges();
// we specify the classification method
this.method = _t('quantile') + ' (' + nbClass + ' ' + _t('classes') + ')';
return this.bounds;
};
/**
* Standard Deviation classification
* Return an array with bounds : ie array(0,
* 0.75, 1.5, 2.25, 3);
*/
this.getClassStdDeviation = function(nbClass, matchBounds) {
var nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer
if (this._nodata())
return;
var tmpMax = this.max();
var tmpMin = this.min();
var tmpStdDev = this.stddev();
var tmpMean = this.mean();
var a = Array();
// number of classes is odd
if(nbClass % 2 == 1) {
// Euclidean division to get the inferior bound
var infBound = Math.floor(nbClass / 2);
var supBound = infBound + 1;
// we set the central bounds
a[infBound] = tmpMean - (tmpStdDev / 2);
a[supBound] = tmpMean + (tmpStdDev / 2);
// Values < to infBound, except first one
for (var i = infBound - 1; i > 0; i--) {
var val = a[i+1] - tmpStdDev;
a[i] = val;
}
// Values > to supBound, except last one
for (var i = supBound + 1; i < nbClass; i++) {
var val = a[i-1] + tmpStdDev;
a[i] = val;
}
// number of classes is even
} else {
var meanBound = nbClass / 2;
// we get the mean value
a[meanBound] = tmpMean;
// Values < to the mean, except first one
for (var i = meanBound - 1; i > 0; i--) {
var val = a[i+1] - tmpStdDev;
a[i] = val;
}
// Values > to the mean, except last one
for (var i = meanBound + 1; i < nbClass; i++) {
var val = a[i-1] + tmpStdDev;
a[i] = val;
}
}
// we finally set the first value
// do we excatly match min value or not ?
a[0] = (typeof matchBounds === "undefined") ? a[1]- tmpStdDev : tmpMin;
// we finally set the last value
// do we excatly match max value or not ?
a[nbClass] = (typeof matchBounds === "undefined") ? a[nbClass-1] + tmpStdDev : tmpMax;
this.setBounds(a);
this.setRanges();
// we specify the classification method
this.method = _t('std deviation') + ' (' + nbClass + ' ' + _t('classes')+ ')';
return this.bounds;
};
/**
* Geometric Progression classification
* http://en.wikipedia.org/wiki/Geometric_progression
* Return an array with bounds : ie array(0,
* 0.75, 1.5, 2.25, 3);
*/
this.getClassGeometricProgression = function(nbClass) {
var nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer
if (this._nodata())
return;
if(this._hasNegativeValue() || this._hasZeroValue()) {
if(this.silent) this.log("[silent mode] " + _t('geometric progression can\'t be applied with a serie containing negative or zero values.'), true);
else throw new TypeError(_t('geometric progression can\'t be applied with a serie containing negative or zero values.'));
return;
}
var a = Array();
var tmpMin = this.min();
var tmpMax = this.max();
var logMax = Math.log(tmpMax) / Math.LN10; // max decimal logarithm (or base 10)
var logMin = Math.log(tmpMin) / Math.LN10;; // min decimal logarithm (or base 10)
var interval = (logMax - logMin) / nbClass;
// we compute log bounds
for (var i = 0; i < nbClass; i++) {
if(i == 0) {
a[i] = logMin;
} else {
a[i] = a[i-1] + interval;
}
}
// we compute antilog
a = a.map(function(x) { return Math.pow(10, x); });
// and we finally add max value
a.push(this.max());
this.setBounds(a);
this.setRanges();
// we specify the classification method
this.method = _t('geometric progression') + ' (' + nbClass + ' ' + _t('classes') + ')';
return this.bounds;
};
/**
* Arithmetic Progression classification
* http://en.wikipedia.org/wiki/Arithmetic_progression
* Return an array with bounds : ie array(0,
* 0.75, 1.5, 2.25, 3);
*/
this.getClassArithmeticProgression = function(nbClass) {
var nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer
if (this._nodata())
return;
var denominator = 0;
// we compute the (french) "Raison"
for (var i = 1; i <= nbClass; i++) {
denominator += i;
}
var a = Array();
var tmpMin = this.min();
var tmpMax = this.max();
var interval = (tmpMax - tmpMin) / denominator;
for (var i = 0; i <= nbClass; i++) {
if(i == 0) {
a[i] = tmpMin;
} else if(i == nbClass) {
a[i] = tmpMax;
} else {
a[i] = a[i-1] + (i * interval);
}
}
this.setBounds(a);
this.setRanges();
// we specify the classification method
this.method = _t('arithmetic progression') + ' (' + nbClass + ' ' + _t('classes') + ')';
return this.bounds;
};
/**
* Credits : Doug Curl (javascript) and Daniel J Lewis (python implementation)
* http://www.arcgis.com/home/item.html?id=0b633ff2f40d412995b8be377211c47b
* http://danieljlewis.org/2010/06/07/jenks-natural-breaks-algorithm-in-python/
*/
this.getClassJenks2 = function(nbClass) {
var nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer
if (this._nodata())
return;
this._classificationCheck(nbClass); // be sure number of classes is valid
var dataList = this.sorted();
// now iterate through the datalist:
// determine mat1 and mat2
// really not sure how these 2 different arrays are set - the code for
// each seems the same!
// but the effect are 2 different arrays: mat1 and mat2
var mat1 = []
for ( var x = 0, xl = dataList.length + 1; x < xl; x++) {
var temp = []
for ( var j = 0, jl = nbClass + 1; j < jl; j++) {
temp.push(0)
}
mat1.push(temp)
}
var mat2 = []
for ( var i = 0, il = dataList.length + 1; i < il; i++) {
var temp2 = []
for ( var c = 0, cl = nbClass + 1; c < cl; c++) {
temp2.push(0)
}
mat2.push(temp2)
}
// absolutely no idea what this does - best I can tell, it sets the 1st
// group in the
// mat1 and mat2 arrays to 1 and 0 respectively
for ( var y = 1, yl = nbClass + 1; y < yl; y++) {
mat1[0][y] = 1
mat2[0][y] = 0
for ( var t = 1, tl = dataList.length + 1; t < tl; t++) {
mat2[t][y] = Infinity
}
var v = 0.0
}
// and this part - I'm a little clueless on - but it works
// pretty sure it iterates across the entire dataset and compares each
// value to
// one another to and adjust the indices until you meet the rules:
// minimum deviation
// within a class and maximum separation between classes
for ( var l = 2, ll = dataList.length + 1; l < ll; l++) {
var s1 = 0.0
var s2 = 0.0
var w = 0.0
for ( var m = 1, ml = l + 1; m < ml; m++) {
var i3 = l - m + 1
var val = parseFloat(dataList[i3 - 1])
s2 += val * val
s1 += val
w += 1
v = s2 - (s1 * s1) / w
var i4 = i3 - 1
if (i4 != 0) {
for ( var p = 2, pl = nbClass + 1; p < pl; p++) {
if (mat2[l][p] >= (v + mat2[i4][p - 1])) {
mat1[l][p] = i3
mat2[l][p] = v + mat2[i4][p - 1]
}
}
}
}
mat1[l][1] = 1
mat2[l][1] = v
}
var k = dataList.length
var kclass = []
// fill the kclass (classification) array with zeros:
for (var i = 0; i <= nbClass; i++) {
kclass.push(0);
}
// this is the last number in the array:
kclass[nbClass] = parseFloat(dataList[dataList.length - 1])
// this is the first number - can set to zero, but want to set to lowest
// to use for legend:
kclass[0] = parseFloat(dataList[0])
var countNum = nbClass
while (countNum >= 2) {
var id = parseInt((mat1[k][countNum]) - 2)
kclass[countNum - 1] = dataList[id]
k = parseInt((mat1[k][countNum] - 1))
// spits out the rank and value of the break values:
// console.log("id="+id,"rank = " + String(mat1[k][countNum]),"val =
// " + String(dataList[id]))
// count down:
countNum -= 1
}
// check to see if the 0 and 1 in the array are the same - if so, set 0
// to 0:
if (kclass[0] == kclass[1]) {
kclass[0] = 0
}
this.setBounds(kclass);
this.setRanges();
this.method = _t('Jenks2') + ' (' + nbClass + ' ' + _t('classes') + ')';
return this.bounds; //array of breaks
}
/**
* Credits from simple-statistics library
* https://github.com/simple-statistics/simple-statistics
* https://gist.githubusercontent.com/tmcw/4969184/raw/cfd9572d00db6bcdc34f07b088738fc3a47846b4/simple_statistics.js
*/
this.getClassJenks = function(nbClass) {
var nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer
if (this._nodata())
return;
this._classificationCheck(nbClass); // be sure number of classes is valid
var dataList = this.sorted();
// Compute the matrices required for Jenks breaks. These matrices
// can be used for any classing of data with `classes <= n_classes`
var jenksMatrices = function(data, n_classes) {
// in the original implementation, these matrices are referred to
// as `LC` and `OP`
//