blacklight_heatmaps 1.3.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2000 @@
1
+ import L$1 from 'leaflet';
2
+
3
+ function getDefaultExportFromCjs (x) {
4
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
5
+ }
6
+
7
+ var geostats$1 = {exports: {}};
8
+
9
+ /**
10
+ * geostats() is a tiny and standalone javascript library for classification
11
+ * Project page - https://github.com/simogeo/geostats
12
+ * Copyright (c) 2011 Simon Georget, http://www.intermezzo-coop.eu
13
+ * Licensed under the MIT license
14
+ */
15
+
16
+ var hasRequiredGeostats;
17
+
18
+ function requireGeostats () {
19
+ if (hasRequiredGeostats) return geostats$1.exports;
20
+ hasRequiredGeostats = 1;
21
+ (function (module, exports) {
22
+ (function (definition) {
23
+ // This file will function properly as a <script> tag, or a module
24
+ // using CommonJS and NodeJS or RequireJS module formats.
25
+
26
+ // CommonJS
27
+ {
28
+ module.exports = definition();
29
+
30
+ // RequireJS
31
+ }
32
+
33
+ })(function () {
34
+
35
+ var isInt = function(n) {
36
+ return typeof n === 'number' && parseFloat(n) == parseInt(n, 10) && !isNaN(n);
37
+ }; // 6 characters
38
+
39
+ var _t = function(str) {
40
+ return str;
41
+ };
42
+
43
+ //taking from http://stackoverflow.com/questions/18082/validate-decimal-numbers-in-javascript-isnumeric
44
+ var isNumber = function(n) {
45
+ return !isNaN(parseFloat(n)) && isFinite(n);
46
+ };
47
+
48
+
49
+
50
+ //indexOf polyfill
51
+ // from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
52
+ if (!Array.prototype.indexOf) {
53
+ Array.prototype.indexOf = function (searchElement, fromIndex) {
54
+ if ( this === undefined || this === null ) {
55
+ throw new TypeError( '"this" is null or not defined' );
56
+ }
57
+
58
+ var length = this.length >>> 0; // Hack to convert object.length to a UInt32
59
+
60
+ fromIndex = +fromIndex || 0;
61
+
62
+ if (Math.abs(fromIndex) === Infinity) {
63
+ fromIndex = 0;
64
+ }
65
+
66
+ if (fromIndex < 0) {
67
+ fromIndex += length;
68
+ if (fromIndex < 0) {
69
+ fromIndex = 0;
70
+ }
71
+ }
72
+
73
+ for (;fromIndex < length; fromIndex++) {
74
+ if (this[fromIndex] === searchElement) {
75
+ return fromIndex;
76
+ }
77
+ }
78
+
79
+ return -1;
80
+ };
81
+ }
82
+
83
+ var geostats = function(a) {
84
+
85
+ this.objectID = '';
86
+ this.separator = ' - ';
87
+ this.legendSeparator = this.separator;
88
+ this.method = '';
89
+ this.precision = 0;
90
+ this.precisionflag = 'auto';
91
+ this.roundlength = 2; // Number of decimals, round values
92
+ this.is_uniqueValues = false;
93
+ this.debug = false;
94
+ this.silent = false;
95
+
96
+ this.bounds = Array();
97
+ this.ranges = Array();
98
+ this.inner_ranges = null;
99
+ this.colors = Array();
100
+ this.counter = Array();
101
+
102
+ // statistics information
103
+ this.stat_sorted = null;
104
+ this.stat_mean = null;
105
+ this.stat_median = null;
106
+ this.stat_sum = null;
107
+ this.stat_max = null;
108
+ this.stat_min = null;
109
+ this.stat_pop = null;
110
+ this.stat_variance = null;
111
+ this.stat_stddev = null;
112
+ this.stat_cov = null;
113
+
114
+
115
+ /**
116
+ * logging method
117
+ */
118
+ this.log = function(msg, force) {
119
+
120
+ if(this.debug == true || force != null)
121
+ console.log(this.objectID + "(object id) :: " + msg);
122
+
123
+ };
124
+
125
+ /**
126
+ * Set bounds
127
+ */
128
+ this.setBounds = function(a) {
129
+
130
+ this.log('Setting bounds (' + a.length + ') : ' + a.join());
131
+
132
+ this.bounds = Array(); // init empty array to prevent bug when calling classification after another with less items (sample getQuantile(6) and getQuantile(4))
133
+
134
+ this.bounds = a;
135
+ //this.bounds = this.decimalFormat(a);
136
+
137
+ };
138
+
139
+ /**
140
+ * Set a new serie
141
+ */
142
+ this.setSerie = function(a) {
143
+
144
+ this.log('Setting serie (' + a.length + ') : ' + a.join());
145
+
146
+ this.serie = Array(); // init empty array to prevent bug when calling classification after another with less items (sample getQuantile(6) and getQuantile(4))
147
+ this.serie = a;
148
+
149
+ //reset statistics after changing serie
150
+ this.resetStatistics();
151
+
152
+ this.setPrecision();
153
+
154
+ };
155
+
156
+ /**
157
+ * Set colors
158
+ */
159
+ this.setColors = function(colors) {
160
+
161
+ this.log('Setting color ramp (' + colors.length + ') : ' + colors.join());
162
+
163
+ this.colors = colors;
164
+
165
+ };
166
+
167
+ /**
168
+ * Get feature count
169
+ * With bounds array(0, 0.75, 1.5, 2.25, 3);
170
+ * should populate this.counter with 5 keys
171
+ * and increment counters for each key
172
+ */
173
+ this.doCount = function() {
174
+
175
+ if (this._nodata())
176
+ return;
177
+
178
+
179
+ var tmp = this.sorted();
180
+
181
+ this.counter = new Array();
182
+
183
+ // we init counter with 0 value
184
+ if(this.is_uniqueValues == true) {
185
+ for (var i = 0; i < this.bounds.length; i++) {
186
+ this.counter[i]= 0;
187
+ }
188
+ } else {
189
+ for (var i = 0; i < this.bounds.length -1; i++) {
190
+ this.counter[i]= 0;
191
+ }
192
+ }
193
+
194
+ for (var j=0; j < tmp.length; j++) {
195
+
196
+ // get current class for value to increment the counter
197
+ var cclass = this.getClass(tmp[j]);
198
+ this.counter[cclass]++;
199
+
200
+ }
201
+
202
+ };
203
+
204
+ /**
205
+ * Set decimal precision according to user input
206
+ * or automatcally determined according
207
+ * to the given serie.
208
+ */
209
+ this.setPrecision = function(decimals) {
210
+
211
+ // only when called from user
212
+ if(typeof decimals !== "undefined") {
213
+ this.precisionflag = 'manual';
214
+ this.precision = decimals;
215
+ }
216
+
217
+ // we calculate the maximal decimal length on given serie
218
+ if(this.precisionflag == 'auto') {
219
+
220
+ for (var i = 0; i < this.serie.length; i++) {
221
+
222
+ // check if the given value is a number and a float
223
+ if (!isNaN((this.serie[i]+"")) && (this.serie[i]+"").toString().indexOf('.') != -1) {
224
+ var precision = (this.serie[i] + "").split(".")[1].length;
225
+ } else {
226
+ var precision = 0;
227
+ }
228
+
229
+ if(precision > this.precision) {
230
+ this.precision = precision;
231
+ }
232
+
233
+ }
234
+
235
+ }
236
+ if(this.precision > 20) {
237
+ // prevent "Uncaught RangeError: toFixed() digits argument must be between 0 and 20" bug. See https://github.com/simogeo/geostats/issues/34
238
+ 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.');
239
+ this.precision = 20;
240
+ }
241
+
242
+ this.log('Calling setPrecision(). Mode : ' + this.precisionflag + ' - Decimals : '+ this.precision);
243
+
244
+ this.serie = this.decimalFormat(this.serie);
245
+
246
+ };
247
+
248
+ /**
249
+ * Format array numbers regarding to precision
250
+ */
251
+ this.decimalFormat = function(a) {
252
+
253
+ var b = new Array();
254
+
255
+ for (var i = 0; i < a.length; i++) {
256
+ // check if the given value is a number
257
+ if (isNumber(a[i])) {
258
+ b[i] = parseFloat(parseFloat(a[i]).toFixed(this.precision));
259
+ } else {
260
+ b[i] = a[i];
261
+ }
262
+ }
263
+
264
+ return b;
265
+ };
266
+
267
+ /**
268
+ * Transform a bounds array to a range array the following array : array(0,
269
+ * 0.75, 1.5, 2.25, 3); becomes : array('0-0.75', '0.75-1.5', '1.5-2.25',
270
+ * '2.25-3');
271
+ */
272
+ this.setRanges = function() {
273
+
274
+ this.ranges = Array(); // init empty array to prevent bug when calling classification after another with less items (sample getQuantile(6) and getQuantile(4))
275
+
276
+ for (var i = 0; i < (this.bounds.length - 1); i++) {
277
+ this.ranges[i] = this.bounds[i] + this.separator + this.bounds[i + 1];
278
+ }
279
+ };
280
+
281
+ /** return min value */
282
+ this.min = function(exclude = []) {
283
+
284
+ if (this._nodata())
285
+ return;
286
+
287
+ if(!exclude.includes(this.serie[0])) this.stat_min = this.serie[0];
288
+ else this.stat_min = 999999999999;
289
+
290
+
291
+ for (var i = 0; i < this.pop(); i++) {
292
+ if (this.serie[i] < this.stat_min && !exclude.includes(this.serie[i])) {
293
+ this.stat_min = this.serie[i];
294
+ }
295
+ }
296
+
297
+ return this.stat_min;
298
+ };
299
+
300
+ /** return max value */
301
+ this.max = function(exclude = []) {
302
+
303
+ if (this._nodata())
304
+ return;
305
+
306
+ if(!exclude.includes(this.serie[0])) this.stat_max = this.serie[0];
307
+ else this.stat_max = -999999999999;
308
+
309
+ for (var i = 0; i < this.pop(); i++) {
310
+ if (this.serie[i] > this.stat_max && !exclude.includes(this.serie[i])) {
311
+ this.stat_max = this.serie[i];
312
+ }
313
+ }
314
+
315
+ return this.stat_max;
316
+ };
317
+
318
+ /** return sum value */
319
+ this.sum = function() {
320
+
321
+ if (this._nodata())
322
+ return;
323
+
324
+ if (this.stat_sum == null) {
325
+
326
+ this.stat_sum = 0;
327
+ for (var i = 0; i < this.pop(); i++) {
328
+ this.stat_sum += parseFloat(this.serie[i]);
329
+ }
330
+
331
+ }
332
+
333
+ return this.stat_sum;
334
+ };
335
+
336
+ /** return population number */
337
+ this.pop = function() {
338
+
339
+ if (this._nodata())
340
+ return;
341
+
342
+ if (this.stat_pop == null) {
343
+
344
+ this.stat_pop = this.serie.length;
345
+
346
+ }
347
+
348
+ return this.stat_pop;
349
+ };
350
+
351
+ /** return mean value */
352
+ this.mean = function() {
353
+
354
+ if (this._nodata())
355
+ return;
356
+
357
+ if (this.stat_mean == null) {
358
+
359
+ this.stat_mean = parseFloat(this.sum() / this.pop());
360
+
361
+ }
362
+
363
+ return this.stat_mean;
364
+ };
365
+
366
+ /** return median value */
367
+ this.median = function() {
368
+
369
+ if (this._nodata())
370
+ return;
371
+
372
+ if (this.stat_median == null) {
373
+
374
+ this.stat_median = 0;
375
+ var tmp = this.sorted();
376
+
377
+ // serie pop is odd
378
+ if (tmp.length % 2) {
379
+ this.stat_median = parseFloat(tmp[(Math.ceil(tmp.length / 2) - 1)]);
380
+
381
+ // serie pop is even
382
+ } else {
383
+ this.stat_median = ( parseFloat(tmp[((tmp.length / 2) - 1)]) + parseFloat(tmp[(tmp.length / 2)]) ) / 2;
384
+ }
385
+
386
+ }
387
+
388
+ return this.stat_median;
389
+ };
390
+
391
+ /** return variance value */
392
+ this.variance = function() {
393
+
394
+ var round = (typeof round === "undefined") ? true : false;
395
+
396
+ if (this._nodata())
397
+ return;
398
+
399
+ if (this.stat_variance == null) {
400
+
401
+ var tmp = 0, serie_mean = this.mean();
402
+ for (var i = 0; i < this.pop(); i++) {
403
+ tmp += Math.pow( (this.serie[i] - serie_mean), 2 );
404
+ }
405
+
406
+ this.stat_variance = tmp / this.pop();
407
+
408
+ if(round == true) {
409
+ this.stat_variance = Math.round(this.stat_variance * Math.pow(10,this.roundlength) )/ Math.pow(10,this.roundlength);
410
+ }
411
+
412
+ }
413
+
414
+ return this.stat_variance;
415
+ };
416
+
417
+ /** return standard deviation value */
418
+ this.stddev = function(round) {
419
+
420
+ var round = (typeof round === "undefined") ? true : false;
421
+
422
+ if (this._nodata())
423
+ return;
424
+
425
+ if (this.stat_stddev == null) {
426
+
427
+ this.stat_stddev = Math.sqrt(this.variance());
428
+
429
+ if(round == true) {
430
+ this.stat_stddev = Math.round(this.stat_stddev * Math.pow(10,this.roundlength) )/ Math.pow(10,this.roundlength);
431
+ }
432
+
433
+ }
434
+
435
+ return this.stat_stddev;
436
+ };
437
+
438
+ /** coefficient of variation - measure of dispersion */
439
+ this.cov = function(round) {
440
+
441
+ var round = (typeof round === "undefined") ? true : false;
442
+
443
+ if (this._nodata())
444
+ return;
445
+
446
+ if (this.stat_cov == null) {
447
+
448
+ this.stat_cov = this.stddev() / this.mean();
449
+
450
+ if(round == true) {
451
+ this.stat_cov = Math.round(this.stat_cov * Math.pow(10,this.roundlength) )/ Math.pow(10,this.roundlength);
452
+ }
453
+
454
+ }
455
+
456
+ return this.stat_cov;
457
+ };
458
+
459
+ /** reset all attributes after setting a new serie */
460
+ this.resetStatistics = function() {
461
+ this.stat_sorted = null;
462
+ this.stat_mean = null;
463
+ this.stat_median = null;
464
+ this.stat_sum = null;
465
+ this.stat_max = null;
466
+ this.stat_min = null;
467
+ this.stat_pop = null;
468
+ this.stat_variance = null;
469
+ this.stat_stddev = null;
470
+ this.stat_cov = null;
471
+ };
472
+
473
+ /** data test */
474
+ this._nodata = function() {
475
+ if (this.serie.length == 0) {
476
+
477
+ if(this.silent) this.log("[silent mode] Error. You should first enter a serie!", true);
478
+ else throw new TypeError("Error. You should first enter a serie!");
479
+ return 1;
480
+ } else
481
+ return 0;
482
+
483
+ };
484
+
485
+ /** data test */
486
+ this._classificationCheck = function(nbClass) {
487
+
488
+ if(nbClass >= this.pop()) {
489
+ 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.';
490
+
491
+ if(this.silent) this.log(errnum, true);
492
+ else {
493
+ alert(errnum);
494
+ throw new TypeError(errnum);
495
+ }
496
+
497
+ }
498
+
499
+ };
500
+
501
+ /** ensure nbClass is an integer */
502
+ this._nbClassInt = function(nbClass) {
503
+
504
+ var nbclassTmp = parseInt(nbClass, 10);
505
+ if (isNaN(nbclassTmp)) {
506
+ if(this.silent) this.log("[silent mode] '" + nbclassTmp + "' is not a valid integer. Enable to set class number.", true);
507
+ else throw new TypeError("'" + nbclassTmp + "' is not a valid integer. Enable to set class number.");
508
+ } else {
509
+ return nbclassTmp;
510
+ }
511
+
512
+ };
513
+
514
+ /** check if the serie contains negative value */
515
+ this._hasNegativeValue = function() {
516
+
517
+ for (var i = 0; i < this.serie.length; i++) {
518
+ if(this.serie[i] < 0)
519
+ return true;
520
+ }
521
+
522
+ return false;
523
+ };
524
+
525
+ /** check if the serie contains zero value */
526
+ this._hasZeroValue = function() {
527
+
528
+ for (var i = 0; i < this.serie.length; i++) {
529
+ if(parseFloat(this.serie[i]) === 0)
530
+ return true;
531
+ }
532
+
533
+ return false;
534
+ };
535
+
536
+ /** return sorted values (as array) */
537
+ this.sorted = function() {
538
+
539
+ if (this.stat_sorted == null) {
540
+
541
+ if(this.is_uniqueValues == false) {
542
+ this.stat_sorted = this.serie.sort(function(a, b) {
543
+ return a - b;
544
+ });
545
+ } else {
546
+ this.stat_sorted = this.serie.sort(function(a,b){
547
+ var nameA=a.toString().toLowerCase(), nameB=b.toString().toLowerCase();
548
+ if(nameA < nameB) return -1;
549
+ if(nameA > nameB) return 1;
550
+ return 0;
551
+ });
552
+ }
553
+ }
554
+
555
+ return this.stat_sorted;
556
+
557
+ };
558
+
559
+ /** return all info */
560
+ this.info = function() {
561
+
562
+ if (this._nodata())
563
+ return;
564
+
565
+ var content = '';
566
+ content += _t('Population') + ' : ' + this.pop() + ' - [' + _t('Min')
567
+ + ' : ' + this.min() + ' | ' + _t('Max') + ' : ' + this.max()
568
+ + ']' + "\n";
569
+ content += _t('Mean') + ' : ' + this.mean() + ' - ' + _t('Median') + ' : ' + this.median() + "\n";
570
+ content += _t('Variance') + ' : ' + this.variance() + ' - ' + _t('Standard deviation') + ' : ' + this.stddev()
571
+ + ' - ' + _t('Coefficient of variation') + ' : ' + this.cov() + "\n";
572
+
573
+ return content;
574
+ };
575
+
576
+ /**
577
+ * Set Manual classification Return an array with bounds : ie array(0,
578
+ * 0.75, 1.5, 2.25, 3);
579
+ * Set ranges and prepare data for displaying legend
580
+ *
581
+ */
582
+ this.setClassManually = function(array) {
583
+
584
+ if (this._nodata())
585
+ return;
586
+
587
+ if(array[0] !== this.min() || array[array.length-1] !== this.max()) {
588
+ 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);
589
+ else throw new TypeError(_t('Given bounds may not be correct! please check your input.\nMin value : ' + this.min() + ' / Max value : ' + this.max()));
590
+ return;
591
+ }
592
+
593
+ this.setBounds(array);
594
+ this.setRanges();
595
+
596
+ // we specify the classification method
597
+ this.method = _t('manual classification') + ' (' + (array.length -1) + ' ' + _t('classes') + ')';
598
+
599
+ return this.bounds;
600
+ };
601
+
602
+ /**
603
+ * Equal intervals classification Return an array with bounds : ie array(0,
604
+ * 0.75, 1.5, 2.25, 3);
605
+ */
606
+ this.getClassEqInterval = function(nbClass, forceMin, forceMax) {
607
+
608
+ var nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer
609
+
610
+ if (this._nodata())
611
+ return;
612
+
613
+ var tmpMin = (typeof forceMin === "undefined") ? this.min() : forceMin;
614
+ var tmpMax = (typeof forceMax === "undefined") ? this.max() : forceMax;
615
+
616
+ var a = Array();
617
+ var val = tmpMin;
618
+ var interval = (tmpMax - tmpMin) / nbClass;
619
+
620
+ for (var i = 0; i <= nbClass; i++) {
621
+ a[i] = val;
622
+ val += interval;
623
+ }
624
+
625
+ //-> Fix last bound to Max of values
626
+ a[nbClass] = tmpMax;
627
+
628
+ this.setBounds(a);
629
+ this.setRanges();
630
+
631
+ // we specify the classification method
632
+ this.method = _t('eq. intervals') + ' (' + nbClass + ' ' + _t('classes') + ')';
633
+
634
+ return this.bounds;
635
+ };
636
+
637
+
638
+ this.getQuantiles = function(nbClass) {
639
+
640
+ var nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer
641
+
642
+ var tmp = this.sorted();
643
+ var quantiles = [];
644
+
645
+ var step = this.pop() / nbClass;
646
+ for (var i = 1; i < nbClass; i++) {
647
+ var qidx = Math.round(i*step+0.49);
648
+ quantiles.push(tmp[qidx-1]); // zero-based
649
+ }
650
+
651
+ return quantiles;
652
+ };
653
+
654
+ /**
655
+ * Quantile classification Return an array with bounds : ie array(0, 0.75,
656
+ * 1.5, 2.25, 3);
657
+ */
658
+ this.getClassQuantile = function(nbClass) {
659
+
660
+ var nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer
661
+
662
+ if (this._nodata())
663
+ return;
664
+
665
+ this._classificationCheck(nbClass); // be sure number of classes is valid
666
+
667
+ var tmp = this.sorted();
668
+ var bounds = this.getQuantiles(nbClass);
669
+ bounds.unshift(tmp[0]);
670
+
671
+ if (bounds[tmp.length - 1] !== tmp[tmp.length - 1])
672
+ bounds.push(tmp[tmp.length - 1]);
673
+
674
+ this.setBounds(bounds);
675
+ this.setRanges();
676
+
677
+ // we specify the classification method
678
+ this.method = _t('quantile') + ' (' + nbClass + ' ' + _t('classes') + ')';
679
+
680
+ return this.bounds;
681
+
682
+ };
683
+
684
+ /**
685
+ * Standard Deviation classification
686
+ * Return an array with bounds : ie array(0,
687
+ * 0.75, 1.5, 2.25, 3);
688
+ */
689
+ this.getClassStdDeviation = function(nbClass, matchBounds) {
690
+
691
+ var nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer
692
+
693
+ if (this._nodata())
694
+ return;
695
+
696
+ var tmpMax = this.max();
697
+ var tmpMin = this.min();
698
+ var tmpStdDev = this.stddev();
699
+ var tmpMean = this.mean();
700
+
701
+ var a = Array();
702
+
703
+ // number of classes is odd
704
+ if(nbClass % 2 == 1) {
705
+
706
+ // Euclidean division to get the inferior bound
707
+ var infBound = Math.floor(nbClass / 2);
708
+
709
+ var supBound = infBound + 1;
710
+
711
+ // we set the central bounds
712
+ a[infBound] = tmpMean - (tmpStdDev / 2);
713
+ a[supBound] = tmpMean + (tmpStdDev / 2);
714
+
715
+ // Values < to infBound, except first one
716
+ for (var i = infBound - 1; i > 0; i--) {
717
+ var val = a[i+1] - tmpStdDev;
718
+ a[i] = val;
719
+ }
720
+
721
+ // Values > to supBound, except last one
722
+ for (var i = supBound + 1; i < nbClass; i++) {
723
+ var val = a[i-1] + tmpStdDev;
724
+ a[i] = val;
725
+ }
726
+
727
+ // number of classes is even
728
+ } else {
729
+
730
+ var meanBound = nbClass / 2;
731
+
732
+ // we get the mean value
733
+ a[meanBound] = tmpMean;
734
+
735
+ // Values < to the mean, except first one
736
+ for (var i = meanBound - 1; i > 0; i--) {
737
+ var val = a[i+1] - tmpStdDev;
738
+ a[i] = val;
739
+ }
740
+
741
+ // Values > to the mean, except last one
742
+ for (var i = meanBound + 1; i < nbClass; i++) {
743
+ var val = a[i-1] + tmpStdDev;
744
+ a[i] = val;
745
+ }
746
+ }
747
+
748
+
749
+ // we finally set the first value
750
+ // do we excatly match min value or not ?
751
+ a[0] = (typeof matchBounds === "undefined") ? a[1]- tmpStdDev : tmpMin;
752
+
753
+ // we finally set the last value
754
+ // do we excatly match max value or not ?
755
+ a[nbClass] = (typeof matchBounds === "undefined") ? a[nbClass-1] + tmpStdDev : tmpMax;
756
+
757
+ this.setBounds(a);
758
+ this.setRanges();
759
+
760
+ // we specify the classification method
761
+ this.method = _t('std deviation') + ' (' + nbClass + ' ' + _t('classes')+ ')';
762
+
763
+ return this.bounds;
764
+ };
765
+
766
+
767
+ /**
768
+ * Geometric Progression classification
769
+ * http://en.wikipedia.org/wiki/Geometric_progression
770
+ * Return an array with bounds : ie array(0,
771
+ * 0.75, 1.5, 2.25, 3);
772
+ */
773
+ this.getClassGeometricProgression = function(nbClass) {
774
+
775
+ var nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer
776
+
777
+ if (this._nodata())
778
+ return;
779
+
780
+ if(this._hasNegativeValue() || this._hasZeroValue()) {
781
+ if(this.silent) this.log("[silent mode] " + _t('geometric progression can\'t be applied with a serie containing negative or zero values.'), true);
782
+ else throw new TypeError(_t('geometric progression can\'t be applied with a serie containing negative or zero values.'));
783
+ return;
784
+ }
785
+
786
+ var a = Array();
787
+ var tmpMin = this.min();
788
+ var tmpMax = this.max();
789
+
790
+ var logMax = Math.log(tmpMax) / Math.LN10; // max decimal logarithm (or base 10)
791
+ var logMin = Math.log(tmpMin) / Math.LN10;
792
+ var interval = (logMax - logMin) / nbClass;
793
+
794
+ // we compute log bounds
795
+ for (var i = 0; i < nbClass; i++) {
796
+ if(i == 0) {
797
+ a[i] = logMin;
798
+ } else {
799
+ a[i] = a[i-1] + interval;
800
+ }
801
+ }
802
+
803
+ // we compute antilog
804
+ a = a.map(function(x) { return Math.pow(10, x); });
805
+
806
+ // and we finally add max value
807
+ a.push(this.max());
808
+
809
+ this.setBounds(a);
810
+ this.setRanges();
811
+
812
+ // we specify the classification method
813
+ this.method = _t('geometric progression') + ' (' + nbClass + ' ' + _t('classes') + ')';
814
+
815
+ return this.bounds;
816
+ };
817
+
818
+ /**
819
+ * Arithmetic Progression classification
820
+ * http://en.wikipedia.org/wiki/Arithmetic_progression
821
+ * Return an array with bounds : ie array(0,
822
+ * 0.75, 1.5, 2.25, 3);
823
+ */
824
+ this.getClassArithmeticProgression = function(nbClass) {
825
+
826
+ var nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer
827
+
828
+ if (this._nodata())
829
+ return;
830
+
831
+ var denominator = 0;
832
+
833
+ // we compute the (french) "Raison"
834
+ for (var i = 1; i <= nbClass; i++) {
835
+ denominator += i;
836
+ }
837
+
838
+ var a = Array();
839
+ var tmpMin = this.min();
840
+ var tmpMax = this.max();
841
+
842
+ var interval = (tmpMax - tmpMin) / denominator;
843
+
844
+ for (var i = 0; i <= nbClass; i++) {
845
+ if(i == 0) {
846
+ a[i] = tmpMin;
847
+ } else if(i == nbClass) {
848
+ a[i] = tmpMax;
849
+ } else {
850
+ a[i] = a[i-1] + (i * interval);
851
+ }
852
+ }
853
+
854
+ this.setBounds(a);
855
+ this.setRanges();
856
+
857
+ // we specify the classification method
858
+ this.method = _t('arithmetic progression') + ' (' + nbClass + ' ' + _t('classes') + ')';
859
+
860
+ return this.bounds;
861
+ };
862
+
863
+ /**
864
+ * Credits : Doug Curl (javascript) and Daniel J Lewis (python implementation)
865
+ * http://www.arcgis.com/home/item.html?id=0b633ff2f40d412995b8be377211c47b
866
+ * http://danieljlewis.org/2010/06/07/jenks-natural-breaks-algorithm-in-python/
867
+ */
868
+ this.getClassJenks2 = function(nbClass) {
869
+
870
+ var nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer
871
+
872
+ if (this._nodata())
873
+ return;
874
+
875
+ this._classificationCheck(nbClass); // be sure number of classes is valid
876
+
877
+ var dataList = this.sorted();
878
+
879
+ // now iterate through the datalist:
880
+ // determine mat1 and mat2
881
+ // really not sure how these 2 different arrays are set - the code for
882
+ // each seems the same!
883
+ // but the effect are 2 different arrays: mat1 and mat2
884
+ var mat1 = [];
885
+ for ( var x = 0, xl = dataList.length + 1; x < xl; x++) {
886
+ var temp = [];
887
+ for ( var j = 0, jl = nbClass + 1; j < jl; j++) {
888
+ temp.push(0);
889
+ }
890
+ mat1.push(temp);
891
+ }
892
+
893
+ var mat2 = [];
894
+ for ( var i = 0, il = dataList.length + 1; i < il; i++) {
895
+ var temp2 = [];
896
+ for ( var c = 0, cl = nbClass + 1; c < cl; c++) {
897
+ temp2.push(0);
898
+ }
899
+ mat2.push(temp2);
900
+ }
901
+
902
+ // absolutely no idea what this does - best I can tell, it sets the 1st
903
+ // group in the
904
+ // mat1 and mat2 arrays to 1 and 0 respectively
905
+ for ( var y = 1, yl = nbClass + 1; y < yl; y++) {
906
+ mat1[0][y] = 1;
907
+ mat2[0][y] = 0;
908
+ for ( var t = 1, tl = dataList.length + 1; t < tl; t++) {
909
+ mat2[t][y] = Infinity;
910
+ }
911
+ var v = 0.0;
912
+ }
913
+
914
+ // and this part - I'm a little clueless on - but it works
915
+ // pretty sure it iterates across the entire dataset and compares each
916
+ // value to
917
+ // one another to and adjust the indices until you meet the rules:
918
+ // minimum deviation
919
+ // within a class and maximum separation between classes
920
+ for ( var l = 2, ll = dataList.length + 1; l < ll; l++) {
921
+ var s1 = 0.0;
922
+ var s2 = 0.0;
923
+ var w = 0.0;
924
+ for ( var m = 1, ml = l + 1; m < ml; m++) {
925
+ var i3 = l - m + 1;
926
+ var val = parseFloat(dataList[i3 - 1]);
927
+ s2 += val * val;
928
+ s1 += val;
929
+ w += 1;
930
+ v = s2 - (s1 * s1) / w;
931
+ var i4 = i3 - 1;
932
+ if (i4 != 0) {
933
+ for ( var p = 2, pl = nbClass + 1; p < pl; p++) {
934
+ if (mat2[l][p] >= (v + mat2[i4][p - 1])) {
935
+ mat1[l][p] = i3;
936
+ mat2[l][p] = v + mat2[i4][p - 1];
937
+ }
938
+ }
939
+ }
940
+ }
941
+ mat1[l][1] = 1;
942
+ mat2[l][1] = v;
943
+ }
944
+
945
+ var k = dataList.length;
946
+ var kclass = [];
947
+
948
+ // fill the kclass (classification) array with zeros:
949
+ for (var i = 0; i <= nbClass; i++) {
950
+ kclass.push(0);
951
+ }
952
+
953
+ // this is the last number in the array:
954
+ kclass[nbClass] = parseFloat(dataList[dataList.length - 1]);
955
+ // this is the first number - can set to zero, but want to set to lowest
956
+ // to use for legend:
957
+ kclass[0] = parseFloat(dataList[0]);
958
+ var countNum = nbClass;
959
+ while (countNum >= 2) {
960
+ var id = parseInt((mat1[k][countNum]) - 2);
961
+ kclass[countNum - 1] = dataList[id];
962
+ k = parseInt((mat1[k][countNum] - 1));
963
+ // spits out the rank and value of the break values:
964
+ // console.log("id="+id,"rank = " + String(mat1[k][countNum]),"val =
965
+ // " + String(dataList[id]))
966
+ // count down:
967
+ countNum -= 1;
968
+ }
969
+ // check to see if the 0 and 1 in the array are the same - if so, set 0
970
+ // to 0:
971
+ if (kclass[0] == kclass[1]) {
972
+ kclass[0] = 0;
973
+ }
974
+
975
+ this.setBounds(kclass);
976
+ this.setRanges();
977
+
978
+
979
+ this.method = _t('Jenks2') + ' (' + nbClass + ' ' + _t('classes') + ')';
980
+
981
+ return this.bounds; //array of breaks
982
+ };
983
+
984
+ /**
985
+ * Credits from simple-statistics library
986
+ * https://github.com/simple-statistics/simple-statistics
987
+ * https://gist.githubusercontent.com/tmcw/4969184/raw/cfd9572d00db6bcdc34f07b088738fc3a47846b4/simple_statistics.js
988
+ */
989
+ this.getClassJenks = function(nbClass) {
990
+
991
+ var nbClass = this._nbClassInt(nbClass); // ensure nbClass is an integer
992
+
993
+ if (this._nodata())
994
+ return;
995
+
996
+ this._classificationCheck(nbClass); // be sure number of classes is valid
997
+
998
+ var dataList = this.sorted();
999
+
1000
+ // Compute the matrices required for Jenks breaks. These matrices
1001
+ // can be used for any classing of data with `classes <= n_classes`
1002
+ var jenksMatrices = function(data, n_classes) {
1003
+
1004
+ // in the original implementation, these matrices are referred to
1005
+ // as `LC` and `OP`
1006
+ //
1007
+ // * lower_class_limits (LC): optimal lower class limits
1008
+ // * variance_combinations (OP): optimal variance combinations for all classes
1009
+ var lower_class_limits = [],
1010
+ variance_combinations = [],
1011
+ // loop counters
1012
+ i, j,
1013
+ // the variance, as computed at each step in the calculation
1014
+ variance = 0;
1015
+
1016
+ // Initialize and fill each matrix with zeroes
1017
+ for (var i = 0; i < data.length + 1; i++) {
1018
+ var tmp1 = [], tmp2 = [];
1019
+ for (var j = 0; j < n_classes + 1; j++) {
1020
+ tmp1.push(0);
1021
+ tmp2.push(0);
1022
+ }
1023
+ lower_class_limits.push(tmp1);
1024
+ variance_combinations.push(tmp2);
1025
+ }
1026
+
1027
+ for (var i = 1; i < n_classes + 1; i++) {
1028
+ lower_class_limits[1][i] = 1;
1029
+ variance_combinations[1][i] = 0;
1030
+ // in the original implementation, 9999999 is used but
1031
+ // since Javascript has `Infinity`, we use that.
1032
+ for (var j = 2; j < data.length + 1; j++) {
1033
+ variance_combinations[j][i] = Infinity;
1034
+ }
1035
+ }
1036
+
1037
+ for (var l = 2; l < data.length + 1; l++) {
1038
+
1039
+ // `SZ` originally. this is the sum of the values seen thus
1040
+ // far when calculating variance.
1041
+ var sum = 0,
1042
+ // `ZSQ` originally. the sum of squares of values seen
1043
+ // thus far
1044
+ sum_squares = 0,
1045
+ // `WT` originally. This is the number of
1046
+ w = 0,
1047
+ // `IV` originally
1048
+ i4 = 0;
1049
+
1050
+ // in several instances, you could say `Math.pow(x, 2)`
1051
+ // instead of `x * x`, but this is slower in some browsers
1052
+ // introduces an unnecessary concept.
1053
+ for (var m = 1; m < l + 1; m++) {
1054
+
1055
+ // `III` originally
1056
+ var lower_class_limit = l - m + 1,
1057
+ val = data[lower_class_limit - 1];
1058
+
1059
+ // here we're estimating variance for each potential classing
1060
+ // of the data, for each potential number of classes. `w`
1061
+ // is the number of data points considered so far.
1062
+ w++;
1063
+
1064
+ // increase the current sum and sum-of-squares
1065
+ sum += val;
1066
+ sum_squares += val * val;
1067
+
1068
+ // the variance at this point in the sequence is the difference
1069
+ // between the sum of squares and the total x 2, over the number
1070
+ // of samples.
1071
+ variance = sum_squares - (sum * sum) / w;
1072
+
1073
+ i4 = lower_class_limit - 1;
1074
+
1075
+ if (i4 !== 0) {
1076
+ for (var j = 2; j < n_classes + 1; j++) {
1077
+ if (variance_combinations[l][j] >=
1078
+ (variance + variance_combinations[i4][j - 1])) {
1079
+ lower_class_limits[l][j] = lower_class_limit;
1080
+ variance_combinations[l][j] = variance +
1081
+ variance_combinations[i4][j - 1];
1082
+ }
1083
+ }
1084
+ }
1085
+ }
1086
+
1087
+ lower_class_limits[l][1] = 1;
1088
+ variance_combinations[l][1] = variance;
1089
+ }
1090
+
1091
+ return {
1092
+ lower_class_limits: lower_class_limits,
1093
+ variance_combinations: variance_combinations
1094
+ };
1095
+ };
1096
+
1097
+ // get our basic matrices
1098
+ var matrices = jenksMatrices(dataList, nbClass),
1099
+ // we only need lower class limits here
1100
+ lower_class_limits = matrices.lower_class_limits,
1101
+ k = dataList.length - 1,
1102
+ kclass = [],
1103
+ countNum = nbClass;
1104
+
1105
+ // the calculation of classes will never include the upper and
1106
+ // lower bounds, so we need to explicitly set them
1107
+ kclass[nbClass] = dataList[dataList.length - 1];
1108
+ kclass[0] = dataList[0];
1109
+
1110
+ // the lower_class_limits matrix is used as indexes into itself
1111
+ // here: the `k` variable is reused in each iteration.
1112
+ while (countNum > 1) {
1113
+ kclass[countNum - 1] = dataList[lower_class_limits[k][countNum] - 2];
1114
+ k = lower_class_limits[k][countNum] - 1;
1115
+ countNum--;
1116
+ }
1117
+
1118
+ this.setBounds(kclass);
1119
+ this.setRanges();
1120
+
1121
+
1122
+ this.method = _t('Jenks') + ' (' + nbClass + ' ' + _t('classes') + ')';
1123
+
1124
+ return this.bounds; //array of breaks
1125
+ };
1126
+
1127
+
1128
+ /**
1129
+ * Unique classification Return as many entries as unique values : ie array('blue', 'red', yellow')
1130
+ * 1.5, 2.25, 3);
1131
+ */
1132
+ this.getClassUniqueValues = function() {
1133
+
1134
+ if (this._nodata())
1135
+ return;
1136
+
1137
+ this.is_uniqueValues = true;
1138
+
1139
+ var tmp = this.sorted(); // display in alphabetical order
1140
+
1141
+ var a = Array();
1142
+
1143
+ for (var i = 0; i < this.pop(); i++) {
1144
+ if(a.indexOf(tmp[i]) === -1)
1145
+ a.push(tmp[i]);
1146
+ }
1147
+
1148
+ this.bounds = a;
1149
+
1150
+ // we specify the classification method
1151
+ this.method = _t('unique values');
1152
+
1153
+ return a;
1154
+
1155
+ };
1156
+
1157
+
1158
+ /**
1159
+ * Return the class of a given value.
1160
+ * For example value : 6
1161
+ * and bounds array = (0, 4, 8, 12);
1162
+ * Return 2
1163
+ */
1164
+ this.getClass = function(value) {
1165
+
1166
+ for (var i = 0; i < this.bounds.length; i++) {
1167
+
1168
+ if(this.is_uniqueValues == true) {
1169
+
1170
+ if(value == this.bounds[i]) {
1171
+ // console.log(value + ' - ' + this.bounds[i] + ' returned value : ' + i);
1172
+ return i;
1173
+ }
1174
+ } else {
1175
+ // parseFloat() is necessary
1176
+ if(parseFloat(value) <= this.bounds[i + 1]) {
1177
+ return i;
1178
+ }
1179
+ }
1180
+ }
1181
+
1182
+ return _t("Unable to get value's class.");
1183
+
1184
+ };
1185
+
1186
+ /**
1187
+ * Return the ranges array : array('0-0.75', '0.75-1.5', '1.5-2.25',
1188
+ * '2.25-3');
1189
+ */
1190
+ this.getRanges = function() {
1191
+
1192
+ return this.ranges;
1193
+
1194
+ };
1195
+
1196
+ /**
1197
+ * Returns the number/index of this.ranges that value falls into
1198
+ */
1199
+ this.getRangeNum = function(value) {
1200
+
1201
+ var bounds, i;
1202
+
1203
+ for (var i = 0; i < this.ranges.length; i++) {
1204
+ bounds = this.ranges[i].split(/ - /);
1205
+ if (value <= parseFloat(bounds[1])) {
1206
+ return i;
1207
+ }
1208
+ }
1209
+ };
1210
+
1211
+ /*
1212
+ * Compute inner ranges based on serie.
1213
+ * Produce discontinous ranges used for legend - return an array similar to :
1214
+ * array('0.00-0.74', '0.98-1.52', '1.78-2.25', '2.99-3.14');
1215
+ * If inner ranges already computed, return array values.
1216
+ */
1217
+ this.getInnerRanges = function() {
1218
+
1219
+ // if already computed, we return the result
1220
+ if(this.inner_ranges != null)
1221
+ return this.inner_ranges;
1222
+
1223
+
1224
+ var a = new Array();
1225
+ var tmp = this.sorted();
1226
+
1227
+ var cnt = 1; // bounds array counter
1228
+
1229
+ for (var i = 0; i < tmp.length; i++) {
1230
+
1231
+ if(i == 0) var range_firstvalue = tmp[i]; // we init first range value
1232
+
1233
+ if(parseFloat(tmp[i]) > parseFloat(this.bounds[cnt])) {
1234
+
1235
+ a[cnt - 1] = '' + range_firstvalue + this.separator + tmp[i-1];
1236
+
1237
+ var range_firstvalue = tmp[i];
1238
+
1239
+ cnt++;
1240
+
1241
+ }
1242
+
1243
+ // we reach the last range, we finally complete manually
1244
+ // and return the array
1245
+ if(cnt == (this.bounds.length - 1)) {
1246
+ // we set the last value
1247
+ a[cnt - 1] = '' + range_firstvalue + this.separator + tmp[tmp.length-1];
1248
+
1249
+ this.inner_ranges = a;
1250
+ return this.inner_ranges;
1251
+ }
1252
+
1253
+
1254
+ }
1255
+
1256
+ };
1257
+
1258
+ this.getSortedlist = function() {
1259
+
1260
+ return this.sorted().join(', ');
1261
+
1262
+ };
1263
+
1264
+ /**
1265
+ * Return an html legend
1266
+ * colors : specify an array of color (hexadecimal values)
1267
+ * legend : specify a text input for the legend. By default, just displays 'legend'
1268
+ * counter : if not null, display counter value
1269
+ * callback : if not null, callback function applied on legend boundaries
1270
+ * mode : null, 'default', 'distinct', 'discontinuous' :
1271
+ * - if mode is null, will display legend as 'default mode'
1272
+ * - 'default' : displays ranges like in ranges array (continuous values), sample : 29.26 - 378.80 / 378.80 - 2762.25 / 2762.25 - 6884.84
1273
+ * - 'distinct' : Add + 1 according to decimal precision to distinguish classes (discrete values), sample : 29.26 - 378.80 / 378.81 - 2762.25 / 2762.26 - 6884.84
1274
+ * - 'discontinuous' : indicates the range of data actually falling in each class , sample : 29.26 - 225.43 / 852.12 - 2762.20 / 3001.25 - 6884.84 / not implemented yet
1275
+ * order : null, 'ASC', 'DESC'
1276
+ */
1277
+ this.getHtmlLegend = function(colors, legend, counter, callback, mode, order) {
1278
+
1279
+ var cnt= '';
1280
+ var elements = new Array();
1281
+
1282
+ this.doCount(); // we do count, even if not displayed
1283
+
1284
+ if(colors != null) {
1285
+ ccolors = colors;
1286
+ }
1287
+ else {
1288
+ ccolors = this.colors;
1289
+ }
1290
+
1291
+ if(legend != null) {
1292
+ lg = legend;
1293
+ }
1294
+ else {
1295
+ lg = 'Legend';
1296
+ }
1297
+
1298
+ if(counter != null) {
1299
+ getcounter = true;
1300
+ }
1301
+ else {
1302
+ getcounter = false;
1303
+ }
1304
+
1305
+ if(callback != null) {
1306
+ fn = callback;
1307
+ }
1308
+ else {
1309
+ fn = function(o) {return o;};
1310
+ }
1311
+ if(mode == null) {
1312
+ mode = 'default';
1313
+ }
1314
+ if(mode == 'discontinuous') {
1315
+ this.getInnerRanges();
1316
+ // check if some classes are not populated / equivalent of in_array function
1317
+ if(this.counter.indexOf(0) !== -1) {
1318
+ if(this.silent) this.log("[silent mode] " + _t("Geostats cannot apply 'discontinuous' mode to the getHtmlLegend() method because some classes are not populated.\nPlease switch to 'default' or 'distinct' modes. Exit!"), true);
1319
+ else throw new TypeError(_t("Geostats cannot apply 'discontinuous' mode to the getHtmlLegend() method because some classes are not populated.\nPlease switch to 'default' or 'distinct' modes. Exit!"));
1320
+ return;
1321
+ }
1322
+
1323
+ }
1324
+ if(order !== 'DESC') order = 'ASC';
1325
+
1326
+ if(ccolors.length < this.ranges.length) {
1327
+ if(this.silent) this.log("[silent mode] " + _t('The number of colors should fit the number of ranges. Exit!'), true);
1328
+ else throw new TypeError(_t('The number of colors should fit the number of ranges. Exit!'));
1329
+ return;
1330
+ }
1331
+
1332
+ if(this.is_uniqueValues == false) {
1333
+
1334
+ for (var i = 0; i < (this.ranges.length); i++) {
1335
+ if(getcounter===true) {
1336
+ cnt = ' <span class="geostats-legend-counter">(' + this.counter[i] + ')</span>';
1337
+ }
1338
+ //console.log("Ranges : " + this.ranges[i]);
1339
+
1340
+ // default mode
1341
+ var tmp = this.ranges[i].split(this.separator);
1342
+
1343
+ var start_value = parseFloat(tmp[0]).toFixed(this.precision);
1344
+ var end_value = parseFloat(tmp[1]).toFixed(this.precision);
1345
+
1346
+
1347
+ // if mode == 'distinct' and we are not working on the first value
1348
+ if(mode == 'distinct' && i != 0) {
1349
+
1350
+ if(isInt(start_value)) {
1351
+ start_value = parseInt(start_value) + 1;
1352
+ // format to float if necessary
1353
+ if(this.precisionflag == 'manual' && this.precision != 0) start_value = parseFloat(start_value).toFixed(this.precision);
1354
+ } else {
1355
+
1356
+ start_value = parseFloat(start_value) + (1 / Math.pow(10,this.precision));
1357
+ // strangely the formula above return sometimes long decimal values,
1358
+ // the following instruction fix it
1359
+ start_value = parseFloat(start_value).toFixed(this.precision);
1360
+ }
1361
+ }
1362
+
1363
+ // if mode == 'discontinuous'
1364
+ if(mode == 'discontinuous') {
1365
+
1366
+ var tmp = this.inner_ranges[i].split(this.separator);
1367
+ // console.log("Ranges : " + this.inner_ranges[i]);
1368
+
1369
+ var start_value = parseFloat(tmp[0]).toFixed(this.precision);
1370
+ var end_value = parseFloat(tmp[1]).toFixed(this.precision);
1371
+
1372
+ }
1373
+
1374
+ // we apply callback function
1375
+ var el = fn(start_value) + this.legendSeparator + fn(end_value);
1376
+
1377
+ var block = '<div><div class="geostats-legend-block" style="background-color:' + ccolors[i] + '"></div> ' + el + cnt + '</div>';
1378
+ elements.push(block);
1379
+ }
1380
+
1381
+ } else {
1382
+
1383
+ // only if classification is done on unique values
1384
+ for (var i = 0; i < (this.bounds.length); i++) {
1385
+ if(getcounter===true) {
1386
+ cnt = ' <span class="geostats-legend-counter">(' + this.counter[i] + ')</span>';
1387
+ }
1388
+ var el = fn(this.bounds[i]);
1389
+ var block = '<div><div class="geostats-legend-block" style="background-color:' + ccolors[i] + '"></div> ' + el + cnt + '</div>';
1390
+
1391
+ elements.push(block);
1392
+ }
1393
+
1394
+ }
1395
+
1396
+ // do we reverse the return legend ?
1397
+ if(order === 'DESC') elements.reverse();
1398
+
1399
+ // finally we create HTML and return it
1400
+ var content = '<div class="geostats-legend"><div class="geostats-legend-title">' + _t(lg) + '</div>';
1401
+ for (var i = 0; i < (elements.length); i++) {
1402
+ content += elements[i];
1403
+ }
1404
+ content += '</div>';
1405
+
1406
+ return content;
1407
+ };
1408
+
1409
+
1410
+
1411
+ // object constructor
1412
+ // At the end of script. If not setPrecision() method is not known
1413
+
1414
+ // we create an object identifier for debugging
1415
+ this.objectID = new Date().getUTCMilliseconds();
1416
+ this.log('Creating new geostats object');
1417
+
1418
+ if(typeof a !== 'undefined' && a.length > 0) {
1419
+ this.serie = a;
1420
+ this.setPrecision();
1421
+ this.log('Setting serie (' + a.length + ') : ' + a.join());
1422
+ } else {
1423
+ this.serie = Array();
1424
+
1425
+ }
1426
+ // creating aliases on classification function for backward compatibility
1427
+ this.getJenks = this.getClassJenks;
1428
+ this.getJenks2 = this.getClassJenks2;
1429
+ this.getGeometricProgression = this.getClassGeometricProgression;
1430
+ this.getEqInterval = this.getClassEqInterval;
1431
+ this.getQuantile = this.getClassQuantile;
1432
+ this.getStdDeviation = this.getClassStdDeviation;
1433
+ this.getUniqueValues = this.getClassUniqueValues;
1434
+ this.getArithmeticProgression = this.getClassArithmeticProgression;
1435
+
1436
+ };
1437
+
1438
+
1439
+ return geostats;
1440
+ });
1441
+ } (geostats$1));
1442
+ return geostats$1.exports;
1443
+ }
1444
+
1445
+ var geostatsExports = requireGeostats();
1446
+ const geostats = /*@__PURE__*/getDefaultExportFromCjs(geostatsExports);
1447
+
1448
+ L.SolrHeatmap = L.GeoJSON.extend({
1449
+ options: {
1450
+ solrRequestHandler: 'select',
1451
+ type: 'geojsonGrid',
1452
+ colors: ['#f1eef6', '#d7b5d8', '#df65b0', '#dd1c77', '#980043'],
1453
+ maxSampleSize: Number.MAX_SAFE_INTEGER, // for Jenks classification
1454
+ logging: false,
1455
+ },
1456
+
1457
+ initialize: function (url, options) {
1458
+ var _this = this;
1459
+ options = L.setOptions(_this, options);
1460
+ _this._solrUrl = url;
1461
+ _this._layers = {};
1462
+ _this._getData();
1463
+ },
1464
+
1465
+ onAdd: function (map) {
1466
+ var _this = this;
1467
+
1468
+ // Call the parent function
1469
+ L.GeoJSON.prototype.onAdd.call(_this, map);
1470
+
1471
+ map.on('moveend', function () {
1472
+ _this._getData();
1473
+ });
1474
+ },
1475
+
1476
+ _computeHeatmapObject: function (data) {
1477
+ var _this = this;
1478
+ _this.facetHeatmap = data.response.facet_heatmaps[this.options.field];
1479
+ this._computeIntArrays();
1480
+ },
1481
+
1482
+ _clearLayers: function () {
1483
+ var _this = this;
1484
+
1485
+ switch (_this.options.type) {
1486
+ case 'geojsonGrid':
1487
+ _this.clearLayers();
1488
+ break;
1489
+ case 'clusters':
1490
+ _this.clusterMarkers.clearLayers();
1491
+ break;
1492
+ case 'heatmap':
1493
+ _this._map.removeLayer(_this.heatmapLayer);
1494
+ break;
1495
+ }
1496
+ },
1497
+
1498
+ _createGeojson: function () {
1499
+ var _this = this;
1500
+ var geojson = {};
1501
+
1502
+ geojson.type = 'FeatureCollection';
1503
+ geojson.features = [];
1504
+
1505
+ _this.facetHeatmap.counts_ints2D.forEach(function(value, row) {
1506
+ if (value === null) {
1507
+ return;
1508
+ }
1509
+
1510
+ value.forEach(function (val, column) {
1511
+ if (val === 0) {
1512
+ return;
1513
+ }
1514
+
1515
+ var newFeature = {
1516
+ type: 'Feature',
1517
+ geometry: {
1518
+ type: 'Polygon',
1519
+ coordinates: [
1520
+ [
1521
+ [_this._minLng(column), _this._minLat(row)],
1522
+ [_this._minLng(column), _this._maxLat(row)],
1523
+ [_this._maxLng(column), _this._maxLat(row)],
1524
+ [_this._maxLng(column), _this._minLat(row)],
1525
+ [_this._minLng(column), _this._minLat(row)]
1526
+ ]
1527
+ ]
1528
+ },
1529
+ properties: {
1530
+ count: val
1531
+ }
1532
+ };
1533
+ geojson.features.push(newFeature);
1534
+ });
1535
+ });
1536
+
1537
+ _this.addData(geojson);
1538
+ var colors = _this.options.colors;
1539
+ if (_this.facetHeatmap.counts_ints2D && _this.facetHeatmap.counts_ints2D.length > 0) {
1540
+ var classifications = _this._getClassifications(colors.length);
1541
+ _this._styleByCount(classifications);
1542
+ _this._showRenderTime();
1543
+ }
1544
+ },
1545
+
1546
+ _createHeatmap: function () {
1547
+ var _this = this;
1548
+ var heatmapCells = [];
1549
+ var cellSize = _this._getCellSize() * .75;
1550
+ var colors = _this.options.colors;
1551
+ var classifications = _this._getClassifications(colors.length - 1);
1552
+ var maxValue = classifications[classifications.length - 1];
1553
+ var gradient = _this._getGradient(classifications);
1554
+
1555
+ _this.facetHeatmap.counts_ints2D.forEach(function(value, row) {
1556
+ if (value === null) {
1557
+ return;
1558
+ }
1559
+
1560
+ value.forEach(function (val, column) {
1561
+ if (val === 0) {
1562
+ return;
1563
+ }
1564
+
1565
+ var scaledValue = Math.min((val / maxValue), 1);
1566
+ var current = [_this._minLat(row), _this._minLng(column), scaledValue];
1567
+ heatmapCells.push(current);
1568
+
1569
+ // need to create options object to set gradient, blu, radius, max
1570
+ });
1571
+ });
1572
+
1573
+ // settting max due to bug
1574
+ // http://stackoverflow.com/questions/26767722/leaflet-heat-issue-with-adding-points-with-intensity
1575
+ var options = { max: .0001, radius: cellSize, gradient: gradient };
1576
+ var heatmapLayer = L.heatLayer(heatmapCells, options);
1577
+ heatmapLayer.addTo(_this._map);
1578
+ _this.heatmapLayer = heatmapLayer;
1579
+ _this._showRenderTime();
1580
+ },
1581
+
1582
+ // heatmap display need hash of scaled counts value, color pairs
1583
+ _getGradient: function (classifications) {
1584
+ var gradient = {};
1585
+ var maxValue = classifications[classifications.length - 1];
1586
+ var colors = _this.options.colors;
1587
+ // skip first lower bound, assumed to be 0 from Jenks
1588
+ for (var i = 1; i < classifications.length; i++)
1589
+ gradient[classifications[i] / maxValue] = colors[i];
1590
+ return gradient;
1591
+ },
1592
+
1593
+ // compute size of heatmap cells in pixels
1594
+ _getCellSize: function () {
1595
+ _this = this;
1596
+ var mapSize = _this._map.getSize(); // should't we use solr returned map extent?
1597
+ var widthInPixels = mapSize.x;
1598
+ var heightInPixels = mapSize.y;
1599
+ var heatmapRows = _this.facetHeatmap.rows;
1600
+ var heatmapColumns = _this.facetHeatmap.columns;
1601
+ var sizeX = widthInPixels / heatmapColumns;
1602
+ var sizeY = heightInPixels / heatmapRows;
1603
+ var size = Math.ceil(Math.max(sizeX, sizeY));
1604
+ return size;
1605
+ },
1606
+
1607
+ _showRenderTime: function () {
1608
+ if (this.options.logging) {
1609
+ var _this = this;
1610
+ var renderTime = 'Render time: ' + (Date.now() - _this.renderStart) + ' ms';
1611
+ console.log(renderTime);
1612
+ }
1613
+ },
1614
+
1615
+ _createClusters: function() {
1616
+ var _this = this;
1617
+
1618
+ _this.clusterMarkers = new L.MarkerClusterGroup({
1619
+ maxClusterRadius: 140,
1620
+ });
1621
+
1622
+ _this.facetHeatmap.counts_ints2D.forEach(function(value, row) {
1623
+ if (value === null) {
1624
+ return;
1625
+ }
1626
+
1627
+ value.forEach(function (val, column) {
1628
+ if (val === 0) {
1629
+ return;
1630
+ }
1631
+
1632
+ var bounds = new L.latLngBounds([
1633
+ [_this._minLat(row), _this._minLng(column)],
1634
+ [_this._maxLat(row), _this._maxLng(column)],
1635
+ ]);
1636
+ _this.clusterMarkers.addLayer(new L.Marker(bounds.getCenter(), {
1637
+ count: val,
1638
+ }).bindPopup(val.toString()));
1639
+ });
1640
+ });
1641
+
1642
+ _this._map.addLayer(_this.clusterMarkers);
1643
+ _this._showRenderTime();
1644
+ },
1645
+
1646
+ _computeIntArrays: function () {
1647
+ var _this = this;
1648
+
1649
+ _this.lengthX = (_this.facetHeatmap.maxX - _this.facetHeatmap.minX) / _this.facetHeatmap.columns;
1650
+ _this.lengthY = (_this.facetHeatmap.maxY - _this.facetHeatmap.minY) / _this.facetHeatmap.rows;
1651
+ _this._clearLayers();
1652
+ switch (_this.options.type) {
1653
+ case 'geojsonGrid':
1654
+ _this._createGeojson();
1655
+ break;
1656
+ case 'clusters':
1657
+ _this._createClusters();
1658
+ break;
1659
+ case 'heatmap':
1660
+ _this._createHeatmap();
1661
+ break;
1662
+ }
1663
+ },
1664
+
1665
+ _getClassifications: function (howMany) {
1666
+ var _this = this;
1667
+ var oneDArray = [];
1668
+ _this.facetHeatmap.counts_ints2D.forEach(function(value, row) {
1669
+ if (value != null) {
1670
+ oneDArray = oneDArray.concat(value);
1671
+ }
1672
+ });
1673
+
1674
+ var sampledArray = _this._sampleCounts(oneDArray);
1675
+
1676
+ var series = new geostats(sampledArray);
1677
+ _this.options.colors;
1678
+ var classifications = series.getClassJenks(howMany - 1);
1679
+ return classifications.reduce(function (previous, current) {
1680
+ if (previous.indexOf(current) == -1) {
1681
+ previous.push(current);
1682
+ }
1683
+
1684
+ return previous;
1685
+ }, []);
1686
+ },
1687
+
1688
+ _styleByCount: function (classifications) {
1689
+ var _this = this;
1690
+ var scale = _this.options.colors.slice(this.options.colors.length - classifications.length, this.options.colors.length);
1691
+
1692
+ _this.eachLayer(function (layer) {
1693
+ var color;
1694
+ classifications.forEach(function (val, i) {
1695
+ if (layer.feature.properties.count >= val) {
1696
+ color = scale[i];
1697
+ }
1698
+ });
1699
+
1700
+ layer.setStyle({
1701
+ fillColor: color,
1702
+ fillOpacity: 0.5,
1703
+ weight: 0,
1704
+ });
1705
+ });
1706
+ },
1707
+
1708
+ // Jenks classification can be slow so we optionally sample the data
1709
+ // typically any big sample of counts are much the same, don't need to classify on all of them
1710
+ _sampleCounts: function (passedArray) {
1711
+ const _this = this;
1712
+ if (passedArray.length <= _this.options.maxSampleSize) {
1713
+ return passedArray; // array too small to sample
1714
+ }
1715
+
1716
+ var maxValue = Math.max.apply(Math, passedArray);
1717
+ var sampledArray = [];
1718
+ var period = Math.ceil(passedArray.length / _this.options.maxSampleSize);
1719
+ for (let i = 0; i < passedArray.length; i = i + period) {
1720
+ sampledArray.push(passedArray[i]);
1721
+ }
1722
+
1723
+ sampledArray.push(maxValue); // make sure largest value gets in, doesn't matter much if duplicated
1724
+ return sampledArray;
1725
+ },
1726
+
1727
+ _minLng: function (column) {
1728
+ return this.facetHeatmap.minX + (this.lengthX * column);
1729
+ },
1730
+
1731
+ _minLat: function (row) {
1732
+ return this.facetHeatmap.maxY - (this.lengthY * row) - this.lengthY;
1733
+ },
1734
+
1735
+ _maxLng: function (column) {
1736
+ return this.facetHeatmap.minX + (this.lengthX * column) + this.lengthX;
1737
+ },
1738
+
1739
+ _maxLat: function (row) {
1740
+ return this.facetHeatmap.maxY - (this.lengthY * row);
1741
+ },
1742
+
1743
+ _getData: function () {
1744
+ var _this = this;
1745
+ var startTime = Date.now();
1746
+
1747
+ var url = new URL(_this._solrUrl);
1748
+ url.searchParams.append('bbox', _this._mapViewToBbox());
1749
+
1750
+ fetch(url, {
1751
+ headers: {
1752
+ 'Accept': 'application/json',
1753
+ }
1754
+ }).then(function (response) {
1755
+ return response.json();
1756
+ }).then(function (data) {
1757
+ var totalTime = 'Solr response time: ' + (Date.now() - startTime) + ' ms';
1758
+ if (_this.options.logging) {
1759
+ console.log(totalTime);
1760
+ }
1761
+
1762
+ _this.docsCount = data.response.numFound;
1763
+ _this.renderStart = Date.now();
1764
+ _this._computeHeatmapObject(data);
1765
+ _this.fireEvent('dataAdded', data);
1766
+ });
1767
+ },
1768
+
1769
+ _mapViewToBbox: function () {
1770
+ if (this._map === undefined) {
1771
+ return '-180,-90,180,90';
1772
+ }
1773
+
1774
+ var bounds = this._map.getBounds();
1775
+ var wrappedSw = bounds.getSouthWest().wrap();
1776
+ var wrappedNe = bounds.getNorthEast().wrap();
1777
+ return [wrappedSw.lng, bounds.getSouth(), wrappedNe.lng, bounds.getNorth()].join(',');
1778
+ },
1779
+
1780
+ _mapViewToEnvelope: function () {
1781
+ if (this._map === undefined) {
1782
+ return ':"Intersects(ENVELOPE(-180, 180, 90, -90))"';
1783
+ }
1784
+
1785
+ var bounds = this._map.getBounds();
1786
+ var wrappedSw = bounds.getSouthWest().wrap();
1787
+ var wrappedNe = bounds.getNorthEast().wrap();
1788
+ return ':"Intersects(ENVELOPE(' + wrappedSw.lng + ', ' + wrappedNe.lng + ', ' + bounds.getNorth() + ', ' + bounds.getSouth() + '))"';
1789
+ },
1790
+
1791
+ _mapViewToWkt: function () {
1792
+ if (this._map === undefined) {
1793
+ return '["-180 -90" TO "180 90"]';
1794
+ }
1795
+
1796
+ var bounds = this._map.getBounds();
1797
+ var wrappedSw = bounds.getSouthWest().wrap();
1798
+ var wrappedNe = bounds.getNorthEast().wrap();
1799
+ return '["' + wrappedSw.lng + ' ' + bounds.getSouth() + '" TO "' + wrappedNe.lng + ' ' + bounds.getNorth() + '"]';
1800
+ },
1801
+
1802
+ _solrQuery: function () {
1803
+ return '/' + this.options.solrRequestHandler + '?' + this.options.field;
1804
+ },
1805
+ });
1806
+
1807
+ L.solrHeatmap = function (url, options) {
1808
+ return new L.SolrHeatmap(url, options);
1809
+ };
1810
+
1811
+ // Check if L.MarkerCluster is included
1812
+ if (typeof L.MarkerCluster !== 'undefined') {
1813
+ L.MarkerCluster.prototype.initialize = function (group, zoom, a, b) {
1814
+
1815
+ L.Marker.prototype.initialize.call(this, a ? (a._cLatLng || a.getLatLng()) : new L.LatLng(0, 0), { icon: this });
1816
+
1817
+ this._group = group;
1818
+ this._zoom = zoom;
1819
+
1820
+ this._markers = [];
1821
+ this._childClusters = [];
1822
+ this._childCount = 0;
1823
+ this._iconNeedsUpdate = true;
1824
+
1825
+ this._bounds = new L.LatLngBounds();
1826
+
1827
+ if (a) {
1828
+ this._addChild(a);
1829
+ }
1830
+
1831
+ if (b) {
1832
+ this._addChild(b);
1833
+ this._childCount = b.options.count;
1834
+ }
1835
+ };
1836
+ }
1837
+
1838
+ const Basemaps = {
1839
+ darkMatter: L.tileLayer(
1840
+ 'https://cartodb-basemaps-{s}.global.ssl.fastly.net/dark_all/{z}/{x}/{y}.png', {
1841
+ attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="http://cartodb.com/attributions">CartoDB</a>',
1842
+ maxZoom: 18,
1843
+ worldCopyJump: true,
1844
+ detectRetina: true,
1845
+ }
1846
+ ),
1847
+ positron: L.tileLayer(
1848
+ 'https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png', {
1849
+ attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="http://cartodb.com/attributions">CartoDB</a>',
1850
+ maxZoom: 18,
1851
+ worldCopyJump: true,
1852
+ detectRetina: true,
1853
+ }
1854
+ ),
1855
+ 'OpenStreetMap.HOT': L.tileLayer(
1856
+ 'https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png', {
1857
+ attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, Tiles courtesy of <a href="http://hot.openstreetmap.org/" target="_blank">Humanitarian OpenStreetMap Team</a>',
1858
+ maxZoom: 19,
1859
+ }
1860
+ ),
1861
+ };
1862
+
1863
+ const Icons = {
1864
+ default: new L.Icon.Default()
1865
+ };
1866
+
1867
+ const IndexView = L.Class.extend({
1868
+ options: {},
1869
+
1870
+ initialize: function (el, options) {
1871
+ var requestUrl = el.dataset.searchUrl + '&format=heatmaps';
1872
+ var geometryField = el.dataset.geometryField;
1873
+ var template = el.dataset.sidebarTemplate;
1874
+ var colorRamp = JSON.parse(el.dataset.colorRamp);
1875
+
1876
+ // Blank out page link content first and disable pagination
1877
+ document.querySelectorAll('#sortAndPerPage .page-links').forEach(function (links) {
1878
+ links.innerHTML = '';
1879
+ });
1880
+ document.querySelectorAll('ul.pagination').forEach(function (links) {
1881
+ links.classList.add('d-none');
1882
+ });
1883
+
1884
+ var map = L.map(el.id).setView([0, 0], 1);
1885
+ BlacklightHeatmaps.selectBasemap(
1886
+ el.dataset.basemapProvider
1887
+ ).addTo(map);
1888
+
1889
+ var solrLayer = L.solrHeatmap(requestUrl, {
1890
+ field: geometryField,
1891
+ maxSampleSize: 50,
1892
+ colors: colorRamp,
1893
+ }).addTo(map);
1894
+
1895
+ var sidebar = L.control.sidebar('index-map-sidebar', {
1896
+ position: 'right',
1897
+ });
1898
+
1899
+ map.addControl(sidebar);
1900
+
1901
+ solrLayer.on('click', function (e) {
1902
+ if (!sidebar.isVisible()) {
1903
+ map.setView(e.latlng);
1904
+ } else {
1905
+ var point = map.project(e.latlng);
1906
+ var offset = sidebar.getOffset();
1907
+ var newPoint = L.point(point.x - (offset / 2), point.y);
1908
+ map.setView(map.unproject(newPoint));
1909
+ }
1910
+
1911
+ sidebar.show();
1912
+ });
1913
+
1914
+ solrLayer.on('dataAdded', function (e) {
1915
+ if (e.response && e.response.docs) {
1916
+ var html = '';
1917
+ e.response.docs.forEach(function (value) {
1918
+ html += L.Util.template(template, value);
1919
+ });
1920
+
1921
+ sidebar.setContent(html);
1922
+
1923
+ var docCount = e.response.pages.total_count;
1924
+
1925
+ document.querySelectorAll('#sortAndPerPage .page-links').forEach(function (links) {
1926
+ links.innerHTML = parseInt(docCount).toLocaleString() + ' ' + (docCount == 1 ? 'item' : 'items') + ' found';
1927
+ });
1928
+ }
1929
+ });
1930
+ },
1931
+
1932
+ pluralize: function (count, word) {
1933
+ switch (count) {
1934
+ case 1:
1935
+ return word;
1936
+ default:
1937
+ return word + 's';
1938
+ }
1939
+ },
1940
+ });
1941
+
1942
+ const ShowView = L.Class.extend({
1943
+ options: {},
1944
+
1945
+ initialize: function (el, options) {
1946
+ var json = JSON.parse(el.dataset.features);
1947
+
1948
+ var map = L.map(el.id).setView([0, 0], 1);
1949
+ BlacklightHeatmaps.selectBasemap(
1950
+ el.dataset.basemapProvider
1951
+ ).addTo(map);
1952
+
1953
+ var features = L.geoJson(json, {
1954
+ pointToLayer: function(feature, latlng) {
1955
+ return L.marker(latlng, {
1956
+ icon: BlacklightHeatmaps.Icons.default
1957
+ })
1958
+ }
1959
+ }).addTo(map);
1960
+
1961
+ map.fitBounds(features.getBounds());
1962
+ },
1963
+ });
1964
+
1965
+ const BlacklightHeatmaps$1 = L$1.Class.extend({
1966
+ statics: {
1967
+ __version__: '0.0.3',
1968
+
1969
+ selectBasemap: function (basemap) {
1970
+ if (basemap && basemap !== undefined) {
1971
+ return BlacklightHeatmaps$1.Basemaps[basemap];
1972
+ } else {
1973
+ return BlacklightHeatmaps$1.Basemaps.positron;
1974
+ }
1975
+ },
1976
+ },
1977
+ });
1978
+
1979
+ BlacklightHeatmaps$1.Basemaps = Basemaps;
1980
+ BlacklightHeatmaps$1.Icons = Icons;
1981
+ BlacklightHeatmaps$1.IndexView = IndexView;
1982
+ BlacklightHeatmaps$1.indexView = function (el, options) {
1983
+ return new IndexView(el, options)
1984
+ };
1985
+ BlacklightHeatmaps$1.ShowView = ShowView;
1986
+ BlacklightHeatmaps$1.showView = function (el, options) {
1987
+ return new ShowView(el, options);
1988
+ };
1989
+
1990
+ Blacklight.onLoad(function () {
1991
+ document.querySelectorAll('[data-index-map]').forEach(function (el) {
1992
+ BlacklightHeatmaps$1.indexView(el, {});
1993
+ });
1994
+ document.querySelectorAll('[data-show-map]').forEach(function (el) {
1995
+ BlacklightHeatmaps$1.showView(el);
1996
+ });
1997
+ });
1998
+
1999
+ export { BlacklightHeatmaps$1 as default };
2000
+ //# sourceMappingURL=default.esm.js.map