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(cli): Create-overview determine max zoom overview from the gsd #2612

Merged
merged 4 commits into from
Nov 30, 2022
Merged
Changes from 3 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
30 changes: 18 additions & 12 deletions packages/cli/src/cli/overview/action.create.overview.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { sha256base58 } from '@basemaps/config';
import { GoogleTms, NamedBounds, Nztm2000QuadTms, QuadKey, TileMatrixSet } from '@basemaps/geo';
import { LogConfig, LogType } from '@basemaps/shared';
import { ChunkSource, SourceMemory } from '@chunkd/core';
import { LogConfig, LogType, Projection } from '@basemaps/shared';
import { SourceMemory } from '@chunkd/core';
import { fsa } from '@chunkd/fs';
import { CogTiff } from '@cogeotiff/core';
import { CotarIndexBinary, CotarIndexBuilder, TarReader } from '@cotar/core';
Expand All @@ -18,6 +18,7 @@ import { JobTiles, tile } from './tile.generator.js';
import { SimpleTimer } from './timer.js';

const DefaultMaxZoom = 15;
const MaxNumberTiles = 250000;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we add a sentence explaining what these numbers do?


export class CommandCreateOverview extends CommandLineAction {
private source: CommandLineStringParameter;
Expand All @@ -39,11 +40,6 @@ export class CommandCreateOverview extends CommandLineAction {
description: 'Path of source imagery files',
required: true,
});
this.maxZoom = this.defineIntegerParameter({
argumentName: 'MAX_ZOOM',
parameterLongName: '--max-zoom',
description: 'Maximum zoom level for the overview',
});
this.output = this.defineStringParameter({
argumentName: 'OUTPUT',
parameterLongName: '--output',
Expand All @@ -55,20 +51,23 @@ export class CommandCreateOverview extends CommandLineAction {
const logger = LogConfig.get();
const source = this.source.value;
if (source == null) throw new Error('Please provide a path for the source imagery.');
const maxZoom = this.maxZoom.value ?? DefaultMaxZoom;

const hash = sha256base58(source);

logger.info({ source, hash, maxZoom }, 'CreateOverview');
logger.info({ source, hash }, 'CreateOverview');
const path = fsa.join('overview', hash);

const st = new SimpleTimer();
logger.debug({ source }, 'CreateOverview:ListTiffs');
const tiffList = (await fsa.toArray(fsa.list(source))).filter(filterTiff);
const tiffSource = tiffList.map((path: string) => fsa.source(path));

logger.info({ source, duration: st.tick() }, 'CreateOverview:ListTiffs:Done');

logger.debug({ source }, 'CreateOverview:PrepareSourceFiles');
const tileMatrix = await this.getTileMatrix(tiffSource);
const tiff = await CogTiff.create(tiffSource[0]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will throw if there are not tiffs, maybe check and exit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a filterTiff for the file lists. So I am ok if the script just die here if the not tiff?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should let it die here, but with a nice error message, stack traces are not super helpful to users

const tileMatrix = await this.getTileMatrix(tiff);
const maxZoom = await this.getMaxZoomFromGSD(tiff, tileMatrix);
logger.info({ source, duration: st.tick() }, 'CreateOverview:PrepareSourceFiles:Done');

logger.debug({ source }, 'CreateOverview:PrepareCovering');
Expand Down Expand Up @@ -115,6 +114,7 @@ export class CommandCreateOverview extends CommandLineAction {
qk = QuadKey.parent(qk);
}
}
if (tiles.size > MaxNumberTiles) this.prepareTiles(files, maxZoom - 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also why is this async?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good pick

return tiles;
}

Expand All @@ -126,8 +126,7 @@ export class CommandCreateOverview extends CommandLineAction {
}
}

async getTileMatrix(sources: ChunkSource[]): Promise<TileMatrixSet> {
const tiff = await CogTiff.create(sources[0]);
async getTileMatrix(tiff: CogTiff): Promise<TileMatrixSet> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesnt need to be async the tiff is tiff.init(true) before.

(you are initing the tiff twice)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need this to run await tiff.getImage(0).loadGeoTiffTags();, before getting the espg. The just tiff.init now working.
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tiff.init(true) is called in the other function you should pull that out to the same place as the new CogTiff

await tiff.getImage(0).loadGeoTiffTags();
const projection = tiff.getImage(0).epsg;
if (projection == null) throw new Error('Failed to find the projection from the imagery.');
Expand All @@ -136,6 +135,13 @@ export class CommandCreateOverview extends CommandLineAction {
else throw new Error(`Projection code: ${projection} not supported`);
}

async getMaxZoomFromGSD(tiff: CogTiff, tileMatrix: TileMatrixSet): Promise<number> {
await tiff.init(true);
const gsd = tiff.getImage(tiff.images.length - 1).resolution[0];
const resZoom = Projection.getTiffResZoom(tileMatrix, gsd);
return Math.min(resZoom + 2, DefaultMaxZoom);
}

async createTar(path: string, logger: LogType): Promise<void> {
const tarFile = 'overviews.tar.co';
const tarFilePath = fsa.join(path, tarFile);
Expand Down