Skip to content

Commit

Permalink
fix: remove GSD unit from stac setup command inputs TDE-1339 TDE-1211 (
Browse files Browse the repository at this point in the history
…#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 <[email protected]>
  • Loading branch information
amfage and blacha authored Dec 12, 2024
1 parent a1e54de commit 29d1871
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
19 changes: 19 additions & 0 deletions src/commands/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,22 @@ export const UrlFolder: Type<string, URL> = {
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<string, string> = {
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);
},
};
2 changes: 1 addition & 1 deletion src/commands/stac-setup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ stac-setup <options>
| --config <str> | Location of role configuration file | optional |
| --start-year <str> | Start year of survey capture | optional |
| --end-year <str> | End year of survey capture | optional |
| --gsd <str> | GSD of dataset | |
| --gsd <value> | GSD of dataset, e.g. 0.3 | |
| --region <str> | Region of dataset | |
| --geographic-description <str> | Geographic description of dataset | |
| --geospatial-category <str> | Geospatial category of dataset | |
Expand Down
23 changes: 20 additions & 3 deletions src/commands/stac-setup/__test__/stac.setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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.'),
);
});
});
21 changes: 9 additions & 12 deletions src/commands/stac-setup/stac.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 29d1871

Please sign in to comment.