From e70280a5729f75cc808f149d81cdad3051bb2574 Mon Sep 17 00:00:00 2001 From: Wentao Kuang Date: Mon, 21 Aug 2023 13:25:39 +1200 Subject: [PATCH] feat(cli): Update the import cli to output preview links for individual configs. BM-869 (#2897) ### Why? As we separated expired/disabled configs as individual config files in [basemaps-config](https://github.com/linz/basemaps-config) repo. Now we need the ci process to automatically pick the config changes and inserts for individual imagery. #### Issue Link: [BM-869](https://toitutewhenua.atlassian.net/browse/BM-869) ### Description of Changes: - Update the basemaps cli import command to output changes for all tileset configs that only have a single layer. - relocate and reuse the prepare preview links logic into separated functions - Tested locally and test output markdown will update into PR comments soon. #### Author Checklist: - [ ] Tests updated - [ ] Docs updated - [ ] Issue linked in PR title [BM-869]: https://toitutewhenua.atlassian.net/browse/BM-869?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ --- packages/cli/src/cli/config/action.import.ts | 149 ++++++++++++++----- 1 file changed, 109 insertions(+), 40 deletions(-) diff --git a/packages/cli/src/cli/config/action.import.ts b/packages/cli/src/cli/config/action.import.ts index 2b6caf172..789a5a8bf 100644 --- a/packages/cli/src/cli/config/action.import.ts +++ b/packages/cli/src/cli/config/action.import.ts @@ -4,8 +4,10 @@ import { ConfigBundle, ConfigBundled, ConfigId, + ConfigLayer, ConfigPrefix, ConfigProviderMemory, + ConfigTileSet, standardizeLayerName, } from '@basemaps/config'; import { GoogleTms, Nztm2000QuadTms, Projection, TileMatrixSet } from '@basemaps/geo'; @@ -159,10 +161,96 @@ export class CommandImport extends CommandLineAction { this.promises.push(promise); } - async outputChange(output: string, mem: BasemapsConfigProvider, cfg: BasemapsConfigProvider): Promise { + /** + * This function prepare for the markdown lines with preview urls for new inserted config layers + * @param mem new config data read from config bundle file + * @param layer new config tileset layer + * @param inserts string array to save all the lines for markdown output + * @param aerial output preview link for aerial map + */ + async outputNewLayers( + mem: ConfigProviderMemory, + layer: ConfigLayer, + inserts: string[], + aerial?: boolean, + ): Promise { + inserts.push(`### ${layer.name}\n`); + if (layer[2193]) { + const urls = await this.prepareUrl(layer[2193], mem, Nztm2000QuadTms); + inserts.push(` - [NZTM2000Quad](${urls.layer})`); + if (aerial) inserts.push(` -- [Aerial](${urls.tag})\n`); + else inserts.push('\n'); + } + if (layer[3857]) { + const urls = await this.prepareUrl(layer[3857], mem, GoogleTms); + inserts.push(` - [WebMercatorQuad](${urls.layer})`); + if (aerial) inserts.push(` -- [Aerial](${urls.tag})\n`); + else inserts.push('\n'); + } + } + + /** + * This function compared new config tileset layer with existing one, then output the markdown lines for updates and preview urls. + * @param mem new config data read from config bundle file + * @param layer new config tileset layer + * @param existing existing config tileset layer + * @param updates string array to save all the lines for markdown output + * @param aerial output preview link for aerial map + */ + async outputUpdatedLayers( + mem: ConfigProviderMemory, + layer: ConfigLayer, + existing: ConfigLayer, + updates: string[], + aerial?: boolean, + ): Promise { + let zoom = undefined; + if (layer.minZoom !== existing.minZoom || layer.maxZoom !== existing.maxZoom) { + zoom = ' - Zoom level updated.'; + if (layer.minZoom !== existing.minZoom) zoom += ` min zoom ${existing.minZoom} -> ${layer.minZoom}`; + if (layer.maxZoom !== existing.maxZoom) zoom += ` max zoom ${existing.maxZoom} -> ${layer.maxZoom}`; + } + + const change: string[] = [`### ${layer.name}\n`]; + if (layer[2193]) { + const urls = await this.prepareUrl(layer[2193], mem, Nztm2000QuadTms); + if (layer[2193] !== existing[2193]) { + change.push(`- Layer update [NZTM2000Quad](${urls.layer})`); + if (aerial) updates.push(` -- [Aerial](${urls.tag})\n`); + else updates.push('\n'); + } + if (zoom) zoom += ` [NZTM2000Quad](${urls.tag})`; + } + if (layer[3857]) { + const urls = await this.prepareUrl(layer[3857], mem, GoogleTms); + if (layer[3857] !== existing[3857]) { + change.push(`- Layer update [WebMercatorQuad](${urls.layer})`); + if (aerial) updates.push(` -- [Aerial](${urls.tag})\n`); + else updates.push('\n'); + } + if (zoom) zoom += ` [WebMercatorQuad](${urls.tag})`; + } + + if (zoom) change.push(`${zoom}\n`); + if (change.length > 1) updates.push(change.join('')); + } + + /** + * This function compared new config with existing and output a markdown document to highlight the inserts and changes + * Changes includes + * - aerial config + * - vector config + * - vector style + * - individual config + * + * @param output a string of output markdown location + * @param mem new config data read from config bundle file + * @param cfg existing config data + */ + async outputChange(output: string, mem: ConfigProviderMemory, cfg: BasemapsConfigProvider): Promise { // Output for aerial config changes - const inserts: string[] = ['# New Layers\n']; - const updates: string[] = ['# Updates\n']; + const inserts: string[] = ['# Aerial Config Inserts\n']; + const updates: string[] = ['# Aerial Config Updates\n']; const aerialId = 'ts_aerial'; const newData = await mem.TileSet.get(aerialId); const oldData = await cfg.TileSet.get(aerialId); @@ -170,44 +258,22 @@ export class CommandImport extends CommandLineAction { for (const layer of newData.layers) { if (layer.name === 'chatham-islands_digital-globe_2014-2019_0-5m') continue; // Ignore duplicated layer. const existing = oldData.layers.find((l) => l.name === layer.name); - if (existing) { - let zoom = undefined; - if (layer.minZoom !== existing.minZoom || layer.maxZoom !== existing.maxZoom) { - zoom = ' - Zoom level updated.'; - if (layer.minZoom !== existing.minZoom) zoom += ` min zoom ${existing.minZoom} -> ${layer.minZoom}`; - if (layer.maxZoom !== existing.maxZoom) zoom += ` max zoom ${existing.maxZoom} -> ${layer.maxZoom}`; - } - - const change: string[] = [`### ${layer.name}\n`]; - if (layer[2193]) { - const urls = await this.prepareUrl(layer[2193], mem, Nztm2000QuadTms); - if (layer[2193] !== existing[2193]) { - change.push(`- Layer update [NZTM2000Quad](${urls.layer}) -- [Aerial](${urls.tag})\n`); - } - if (zoom) zoom += ` [NZTM2000Quad](${urls.tag})`; - } - if (layer[3857]) { - const urls = await this.prepareUrl(layer[3857], mem, GoogleTms); - if (layer[3857] !== existing[3857]) { - change.push(`- Layer update [WebMercatorQuad](${urls.layer}) -- [Aerial](${urls.tag})\n`); - } - if (zoom) zoom += ` [WebMercatorQuad](${urls.tag})`; - } + if (existing) await this.outputUpdatedLayers(mem, layer, existing, updates, true); + else await this.outputNewLayers(mem, layer, inserts, true); + } - if (zoom) change.push(`${zoom}\n`); - if (change.length > 1) updates.push(change.join('')); - } else { - // New layers - inserts.push(`### ${layer.name}\n`); - if (layer[2193]) { - const urls = await this.prepareUrl(layer[2193], mem, Nztm2000QuadTms); - inserts.push(` - [NZTM2000Quad](${urls.layer}) -- [Aerial](${urls.tag})\n`); - } - if (layer[3857]) { - const urls = await this.prepareUrl(layer[3857], mem, GoogleTms); - inserts.push(` - [WebMercatorQuad](${urls.layer}) -- [Aerial](${urls.tag})\n`); - } - } + // Output for individual tileset config changes or inserts + const individualInserts: string[] = ['# Individual Layer Inserts\n']; + const individualUpdates: string[] = ['# Individual Layer Updates\n']; + for (const config of mem.objects.values()) { + if (!config.id.startsWith(ConfigPrefix.TileSet)) continue; + if (config.id === 'ts_aerial' || config.id === 'ts_topographic') continue; + const tileSet = config as ConfigTileSet; + if (tileSet.layers.length > 1) continue; // Not an individual layer + const existing = await cfg.TileSet.get(config.id); + const layer = tileSet.layers[0]; + if (existing) await this.outputUpdatedLayers(mem, layer, existing.layers[0], individualUpdates); + else await this.outputNewLayers(mem, layer, individualInserts); } // Output for vector config changes @@ -226,9 +292,12 @@ export class CommandImport extends CommandLineAction { styleUpdate.push(`* [${style}](${PublicUrlBase}?config=${this.config.value}&i=topographic&s=${style}&debug)\n`); } } + let md = ''; if (inserts.length > 1) md += inserts.join(''); if (updates.length > 1) md += updates.join(''); + if (individualInserts.length > 1) md += individualInserts.join(''); + if (individualUpdates.length > 1) md += individualUpdates.join(''); if (vectorUpdate.length > 1) md += vectorUpdate.join(''); if (styleUpdate.length > 1) md += styleUpdate.join('');