From 24087c374cdb7d8b908aebdb657f841ed1804859 Mon Sep 17 00:00:00 2001 From: Tawera Manaena Date: Wed, 5 Feb 2025 09:33:59 +1300 Subject: [PATCH] refactor(cogify): simplify the StacItem creation function --- .../cogify/src/cogify/topo/stac.creation.ts | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/packages/cogify/src/cogify/topo/stac.creation.ts b/packages/cogify/src/cogify/topo/stac.creation.ts index 16ddaab78..18d2df110 100644 --- a/packages/cogify/src/cogify/topo/stac.creation.ts +++ b/packages/cogify/src/cogify/topo/stac.creation.ts @@ -90,36 +90,41 @@ export function createStacItems( latest: Map, logger?: LogType, ): { all: TopoStacItem[]; latest: TopoStacItem[] } { - const allStacItems = all.map((item) => - createBaseStacItem(`${item.mapCode}_${item.version}`, item, tileMatrix, logger), - ); - - // add link to all items pointing to the latest version and create stac for latest items - for (const item of allStacItems) { - const latestTiff = latest.get(item.properties.map_code); - if (latestTiff == null) throw new Error(`Failed to find latest item for map code '${item.properties.map_code}'`); - item.links.push({ + // create origin StacItem files + const allStacItems = all.map((item) => { + const latestTiff = latest.get(item.mapCode); + if (latestTiff == null) throw new Error(`Failed to find latest item for map code '${item.mapCode}'`); + + const originStacItem = createBaseStacItem(`${item.mapCode}_${item.version}`, item, tileMatrix, logger); + + // add link referencing the 'latest version' origin StacItem file that will live in the same directory + originStacItem.links.push({ href: `./${latestTiff.mapCode}_${latestTiff.version}.json`, rel: 'latest-version', type: 'application/json', }); - } + return originStacItem; + }); + + // create latest StacItem files const latestStacItems = Array.from(latest.values()).map((item) => { const latestStacItem = createBaseStacItem(item.mapCode, item, tileMatrix, logger); + + // add link referencing this StacItem's origin file that will live in the topo[50/250] directory latestStacItem.links.push({ - // from: /_latest// - // ../../../ = - // to: /// + // directory into which we save this StacItem file: /_latest///[latest_stac_item] + // directory inside which we save this StacItem's origin file: ////[origin_stac_item] + // + // `../../../` takes us up to the directory href: `../../../${scale}/${resolution}/${item.epsg.code}/${item.mapCode}_${item.version}.json`, rel: 'derived-from', type: 'application/json', }); + return latestStacItem; }); - // add link to the latest item referencing its copy that will live in the topo[50/250] directory - return { latest: latestStacItems, all: allStacItems }; }