Skip to content

Commit

Permalink
fix(lambda-tiler): wmts should support tile pipelines (#3305)
Browse files Browse the repository at this point in the history
### Motivation

When viewing elevation via WMTS you need to supply a terrain-rgb
pipeline
<!-- TODO: Say why you made your changes. -->

### Modifications

adds a default pipeline to wmts outputs if requested
Removes unused filter logic

<!-- TODO: Say what changes you made. -->

<!-- TODO: Attach screenshots if you changed the UI. -->

### Verification

<!-- TODO: Say how you tested your changes. -->

---------

Co-authored-by: Wentao Kuang <wkuang@linz.govt.nz>
  • Loading branch information
blacha and Wentao-Kuang committed Jul 1, 2024
1 parent 4dc0068 commit 3ff3f7f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
24 changes: 23 additions & 1 deletion packages/lambda-tiler/src/routes/__tests__/wmts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { afterEach, beforeEach, describe, it } from 'node:test';
import { ConfigProviderMemory, ConfigTileSetRaster } from '@basemaps/config';
import { Env } from '@basemaps/shared';

import { Imagery2193, Imagery3857, Provider, TileSetAerial } from '../../__tests__/config.data.js';
import { Imagery2193, Imagery3857, Provider, TileSetAerial, TileSetElevation } from '../../__tests__/config.data.js';
import { Api, mockUrlRequest } from '../../__tests__/xyz.util.js';
import { handler } from '../../index.js';
import { ConfigLoader } from '../../util/config.loader.js';
Expand All @@ -27,6 +27,28 @@ describe('WMTSRouting', () => {
config.objects.clear();
});

it('should support pipeline', async (t) => {
t.mock.method(Env, 'get', (arg: string) => {
if (arg === Env.PublicUrlBase) return 'https://tiles.test';
return process.env[arg];
});
config.put(TileSetElevation);
t.mock.method(ConfigLoader, 'load', () => Promise.resolve(config));
const req = mockUrlRequest(
'/v1/tiles/elevation/WebMercatorQuad/WMTSCapabilities.xml',
`tileFormat=png&api=${Api.key}&config=s3://linz-basemaps/config.json&pipeline=terrain-rgb`,
);
const res = await handler.router.handle(req);

assert.equal(res.status, 200);
const lines = Buffer.from(res.body, 'base64').toString().split('\n');
const resourceUrl = lines.find((f) => f.includes('ResourceURL'))?.trim();

assert.ok(resourceUrl);
assert.ok(resourceUrl.includes('amp;pipeline=terrain-rgb'), `includes pipeline=terrain-rgb in ${resourceUrl}`);
assert.ok(resourceUrl.includes('.png'), `includes .png in ${resourceUrl}`);
});

it('should default to the aerial layer', async (t) => {
t.mock.method(Env, 'get', (arg: string) => {
if (arg === Env.PublicUrlBase) return 'https://tiles.test';
Expand Down
1 change: 1 addition & 0 deletions packages/lambda-tiler/src/routes/tile.wmts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export async function wmtsCapabilitiesGet(req: LambdaHttpRequest<WmtsCapabilitie
imagery,
formats: Validate.getRequestedFormats(req) ?? [],
layers: req.params.tileMatrix == null ? tileSet.layers : undefined,
pipeline: req.query.get('pipeline'),
});

const xml = wmts.toXml();
Expand Down
26 changes: 17 additions & 9 deletions packages/lambda-tiler/src/wmts.capability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ export interface WmtsBuilderParams {
apiKey?: string;
/** Config location */
config?: string | null;
/** Specific DateRange filter for the wmts layers */
filters?: Record<string, string | undefined>;
/** Default pipeline to use */
pipeline?: string;
}

export class WmtsBuilder {
httpBase: string;
apiKey?: string;
config?: string | null;
pipeline?: string;
filters?: Record<string, string | undefined>;

/** All the imagery used by the tileSet and tileMatrixes */
Expand All @@ -58,7 +59,7 @@ export class WmtsBuilder {
this.httpBase = params.httpBase;
this.apiKey = params.apiKey;
this.config = params.config;
this.filters = params.filters;
this.pipeline = params.pipeline;
}

addImagery(...imagery: ConfigImagery[]): void {
Expand Down Expand Up @@ -153,17 +154,17 @@ export class WmtsBuilder {
return V('Style', { isDefault: 'true' }, [V('ows:Title', 'Default Style'), V('ows:Identifier', 'default')]);
}

buildResourceUrl(tileSetId: string, suffix: string, addFilter = false): VNodeElement {
buildResourceUrl(tileSetId: string, suffix: string): VNodeElement {
return V('ResourceURL', {
format: 'image/' + suffix,
resourceType: 'tile',
template: this.buildTileUrl(tileSetId, suffix, addFilter),
template: this.buildTileUrl(tileSetId, suffix),
});
}

buildTileUrl(tileSetId: string, suffix: string, addFilter = false): string {
let query = { api: this.apiKey, config: this.config };
if (addFilter) query = { api: this.apiKey, config: this.config, ...this.filters };
buildTileUrl(tileSetId: string, suffix: string): string {
// TODO this should restrict the output formats to supported formats in pipelines
const query = { api: this.apiKey, config: this.config, pipeline: this.pipeline };

return [
this.httpBase,
Expand Down Expand Up @@ -239,6 +240,8 @@ export interface WmtsCapabilitiesParams {
formats: ImageFormat[];
/** Specific layers to add to the WMTS */
layers?: ConfigLayer[];
/** Default output pipeline to use */
pipeline?: string | null;
}

/**
Expand Down Expand Up @@ -277,6 +280,10 @@ export class WmtsCapabilities extends WmtsBuilder {
this.provider = provider;
}

addPipeline(pipeline: string): void {
this.pipeline = pipeline;
}

toProviderVNode(provider?: WmtsProvider): VNodeElement[] | [] {
if (provider == null) return [];
const { serviceIdentification, serviceProvider } = provider;
Expand Down Expand Up @@ -338,7 +345,7 @@ export class WmtsCapabilities extends WmtsBuilder {
this.buildStyle(),
...this.buildFormats(),
...this.buildTileMatrixLink(tileSet),
...this.getFormats().map((fmt) => this.buildResourceUrl(layerNameId, fmt, true)),
...this.getFormats().map((fmt) => this.buildResourceUrl(layerNameId, fmt)),
]);
}

Expand Down Expand Up @@ -408,5 +415,6 @@ export class WmtsCapabilities extends WmtsBuilder {
this.addTileSet(params.tileSet);
this.addLayers(params.layers);
this.addProvider(params.provider);
if (params.pipeline) this.addPipeline(params.pipeline);
}
}

0 comments on commit 3ff3f7f

Please sign in to comment.