Skip to content

Commit

Permalink
Add the unit tests back and server cli for screenshots.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wentao-Kuang committed Oct 16, 2023
1 parent 157f9e4 commit abe178e
Show file tree
Hide file tree
Showing 10 changed files with 1,409 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"dependencies": {
"@basemaps/config": "^6.46.0",
"@basemaps/geo": "^6.44.0",
"@basemaps/server": "^6.46.0",
"@basemaps/shared": "^6.46.0",
"@basemaps/tiler": "^6.44.0",
"@basemaps/tiler-sharp": "^6.45.0",
Expand Down
60 changes: 60 additions & 0 deletions packages/cli/src/cli/server/action.serve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { createServer } from '@basemaps/server';
import { Const, Env, LogConfig } from '@basemaps/shared';
import {
CommandLineAction,
CommandLineFlagParameter,
CommandLineIntegerParameter,
CommandLineStringParameter,
} from '@rushstack/ts-command-line';

const DefaultPort = 5000;

export class CommandServe extends CommandLineAction {
config: CommandLineStringParameter;
assets: CommandLineStringParameter;
port: CommandLineIntegerParameter;
noConfig: CommandLineFlagParameter;

public constructor() {
super({
actionName: 'serve',
summary: 'Cli tool to create sprite sheet',
documentation: 'Create a sprite sheet from a folder of sprites',
});
}

protected onDefineParameters(): void {
this.config = this.defineStringParameter({
argumentName: 'CONFIG',
parameterLongName: '--config',
description: 'Configuration source to use',
});
this.assets = this.defineStringParameter({
argumentName: 'ASSETS',
parameterLongName: '--assets',
description: 'Where the assets (sprites, fonts) are located',
});
this.port = this.defineIntegerParameter({
argumentName: 'PORT',
parameterLongName: '--port',
description: 'port to use',
defaultValue: DefaultPort,
});
}

protected async onExecute(): Promise<void> {
const logger = LogConfig.get();

const config = this.config.value ?? 'dynamodb://' + Const.TileMetadata.TableName;
const port = this.port.value;
const assets = this.assets.value;

// Force a default url base so WMTS requests know their relative url
const ServerUrl = Env.get(Env.PublicUrlBase) ?? `http://localhost:${port}`;
process.env[Env.PublicUrlBase] = ServerUrl;

const server = await createServer({ config, assets }, logger);
await server.listen({ port: port ?? DefaultPort, host: '0.0.0.0' });
logger.info({ url: ServerUrl }, 'ServerStarted');
}
}
98 changes: 98 additions & 0 deletions packages/cli/src/cog/__tests__/builder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { Bounds, Epsg, EpsgCode, GoogleTms } from '@basemaps/geo';
import { LogConfig } from '@basemaps/shared';
import { fsa } from '@chunkd/fs';
import { CogTiff } from '@cogeotiff/core';

import o from 'ospec';
import { CogBuilder, guessProjection } from '../builder.js';

o.spec('Builder', () => {
o('should guess WKT', () => {
o(
guessProjection(
'PCS Name = NZGD_2000_New_Zealand_Transverse_Mercator|GCS Name = GCS_NZGD_2000|Ellipsoid = GRS_1980|Primem = Greenwich||',
),
).equals(Epsg.Nztm2000);

o(
guessProjection('NZGD2000_New_Zealand_Transverse_Mercator_2000|GCS Name = GCS_NZGD_2000|Primem = Greenwich||'),
).equals(Epsg.Nztm2000);
});

o('should not guess unknown wkt', () => {
o(guessProjection('')).equals(null);
o(guessProjection('NZTM')).equals(null);
o(guessProjection('NZGD2000')).equals(null);
});

o.spec('tiff', () => {
const googleBuilder = new CogBuilder(GoogleTms, 1, LogConfig.get());
const origInit = CogTiff.prototype.init;
const origGetImage = CogTiff.prototype.getImage;

o.after(() => {
CogTiff.prototype.init = origInit;
CogTiff.prototype.getImage = origGetImage;
});

o('bounds', async () => {
const localTiff = fsa.source('/local/file.tiff')!;
const s3Tiff = fsa.source('s3://bucket/file.tiff')!;

const imageLocal = {
resolution: [0.1],
value: (): any => [1],
valueGeo: (): any => EpsgCode.Nztm2000,
bbox: Bounds.fromJson({
x: 1492000,
y: 6198000,
width: 24000,
height: 36000,
}).toBbox(),
};

const imageS3 = {
resolution: [0.1],
value: (): any => [1],
valueGeo: (): any => EpsgCode.Nztm2000,
bbox: Bounds.fromJson({
x: 1492000 + 24000,
y: 6198000,
width: 24000,
height: 36000,
}).toBbox(),
};

CogTiff.prototype.init = o.spy() as any;
CogTiff.prototype.getImage = function (): any {
return this.source === localTiff ? imageLocal : imageS3;
};

const ans = await googleBuilder.bounds([localTiff, s3Tiff]);

o(ans).deepEquals({
projection: 2193,
nodata: 1,
bands: 1,
bounds: [
{
x: 1492000,
y: 6198000,
width: 24000,
height: 36000,
name: '/local/file.tiff',
},
{
x: 1516000,
y: 6198000,
width: 24000,
height: 36000,
name: 's3://bucket/file.tiff',
},
],
pixelScale: 0.1,
resZoom: 21,
});
});
});
});
Loading

0 comments on commit abe178e

Please sign in to comment.