@jupytergis/base 0.1.6 → 0.1.7

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.
Files changed (53) hide show
  1. package/lib/classificationModes.d.ts +13 -0
  2. package/lib/classificationModes.js +326 -0
  3. package/lib/commands.js +1 -1
  4. package/lib/dialogs/symbology/classificationModes.d.ts +13 -0
  5. package/lib/dialogs/symbology/classificationModes.js +326 -0
  6. package/lib/dialogs/symbology/components/color_ramp/CanvasSelectComponent.d.ts +11 -0
  7. package/lib/dialogs/symbology/components/color_ramp/CanvasSelectComponent.js +119 -0
  8. package/lib/dialogs/symbology/components/color_ramp/ColorRamp.d.ts +15 -0
  9. package/lib/dialogs/symbology/components/color_ramp/ColorRamp.js +33 -0
  10. package/lib/dialogs/symbology/components/color_ramp/ColorRampEntry.d.ts +9 -0
  11. package/lib/dialogs/symbology/components/color_ramp/ColorRampEntry.js +24 -0
  12. package/lib/dialogs/symbology/components/color_ramp/ModeSelectRow.d.ts +10 -0
  13. package/lib/dialogs/symbology/components/color_ramp/ModeSelectRow.js +11 -0
  14. package/lib/dialogs/symbology/components/color_stops/StopContainer.d.ts +9 -0
  15. package/lib/dialogs/symbology/components/color_stops/StopContainer.js +28 -0
  16. package/lib/dialogs/{components/symbology → symbology/components/color_stops}/StopRow.js +9 -2
  17. package/lib/dialogs/symbology/hooks/useGetProperties.d.ts +12 -0
  18. package/lib/dialogs/symbology/hooks/useGetProperties.js +47 -0
  19. package/lib/dialogs/{symbologyDialog.js → symbology/symbologyDialog.js} +3 -3
  20. package/lib/dialogs/symbology/symbologyUtils.d.ts +9 -0
  21. package/lib/dialogs/symbology/symbologyUtils.js +94 -0
  22. package/lib/dialogs/symbology/tiff_layer/TiffRendering.d.ts +4 -0
  23. package/lib/dialogs/{components/symbology/BandRendering.js → symbology/tiff_layer/TiffRendering.js} +3 -3
  24. package/lib/dialogs/{components/symbology → symbology/tiff_layer/components}/BandRow.d.ts +1 -1
  25. package/lib/dialogs/{components/symbology → symbology/tiff_layer/types}/SingleBandPseudoColor.d.ts +9 -1
  26. package/lib/dialogs/{components/symbology → symbology/tiff_layer/types}/SingleBandPseudoColor.js +113 -57
  27. package/lib/dialogs/{components/symbology → symbology/vector_layer}/VectorRendering.d.ts +1 -1
  28. package/lib/dialogs/{components/symbology → symbology/vector_layer}/VectorRendering.js +10 -13
  29. package/lib/dialogs/symbology/vector_layer/components/ValueSelect.d.ts +8 -0
  30. package/lib/dialogs/symbology/vector_layer/components/ValueSelect.js +7 -0
  31. package/lib/dialogs/symbology/vector_layer/types/Categorized.d.ts +4 -0
  32. package/lib/dialogs/symbology/vector_layer/types/Categorized.js +94 -0
  33. package/lib/dialogs/symbology/vector_layer/types/Graduated.js +169 -0
  34. package/lib/dialogs/{components/symbology → symbology/vector_layer/types}/SimpleSymbol.js +8 -13
  35. package/lib/formbuilder/objectform/vectorlayerform.js +1 -0
  36. package/lib/formbuilder/objectform/webGlLayerForm.js +1 -0
  37. package/lib/index.d.ts +6 -4
  38. package/lib/index.js +6 -4
  39. package/lib/mainview/mainView.d.ts +3 -1
  40. package/lib/mainview/mainView.js +66 -18
  41. package/lib/store.d.ts +9 -0
  42. package/lib/store.js +25 -0
  43. package/lib/toolbar/widget.js +1 -1
  44. package/lib/types.d.ts +14 -0
  45. package/package.json +16 -17
  46. package/style/symbologyDialog.css +104 -3
  47. package/lib/dialogs/components/symbology/BandRendering.d.ts +0 -4
  48. package/lib/dialogs/components/symbology/Graduated.js +0 -188
  49. /package/lib/dialogs/{components/symbology → symbology/components/color_stops}/StopRow.d.ts +0 -0
  50. /package/lib/dialogs/{symbologyDialog.d.ts → symbology/symbologyDialog.d.ts} +0 -0
  51. /package/lib/dialogs/{components/symbology → symbology/tiff_layer/components}/BandRow.js +0 -0
  52. /package/lib/dialogs/{components/symbology → symbology/vector_layer/types}/Graduated.d.ts +0 -0
  53. /package/lib/dialogs/{components/symbology → symbology/vector_layer/types}/SimpleSymbol.d.ts +0 -0
@@ -0,0 +1,13 @@
1
+ import { InterpolationType } from './dialogs/symbology/tiff_layer/types/SingleBandPseudoColor';
2
+ export declare namespace VectorClassifications {
3
+ const calculateQuantileBreaks: (values: number[], nClasses: number) => number[];
4
+ const calculateEqualIntervalBreaks: (values: number[], nClasses: number) => number[];
5
+ const calculateJenksBreaks: (values: number[], nClasses: number) => any[];
6
+ const calculatePrettyBreaks: (values: number[], nClasses: number) => number[];
7
+ const calculateLogarithmicBreaks: (values: number[], nClasses: number) => number[];
8
+ }
9
+ export declare namespace GeoTiffClassifications {
10
+ const classifyQuantileBreaks: (nClasses: number, bandNumber: number, url: string, colorRampType: string) => Promise<number[]>;
11
+ const classifyContinuousBreaks: (nClasses: number, minimumValue: number, maximumValue: number, colorRampType: InterpolationType) => number[];
12
+ const classifyEqualIntervalBreaks: (nClasses: number, minimumValue: number, maximumValue: number, colorRampType: InterpolationType) => number[];
13
+ }
@@ -0,0 +1,326 @@
1
+ // Adapted from https://github.com/qgis/QGIS/blob/master/src/core/classification/
2
+ import { Pool, fromUrl } from 'geotiff';
3
+ export var VectorClassifications;
4
+ (function (VectorClassifications) {
5
+ VectorClassifications.calculateQuantileBreaks = (values, nClasses) => {
6
+ // q-th quantile of a data set:
7
+ // value where q fraction of data is below and (1-q) fraction is above this value
8
+ // Xq = (1 - r) * X_NI1 + r * X_NI2
9
+ // NI1 = (int) (q * (n+1))
10
+ // NI2 = NI1 + 1
11
+ // r = q * (n+1) - (int) (q * (n+1))
12
+ // (indices of X: 1...n)
13
+ const sortedValues = [...values].sort((a, b) => a - b);
14
+ const breaks = [];
15
+ if (!sortedValues) {
16
+ return [];
17
+ }
18
+ const n = sortedValues.length;
19
+ let xq = n > 0 ? sortedValues[0] : 0;
20
+ for (let i = 1; i < nClasses; i++) {
21
+ if (n > 1) {
22
+ const q = i / nClasses;
23
+ const a = q * (n - 1);
24
+ const aa = Math.floor(a);
25
+ const r = a - aa;
26
+ xq = (1 - r) * sortedValues[aa] + r * sortedValues[aa + 1];
27
+ }
28
+ breaks.push(xq);
29
+ }
30
+ breaks.push(sortedValues[n - 1]);
31
+ return breaks;
32
+ };
33
+ VectorClassifications.calculateEqualIntervalBreaks = (values, nClasses) => {
34
+ const minimum = Math.min(...values);
35
+ const maximum = Math.max(...values);
36
+ const breaks = [];
37
+ const step = (maximum - minimum) / nClasses;
38
+ let value = minimum;
39
+ for (let i = 0; i < nClasses; i++) {
40
+ value += step;
41
+ breaks.push(value);
42
+ }
43
+ breaks[nClasses - 1] = maximum;
44
+ return breaks;
45
+ };
46
+ VectorClassifications.calculateJenksBreaks = (values, nClasses) => {
47
+ const maximum = Math.max(...values);
48
+ if (values.length === 0) {
49
+ return [];
50
+ }
51
+ if (nClasses <= 1) {
52
+ return [maximum];
53
+ }
54
+ if (nClasses >= values.length) {
55
+ return values;
56
+ }
57
+ const sample = [...values].sort((a, b) => a - b);
58
+ const n = sample.length;
59
+ const matrixOne = Array.from({ length: n + 1 }, () => Array(nClasses + 1).fill(0));
60
+ const matrixTwo = Array.from({ length: n + 1 }, () => Array(nClasses + 1).fill(Number.MAX_VALUE));
61
+ for (let i = 1; i <= nClasses; i++) {
62
+ matrixOne[0][i] = 1;
63
+ matrixOne[1][i] = 1;
64
+ matrixTwo[0][i] = 0.0;
65
+ for (let j = 2; j <= n; j++) {
66
+ matrixTwo[j][i] = Number.MAX_VALUE;
67
+ }
68
+ }
69
+ for (let l = 2; l <= n; l++) {
70
+ let s1 = 0.0;
71
+ let s2 = 0.0;
72
+ let w = 0;
73
+ let v = 0.0;
74
+ for (let m = 1; m <= l; m++) {
75
+ const i3 = l - m + 1;
76
+ const val = sample[i3 - 1];
77
+ s2 += val * val;
78
+ s1 += val;
79
+ w++;
80
+ v = s2 - (s1 * s1) / w;
81
+ const i4 = i3 - 1;
82
+ if (i4 !== 0) {
83
+ for (let j = 2; j <= nClasses; j++) {
84
+ if (matrixTwo[l][j] >= v + matrixTwo[i4][j - 1]) {
85
+ matrixOne[l][j] = i4;
86
+ matrixTwo[l][j] = v + matrixTwo[i4][j - 1];
87
+ }
88
+ }
89
+ }
90
+ }
91
+ matrixOne[l][1] = 1;
92
+ matrixTwo[l][1] = v;
93
+ }
94
+ const breaks = Array(nClasses);
95
+ breaks[nClasses - 1] = sample[n - 1];
96
+ for (let j = nClasses, k = n; j >= 2; j--) {
97
+ const id = matrixOne[k][j] - 1;
98
+ breaks[j - 2] = sample[id];
99
+ k = matrixOne[k][j] - 1;
100
+ }
101
+ return breaks;
102
+ };
103
+ VectorClassifications.calculatePrettyBreaks = (values, nClasses) => {
104
+ const minimum = Math.min(...values);
105
+ const maximum = Math.max(...values);
106
+ const breaks = [];
107
+ if (nClasses < 1) {
108
+ breaks.push(maximum);
109
+ return breaks;
110
+ }
111
+ const minimumCount = Math.floor(nClasses / 3);
112
+ const shrink = 0.75;
113
+ const highBias = 1.5;
114
+ const adjustBias = 0.5 + 1.5 * highBias;
115
+ const divisions = nClasses;
116
+ const h = highBias;
117
+ let cell;
118
+ let small = false;
119
+ const dx = maximum - minimum;
120
+ let U;
121
+ cell = Math.max(Math.abs(minimum), Math.abs(maximum));
122
+ if (adjustBias >= 1.5 * h + 0.5) {
123
+ U = 1 + 1.0 / (1 + h);
124
+ }
125
+ else {
126
+ U = 1 + 1.5 / (1 + adjustBias);
127
+ }
128
+ small = dx < cell * U * Math.max(1, divisions) * 1e-7 * 3.0;
129
+ if (small) {
130
+ if (cell > 10) {
131
+ cell = 9 + cell / 10;
132
+ cell = cell * shrink;
133
+ }
134
+ if (minimumCount > 1) {
135
+ cell = cell / minimumCount;
136
+ }
137
+ }
138
+ else {
139
+ cell = dx;
140
+ if (divisions > 1) {
141
+ cell = cell / divisions;
142
+ }
143
+ }
144
+ if (cell < 20 * 1e-7) {
145
+ cell = 20 * 1e-7;
146
+ }
147
+ const base = Math.pow(10.0, Math.floor(Math.log10(cell)));
148
+ let unit = base;
149
+ if (2 * base - cell < h * (cell - unit)) {
150
+ unit = 2.0 * base;
151
+ if (5 * base - cell < adjustBias * (cell - unit)) {
152
+ unit = 5.0 * base;
153
+ if (10.0 * base - cell < h * (cell - unit)) {
154
+ unit = 10.0 * base;
155
+ }
156
+ }
157
+ }
158
+ let start = Math.floor(minimum / unit + 1e-7);
159
+ let end = Math.ceil(maximum / unit - 1e-7);
160
+ while (start * unit > minimum + 1e-7 * unit) {
161
+ start = start - 1;
162
+ }
163
+ while (end * unit < maximum - 1e-7 * unit) {
164
+ end = end + 1;
165
+ }
166
+ let k = Math.floor(0.5 + end - start);
167
+ if (k < minimumCount) {
168
+ k = minimumCount - k;
169
+ if (start >= 0) {
170
+ end = end + k / 2;
171
+ start = start - k / 2 + (k % 2);
172
+ }
173
+ else {
174
+ start = start - k / 2;
175
+ end = end + k / 2 + (k % 2);
176
+ }
177
+ }
178
+ const minimumBreak = start * unit;
179
+ const count = end - start;
180
+ for (let i = 1; i < count + 1; i++) {
181
+ breaks.push(minimumBreak + i * unit);
182
+ }
183
+ if (breaks.length === 0) {
184
+ return breaks;
185
+ }
186
+ if (breaks[0] < minimum) {
187
+ breaks[0] = minimum;
188
+ }
189
+ if (breaks[breaks.length - 1] > maximum) {
190
+ breaks[breaks.length - 1] = maximum;
191
+ }
192
+ if (minimum < 0.0 && maximum > 0.0) {
193
+ const breaksMinusZero = breaks.map(b => b - 0.0);
194
+ let posOfMin = 0;
195
+ for (let i = 1; i < breaks.length; i++) {
196
+ if (Math.abs(breaksMinusZero[i]) < Math.abs(breaksMinusZero[posOfMin])) {
197
+ posOfMin = i;
198
+ }
199
+ }
200
+ breaks[posOfMin] = 0.0; // Set the closest break to zero
201
+ }
202
+ return breaks;
203
+ };
204
+ VectorClassifications.calculateLogarithmicBreaks = (values, nClasses) => {
205
+ const minimum = Math.min(...values);
206
+ const maximum = Math.max(...values);
207
+ let positiveMinimum = Number.MAX_VALUE;
208
+ let breaks = [];
209
+ positiveMinimum = minimum;
210
+ const actualLogMin = Math.log10(positiveMinimum);
211
+ let logMin = Math.floor(actualLogMin);
212
+ const logMax = Math.ceil(Math.log10(maximum));
213
+ let prettyBreaks = VectorClassifications.calculatePrettyBreaks([logMin, logMax], nClasses);
214
+ while (prettyBreaks.length > 0 && prettyBreaks[0] < actualLogMin) {
215
+ logMin += 1.0;
216
+ prettyBreaks = VectorClassifications.calculatePrettyBreaks([logMin, logMax], nClasses);
217
+ }
218
+ breaks = prettyBreaks;
219
+ for (let i = 0; i < breaks.length; i++) {
220
+ breaks[i] = Math.pow(10, breaks[i]);
221
+ }
222
+ return breaks;
223
+ };
224
+ })(VectorClassifications || (VectorClassifications = {}));
225
+ export var GeoTiffClassifications;
226
+ (function (GeoTiffClassifications) {
227
+ GeoTiffClassifications.classifyQuantileBreaks = async (nClasses, bandNumber, url, colorRampType) => {
228
+ const breaks = [];
229
+ const isDiscrete = colorRampType === 'discrete';
230
+ const pool = new Pool();
231
+ const tiff = await fromUrl(url);
232
+ const image = await tiff.getImage();
233
+ const values = await image.readRasters({ pool });
234
+ // Band numbers are 1 indexed
235
+ const bandValues = values[bandNumber - 1];
236
+ const bandSortedValues = bandValues
237
+ .filter(value => value !== 0)
238
+ .sort((a, b) => a - b);
239
+ pool.destroy();
240
+ if (!bandSortedValues) {
241
+ return [];
242
+ }
243
+ // Adapted from https://github.com/GeoTIFF/geoblaze/blob/master/src/histogram/histogram.core.js#L64
244
+ // iterate through values and use a counter to
245
+ // decide when to set up the next bin.
246
+ let numValuesInCurrentBin;
247
+ let valuesPerBin;
248
+ let startIndex;
249
+ if (isDiscrete) {
250
+ valuesPerBin = bandSortedValues.length / nClasses;
251
+ numValuesInCurrentBin = 0;
252
+ startIndex = 0;
253
+ }
254
+ else {
255
+ valuesPerBin = bandSortedValues.length / (nClasses - 1);
256
+ breaks.push(1);
257
+ numValuesInCurrentBin = 1;
258
+ startIndex = 1;
259
+ }
260
+ for (let i = startIndex; i < bandSortedValues.length; i++) {
261
+ if (numValuesInCurrentBin + 1 < valuesPerBin) {
262
+ numValuesInCurrentBin++;
263
+ }
264
+ else {
265
+ breaks.push(bandSortedValues[i]);
266
+ numValuesInCurrentBin = 0;
267
+ }
268
+ }
269
+ if (breaks.length !== nClasses) {
270
+ //TODO: This should be set based on the type of bandSortedValues I think
271
+ breaks.push(65535);
272
+ }
273
+ return breaks;
274
+ };
275
+ GeoTiffClassifications.classifyContinuousBreaks = (nClasses, minimumValue, maximumValue, colorRampType) => {
276
+ const min = minimumValue;
277
+ const max = maximumValue;
278
+ if (min > max) {
279
+ return [];
280
+ }
281
+ const isDiscrete = colorRampType === 'discrete';
282
+ const breaks = [];
283
+ const numberOfEntries = nClasses;
284
+ if (isDiscrete) {
285
+ const intervalDiff = ((max - min) * (numberOfEntries - 1)) / numberOfEntries;
286
+ for (let i = 1; i < numberOfEntries; i++) {
287
+ const val = i / numberOfEntries;
288
+ breaks.push(min + val * intervalDiff);
289
+ }
290
+ breaks.push(max);
291
+ }
292
+ else {
293
+ for (let i = 0; i <= numberOfEntries; i++) {
294
+ if (i === 26) {
295
+ continue;
296
+ }
297
+ const val = i / numberOfEntries;
298
+ breaks.push(min + val * (max - min));
299
+ }
300
+ }
301
+ return breaks;
302
+ };
303
+ GeoTiffClassifications.classifyEqualIntervalBreaks = (nClasses, minimumValue, maximumValue, colorRampType) => {
304
+ const min = minimumValue;
305
+ const max = maximumValue;
306
+ if (min > max) {
307
+ return [];
308
+ }
309
+ const isDiscrete = colorRampType === 'discrete';
310
+ const breaks = [];
311
+ if (isDiscrete) {
312
+ const intervalDiff = (max - min) / nClasses;
313
+ for (let i = 1; i < nClasses; i++) {
314
+ breaks.push(min + i * intervalDiff);
315
+ }
316
+ breaks.push(max);
317
+ }
318
+ else {
319
+ const intervalDiff = (max - min) / (nClasses - 1);
320
+ for (let i = 0; i < nClasses; i++) {
321
+ breaks.push(min + i * intervalDiff);
322
+ }
323
+ }
324
+ return breaks;
325
+ };
326
+ })(GeoTiffClassifications || (GeoTiffClassifications = {}));
package/lib/commands.js CHANGED
@@ -2,7 +2,7 @@ import { showErrorMessage } from '@jupyterlab/apputils';
2
2
  import { CommandIDs, icons } from './constants';
3
3
  import { CreationFormDialog } from './dialogs/formdialog';
4
4
  import { LayerBrowserWidget } from './dialogs/layerBrowserDialog';
5
- import { SymbologyWidget } from './dialogs/symbologyDialog';
5
+ import { SymbologyWidget } from './dialogs/symbology/symbologyDialog';
6
6
  /**
7
7
  * Add the commands to the application's command registry.
8
8
  */
@@ -0,0 +1,13 @@
1
+ import { InterpolationType } from './tiff_layer/types/SingleBandPseudoColor';
2
+ export declare namespace VectorClassifications {
3
+ const calculateQuantileBreaks: (values: number[], nClasses: number) => number[];
4
+ const calculateEqualIntervalBreaks: (values: number[], nClasses: number) => number[];
5
+ const calculateJenksBreaks: (values: number[], nClasses: number) => any[];
6
+ const calculatePrettyBreaks: (values: number[], nClasses: number) => number[];
7
+ const calculateLogarithmicBreaks: (values: number[], nClasses: number) => number[];
8
+ }
9
+ export declare namespace GeoTiffClassifications {
10
+ const classifyQuantileBreaks: (nClasses: number, bandNumber: number, url: string, colorRampType: string) => Promise<number[]>;
11
+ const classifyContinuousBreaks: (nClasses: number, minimumValue: number, maximumValue: number, colorRampType: InterpolationType) => number[];
12
+ const classifyEqualIntervalBreaks: (nClasses: number, minimumValue: number, maximumValue: number, colorRampType: InterpolationType) => number[];
13
+ }
@@ -0,0 +1,326 @@
1
+ // Adapted from https://github.com/qgis/QGIS/blob/master/src/core/classification/
2
+ import { Pool, fromUrl } from 'geotiff';
3
+ export var VectorClassifications;
4
+ (function (VectorClassifications) {
5
+ VectorClassifications.calculateQuantileBreaks = (values, nClasses) => {
6
+ // q-th quantile of a data set:
7
+ // value where q fraction of data is below and (1-q) fraction is above this value
8
+ // Xq = (1 - r) * X_NI1 + r * X_NI2
9
+ // NI1 = (int) (q * (n+1))
10
+ // NI2 = NI1 + 1
11
+ // r = q * (n+1) - (int) (q * (n+1))
12
+ // (indices of X: 1...n)
13
+ const sortedValues = [...values].sort((a, b) => a - b);
14
+ const breaks = [];
15
+ if (!sortedValues) {
16
+ return [];
17
+ }
18
+ const n = sortedValues.length;
19
+ let xq = n > 0 ? sortedValues[0] : 0;
20
+ for (let i = 1; i < nClasses; i++) {
21
+ if (n > 1) {
22
+ const q = i / nClasses;
23
+ const a = q * (n - 1);
24
+ const aa = Math.floor(a);
25
+ const r = a - aa;
26
+ xq = (1 - r) * sortedValues[aa] + r * sortedValues[aa + 1];
27
+ }
28
+ breaks.push(xq);
29
+ }
30
+ breaks.push(sortedValues[n - 1]);
31
+ return breaks;
32
+ };
33
+ VectorClassifications.calculateEqualIntervalBreaks = (values, nClasses) => {
34
+ const minimum = Math.min(...values);
35
+ const maximum = Math.max(...values);
36
+ const breaks = [];
37
+ const step = (maximum - minimum) / nClasses;
38
+ let value = minimum;
39
+ for (let i = 0; i < nClasses; i++) {
40
+ value += step;
41
+ breaks.push(value);
42
+ }
43
+ breaks[nClasses - 1] = maximum;
44
+ return breaks;
45
+ };
46
+ VectorClassifications.calculateJenksBreaks = (values, nClasses) => {
47
+ const maximum = Math.max(...values);
48
+ if (values.length === 0) {
49
+ return [];
50
+ }
51
+ if (nClasses <= 1) {
52
+ return [maximum];
53
+ }
54
+ if (nClasses >= values.length) {
55
+ return values;
56
+ }
57
+ const sample = [...values].sort((a, b) => a - b);
58
+ const n = sample.length;
59
+ const matrixOne = Array.from({ length: n + 1 }, () => Array(nClasses + 1).fill(0));
60
+ const matrixTwo = Array.from({ length: n + 1 }, () => Array(nClasses + 1).fill(Number.MAX_VALUE));
61
+ for (let i = 1; i <= nClasses; i++) {
62
+ matrixOne[0][i] = 1;
63
+ matrixOne[1][i] = 1;
64
+ matrixTwo[0][i] = 0.0;
65
+ for (let j = 2; j <= n; j++) {
66
+ matrixTwo[j][i] = Number.MAX_VALUE;
67
+ }
68
+ }
69
+ for (let l = 2; l <= n; l++) {
70
+ let s1 = 0.0;
71
+ let s2 = 0.0;
72
+ let w = 0;
73
+ let v = 0.0;
74
+ for (let m = 1; m <= l; m++) {
75
+ const i3 = l - m + 1;
76
+ const val = sample[i3 - 1];
77
+ s2 += val * val;
78
+ s1 += val;
79
+ w++;
80
+ v = s2 - (s1 * s1) / w;
81
+ const i4 = i3 - 1;
82
+ if (i4 !== 0) {
83
+ for (let j = 2; j <= nClasses; j++) {
84
+ if (matrixTwo[l][j] >= v + matrixTwo[i4][j - 1]) {
85
+ matrixOne[l][j] = i4;
86
+ matrixTwo[l][j] = v + matrixTwo[i4][j - 1];
87
+ }
88
+ }
89
+ }
90
+ }
91
+ matrixOne[l][1] = 1;
92
+ matrixTwo[l][1] = v;
93
+ }
94
+ const breaks = Array(nClasses);
95
+ breaks[nClasses - 1] = sample[n - 1];
96
+ for (let j = nClasses, k = n; j >= 2; j--) {
97
+ const id = matrixOne[k][j] - 1;
98
+ breaks[j - 2] = sample[id];
99
+ k = matrixOne[k][j] - 1;
100
+ }
101
+ return breaks;
102
+ };
103
+ VectorClassifications.calculatePrettyBreaks = (values, nClasses) => {
104
+ const minimum = Math.min(...values);
105
+ const maximum = Math.max(...values);
106
+ const breaks = [];
107
+ if (nClasses < 1) {
108
+ breaks.push(maximum);
109
+ return breaks;
110
+ }
111
+ const minimumCount = Math.floor(nClasses / 3);
112
+ const shrink = 0.75;
113
+ const highBias = 1.5;
114
+ const adjustBias = 0.5 + 1.5 * highBias;
115
+ const divisions = nClasses;
116
+ const h = highBias;
117
+ let cell;
118
+ let small = false;
119
+ const dx = maximum - minimum;
120
+ let U;
121
+ cell = Math.max(Math.abs(minimum), Math.abs(maximum));
122
+ if (adjustBias >= 1.5 * h + 0.5) {
123
+ U = 1 + 1.0 / (1 + h);
124
+ }
125
+ else {
126
+ U = 1 + 1.5 / (1 + adjustBias);
127
+ }
128
+ small = dx < cell * U * Math.max(1, divisions) * 1e-7 * 3.0;
129
+ if (small) {
130
+ if (cell > 10) {
131
+ cell = 9 + cell / 10;
132
+ cell = cell * shrink;
133
+ }
134
+ if (minimumCount > 1) {
135
+ cell = cell / minimumCount;
136
+ }
137
+ }
138
+ else {
139
+ cell = dx;
140
+ if (divisions > 1) {
141
+ cell = cell / divisions;
142
+ }
143
+ }
144
+ if (cell < 20 * 1e-7) {
145
+ cell = 20 * 1e-7;
146
+ }
147
+ const base = Math.pow(10.0, Math.floor(Math.log10(cell)));
148
+ let unit = base;
149
+ if (2 * base - cell < h * (cell - unit)) {
150
+ unit = 2.0 * base;
151
+ if (5 * base - cell < adjustBias * (cell - unit)) {
152
+ unit = 5.0 * base;
153
+ if (10.0 * base - cell < h * (cell - unit)) {
154
+ unit = 10.0 * base;
155
+ }
156
+ }
157
+ }
158
+ let start = Math.floor(minimum / unit + 1e-7);
159
+ let end = Math.ceil(maximum / unit - 1e-7);
160
+ while (start * unit > minimum + 1e-7 * unit) {
161
+ start = start - 1;
162
+ }
163
+ while (end * unit < maximum - 1e-7 * unit) {
164
+ end = end + 1;
165
+ }
166
+ let k = Math.floor(0.5 + end - start);
167
+ if (k < minimumCount) {
168
+ k = minimumCount - k;
169
+ if (start >= 0) {
170
+ end = end + k / 2;
171
+ start = start - k / 2 + (k % 2);
172
+ }
173
+ else {
174
+ start = start - k / 2;
175
+ end = end + k / 2 + (k % 2);
176
+ }
177
+ }
178
+ const minimumBreak = start * unit;
179
+ const count = end - start;
180
+ for (let i = 1; i < count + 1; i++) {
181
+ breaks.push(minimumBreak + i * unit);
182
+ }
183
+ if (breaks.length === 0) {
184
+ return breaks;
185
+ }
186
+ if (breaks[0] < minimum) {
187
+ breaks[0] = minimum;
188
+ }
189
+ if (breaks[breaks.length - 1] > maximum) {
190
+ breaks[breaks.length - 1] = maximum;
191
+ }
192
+ if (minimum < 0.0 && maximum > 0.0) {
193
+ const breaksMinusZero = breaks.map(b => b - 0.0);
194
+ let posOfMin = 0;
195
+ for (let i = 1; i < breaks.length; i++) {
196
+ if (Math.abs(breaksMinusZero[i]) < Math.abs(breaksMinusZero[posOfMin])) {
197
+ posOfMin = i;
198
+ }
199
+ }
200
+ breaks[posOfMin] = 0.0; // Set the closest break to zero
201
+ }
202
+ return breaks;
203
+ };
204
+ VectorClassifications.calculateLogarithmicBreaks = (values, nClasses) => {
205
+ const minimum = Math.min(...values);
206
+ const maximum = Math.max(...values);
207
+ let positiveMinimum = Number.MAX_VALUE;
208
+ let breaks = [];
209
+ positiveMinimum = minimum;
210
+ const actualLogMin = Math.log10(positiveMinimum);
211
+ let logMin = Math.floor(actualLogMin);
212
+ const logMax = Math.ceil(Math.log10(maximum));
213
+ let prettyBreaks = VectorClassifications.calculatePrettyBreaks([logMin, logMax], nClasses);
214
+ while (prettyBreaks.length > 0 && prettyBreaks[0] < actualLogMin) {
215
+ logMin += 1.0;
216
+ prettyBreaks = VectorClassifications.calculatePrettyBreaks([logMin, logMax], nClasses);
217
+ }
218
+ breaks = prettyBreaks;
219
+ for (let i = 0; i < breaks.length; i++) {
220
+ breaks[i] = Math.pow(10, breaks[i]);
221
+ }
222
+ return breaks;
223
+ };
224
+ })(VectorClassifications || (VectorClassifications = {}));
225
+ export var GeoTiffClassifications;
226
+ (function (GeoTiffClassifications) {
227
+ GeoTiffClassifications.classifyQuantileBreaks = async (nClasses, bandNumber, url, colorRampType) => {
228
+ const breaks = [];
229
+ const isDiscrete = colorRampType === 'discrete';
230
+ const pool = new Pool();
231
+ const tiff = await fromUrl(url);
232
+ const image = await tiff.getImage();
233
+ const values = await image.readRasters({ pool });
234
+ // Band numbers are 1 indexed
235
+ const bandValues = values[bandNumber - 1];
236
+ const bandSortedValues = bandValues
237
+ .filter(value => value !== 0)
238
+ .sort((a, b) => a - b);
239
+ pool.destroy();
240
+ if (!bandSortedValues) {
241
+ return [];
242
+ }
243
+ // Adapted from https://github.com/GeoTIFF/geoblaze/blob/master/src/histogram/histogram.core.js#L64
244
+ // iterate through values and use a counter to
245
+ // decide when to set up the next bin.
246
+ let numValuesInCurrentBin;
247
+ let valuesPerBin;
248
+ let startIndex;
249
+ if (isDiscrete) {
250
+ valuesPerBin = bandSortedValues.length / nClasses;
251
+ numValuesInCurrentBin = 0;
252
+ startIndex = 0;
253
+ }
254
+ else {
255
+ valuesPerBin = bandSortedValues.length / (nClasses - 1);
256
+ breaks.push(1);
257
+ numValuesInCurrentBin = 1;
258
+ startIndex = 1;
259
+ }
260
+ for (let i = startIndex; i < bandSortedValues.length; i++) {
261
+ if (numValuesInCurrentBin + 1 < valuesPerBin) {
262
+ numValuesInCurrentBin++;
263
+ }
264
+ else {
265
+ breaks.push(bandSortedValues[i]);
266
+ numValuesInCurrentBin = 0;
267
+ }
268
+ }
269
+ if (breaks.length !== nClasses) {
270
+ //TODO: This should be set based on the type of bandSortedValues I think
271
+ breaks.push(65535);
272
+ }
273
+ return breaks;
274
+ };
275
+ GeoTiffClassifications.classifyContinuousBreaks = (nClasses, minimumValue, maximumValue, colorRampType) => {
276
+ const min = minimumValue;
277
+ const max = maximumValue;
278
+ if (min > max) {
279
+ return [];
280
+ }
281
+ const isDiscrete = colorRampType === 'discrete';
282
+ const breaks = [];
283
+ const numberOfEntries = nClasses;
284
+ if (isDiscrete) {
285
+ const intervalDiff = ((max - min) * (numberOfEntries - 1)) / numberOfEntries;
286
+ for (let i = 1; i < numberOfEntries; i++) {
287
+ const val = i / numberOfEntries;
288
+ breaks.push(min + val * intervalDiff);
289
+ }
290
+ breaks.push(max);
291
+ }
292
+ else {
293
+ for (let i = 0; i <= numberOfEntries; i++) {
294
+ if (i === 26) {
295
+ continue;
296
+ }
297
+ const val = i / numberOfEntries;
298
+ breaks.push(min + val * (max - min));
299
+ }
300
+ }
301
+ return breaks;
302
+ };
303
+ GeoTiffClassifications.classifyEqualIntervalBreaks = (nClasses, minimumValue, maximumValue, colorRampType) => {
304
+ const min = minimumValue;
305
+ const max = maximumValue;
306
+ if (min > max) {
307
+ return [];
308
+ }
309
+ const isDiscrete = colorRampType === 'discrete';
310
+ const breaks = [];
311
+ if (isDiscrete) {
312
+ const intervalDiff = (max - min) / nClasses;
313
+ for (let i = 1; i < nClasses; i++) {
314
+ breaks.push(min + i * intervalDiff);
315
+ }
316
+ breaks.push(max);
317
+ }
318
+ else {
319
+ const intervalDiff = (max - min) / (nClasses - 1);
320
+ for (let i = 0; i < nClasses; i++) {
321
+ breaks.push(min + i * intervalDiff);
322
+ }
323
+ }
324
+ return breaks;
325
+ };
326
+ })(GeoTiffClassifications || (GeoTiffClassifications = {}));