Skip to content

Commit

Permalink
feat: retile datasets TDE-1352 (#1152)
Browse files Browse the repository at this point in the history
#### Motivation

As a user submitting datasets I want to poorly aligned source TIFF files
to get aligned to our NZ 1:50k tile grid.

#### Modification

Updated `tileindex.validate.ts` to align input tiffs to our 1:50k tile
grid.

#### Checklist

_If not applicable, provide explanation of why._

- [x] Tests updated
- [ ] Docs updated
- [x] Issue linked in Title

---------

Co-authored-by: Blayne Chard <[email protected]>
  • Loading branch information
schmidtnz and blacha authored Dec 20, 2024
1 parent 29d1871 commit e339419
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 60 deletions.
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 @@ -325,3 +328,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

0 comments on commit e339419

Please sign in to comment.