Skip to content

Commit

Permalink
Some refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
Wentao-Kuang committed Dec 22, 2024
1 parent 7a24780 commit 9cf6e2f
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/cogify/src/cogify/cli/cli.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const BasemapsCogifyConfigCommand = command({
const q = pLimit(args.concurrency);

metrics.start('imagery:load');
const im = await initImageryFromTiffUrl(args.path, q, undefined, undefined, logger);
const im = await initImageryFromTiffUrl(args.path, q, undefined, logger);
const ts = ConfigProviderMemory.imageryToTileSet(im) as ConfigTileSetRaster;
provider.put(im);
metrics.end('imagery:load');
Expand Down
11 changes: 3 additions & 8 deletions packages/cogify/src/cogify/cli/cli.cover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { CutlineOptimizer } from '../../cutline.js';
import { getLogger, logArguments } from '../../log.js';
import { Presets } from '../../preset.js';
import { createTileCover, TileCoverContext } from '../../tile.cover.js';
import { Rgba, Url, UrlFolder } from '../parsers.js';
import { RgbaType, Url, UrlFolder } from '../parsers.js';
import { createFileStats } from '../stac.js';

const SupportedTileMatrix = [GoogleTms, Nztm2000QuadTms];
Expand Down Expand Up @@ -62,13 +62,8 @@ export const BasemapsCogifyCoverCommand = command({
defaultValue: () => false,
defaultValueIsSerializable: true,
}),
name: option({
type: optional(string),
long: 'name',
description: 'Define the name of the output imagery',
}),
background: option({
type: optional(Rgba),
type: optional(RgbaType),
long: 'background',
description: 'Replace all transparent COG pixels with this RGBA hexstring color',
}),
Expand All @@ -79,7 +74,7 @@ export const BasemapsCogifyCoverCommand = command({

const mem = new ConfigProviderMemory();
metrics.start('imagery:load');
const cfg = await initConfigFromUrls(mem, args.paths, args.name);
const cfg = await initConfigFromUrls(mem, args.paths);
const imageryLoadTime = metrics.end('imagery:load');
if (cfg.imagery.length === 0) throw new Error('No imagery found');
const im = cfg.imagery[0];
Expand Down
10 changes: 5 additions & 5 deletions packages/cogify/src/cogify/gdal.command.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { RGBA } from '@basemaps/config';
import { Rgba } from '@basemaps/config';
import { Epsg, EpsgCode, TileMatrixSets } from '@basemaps/geo';
import { urlToString } from '@basemaps/shared';

import { Presets } from '../preset.js';
import { GdalCommand } from './gdal.runner.js';
import { CogifyCreationOptions } from './stac.js';

const isPowerOfTwo = (x: number): boolean => (x & (x - 1)) === 0;

export function gdalBuildVrt(targetVrt: URL, source: URL[]): GdalCommand {
if (source.length === 0) throw new Error('No source files given for :' + targetVrt.href);
return {
Expand Down Expand Up @@ -109,7 +111,7 @@ export function gdalBuildCog(targetTiff: URL, sourceVrt: URL, opt: CogifyCreatio
*
* @returns a 'gdal_create' GdalCommand object
*/
export function gdalCreate(targetTiff: URL, color: RGBA, opt: CogifyCreationOptions): GdalCommand {
export function gdalCreate(targetTiff: URL, color: Rgba, opt: CogifyCreationOptions): GdalCommand {
const cfg = { ...Presets[opt.preset], ...opt };

const tileMatrix = TileMatrixSets.find(cfg.tileMatrix);
Expand All @@ -120,9 +122,7 @@ export function gdalCreate(targetTiff: URL, color: RGBA, opt: CogifyCreationOpti
const size = Math.round(bounds.width / pixelScale);

// if the value of 'size' is not a power of 2
if ((Math.log(size) / Math.log(size)) % 1 !== 0) {
throw new Error('Size did not compute to a power of 2');
}
if (!isPowerOfTwo(size)) throw new Error('Size did not compute to a power of 2');

return {
command: 'gdal_create',
Expand Down
4 changes: 2 additions & 2 deletions packages/cogify/src/cogify/parsers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { pathToFileURL } from 'node:url';

import { parseRgba, RGBA } from '@basemaps/config';
import { parseRgba, Rgba } from '@basemaps/config';
import { fsa } from '@basemaps/shared';
import { Type } from 'cmd-ts';

Expand All @@ -9,7 +9,7 @@ import { Type } from 'cmd-ts';
*
* Throws an error if the RGBA hexstring is invalid.
**/
export const Rgba: Type<string, RGBA> = {
export const RgbaType: Type<string, Rgba> = {
from(str) {
return Promise.resolve(parseRgba(str));
},
Expand Down
4 changes: 2 additions & 2 deletions packages/cogify/src/cogify/stac.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createHash } from 'node:crypto';

import { RGBA } from '@basemaps/config';
import { Rgba } from '@basemaps/config';
import { Tile } from '@basemaps/geo';
import { StacCollection, StacItem, StacLink } from 'stac-ts';

Expand Down Expand Up @@ -59,7 +59,7 @@ export interface CogifyCreationOptions {
overviewResampling?: GdalResampling;

/** Color with which to replace all transparent COG pixels */
background?: RGBA;
background?: Rgba;
}

export type GdalResampling = 'nearest' | 'bilinear' | 'cubic' | 'cubicspline' | 'lanczos' | 'average' | 'mode';
Expand Down
4 changes: 2 additions & 2 deletions packages/cogify/src/tile.cover.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RGBA } from '@basemaps/config';
import { Rgba } from '@basemaps/config';
import { ConfigImageryTiff } from '@basemaps/config-loader';
import { BoundingBox, Bounds, EpsgCode, Projection, ProjectionLoader, TileId, TileMatrixSet } from '@basemaps/geo';
import { fsa, LogType, urlToString } from '@basemaps/shared';
Expand Down Expand Up @@ -34,7 +34,7 @@ export interface TileCoverContext {
/** GDAL configuration preset */
preset: string;
/** Optional color with which to replace all transparent COG pixels */
background?: RGBA;
background?: Rgba;
/**
* Override the base zoom to store the output COGS as
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/config-loader/src/json/json.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export class ConfigJson {
const id = ConfigId.prefix(ConfigPrefix.Imagery, imageId);
this.logger.trace({ url: url.href, imageId: id }, 'Imagery:Fetch');

const img = await initImageryFromTiffUrl(url, this.Q, undefined, this.imageryConfigCache, this.logger);
const img = await initImageryFromTiffUrl(url, this.Q, this.imageryConfigCache, this.logger);
img.id = id; // TODO could we use img.collection.id for this?

// TODO should we be overwriting the name and title when it is loaded from the STAC metadata?
Expand Down
6 changes: 2 additions & 4 deletions packages/config-loader/src/json/tiff.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,6 @@ export async function loadTiffsFromPaths(sourceFiles: URL[], Q: LimitFunction):
export async function initImageryFromTiffUrl(
target: URL,
Q: LimitFunction,
name?: string,
configCache?: URL,
log?: LogType,
): Promise<ConfigImageryTiff> {
Expand All @@ -365,7 +364,7 @@ export async function initImageryFromTiffUrl(
if (stac == null) log?.warn({ target: target }, 'Tiff:StacNotFound');
const params = await computeTiffSummary(target, tiffs);

const imageryName = name ? name : getImageryName(target);
const imageryName = getImageryName(target);
const title = stac?.title ?? imageryName;
const tileMatrix =
params.projection === EpsgCode.Nztm2000 ? Nztm2000QuadTms : TileMatrixSets.tryGet(params.projection);
Expand Down Expand Up @@ -437,15 +436,14 @@ export async function initImageryFromTiffUrl(
export async function initConfigFromUrls(
provider: ConfigProviderMemory,
targets: URL[],
name?: string,
concurrency = 25,
configCache?: URL,
log?: LogType,
): Promise<{ tileSet: ConfigTileSetRaster; tileSets: ConfigTileSetRaster[]; imagery: ConfigImageryTiff[] }> {
const q = pLimit(concurrency);

const imageryConfig: Promise<ConfigImageryTiff>[] = [];
for (const target of targets) imageryConfig.push(initImageryFromTiffUrl(target, q, name, configCache, log));
for (const target of targets) imageryConfig.push(initImageryFromTiffUrl(target, q, configCache, log));

const aerialTileSet: ConfigTileSetRaster = {
id: 'ts_aerial',
Expand Down
4 changes: 2 additions & 2 deletions packages/config/src/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function parseHex(str: string): number {
return val;
}

export interface RGBA {
export interface Rgba {
r: number;
g: number;
b: number;
Expand All @@ -24,7 +24,7 @@ export interface RGBA {
* Defaults to 0 if missing values
* @param str string to parse
*/
export function parseRgba(str: string): RGBA {
export function parseRgba(str: string): Rgba {
if (str.startsWith('0x')) str = str.slice(2);
else if (str.startsWith('#')) str = str.slice(1);
if (str.length !== 6 && str.length !== 8) {
Expand Down
2 changes: 1 addition & 1 deletion packages/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export {
} from './base.config.js';
export { base58, isBase58 } from './base58.js';
export { ensureBase58, sha256base58 } from './base58.node.js';
export { parseHex, parseRgba, RGBA } from './color.js';
export { parseHex, parseRgba, Rgba } from './color.js';
export { ConfigBase as BaseConfig } from './config/base.js';
export { ConfigBundle } from './config/config.bundle.js';
export { ConfigImagery, ConfigImageryOverview, ImageryBandType, ImageryDataType } from './config/imagery.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function loadConfig(opts: ServerOptions, logger: LogType): Promise<
// Load the config directly from the source tiff files
if ('paths' in opts) {
const mem = new ConfigProviderMemory();
const ret = await initConfigFromUrls(mem, opts.paths, undefined, TiffLoadConcurrency, opts.configCache, logger);
const ret = await initConfigFromUrls(mem, opts.paths, TiffLoadConcurrency, opts.configCache, logger);
for (const ts of ret.tileSets) {
logger.info(
{ tileSet: ts.name, layers: ts.layers.length, outputs: ts.outputs?.map((f) => f.name) },
Expand Down

0 comments on commit 9cf6e2f

Please sign in to comment.