Skip to content

Commit aecb5e6

Browse files
authored
feat(cogify): error early if no source collection.json is found BM-1047 (#3296)
### Motivation When importing imagery into basemaps we know if there is a source stac collection at the first step, but we only warn that we cannot find it. In most of our import processes not having a stac collection should be a errro <!-- TODO: Say why you made your changes. --> ### Modifications Add `--require-stac-collection` to throw a error if a stac collection cannot be found when createing a cover <!-- TODO: Say what changes you made. --> <!-- TODO: Attach screenshots if you changed the UI. --> ### Verification <!-- TODO: Say how you tested your changes. --> Added new integration test to covering creation with and without the new flag.
1 parent 1db9002 commit aecb5e6

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import assert from 'node:assert';
2+
import { beforeEach, describe, it } from 'node:test';
3+
4+
import { fsa, FsMemory, LogConfig } from '@basemaps/shared';
5+
import { TestTiff } from '@basemaps/test';
6+
import { StacCollection } from 'stac-ts';
7+
8+
import { BasemapsCogifyCoverCommand } from '../cli.cover.js';
9+
10+
describe('cli.cover', () => {
11+
const fsMemory = new FsMemory();
12+
13+
beforeEach(async () => {
14+
LogConfig.get().level = 'silent';
15+
fsa.register('memory://', fsMemory);
16+
fsMemory.files.clear();
17+
18+
await fsa.write(new URL('memory://source/google.tiff'), fsa.readStream(TestTiff.Google));
19+
});
20+
21+
const baseArgs = {
22+
paths: [new URL('memory://source/')],
23+
target: new URL('memory://target/'),
24+
preset: 'webp',
25+
tileMatrix: 'WebMercatorQuad',
26+
27+
cutline: undefined,
28+
cutlineBlend: 20,
29+
baseZoomOffset: undefined,
30+
verbose: false,
31+
extraVerbose: false,
32+
requireStacCollection: false,
33+
};
34+
35+
it('should generate a covering', async () => {
36+
const ret = await BasemapsCogifyCoverCommand.handler({ ...baseArgs }).catch((e) => String(e));
37+
assert.equal(ret, undefined); // no errors returned
38+
39+
const files = [...fsMemory.files.keys()];
40+
const collectionJsonPath = files.find((f) => f.endsWith('collection.json') && f.startsWith('memory://target/'));
41+
assert.ok(collectionJsonPath);
42+
43+
const collectionJson = JSON.parse(String(fsMemory.files.get(collectionJsonPath)?.buffer ?? '{}')) as StacCollection;
44+
assert.equal(collectionJson['description'], 'Missing source STAC');
45+
});
46+
47+
it('should error if no collection.json is found', async () => {
48+
const ret = await BasemapsCogifyCoverCommand.handler({
49+
...baseArgs,
50+
paths: [new URL('memory://source/')],
51+
target: new URL('memory://target/'),
52+
preset: 'webp',
53+
54+
requireStacCollection: true,
55+
tileMatrix: 'WebMercatorQuad',
56+
}).catch((e) => String(e));
57+
58+
assert.equal(ret, 'Error: No collection.json found with imagery: memory://source/');
59+
});
60+
});

packages/cogify/src/cogify/cli/cli.cover.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { GoogleTms, Nztm2000QuadTms, TileId } from '@basemaps/geo';
44
import { fsa, urlToString } from '@basemaps/shared';
55
import { CliId, CliInfo } from '@basemaps/shared/build/cli/info.js';
66
import { Metrics } from '@linzjs/metrics';
7-
import { command, number, oneOf, option, optional, restPositionals, string } from 'cmd-ts';
7+
import { command, flag, number, oneOf, option, optional, restPositionals, string } from 'cmd-ts';
88

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

78+
if (im.collection == null && args.requireStacCollection) {
79+
throw new Error(`No collection.json found with imagery: ${im.url.href}`);
80+
}
81+
7282
const tms = SupportedTileMatrix.find((f) => f.identifier.toLowerCase() === args.tileMatrix.toLowerCase());
7383
if (tms == null) throw new Error('--tile-matrix: ' + args.tileMatrix + ' not found');
7484

0 commit comments

Comments
 (0)