cog-tiler-wasm 0.3.3 → 0.3.5

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.
package/cog-tiler.js CHANGED
@@ -26,6 +26,7 @@ import proj4 from "proj4";
26
26
  import * as GeoTIFF from "geotiff";
27
27
  import geokeysToProj4 from "geotiff-geokeys-to-proj4";
28
28
  import { sampleWindowBilinear } from "./sampling.js";
29
+ import { computeStats } from "./statistics.js";
29
30
 
30
31
  const OS = 20037508.342789244; // Web Mercator half-extent (m)
31
32
  const TILE = 256; // output tile size (px)
@@ -65,44 +66,6 @@ function dtypeOf(lv) {
65
66
  return (f || "uint") + b;
66
67
  }
67
68
 
68
- /** Min/max/mean/std/count/valid_percent/percentiles/histogram for a band buffer. */
69
- function computeStats(buf, nodata) {
70
- let min = Infinity, max = -Infinity, sum = 0, sumsq = 0;
71
- const valid = [];
72
- for (let i = 0; i < buf.length; i++) {
73
- const v = buf[i];
74
- if (Number.isNaN(v)) continue;
75
- if (nodata != null && v === nodata) continue;
76
- if (v < min) min = v;
77
- if (v > max) max = v;
78
- sum += v;
79
- sumsq += v * v;
80
- valid.push(v);
81
- }
82
- const count = valid.length;
83
- if (count === 0) return { count: 0, valid_percent: 0 };
84
- const mean = sum / count;
85
- const std = Math.sqrt(Math.max(0, sumsq / count - mean * mean));
86
- valid.sort((a, b) => a - b);
87
- const pct = (p) => valid[Math.min(count - 1, Math.floor((p / 100) * count))];
88
- const bins = 10, span = max - min || 1, hist = new Array(bins).fill(0);
89
- for (const v of valid) {
90
- let k = Math.floor(((v - min) / span) * bins);
91
- if (k >= bins) k = bins - 1;
92
- if (k < 0) k = 0;
93
- hist[k]++;
94
- }
95
- const edges = Array.from({ length: bins + 1 }, (_, i) => min + (span * i) / bins);
96
- return {
97
- min, max, mean, std, count,
98
- valid_percent: (count / buf.length) * 100,
99
- median: pct(50),
100
- percentile_2: pct(2),
101
- percentile_98: pct(98),
102
- histogram: [hist, edges],
103
- };
104
- }
105
-
106
69
  /** Transfer curve for a rescaled value in 0..1: stretch then gamma (mirrors the
107
70
  * Rust `transfer`; reversal is colormap-only so it's not applied to RGB). */
108
71
  function transferCurve(t, stretch, gamma) {
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "Qiusheng Wu <giswqs@gmail.com>"
6
6
  ],
7
7
  "description": "Serverless Cloud Optimized GeoTIFF (COG) dynamic tiling in WebAssembly. TiTiler-style XYZ tiles, no backend.",
8
- "version": "0.3.3",
8
+ "version": "0.3.5",
9
9
  "license": "MIT",
10
10
  "repository": {
11
11
  "type": "git",
@@ -17,6 +17,7 @@
17
17
  "cog_tiler_wasm.d.ts",
18
18
  "cog-tiler.js",
19
19
  "sampling.js",
20
+ "statistics.js",
20
21
  "cog-tiler.d.ts",
21
22
  "README.md",
22
23
  "LICENSE"
@@ -38,7 +39,7 @@
38
39
  }
39
40
  },
40
41
  "peerDependencies": {
41
- "whitebox-wasm": "^0.4.1",
42
+ "whitebox-wasm": "^0.4.1 || ^0.5.1",
42
43
  "proj4": "^2.15.0",
43
44
  "geotiff": "^2.1.0 || ^3.0.0",
44
45
  "geotiff-geokeys-to-proj4": "^2024.4.13 || ^2026.8.16"
package/statistics.js ADDED
@@ -0,0 +1,44 @@
1
+ /** Number of histogram buckets exposed through the TiTiler-style statistics API. */
2
+ export const HISTOGRAM_BINS = 128;
3
+
4
+ /** Min/max/mean/std/count/valid_percent/percentiles/histogram for a band buffer. */
5
+ export function computeStats(buf, nodata) {
6
+ let min = Infinity, max = -Infinity, sum = 0, sumsq = 0;
7
+ const valid = [];
8
+ for (let i = 0; i < buf.length; i++) {
9
+ const v = buf[i];
10
+ if (Number.isNaN(v)) continue;
11
+ if (nodata != null && v === nodata) continue;
12
+ if (v < min) min = v;
13
+ if (v > max) max = v;
14
+ sum += v;
15
+ sumsq += v * v;
16
+ valid.push(v);
17
+ }
18
+ const count = valid.length;
19
+ if (count === 0) return { count: 0, valid_percent: 0 };
20
+ const mean = sum / count;
21
+ const std = Math.sqrt(Math.max(0, sumsq / count - mean * mean));
22
+ valid.sort((a, b) => a - b);
23
+ const pct = (p) => valid[Math.min(count - 1, Math.floor((p / 100) * count))];
24
+ const span = max - min || 1;
25
+ const histogram = new Array(HISTOGRAM_BINS).fill(0);
26
+ for (const v of valid) {
27
+ let bin = Math.floor(((v - min) / span) * HISTOGRAM_BINS);
28
+ if (bin >= HISTOGRAM_BINS) bin = HISTOGRAM_BINS - 1;
29
+ if (bin < 0) bin = 0;
30
+ histogram[bin]++;
31
+ }
32
+ const edges = Array.from(
33
+ { length: HISTOGRAM_BINS + 1 },
34
+ (_, i) => min + (span * i) / HISTOGRAM_BINS,
35
+ );
36
+ return {
37
+ min, max, mean, std, count,
38
+ valid_percent: (count / buf.length) * 100,
39
+ median: pct(50),
40
+ percentile_2: pct(2),
41
+ percentile_98: pct(98),
42
+ histogram: [histogram, edges],
43
+ };
44
+ }