From 849f52ea155d2bf4223c27bc78c8377479f66538 Mon Sep 17 00:00:00 2001 From: Tawera Manaena Date: Mon, 16 Dec 2024 16:13:41 +1300 Subject: [PATCH] refactor: remove unused gdal command options --- .../gdal/gdal-build-cog.ts | 22 +++++------ .../gdal/gdal-build-vrt-warp.ts | 29 ++------------ .../gdal/gdal-build-vrt.ts | 38 ++----------------- .../basemaps-topo-import/gdal/gdal-opts.ts | 6 --- .../basemaps-topo-import/topo-cog-creation.ts | 27 +++---------- 5 files changed, 23 insertions(+), 99 deletions(-) delete mode 100644 src/commands/basemaps-topo-import/gdal/gdal-opts.ts diff --git a/src/commands/basemaps-topo-import/gdal/gdal-build-cog.ts b/src/commands/basemaps-topo-import/gdal/gdal-build-cog.ts index 44e9b567..7739eee0 100644 --- a/src/commands/basemaps-topo-import/gdal/gdal-build-cog.ts +++ b/src/commands/basemaps-topo-import/gdal/gdal-build-cog.ts @@ -1,5 +1,4 @@ import { GdalCommand } from '@basemaps/cogify/build/cogify/gdal.runner.js'; -import { GdalResampling } from '@basemaps/cogify/build/cogify/stac.js'; import { urlToString } from '../../common.js'; @@ -7,12 +6,13 @@ export const DEFAULT_TRIM_PIXEL_RIGHT = 1.7; interface gdalBuildCogOptions { /** - * Resampling algorithm. Nearest is the default. + * The number of pixels to remove from the right side of the imagery. + * + * If the value is a decimal number ending in above .5, the number of + * pixels removed will be the value rounded up to the nearest integer. + * The imagery will then be scaled to fulfil the difference. * - * @link - * https://gdal.org/en/stable/programs/gdal_translate.html#cmdoption-gdal_translate-r */ - resamplingMethod?: GdalResampling; pixelTrim?: number; } @@ -32,6 +32,7 @@ export function gdalBuildCogCommands( opts?: gdalBuildCogOptions, ): GdalCommand { const pixelTrim = opts?.pixelTrim ?? DEFAULT_TRIM_PIXEL_RIGHT; + const command: GdalCommand = { output, command: 'gdal_translate', @@ -39,9 +40,9 @@ export function gdalBuildCogCommands( ['-q'], // Supress non-error output ['-stats'], // Force stats (re)computation ['-of', 'COG'], // Output format + ['-srcwin', '0', '0', `${width - pixelTrim}`, `${height}`], // https://gdal.org/en/latest/drivers/raster/cog.html#creation-options - ['-srcwin', `0`, `0`, `${width - pixelTrim}`, `${height}`], ['-co', 'BIGTIFF=NO'], ['-co', 'BLOCKSIZE=512'], ['-co', 'COMPRESS=WEBP'], @@ -55,17 +56,14 @@ export function gdalBuildCogCommands( // https://gdal.org/en/latest/drivers/raster/cog.html#reprojection-related-creation-options ['-co', 'ADD_ALPHA=YES'], + + urlToString(input), + urlToString(output), ] .filter((f) => f != null) .flat() .map(String), }; - if (opts?.resamplingMethod != null) { - command.args.push('-r', opts.resamplingMethod); - } - - command.args.push(urlToString(input), urlToString(output)); - return command; } diff --git a/src/commands/basemaps-topo-import/gdal/gdal-build-vrt-warp.ts b/src/commands/basemaps-topo-import/gdal/gdal-build-vrt-warp.ts index 12f7b49b..d4ee448d 100644 --- a/src/commands/basemaps-topo-import/gdal/gdal-build-vrt-warp.ts +++ b/src/commands/basemaps-topo-import/gdal/gdal-build-vrt-warp.ts @@ -1,19 +1,8 @@ import { GdalCommand } from '@basemaps/cogify/build/cogify/gdal.runner.js'; -import { GdalResampling } from '@basemaps/cogify/build/cogify/stac.js'; import { Epsg } from '@basemaps/geo'; import { urlToString } from '../../common.js'; -interface gdalBuildVrtWarpOptions { - /** - * Resampling algorithm. Nearest is the default. - * - * @link - * https://gdal.org/en/latest/programs/gdalwarp.html#cmdoption-gdalwarp-r - */ - resamplingMethod?: GdalResampling; -} - /** * Constructs a 'gdalwarp' GdalCommand. * @@ -23,12 +12,7 @@ interface gdalBuildVrtWarpOptions { * @param opts * @returns */ -export function gdalBuildVrtWarp( - targetVrt: URL, - sourceVrt: URL, - sourceProj: Epsg, - opts?: gdalBuildVrtWarpOptions, -): GdalCommand { +export function gdalBuildVrtWarp(targetVrt: URL, sourceVrt: URL, sourceProj: Epsg): GdalCommand { const command: GdalCommand = { output: targetVrt, command: 'gdalwarp', @@ -37,19 +21,14 @@ export function gdalBuildVrtWarp( ['-of', 'vrt'], // Output as a VRT ['-wo', 'NUM_THREADS=ALL_CPUS'], // Multithread the warp ['-s_srs', sourceProj.toEpsgString()], // Source EPSG - // ['-t_srs', Nztm2000QuadTms.projection.toEpsgString()], // Target EPSG - // ['-tr', targetResolution, targetResolution], + + urlToString(sourceVrt), + urlToString(targetVrt), ] .filter((f) => f != null) .flat() .map(String), }; - if (opts?.resamplingMethod != null) { - command.args.push('-r', opts.resamplingMethod); - } - - command.args.push(urlToString(sourceVrt), urlToString(targetVrt)); - return command; } diff --git a/src/commands/basemaps-topo-import/gdal/gdal-build-vrt.ts b/src/commands/basemaps-topo-import/gdal/gdal-build-vrt.ts index 55c195a8..09892191 100644 --- a/src/commands/basemaps-topo-import/gdal/gdal-build-vrt.ts +++ b/src/commands/basemaps-topo-import/gdal/gdal-build-vrt.ts @@ -1,30 +1,7 @@ import { GdalCommand } from '@basemaps/cogify/build/cogify/gdal.runner.js'; -import { GdalResampling } from '@basemaps/cogify/build/cogify/stac.js'; import { urlToString } from '../../common.js'; -interface GdalBuildVrtOptions { - /** - * Resampling algorithm. Nearest is the default. - * - * @link - * https://gdal.org/en/latest/programs/gdalbuildvrt.html#cmdoption-gdalbuildvrt-r - */ - resamplingMethod?: GdalResampling; - - /** - * Background color. - * - * @example - * '208 231 244' // NZ Topo Raster sea colour (RGB as 'RRR GGG BBB') - * - * @links - * https://gdal.org/en/latest/programs/gdalbuildvrt.html#cmdoption-gdalbuildvrt-vrtnodata - * https://gdal.org/en/latest/programs/gdalbuildvrt.html#vrtnodata - */ - background?: string; -} - /** * Constructs a 'gdalBuildVrt' GdalCommand. * @@ -33,23 +10,14 @@ interface GdalBuildVrtOptions { * @param opts * @returns */ -export function gdalBuildVrt(targetVrt: URL, source: URL[], opts?: GdalBuildVrtOptions): GdalCommand { +export function gdalBuildVrt(targetVrt: URL, source: URL[]): GdalCommand { if (source.length === 0) throw new Error('No source files given for :' + targetVrt.href); + const command: GdalCommand = { output: targetVrt, command: 'gdalbuildvrt', - args: ['-addalpha'], + args: ['-addalpha', urlToString(targetVrt), ...source.map(urlToString)], }; - if (opts?.background != null) { - command.args.push('-hidenodata', '-vrtnodata', opts.background); - } - - if (opts?.resamplingMethod != null) { - command.args.push('-r', opts.resamplingMethod); - } - - command.args.push(urlToString(targetVrt), ...source.map(urlToString)); - return command; } diff --git a/src/commands/basemaps-topo-import/gdal/gdal-opts.ts b/src/commands/basemaps-topo-import/gdal/gdal-opts.ts deleted file mode 100644 index 13b105f6..00000000 --- a/src/commands/basemaps-topo-import/gdal/gdal-opts.ts +++ /dev/null @@ -1,6 +0,0 @@ -const resamplingOptions = ['nearest', 'bilinear', 'cubic', 'cubicspline', 'lanczos', 'average', 'mode'] as const; -type ResamplingOption = (typeof resamplingOptions)[number]; - -export function checkResamplingMethod(resamplingMethod: string): resamplingMethod is ResamplingOption { - return (resamplingOptions as readonly string[]).includes(resamplingMethod); -} diff --git a/src/commands/basemaps-topo-import/topo-cog-creation.ts b/src/commands/basemaps-topo-import/topo-cog-creation.ts index a18dffc2..5077b3a8 100644 --- a/src/commands/basemaps-topo-import/topo-cog-creation.ts +++ b/src/commands/basemaps-topo-import/topo-cog-creation.ts @@ -1,7 +1,7 @@ import { tmpdir } from 'node:os'; import { GdalRunner } from '@basemaps/cogify/build/cogify/gdal.runner.js'; -import { createFileStats, GdalResampling } from '@basemaps/cogify/build/cogify/stac.js'; +import { createFileStats } from '@basemaps/cogify/build/cogify/stac.js'; import { Epsg } from '@basemaps/geo'; import { fsa, Tiff } from '@basemaps/shared'; import { CliId } from '@basemaps/shared/build/cli/info.js'; @@ -19,7 +19,6 @@ import { loadInput } from '../group/group.js'; import { gdalBuildCogCommands as gdalBuildCog } from './gdal/gdal-build-cog.js'; import { gdalBuildVrt } from './gdal/gdal-build-vrt.js'; import { gdalBuildVrtWarp } from './gdal/gdal-build-vrt-warp.js'; -import { checkResamplingMethod as isValidResamplingOption } from './gdal/gdal-opts.js'; const Q = pLimit(10); @@ -49,11 +48,6 @@ export const topoCogCreation = command({ long: 'from-file', description: 'JSON file to load inputs from, must be a JSON Array', }), - resamplingMethod: option({ - type: optional(string), - long: 'resampling-method', - description: 'the GDAL resampling algorithm to use', - }), pixelTrim: option({ type: optional(number), long: 'pixel-trim', @@ -65,15 +59,6 @@ export const topoCogCreation = command({ registerCli(this, args); logger.info('ListJobs:Start'); - // check if resampling method is a valid option - const resamplingMethod = args.resamplingMethod; - - if (resamplingMethod != null) { - if (!isValidResamplingOption(resamplingMethod)) { - throw new Error('the provided resampling method is not a valid option'); - } - } - // Load the items for processing const inputs: string[] = []; for (const input of args.inputs) inputs.push(...loadInput(input)); @@ -94,7 +79,7 @@ export const topoCogCreation = command({ await Promise.all( inputs.map((input) => Q(async () => { - await createCogs(tryParseUrl(input), tmpFolder, args.pixelTrim, resamplingMethod); + await createCogs(tryParseUrl(input), tmpFolder, args.pixelTrim); }), ), ); @@ -103,7 +88,7 @@ export const topoCogCreation = command({ }, }); -async function createCogs(input: URL, tmp: URL, pixelTrim?: number, resamplingMethod?: GdalResampling): Promise { +async function createCogs(input: URL, tmp: URL, pixelTrim?: number): Promise { const startTime = performance.now(); const item = await fsa.readJson(input); const tmpFolder = new URL(item.id, tmp); @@ -142,7 +127,7 @@ async function createCogs(input: URL, tmp: URL, pixelTrim?: number, resamplingMe logger.info({ item: item.id }, 'CogCreation:gdalbuildvrt'); const vrtPath = new URL(`${item.id}.vrt`, tmpFolder); - const commandBuildVrt = gdalBuildVrt(vrtPath, [inputPath], { resamplingMethod }); + const commandBuildVrt = gdalBuildVrt(vrtPath, [inputPath]); await new GdalRunner(commandBuildVrt).run(logger); @@ -158,7 +143,7 @@ async function createCogs(input: URL, tmp: URL, pixelTrim?: number, resamplingMe if (sourceProj == null) throw new Error(`Unknown source projection ${sourceEpsg}`); const vrtWarpPath = new URL(`${item.id}-warp.vrt`, tmpFolder); - const commandBuildVrtWarp = gdalBuildVrtWarp(vrtWarpPath, vrtPath, sourceProj, { resamplingMethod }); + const commandBuildVrtWarp = gdalBuildVrtWarp(vrtWarpPath, vrtPath, sourceProj); await new GdalRunner(commandBuildVrtWarp).run(logger); @@ -170,7 +155,7 @@ async function createCogs(input: URL, tmp: URL, pixelTrim?: number, resamplingMe const width = Number(item.properties['source.width']); const height = Number(item.properties['source.height']); const tempPath = new URL(`${item.id}.tiff`, tmpFolder); - const commandTranslate = gdalBuildCog(vrtWarpPath, tempPath, width, height, { resamplingMethod, pixelTrim }); + const commandTranslate = gdalBuildCog(vrtWarpPath, tempPath, width, height, { pixelTrim }); await new GdalRunner(commandTranslate).run(logger);