Skip to content

Commit

Permalink
fix: tileindex-validate errors should not been thrown asap TDE-1013 (#…
Browse files Browse the repository at this point in the history
…877)

#### Motivation

In order to have a better overview of the errors, errors should be
collected and logged at the end rathen than thrown ASAP.

#### Modification

- Avoid throwing error as soon as they're detected
- Couple of refactors to simplify the code

#### Checklist

- [ ] Tests updated N/A
- [ ] Docs updated N/A
- [x] Issue linked in Title
  • Loading branch information
paulfouquet authored Feb 19, 2024
1 parent 43056b1 commit 0f95e4f
Showing 1 changed file with 49 additions and 19 deletions.
68 changes: 49 additions & 19 deletions src/commands/tileindex-validate/tileindex.validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,11 @@ export const commandTileIndexValidate = command({

// Validate that all tiffs align to tile grid
if (args.validate) {
let validationFailed = false;
let allValid = true;
for (const tiff of locations) {
const ret = validateTiffAlignment(tiff);
if (ret === true) continue;
logger.error({ reason: ret.message, source: tiff.source }, 'TileInvalid:Validation:Failed');
validationFailed = true;
allValid = allValid && validateTiffAlignment(tiff);
}
if (validationFailed) throw new Error(`Tile alignment validation failed`);
if (!allValid) throw new Error(`Tile alignment validation failed`);
}

if (retileNeeded) throw new Error(`Duplicate files found, see output.geojson`);
Expand Down Expand Up @@ -295,15 +292,26 @@ export async function extractTiffLocations(
const bbox = await findBoundingBox(tiff);

const sourceEpsg = forceSourceEpsg ?? tiff.images[0]?.epsg;
if (sourceEpsg == null) throw new Error(`EPSG is missing: ${tiff.source.url}`);
if (sourceEpsg == null) {
logger.error({ reason: 'EPSG is missing', source: tiff.source }, 'MissingEPSG:ExtracTiffLocations:Failed');
return null;
}

const centerX = (bbox[0] + bbox[2]) / 2;
const centerY = (bbox[1] + bbox[3]) / 2;
// bbox is not epsg:2193
const targetProjection = Projection.get(2193);
const sourceProjection = Projection.get(sourceEpsg);

const [x, y] = targetProjection.fromWgs84(sourceProjection.toWgs84([centerX, centerY]));
if (x == null || y == null) throw new Error(`Failed to reproject point: ${tiff.source.url}`);
if (x == null || y == null) {
logger.error(
{ reason: 'Failed to reproject point', source: tiff.source },
'Reprojection:ExtracTiffLocations:Failed',
);
return null;
}

// Tilename from center
const tileName = getTileName(x, y, gridSize);

Expand All @@ -314,7 +322,7 @@ export async function extractTiffLocations(
// }
return { bbox, source: tiff.source.url.href, tileName, epsg: tiff.images[0]?.epsg };
} catch (e) {
console.log(tiff.source.url, e);
logger.error({ reason: e, source: tiff.source }, 'ExtractTiffLocation:Failed');
return null;
} finally {
await tiff.source.close?.();
Expand All @@ -330,25 +338,47 @@ export function getSize(extent: [number, number, number, number]): Size {
return { width: extent[2] - extent[0], height: extent[3] - extent[1] };
}

export function validateTiffAlignment(tiff: TiffLocation, allowedError = 0.015): true | Error {
export function validateTiffAlignment(tiff: TiffLocation, allowedError = 0.015): boolean {
const mapTileIndex = MapSheet.getMapTileIndex(tiff.tileName);
if (mapTileIndex == null) throw new Error('Failed to extract bounding box from: ' + tiff.tileName);
if (mapTileIndex == null) {
logger.error(
{ reason: `Failed to extract bounding box from: ${tiff.tileName}`, source: tiff.source },
'TileInvalid:Validation:Failed',
);
return false;
}
// Top Left
const errX = Math.abs(tiff.bbox[0] - mapTileIndex.bbox[0]);
const errY = Math.abs(tiff.bbox[3] - mapTileIndex.bbox[3]);
if (errX > allowedError || errY > allowedError)
return new Error(`The origin is invalid x:${tiff.bbox[0]}, y:${tiff.bbox[3]} source:${tiff.source}`);
if (errX > allowedError || errY > allowedError) {
logger.error(
{ reason: `The origin is invalid x:${tiff.bbox[0]}, y:${tiff.bbox[3]}`, source: tiff.source },
'TileInvalid:Validation:Failed',
);
return false;
}

// TODO do we validate bottom right
const tiffSize = getSize(tiff.bbox);
if (tiffSize.width !== mapTileIndex.width)
return new Error(
`Tiff size is invalid width:${tiffSize.width}, expected:${mapTileIndex.width} source:${tiff.source}`,
if (tiffSize.width !== mapTileIndex.width) {
logger.error(
{ reason: `Tiff size is invalid width:${tiffSize.width}, expected:${mapTileIndex.width}`, source: tiff.source },
'TileInvalid:Validation:Failed',
);
if (tiffSize.height !== mapTileIndex.height)
return new Error(
`Tiff size is invalid height:${tiffSize.height}, expected:${mapTileIndex.height} source:${tiff.source}`,
return false;
}

if (tiffSize.height !== mapTileIndex.height) {
logger.error(
{
reason: `Tiff size is invalid height:${tiffSize.height}, expected:${mapTileIndex.height}`,
source: tiff.source,
},
'TileInvalid:Validation:Failed',
);
return false;
}

return true;
}

Expand Down

0 comments on commit 0f95e4f

Please sign in to comment.