From 29d18710225db7b371dce3c9eca256088c92ba5f Mon Sep 17 00:00:00 2001 From: Alice Fage Date: Fri, 13 Dec 2024 11:27:09 +1300 Subject: [PATCH] fix: remove GSD unit from stac setup command inputs TDE-1339 TDE-1211 (#1153) #### Motivation A change was made recently to accept the GSD in the format without a trailing `m` e.g. `0.3` rather than `0.3m`. However, the old format should still be accepted as this may accidentally be entered as a workflow parameter. #### Modification Small refactor to match refactor made to `generate-path` command when handling `GeospatialDataCategories`. Add a function `formatGsd` to check the format of the GSD, remove a trailing `m` and log a warning. #### Checklist - [x] Tests updated - [x] Docs updated - [x] Issue linked in Title --------- Co-authored-by: Blayne Chard --- src/commands/common.ts | 19 +++++++++++++++ src/commands/stac-setup/README.md | 2 +- .../stac-setup/__test__/stac.setup.test.ts | 23 ++++++++++++++++--- src/commands/stac-setup/stac.setup.ts | 21 ++++++++--------- 4 files changed, 49 insertions(+), 16 deletions(-) diff --git a/src/commands/common.ts b/src/commands/common.ts index a1e6f2dc..513d55b8 100644 --- a/src/commands/common.ts +++ b/src/commands/common.ts @@ -159,3 +159,22 @@ export const UrlFolder: Type = { return url; }, }; + +/** + * Remove a trailing 'm' from a input value and validate the input is a number. + * + * @param str input value + * @returns value without trailing 'm' (if it exists) + * @throws if input is not a valid number + */ +export const MeterAsString: Type = { + from(str) { + const meters = str.endsWith('m') ? str.slice(0, -1) : str; + + if (isNaN(Number(meters))) { + throw new Error(`Invalid value: ${meters}. must be a number.`); + } + + return Promise.resolve(meters); + }, +}; diff --git a/src/commands/stac-setup/README.md b/src/commands/stac-setup/README.md index 0801fcb4..8f683982 100644 --- a/src/commands/stac-setup/README.md +++ b/src/commands/stac-setup/README.md @@ -13,7 +13,7 @@ stac-setup | --config | Location of role configuration file | optional | | --start-year | Start year of survey capture | optional | | --end-year | End year of survey capture | optional | -| --gsd | GSD of dataset | | +| --gsd | GSD of dataset, e.g. 0.3 | | | --region | Region of dataset | | | --geographic-description | Geographic description of dataset | | | --geospatial-category | Geospatial category of dataset | | diff --git a/src/commands/stac-setup/__test__/stac.setup.test.ts b/src/commands/stac-setup/__test__/stac.setup.test.ts index 480ac306..7fd98a73 100644 --- a/src/commands/stac-setup/__test__/stac.setup.test.ts +++ b/src/commands/stac-setup/__test__/stac.setup.test.ts @@ -4,8 +4,8 @@ import { afterEach, before, describe, it } from 'node:test'; import { fsa } from '@chunkd/fs'; import { FsMemory } from '@chunkd/source-memory'; -import { commandStacSetup } from '../stac.setup.js'; -import { formatDate, slugFromMetadata, SlugMetadata } from '../stac.setup.js'; +import { MeterAsString } from '../../common.js'; +import { commandStacSetup, formatDate, slugFromMetadata, SlugMetadata } from '../stac.setup.js'; import { SampleCollection } from './sample.js'; describe('stac-setup', () => { @@ -190,10 +190,27 @@ describe('formatDate', () => { const endYear = '2023'; assert.equal(formatDate(startYear, endYear), '2023'); }); - it('Should return date as two years', async () => { const startYear = '2023'; const endYear = '2024'; assert.equal(formatDate(startYear, endYear), '2023-2024'); }); }); + +describe('checkGsd', () => { + it('Should return GSD unaltered', async () => { + assert.equal(await MeterAsString.from('0.3'), '0.3'); + }); + + it('Should return GSD with trailing m removed', async () => { + assert.equal(await MeterAsString.from('0.3m'), '0.3'); + }); + + it('Should throw error if GSD is not a number', async () => { + await assert.rejects(async () => await MeterAsString.from('foo'), Error('Invalid value: foo. must be a number.')); + await assert.rejects( + async () => await MeterAsString.from('1.4deg'), + Error('Invalid value: 1.4deg. must be a number.'), + ); + }); +}); diff --git a/src/commands/stac-setup/stac.setup.ts b/src/commands/stac-setup/stac.setup.ts index 88460752..a8441944 100644 --- a/src/commands/stac-setup/stac.setup.ts +++ b/src/commands/stac-setup/stac.setup.ts @@ -7,7 +7,7 @@ import { CliInfo } from '../../cli.info.js'; import { logger } from '../../log.js'; import { GeospatialDataCategories, StacCollectionLinz } from '../../utils/metadata.js'; import { slugify } from '../../utils/slugify.js'; -import { config, registerCli, tryParseUrl, UrlFolder, urlToString, verbose } from '../common.js'; +import { config, MeterAsString, registerCli, tryParseUrl, UrlFolder, urlToString, verbose } from '../common.js'; export interface SlugMetadata { geospatialCategory: string; @@ -39,9 +39,9 @@ export const commandStacSetup = command({ }), gsd: option({ - type: string, + type: MeterAsString, long: 'gsd', - description: 'GSD of dataset', + description: 'GSD of dataset, e.g. 0.3', }), region: option({ @@ -123,19 +123,16 @@ export function slugFromMetadata(metadata: SlugMetadata): string { const slug = slugify(metadata.date ? `${geographicDescription}_${metadata.date}` : geographicDescription); if ( - ( - [ - GeospatialDataCategories.AerialPhotos, - GeospatialDataCategories.RuralAerialPhotos, - GeospatialDataCategories.SatelliteImagery, - GeospatialDataCategories.UrbanAerialPhotos, - ] as string[] - ).includes(metadata.geospatialCategory) + metadata.geospatialCategory === GeospatialDataCategories.AerialPhotos || + metadata.geospatialCategory === GeospatialDataCategories.RuralAerialPhotos || + metadata.geospatialCategory === GeospatialDataCategories.SatelliteImagery || + metadata.geospatialCategory === GeospatialDataCategories.UrbanAerialPhotos ) { return `${slug}_${metadata.gsd}m`; } if ( - ([GeospatialDataCategories.Dem, GeospatialDataCategories.Dsm] as string[]).includes(metadata.geospatialCategory) + metadata.geospatialCategory === GeospatialDataCategories.Dem || + metadata.geospatialCategory === GeospatialDataCategories.Dsm ) { return slug; }