|
| 1 | +import assert from 'node:assert'; |
| 2 | +import { describe, it } from 'node:test'; |
| 3 | + |
| 4 | +import { ImageryBandDataType, ImageryBandType } from '@basemaps/config'; |
| 5 | + |
| 6 | +import { findOptimialCoveringZoomOffset } from '../tile.cover.js'; |
| 7 | + |
| 8 | +const TileSize = 512; |
| 9 | + |
| 10 | +function rawDataSize(imageryType: ImageryBandDataType): number { |
| 11 | + switch (imageryType) { |
| 12 | + case 'uint8': |
| 13 | + return 1; |
| 14 | + case 'uint16': |
| 15 | + return 2; |
| 16 | + case 'float32': |
| 17 | + return 4; |
| 18 | + default: |
| 19 | + throw new Error(`Unknown imagery type: ${imageryType}`); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +function rawTileSize(sourceBands: ImageryBandType[]): number { |
| 24 | + let size = 0; |
| 25 | + for (const b of sourceBands) size += rawDataSize(b.type); |
| 26 | + return size * TileSize * TileSize; |
| 27 | +} |
| 28 | + |
| 29 | +function tileCount(zoomOffset: number): number { |
| 30 | + let count = 0; |
| 31 | + for (let z = 0; z < zoomOffset; z++) { |
| 32 | + // z:0 - 1x1 - 1 tile |
| 33 | + // z:1 - 2x2 - 4 tiles |
| 34 | + // z:3 - 4x4 - 16 tiles |
| 35 | + // z:3 - 8x8 - 64 tiles |
| 36 | + count = count + 2 ** z * 2 ** z; |
| 37 | + } |
| 38 | + return count; |
| 39 | +} |
| 40 | + |
| 41 | +function estimatedCogSize(zoomOffset: number, sourceBands: ImageryBandType[]): number { |
| 42 | + const tileCountValue = tileCount(zoomOffset); |
| 43 | + const tileSizeValue = rawTileSize(sourceBands); |
| 44 | + return tileCountValue * tileSizeValue; |
| 45 | +} |
| 46 | + |
| 47 | +describe('coveringOffset', () => { |
| 48 | + const uint8 = { type: 'uint8' } as const; |
| 49 | + const float32 = { type: 'float32' } as const; |
| 50 | + const uint16 = { type: 'uint16' } as const; |
| 51 | + |
| 52 | + // TODO use these estimated sizes to determine if the covering zoom level offset is ok |
| 53 | + it('should calculate raw size of a 6 level cog correctly', () => { |
| 54 | + assert.equal(tileCount(1), 1); |
| 55 | + assert.equal(tileCount(2), 1 + 4); |
| 56 | + assert.equal(tileCount(3), 1 + 4 + 16); |
| 57 | + assert.equal(tileCount(4), 1 + 4 + 16 + 64); |
| 58 | + assert.equal(tileCount(5), 1 + 4 + 16 + 64 + 256); |
| 59 | + |
| 60 | + assert.equal(estimatedCogSize(1, [uint8]), 1 * 512 * 512 * 1); |
| 61 | + assert.equal(estimatedCogSize(1, [uint16]), 1 * 512 * 512 * 2); |
| 62 | + assert.equal(estimatedCogSize(1, [uint16, uint16]), 1 * 512 * 512 * 2 * 2); |
| 63 | + assert.equal(estimatedCogSize(1, [float32]), 1 * 512 * 512 * 4); |
| 64 | + |
| 65 | + rawTileSize([uint16, uint16, uint16, uint16, uint16]); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should cover singleband uint8 or uint16 correctly', () => { |
| 69 | + for (const preset of ['lerc_1mm', 'lerc_10mm', 'zstd_17'] as const) { |
| 70 | + assert.equal(7, findOptimialCoveringZoomOffset(preset, [uint8])); |
| 71 | + assert.equal(7, findOptimialCoveringZoomOffset(preset, [uint16])); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + it('should cover a webp rgb(a) correctly', () => { |
| 76 | + assert.equal(7, findOptimialCoveringZoomOffset('webp', [uint8, uint8, uint8, uint8])); |
| 77 | + assert.equal(7, findOptimialCoveringZoomOffset('webp', [uint8, uint8, uint8])); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should cover a float32 single band correctly', () => { |
| 81 | + assert.equal(7, findOptimialCoveringZoomOffset('lerc_1mm', [float32])); |
| 82 | + assert.equal(7, findOptimialCoveringZoomOffset('lerc_10mm', [float32])); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should reduce the size with zstd compression', () => { |
| 86 | + assert.equal(6, findOptimialCoveringZoomOffset('zstd_17', [float32])); |
| 87 | + assert.equal(6, findOptimialCoveringZoomOffset('zstd_17', [uint8, uint8, uint8])); |
| 88 | + assert.equal(6, findOptimialCoveringZoomOffset('zstd_17', [uint8, uint8, uint8, uint8])); |
| 89 | + |
| 90 | + // RGBI+Alpha |
| 91 | + assert.equal(6, findOptimialCoveringZoomOffset('zstd_17', [uint8, uint8, uint8, uint8, uint8])); |
| 92 | + assert.equal(6, findOptimialCoveringZoomOffset('zstd_17', [uint16, uint16, uint16, uint16, uint16])); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should default to level 6 for lzw compression', () => { |
| 96 | + assert.equal(6, findOptimialCoveringZoomOffset('lzw', [float32])); |
| 97 | + assert.equal(6, findOptimialCoveringZoomOffset('lzw', [uint8, uint8, uint8])); |
| 98 | + assert.equal(6, findOptimialCoveringZoomOffset('lzw', [uint8, uint8, uint8, uint8])); |
| 99 | + |
| 100 | + // RGBI+Alpha - these have not been tested and are assumed to be ok |
| 101 | + assert.equal(6, findOptimialCoveringZoomOffset('lzw', [uint8, uint8, uint8, uint8, uint8])); |
| 102 | + assert.equal(6, findOptimialCoveringZoomOffset('lzw', [uint16, uint16, uint16, uint16, uint16])); |
| 103 | + }); |
| 104 | +}); |
0 commit comments