Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: retile datasets TDE-1352 #1152

Merged
merged 12 commits into from
Dec 20, 2024
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import assert from 'node:assert';
import { before, beforeEach, describe, it } from 'node:test';

import { Projection } from '@basemaps/geo';
import { fsa } from '@chunkd/fs';
import { FsMemory } from '@chunkd/source-memory';
import { BBox } from '@linzjs/geojson';
import { FeatureCollection } from 'geojson';

import { MapSheetData } from '../../../utils/__test__/mapsheet.data.js';
Expand All @@ -14,6 +16,7 @@ import {
getTileName,
GridSizeFromString,
groupByTileName,
reprojectIfNeeded,
TiffLoader,
validate8BitsTiff,
} from '../tileindex.validate.js';
Expand Down Expand Up @@ -89,8 +92,8 @@ describe('tiffLocation', () => {
TiffAy29.images[0].origin[0] = 1684000;
TiffAy29.images[0].origin[1] = 6018000;
const location = await extractTiffLocations([TiffAs21, TiffAy29], 1000);
assert.equal(location[0]?.tileName, 'AS21_1000_0101');
assert.equal(location[1]?.tileName, 'AY29_1000_0101');
assert.deepEqual(location[0]?.tileNames, ['AS21_1000_0101']);
assert.deepEqual(location[1]?.tileNames, ['AY29_1000_0101']);
});

it('should find duplicates', async () => {
Expand Down Expand Up @@ -118,7 +121,7 @@ describe('tiffLocation', () => {
TiffAy29.images[0].origin[0] = 19128043.69337794;
TiffAy29.images[0].origin[1] = -4032710.6009459053;
const location = await extractTiffLocations([TiffAy29], 1000);
assert.equal(location[0]?.tileName, 'AS21_1000_0101');
assert.deepEqual(location[0]?.tileNames, ['AS21_1000_0101']);
});

it('should fail if one location is not extracted', async () => {
Expand Down Expand Up @@ -309,3 +312,49 @@ describe('is8BitsTiff', () => {
});
});
});

describe('TiffFromMisalignedTiff', () => {
it('should properly identify all tiles under a tiff not aligned to our grid', async () => {
const fakeTiffCover4 = FakeCogTiff.fromTileName('CJ09');
fakeTiffCover4.images[0].origin[0] -= 10;
fakeTiffCover4.images[0].origin[1] += 10;
const fakeTiffCover9 = FakeCogTiff.fromTileName('BA33');
fakeTiffCover9.images[0].origin[0] -= 10;
fakeTiffCover9.images[0].origin[1] += 10;
fakeTiffCover9.images[0].size.width += 100;
fakeTiffCover9.images[0].size.height += 100;
const fakeTiffCover3 = FakeCogTiff.fromTileName('BL32');
fakeTiffCover3.images[0].origin[1] -= 10;
fakeTiffCover3.images[0].size.height *= 2;
const locations = await extractTiffLocations([fakeTiffCover4, fakeTiffCover9, fakeTiffCover3], 50000);

assert.deepEqual(locations[0]?.tileNames, ['CH08', 'CH09', 'CJ08', 'CJ09']);
assert.deepEqual(locations[1]?.tileNames, ['AZ32', 'AZ33', 'AZ34', 'BA32', 'BA33', 'BA34', 'BB32', 'BB33', 'BB34']);
assert.deepEqual(locations[2]?.tileNames, ['BL32', 'BM32', 'BN32']);
});
});

describe('reprojectIfNeeded', () => {
it('should reproject the bounding box if projections are different', () => {
const sourceProjection = Projection.get(4326); // WGS84
const targetProjection = Projection.get(2193); // New Zealand Transverse Mercator 2000
const bbox: BBox = [172, -41, 174, -38]; // Example bounding box in WGS84

const reprojectedBbox = reprojectIfNeeded(bbox, sourceProjection, targetProjection) as BBox;
assert.notDeepEqual(bbox.map(Math.round), reprojectedBbox.map(Math.round)); // expect output to be quite different
assert.ok(reprojectedBbox);

const roundtripBbox = reprojectIfNeeded(reprojectedBbox, targetProjection, sourceProjection);
assert.deepEqual(bbox.map(Math.round), roundtripBbox?.map(Math.round)); // expect output to be very similar (floating point / rounding errors)
});

it('should return the same bounding box if projections are the same', () => {
const sourceProjection = Projection.get(2193); // New Zealand Transverse Mercator 2000
const targetProjection = Projection.get(2193); // New Zealand Transverse Mercator 2000
const bbox: BBox = [1, 2, 3, 4]; // Example bounding box

const reprojectedBbox = reprojectIfNeeded(bbox, sourceProjection, targetProjection);

assert.deepEqual(reprojectedBbox, bbox);
});
});
Loading
Loading