11import assert from 'node:assert' ;
22import { before , beforeEach , describe , it } from 'node:test' ;
33
4+ import { Projection } from '@basemaps/geo' ;
45import { fsa } from '@chunkd/fs' ;
56import { FsMemory } from '@chunkd/source-memory' ;
7+ import { BBox } from '@linzjs/geojson' ;
68import { FeatureCollection } from 'geojson' ;
79
810import { MapSheetData } from '../../../utils/__test__/mapsheet.data.js' ;
@@ -14,6 +16,7 @@ import {
1416 getTileName ,
1517 GridSizeFromString ,
1618 groupByTileName ,
19+ reprojectIfNeeded ,
1720 TiffLoader ,
1821 validate8BitsTiff ,
1922} from '../tileindex.validate.js' ;
@@ -89,8 +92,8 @@ describe('tiffLocation', () => {
8992 TiffAy29 . images [ 0 ] . origin [ 0 ] = 1684000 ;
9093 TiffAy29 . images [ 0 ] . origin [ 1 ] = 6018000 ;
9194 const location = await extractTiffLocations ( [ TiffAs21 , TiffAy29 ] , 1000 ) ;
92- assert . equal ( location [ 0 ] ?. tileName , 'AS21_1000_0101' ) ;
93- assert . equal ( location [ 1 ] ?. tileName , 'AY29_1000_0101' ) ;
95+ assert . deepEqual ( location [ 0 ] ?. tileNames , [ 'AS21_1000_0101' ] ) ;
96+ assert . deepEqual ( location [ 1 ] ?. tileNames , [ 'AY29_1000_0101' ] ) ;
9497 } ) ;
9598
9699 it ( 'should find duplicates' , async ( ) => {
@@ -118,7 +121,7 @@ describe('tiffLocation', () => {
118121 TiffAy29 . images [ 0 ] . origin [ 0 ] = 19128043.69337794 ;
119122 TiffAy29 . images [ 0 ] . origin [ 1 ] = - 4032710.6009459053 ;
120123 const location = await extractTiffLocations ( [ TiffAy29 ] , 1000 ) ;
121- assert . equal ( location [ 0 ] ?. tileName , 'AS21_1000_0101' ) ;
124+ assert . deepEqual ( location [ 0 ] ?. tileNames , [ 'AS21_1000_0101' ] ) ;
122125 } ) ;
123126
124127 it ( 'should fail if one location is not extracted' , async ( ) => {
@@ -325,3 +328,49 @@ describe('is8BitsTiff', () => {
325328 } ) ;
326329 } ) ;
327330} ) ;
331+
332+ describe ( 'TiffFromMisalignedTiff' , ( ) => {
333+ it ( 'should properly identify all tiles under a tiff not aligned to our grid' , async ( ) => {
334+ const fakeTiffCover4 = FakeCogTiff . fromTileName ( 'CJ09' ) ;
335+ fakeTiffCover4 . images [ 0 ] . origin [ 0 ] -= 10 ;
336+ fakeTiffCover4 . images [ 0 ] . origin [ 1 ] += 10 ;
337+ const fakeTiffCover9 = FakeCogTiff . fromTileName ( 'BA33' ) ;
338+ fakeTiffCover9 . images [ 0 ] . origin [ 0 ] -= 10 ;
339+ fakeTiffCover9 . images [ 0 ] . origin [ 1 ] += 10 ;
340+ fakeTiffCover9 . images [ 0 ] . size . width += 100 ;
341+ fakeTiffCover9 . images [ 0 ] . size . height += 100 ;
342+ const fakeTiffCover3 = FakeCogTiff . fromTileName ( 'BL32' ) ;
343+ fakeTiffCover3 . images [ 0 ] . origin [ 1 ] -= 10 ;
344+ fakeTiffCover3 . images [ 0 ] . size . height *= 2 ;
345+ const locations = await extractTiffLocations ( [ fakeTiffCover4 , fakeTiffCover9 , fakeTiffCover3 ] , 50000 ) ;
346+
347+ assert . deepEqual ( locations [ 0 ] ?. tileNames , [ 'CH08' , 'CH09' , 'CJ08' , 'CJ09' ] ) ;
348+ assert . deepEqual ( locations [ 1 ] ?. tileNames , [ 'AZ32' , 'AZ33' , 'AZ34' , 'BA32' , 'BA33' , 'BA34' , 'BB32' , 'BB33' , 'BB34' ] ) ;
349+ assert . deepEqual ( locations [ 2 ] ?. tileNames , [ 'BL32' , 'BM32' , 'BN32' ] ) ;
350+ } ) ;
351+ } ) ;
352+
353+ describe ( 'reprojectIfNeeded' , ( ) => {
354+ it ( 'should reproject the bounding box if projections are different' , ( ) => {
355+ const sourceProjection = Projection . get ( 4326 ) ; // WGS84
356+ const targetProjection = Projection . get ( 2193 ) ; // New Zealand Transverse Mercator 2000
357+ const bbox : BBox = [ 172 , - 41 , 174 , - 38 ] ; // Example bounding box in WGS84
358+
359+ const reprojectedBbox = reprojectIfNeeded ( bbox , sourceProjection , targetProjection ) as BBox ;
360+ assert . notDeepEqual ( bbox . map ( Math . round ) , reprojectedBbox . map ( Math . round ) ) ; // expect output to be quite different
361+ assert . ok ( reprojectedBbox ) ;
362+
363+ const roundtripBbox = reprojectIfNeeded ( reprojectedBbox , targetProjection , sourceProjection ) ;
364+ assert . deepEqual ( bbox . map ( Math . round ) , roundtripBbox ?. map ( Math . round ) ) ; // expect output to be very similar (floating point / rounding errors)
365+ } ) ;
366+
367+ it ( 'should return the same bounding box if projections are the same' , ( ) => {
368+ const sourceProjection = Projection . get ( 2193 ) ; // New Zealand Transverse Mercator 2000
369+ const targetProjection = Projection . get ( 2193 ) ; // New Zealand Transverse Mercator 2000
370+ const bbox : BBox = [ 1 , 2 , 3 , 4 ] ; // Example bounding box
371+
372+ const reprojectedBbox = reprojectIfNeeded ( bbox , sourceProjection , targetProjection ) ;
373+
374+ assert . deepEqual ( reprojectedBbox , bbox ) ;
375+ } ) ;
376+ } ) ;
0 commit comments