Skip to content

Commit

Permalink
refactor: remove unused gdal command options
Browse files Browse the repository at this point in the history
  • Loading branch information
tawera-manaena committed Dec 16, 2024
1 parent db4fab3 commit 849f52e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 99 deletions.
22 changes: 10 additions & 12 deletions src/commands/basemaps-topo-import/gdal/gdal-build-cog.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { GdalCommand } from '@basemaps/cogify/build/cogify/gdal.runner.js';
import { GdalResampling } from '@basemaps/cogify/build/cogify/stac.js';

import { urlToString } from '../../common.js';

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;
}

Expand All @@ -32,16 +32,17 @@ export function gdalBuildCogCommands(
opts?: gdalBuildCogOptions,
): GdalCommand {
const pixelTrim = opts?.pixelTrim ?? DEFAULT_TRIM_PIXEL_RIGHT;

const command: GdalCommand = {
output,
command: 'gdal_translate',
args: [
['-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'],
Expand All @@ -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;
}
29 changes: 4 additions & 25 deletions src/commands/basemaps-topo-import/gdal/gdal-build-vrt-warp.ts
Original file line number Diff line number Diff line change
@@ -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.
*
Expand All @@ -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',
Expand All @@ -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;
}
38 changes: 3 additions & 35 deletions src/commands/basemaps-topo-import/gdal/gdal-build-vrt.ts
Original file line number Diff line number Diff line change
@@ -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.
*
Expand All @@ -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;
}
6 changes: 0 additions & 6 deletions src/commands/basemaps-topo-import/gdal/gdal-opts.ts

This file was deleted.

27 changes: 6 additions & 21 deletions src/commands/basemaps-topo-import/topo-cog-creation.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);

Expand Down Expand Up @@ -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',
Expand All @@ -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));
Expand All @@ -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);
}),
),
);
Expand All @@ -103,7 +88,7 @@ export const topoCogCreation = command({
},
});

async function createCogs(input: URL, tmp: URL, pixelTrim?: number, resamplingMethod?: GdalResampling): Promise<void> {
async function createCogs(input: URL, tmp: URL, pixelTrim?: number): Promise<void> {
const startTime = performance.now();
const item = await fsa.readJson<StacItem>(input);
const tmpFolder = new URL(item.id, tmp);
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down

0 comments on commit 849f52e

Please sign in to comment.