blacklight_heatmaps 1.3.1 → 1.4.0

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