Skip to content

Commit 29d1871

Browse files
amfageblacha
andauthored
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 <[email protected]>
1 parent a1e54de commit 29d1871

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

src/commands/common.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,22 @@ export const UrlFolder: Type<string, URL> = {
159159
return url;
160160
},
161161
};
162+
163+
/**
164+
* Remove a trailing 'm' from a input value and validate the input is a number.
165+
*
166+
* @param str input value
167+
* @returns value without trailing 'm' (if it exists)
168+
* @throws if input is not a valid number
169+
*/
170+
export const MeterAsString: Type<string, string> = {
171+
from(str) {
172+
const meters = str.endsWith('m') ? str.slice(0, -1) : str;
173+
174+
if (isNaN(Number(meters))) {
175+
throw new Error(`Invalid value: ${meters}. must be a number.`);
176+
}
177+
178+
return Promise.resolve(meters);
179+
},
180+
};

src/commands/stac-setup/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ stac-setup <options>
1313
| --config <str> | Location of role configuration file | optional |
1414
| --start-year <str> | Start year of survey capture | optional |
1515
| --end-year <str> | End year of survey capture | optional |
16-
| --gsd <str> | GSD of dataset | |
16+
| --gsd <value> | GSD of dataset, e.g. 0.3 | |
1717
| --region <str> | Region of dataset | |
1818
| --geographic-description <str> | Geographic description of dataset | |
1919
| --geospatial-category <str> | Geospatial category of dataset | |

src/commands/stac-setup/__test__/stac.setup.test.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { afterEach, before, describe, it } from 'node:test';
44
import { fsa } from '@chunkd/fs';
55
import { FsMemory } from '@chunkd/source-memory';
66

7-
import { commandStacSetup } from '../stac.setup.js';
8-
import { formatDate, slugFromMetadata, SlugMetadata } from '../stac.setup.js';
7+
import { MeterAsString } from '../../common.js';
8+
import { commandStacSetup, formatDate, slugFromMetadata, SlugMetadata } from '../stac.setup.js';
99
import { SampleCollection } from './sample.js';
1010

1111
describe('stac-setup', () => {
@@ -190,10 +190,27 @@ describe('formatDate', () => {
190190
const endYear = '2023';
191191
assert.equal(formatDate(startYear, endYear), '2023');
192192
});
193-
194193
it('Should return date as two years', async () => {
195194
const startYear = '2023';
196195
const endYear = '2024';
197196
assert.equal(formatDate(startYear, endYear), '2023-2024');
198197
});
199198
});
199+
200+
describe('checkGsd', () => {
201+
it('Should return GSD unaltered', async () => {
202+
assert.equal(await MeterAsString.from('0.3'), '0.3');
203+
});
204+
205+
it('Should return GSD with trailing m removed', async () => {
206+
assert.equal(await MeterAsString.from('0.3m'), '0.3');
207+
});
208+
209+
it('Should throw error if GSD is not a number', async () => {
210+
await assert.rejects(async () => await MeterAsString.from('foo'), Error('Invalid value: foo. must be a number.'));
211+
await assert.rejects(
212+
async () => await MeterAsString.from('1.4deg'),
213+
Error('Invalid value: 1.4deg. must be a number.'),
214+
);
215+
});
216+
});

src/commands/stac-setup/stac.setup.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { CliInfo } from '../../cli.info.js';
77
import { logger } from '../../log.js';
88
import { GeospatialDataCategories, StacCollectionLinz } from '../../utils/metadata.js';
99
import { slugify } from '../../utils/slugify.js';
10-
import { config, registerCli, tryParseUrl, UrlFolder, urlToString, verbose } from '../common.js';
10+
import { config, MeterAsString, registerCli, tryParseUrl, UrlFolder, urlToString, verbose } from '../common.js';
1111

1212
export interface SlugMetadata {
1313
geospatialCategory: string;
@@ -39,9 +39,9 @@ export const commandStacSetup = command({
3939
}),
4040

4141
gsd: option({
42-
type: string,
42+
type: MeterAsString,
4343
long: 'gsd',
44-
description: 'GSD of dataset',
44+
description: 'GSD of dataset, e.g. 0.3',
4545
}),
4646

4747
region: option({
@@ -123,19 +123,16 @@ export function slugFromMetadata(metadata: SlugMetadata): string {
123123
const slug = slugify(metadata.date ? `${geographicDescription}_${metadata.date}` : geographicDescription);
124124

125125
if (
126-
(
127-
[
128-
GeospatialDataCategories.AerialPhotos,
129-
GeospatialDataCategories.RuralAerialPhotos,
130-
GeospatialDataCategories.SatelliteImagery,
131-
GeospatialDataCategories.UrbanAerialPhotos,
132-
] as string[]
133-
).includes(metadata.geospatialCategory)
126+
metadata.geospatialCategory === GeospatialDataCategories.AerialPhotos ||
127+
metadata.geospatialCategory === GeospatialDataCategories.RuralAerialPhotos ||
128+
metadata.geospatialCategory === GeospatialDataCategories.SatelliteImagery ||
129+
metadata.geospatialCategory === GeospatialDataCategories.UrbanAerialPhotos
134130
) {
135131
return `${slug}_${metadata.gsd}m`;
136132
}
137133
if (
138-
([GeospatialDataCategories.Dem, GeospatialDataCategories.Dsm] as string[]).includes(metadata.geospatialCategory)
134+
metadata.geospatialCategory === GeospatialDataCategories.Dem ||
135+
metadata.geospatialCategory === GeospatialDataCategories.Dsm
139136
) {
140137
return slug;
141138
}

0 commit comments

Comments
 (0)