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(cogify): error early if no source collection.json is found BM-1047 #3296

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions packages/cogify/src/cogify/cli/__test__/cli.cover.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import assert from 'node:assert';
import { beforeEach, describe, it } from 'node:test';

import { fsa, FsMemory, LogConfig } from '@basemaps/shared';
import { TestTiff } from '@basemaps/test';
import { StacCollection } from 'stac-ts';

import { BasemapsCogifyCoverCommand } from '../cli.cover.js';

describe('cli.cover', () => {
const fsMemory = new FsMemory();

beforeEach(async () => {
LogConfig.get().level = 'silent';
fsa.register('memory://', fsMemory);
fsMemory.files.clear();

await fsa.write(new URL('memory://source/google.tiff'), fsa.readStream(TestTiff.Google));
});

const baseArgs = {
paths: [new URL('memory://source/')],
target: new URL('memory://target/'),
preset: 'webp',
tileMatrix: 'WebMercatorQuad',

cutline: undefined,
cutlineBlend: 20,
baseZoomOffset: undefined,
verbose: false,
extraVerbose: false,
requireStacCollection: false,
};

it('should generate a covering', async () => {
const ret = await BasemapsCogifyCoverCommand.handler({ ...baseArgs }).catch((e) => String(e));
assert.equal(ret, undefined); // no errors returned

const files = [...fsMemory.files.keys()];
const collectionJsonPath = files.find((f) => f.endsWith('collection.json') && f.startsWith('memory://target/'));
assert.ok(collectionJsonPath);

const collectionJson = JSON.parse(String(fsMemory.files.get(collectionJsonPath)?.buffer ?? '{}')) as StacCollection;
assert.equal(collectionJson['description'], 'Missing source STAC');
});

it('should error if no collection.json is found', async () => {
const ret = await BasemapsCogifyCoverCommand.handler({
...baseArgs,
paths: [new URL('memory://source/')],
target: new URL('memory://target/'),
preset: 'webp',

requireStacCollection: true,
tileMatrix: 'WebMercatorQuad',
}).catch((e) => String(e));

assert.equal(ret, 'Error: No collection.json found with imagery: memory://source/');
});
});
12 changes: 11 additions & 1 deletion packages/cogify/src/cogify/cli/cli.cover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GoogleTms, Nztm2000QuadTms, TileId } from '@basemaps/geo';
import { fsa, urlToString } from '@basemaps/shared';
import { CliId, CliInfo } from '@basemaps/shared/build/cli/info.js';
import { Metrics } from '@linzjs/metrics';
import { command, number, oneOf, option, optional, restPositionals, string } from 'cmd-ts';
import { command, flag, number, oneOf, option, optional, restPositionals, string } from 'cmd-ts';

import { isArgo } from '../../argo.js';
import { CutlineOptimizer } from '../../cutline.js';
Expand Down Expand Up @@ -56,6 +56,12 @@ export const BasemapsCogifyCoverCommand = command({
description:
'Adjust the base zoom level of the output COGS, "-1" reduce the target output resolution by one zoom level',
}),
requireStacCollection: flag({
long: 'require-stac-collection',
description: 'Require the source dataset to have a STAC collection.json',
defaultValue: () => false,
defaultValueIsSerializable: true,
}),
},
async handler(args) {
const metrics = new Metrics();
Expand All @@ -69,6 +75,10 @@ export const BasemapsCogifyCoverCommand = command({
const im = cfg.imagery[0];
logger.info({ files: im.files.length, title: im.title, duration: imageryLoadTime }, 'Imagery:Loaded');

if (im.collection == null && args.requireStacCollection) {
throw new Error(`No collection.json found with imagery: ${im.url.href}`);
}

const tms = SupportedTileMatrix.find((f) => f.identifier.toLowerCase() === args.tileMatrix.toLowerCase());
if (tms == null) throw new Error('--tile-matrix: ' + args.tileMatrix + ' not found');

Expand Down
Loading