@radiantearth/stac-migrate 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,23 +2,38 @@
2
2
 
3
3
  A library to update STAC files to the latest version (**1.0.0** at the moment of writing). Supports updating **STAC versions 0.6.0 and later**.
4
4
 
5
- Version of this library: **1.2.0**
5
+ Version of this library: **1.4.0**
6
6
 
7
7
  ## Usage
8
8
 
9
+ ### JavaScript / Node
10
+
9
11
  Add to your project with `npm install @radiantearth/stac-migrate --save`
10
12
 
11
13
  Import the library: `const Migrate = require('@radiantearth/stac-migrate');`
12
14
 
13
- * Migrate (auto-detection): `Migrate.stac(stac: object) => object`<br />
15
+ * Migrate (auto-detection): `Migrate.stac(stac: object, updateVersionNumber: boolean = true) => object`<br />
14
16
  *This method does not migrate the Commons extension - use `Migrate.item` if you have used the Commons extension.*
15
- * Migrate a STAC Collection: `Migrate.collection(collection: object) => object`
16
- * Migrate a STAC Catalog: `Migrate.catalog(catalog: object) => object`
17
- * Migrate a STAC Item: `Migrate.item(item: object, collection: object = null) => object`<br />
17
+ * Migrate a STAC Collection: `Migrate.collection(collection: object, updateVersionNumber: boolean = true) => object`
18
+ * Migrate a STAC Catalog: `Migrate.catalog(catalog: object, updateVersionNumber: boolean = true) => object`
19
+ * Migrate a STAC Item: `Migrate.item(item: object, collection: object = null, updateVersionNumber: boolean = true) => object`<br />
18
20
  *The `collection` parameter is only required to migrate the Commons extension. Otherwise, you don't need to pass this paramater.*
21
+ * Migrate a STAC ItemCollection: `Migrate.itemCollection(catalog: object, updateVersionNumber: boolean = true) => object`
19
22
 
20
23
  **Note:** All changes will be applied in-place! If you don't want the input object to change, make a deep clone before. If you don't have a library which supports this (e.g. [lodash](https://lodash.com/docs/4.17.15#cloneDeep)) you can simply use `var clone = JSON.parse(JSON.stringify(object));`.
21
24
 
25
+ ### CLI
26
+
27
+ You can also use the CLI to migrate a single file.
28
+ The commands follow the different methods above and has the same "restrictions" as above.
29
+
30
+ * Migrate (auto-detection, override): `npx stac-migrate <source_path>`
31
+ * Migrate (auto-detection, save to a different file with 4 spaces indentation): `npx stac-migrate <source_path> --dest <dest_path> --indent 4`
32
+ * Migrate a STAC Collection: `npx stac-migrate <source_path> <dest_path> --collection`
33
+ * Migrate a STAC Catalog: `npx stac-migrate <source_path> <dest_path> --catalog`
34
+ * Migrate a STAC Item: `npx stac-migrate <source_path> <dest_path> --item --collection_path <collection_path>`
35
+ * Migrate a STAC Item Collection: `npx stac-migrate <source_path> <dest_path> --item_collection`
36
+
22
37
  ## Supported Extensions
23
38
 
24
39
  * Checksum (legacy, use `Migrate.enableMultihash(require('multihashes'))` to enable conversion from pre-0.9 checksums - disabled by default to keep the bundle size low)
package/bin/cli.js ADDED
@@ -0,0 +1,78 @@
1
+ #!/usr/bin/env node
2
+ const fs = require('fs');
3
+ const yargs = require('yargs/yargs')
4
+ const { hideBin } = require('yargs/helpers')
5
+ const Migrate = require('../migrate');
6
+ const package = require("../package.json");
7
+
8
+ const args = yargs(hideBin(process.argv))
9
+ .option('_', {
10
+ type: 'string',
11
+ describe: 'File to load the STAC data from'
12
+ })
13
+ .option('dest', {
14
+ type: 'string',
15
+ description: 'File to save the result to',
16
+ alias: 'd'
17
+ })
18
+ .option('collection', {
19
+ type: 'boolean',
20
+ description: 'Enforce the collection migration',
21
+ conflicts: ['catalog', 'item', 'item_collection'],
22
+
23
+ })
24
+ .option('catalog', {
25
+ type: 'boolean',
26
+ description: 'Enforce the catalog migration',
27
+ conflicts: ['collection', 'item', 'item_collection']
28
+ })
29
+ .option('item', {
30
+ type: 'boolean',
31
+ description: 'Enforce the item migration',
32
+ conflicts: ['catalog', 'collection', 'item_collection']
33
+ })
34
+ .option('item_collection', {
35
+ type: 'boolean',
36
+ description: 'Enforce the item collection migration',
37
+ conflicts: ['catalog', 'collection', 'item']
38
+ })
39
+ .option('collection_path', {
40
+ type: 'string',
41
+ description: 'Pass the path to a collection to the item, only possible if --item has been specified.',
42
+ conflicts: ['catalog', 'collection', 'item_collection']
43
+ })
44
+ .option('indent', {
45
+ type: 'number',
46
+ description: 'Number of spaces to use for JSON indentation',
47
+ default: 2
48
+ })
49
+ .help()
50
+ .parse()
51
+
52
+ run(args);
53
+
54
+ function run(args) {
55
+ console.log("STAC Migrate v" + package.version);
56
+ let fn = ['collection', 'catalog', 'item', 'item_collection'].find(type => Boolean(args[type])) || 'stac';
57
+ if (fn === 'item_collection') {
58
+ fn = 'itemCollection';
59
+ }
60
+ if (args._.length !== 1) {
61
+ throw new Error("Please provide exactly one source file.");
62
+ }
63
+ let src = args._[0];
64
+ let dest = args.dest || src;
65
+
66
+ let dataStr = fs.readFileSync(src);
67
+ let dataObj = JSON.parse(dataStr);
68
+ let options = [dataObj];
69
+ if (fn === 'item') {
70
+ let collestionStr = fs.readFileSync(args.collection_path);
71
+ let collestionObj = JSON.parse(collestionStr);
72
+ options.push(collestionObj);
73
+ }
74
+ Migrate[fn](...options)
75
+ dataStr = JSON.stringify(dataObj, null, args.indent);
76
+ fs.writeFileSync(dest, dataStr);
77
+ console.log("Migrated successfully: " + dest);
78
+ }
package/migrate.js CHANGED
@@ -402,6 +402,8 @@ var Catalog = {
402
402
  _.runAll(Catalog, catalog, catalog);
403
403
 
404
404
  V.before('0.8.0') && _.populateExtensions(catalog, 'catalog') && DONE;
405
+
406
+ return catalog;
405
407
  },
406
408
 
407
409
  openeo(obj) {
@@ -442,6 +444,8 @@ var Collection = {
442
444
 
443
445
  V.before('0.8.0') && _.populateExtensions(collection, 'collection') && DONE;
444
446
  V.before('1.0.0-beta.1') && _.mapValues(collection, 'stac_extensions', ['assets'], ['item-assets']) && DONE;
447
+
448
+ return collection;
445
449
  },
446
450
 
447
451
  extent(collection) {
@@ -652,10 +656,28 @@ var Item = {
652
656
 
653
657
  // Also populate extensions if commons has been implemented
654
658
  (V.before('0.8.0') || commons) && _.populateExtensions(item, 'item') && DONE;
659
+
660
+ return item;
655
661
  }
656
662
 
657
663
  };
658
664
 
665
+ var ItemCollection = {
666
+
667
+ migrate(itemCollection, updateVersionNumber = true) {
668
+ _.ensure(itemCollection, 'type', 'FeatureCollection') && DONE;
669
+ _.ensure(itemCollection, 'features', []) && DONE;
670
+ _.ensure(itemCollection, 'links', []) && DONE;
671
+
672
+ _.runAll(ItemCollection, itemCollection, itemCollection);
673
+
674
+ itemCollection.features = itemCollection.features.map(feature => Item.migrate(feature, null, updateVersionNumber));
675
+
676
+ return itemCollection;
677
+ },
678
+
679
+ };
680
+
659
681
  var Asset = {
660
682
 
661
683
  migrateAll(context, field = 'assets') {
@@ -666,7 +688,10 @@ var Asset = {
666
688
 
667
689
  migrate(asset, context) {
668
690
  _.runAll(Asset, asset, context);
691
+
669
692
  Fields.migrate(asset, context);
693
+
694
+ return asset;
670
695
  },
671
696
 
672
697
  mediaTypes(asset) {
@@ -699,6 +724,8 @@ var Fields = {
699
724
 
700
725
  migrate(obj, context, summaries = false) {
701
726
  _.runAll(Fields, obj, context, summaries);
727
+
728
+ return obj;
702
729
  },
703
730
 
704
731
  _commonMetadata(obj) {
@@ -892,24 +919,28 @@ var Fields = {
892
919
  var Migrate = {
893
920
 
894
921
  item(item, collection = null, updateVersionNumber = true) {
895
- Item.migrate(item, collection, updateVersionNumber);
896
- return item;
922
+ return Item.migrate(item, collection, updateVersionNumber);
897
923
  },
898
924
 
899
925
  catalog(catalog, updateVersionNumber = true) {
900
- Catalog.migrate(catalog, updateVersionNumber);
901
- return catalog;
926
+ return Catalog.migrate(catalog, updateVersionNumber);
902
927
  },
903
928
 
904
929
  collection(collection, updateVersionNumber = true) {
905
- Collection.migrate(collection, updateVersionNumber);
906
- return collection;
930
+ return Collection.migrate(collection, updateVersionNumber);
931
+ },
932
+
933
+ itemCollection(itemCollection, updateVersionNumber = true) {
934
+ return ItemCollection.migrate(itemCollection, updateVersionNumber);
907
935
  },
908
936
 
909
937
  stac(object, updateVersionNumber = true) {
910
938
  if (object.type === 'Feature') {
911
939
  return Migrate.item(object, null, updateVersionNumber);
912
940
  }
941
+ else if (object.type === 'FeatureCollection') {
942
+ return Migrate.itemCollection(object, updateVersionNumber);
943
+ }
913
944
  else if (object.type === 'Collection' || _.isDefined(object.extent) || _.isDefined(object.license)) {
914
945
  return Migrate.collection(object, updateVersionNumber);
915
946
  }
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "@radiantearth/stac-migrate",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "A library to update STAC files to the latest version.",
5
5
  "main": "migrate.js",
6
+ "bin": {
7
+ "stac-migrate": "./bin/cli.js"
8
+ },
6
9
  "bugs": {
7
10
  "url": "https://github.com/stac-utils/stac-migrate/issues"
8
11
  },
@@ -19,13 +22,15 @@
19
22
  "license": "Apache-2.0",
20
23
  "dependencies": {
21
24
  "compare-versions": "^3.6.0",
22
- "multihashes": "^3.1.2"
25
+ "multihashes": "^3.1.2",
26
+ "yargs": "^17.6.2"
23
27
  },
24
28
  "devDependencies": {
25
29
  "jest": "^29.0.0",
26
30
  "jest-html-reporter": "^3.3.0"
27
31
  },
28
32
  "files": [
33
+ "bin/cli.js",
29
34
  "migrate.js"
30
35
  ],
31
36
  "scripts": {