Skip to content

Commit

Permalink
feat(cli): Update the import cli to output preview links for individu…
Browse files Browse the repository at this point in the history
…al 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
  • Loading branch information
Wentao-Kuang authored Aug 21, 2023
1 parent 348a7f7 commit e70280a
Showing 1 changed file with 109 additions and 40 deletions.
149 changes: 109 additions & 40 deletions packages/cli/src/cli/config/action.import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import {
ConfigBundle,
ConfigBundled,
ConfigId,
ConfigLayer,
ConfigPrefix,
ConfigProviderMemory,
ConfigTileSet,
standardizeLayerName,
} from '@basemaps/config';
import { GoogleTms, Nztm2000QuadTms, Projection, TileMatrixSet } from '@basemaps/geo';
Expand Down Expand Up @@ -159,55 +161,119 @@ export class CommandImport extends CommandLineAction {
this.promises.push(promise);
}

async outputChange(output: string, mem: BasemapsConfigProvider, cfg: BasemapsConfigProvider): Promise<void> {
/**
* 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<void> {
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<void> {
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<void> {
// 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);
if (newData == null || oldData == null) throw new Error('Failed to fetch aerial config data.');
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
Expand All @@ -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('');

Expand Down

0 comments on commit e70280a

Please sign in to comment.