Skip to content

Commit

Permalink
feat(lambda-tiler): automatically rescale style JSON's into NZTM2000Q…
Browse files Browse the repository at this point in the history
…uad when requests (#3339)

### Motivation

NZTM2000Quad is offset by two zoom levels from web mercator quad, when
the stylejsons are created for both projections the style json needs to
adjust all the zoom levels by two

### Modifications

Convert styles zoom offsets automatically when asking for a NZTM2000Quad
style json
updated comments across the style json endpoint
refactored the style generation into a single point.

### Verification

unit tests
  • Loading branch information
blacha authored Aug 29, 2024
1 parent 8eede97 commit 960b926
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 152 deletions.
23 changes: 16 additions & 7 deletions packages/lambda-tiler/src/__tests__/tile.style.json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { StyleJson } from '@basemaps/config';
import { GoogleTms, Nztm2000QuadTms } from '@basemaps/geo';
import { Env } from '@basemaps/shared';

import { convertRelativeUrl, convertStyleJson } from '../routes/tile.style.json.js';
import { convertRelativeUrl, setStyleUrls } from '../routes/tile.style.json.js';

describe('TileStyleJson', () => {
const host = 'https://tiles.test';
Expand Down Expand Up @@ -75,7 +75,8 @@ describe('TileStyleJson', () => {

it('should not destroy the original configuration', () => {
const apiKey = 'abc123';
const converted = convertStyleJson(baseStyleJson, GoogleTms, apiKey, null);
const converted = structuredClone(baseStyleJson);
setStyleUrls(converted, GoogleTms, apiKey, null);

assert.deepEqual(converted.sources['vector'], {
type: 'vector',
Expand All @@ -92,7 +93,9 @@ describe('TileStyleJson', () => {

assert.equal(JSON.stringify(baseStyleJson).includes(apiKey), false);

const convertedB = convertStyleJson(baseStyleJson, GoogleTms, '0x1234', null);
const convertedB = structuredClone(baseStyleJson);

setStyleUrls(convertedB, GoogleTms, '0x1234', null);
assert.deepEqual(convertedB.sources['vector'], {
type: 'vector',
url: 'https://tiles.test/v1/tiles/topographic/WebMercatorQuad/tile.json?api=0x1234',
Expand Down Expand Up @@ -124,7 +127,8 @@ describe('TileStyleJson', () => {

it('should cover raster style Json without metadata, sprite and glyphs', () => {
const apiKey = 'abc123';
const converted = convertStyleJson(rasterStyleJson, GoogleTms, apiKey, null);
const converted = structuredClone(rasterStyleJson);
setStyleUrls(rasterStyleJson, GoogleTms, apiKey, null);

assert.equal(converted.metadata, null);
assert.equal(converted.sprite, null);
Expand All @@ -140,7 +144,8 @@ describe('TileStyleJson', () => {
},
};

const converted = convertStyleJson(rasterStyleJson, Nztm2000QuadTms, 'abc123', null);
const converted = structuredClone(rasterStyleJson);
setStyleUrls(converted, Nztm2000QuadTms, 'abc123', null);

assert.deepEqual(converted.sources['raster'], {
type: 'raster',
Expand All @@ -154,7 +159,9 @@ describe('TileStyleJson', () => {

it('should convert relative glyphs and sprites', () => {
const apiKey = '0x9f9f';
const converted = convertStyleJson(baseStyleJson, GoogleTms, apiKey, null);
const converted = structuredClone(baseStyleJson);

setStyleUrls(converted, GoogleTms, apiKey, null);
assert.equal(converted.sprite, 'https://tiles.test/v1/sprites');
assert.equal(converted.glyphs, 'https://tiles.test/v1/glyphs');

Expand All @@ -164,7 +171,9 @@ describe('TileStyleJson', () => {

it('should convert with config', () => {
const apiKey = '0x9f9f';
const converted = convertStyleJson(baseStyleJson, GoogleTms, apiKey, 'config.json');
const converted = structuredClone(baseStyleJson);

setStyleUrls(converted, GoogleTms, apiKey, 'config.json');
assert.equal(converted.sprite, 'https://tiles.test/v1/sprites?config=config.json');
assert.equal(converted.glyphs, 'https://tiles.test/v1/glyphs?config=config.json');

Expand Down
60 changes: 60 additions & 0 deletions packages/lambda-tiler/src/routes/__tests__/tile.style.json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,64 @@ describe('/v1/styles', () => {
assert.equal(res.status, 400, res.statusDescription);
assert.equal(res.statusDescription.includes('Background1'), true);
});

it('should convert NZTM2000Quad styles', async () => {
const fakeStyle = {
id: 'st_labels',
name: 'source',
style: {
layers: [
{
minzoom: 5,
maxzoom: 5,
layout: {
'line-width': {
stops: [
[16, 0.75],
[24, 1.5],
],
},
},

paint: {
'line-width': {
stops: [
[16, 0.75],
[24, 1.5],
],
},
},
},
],
terrain: {
exaggeration: 1.1,
},
},
};
config.put(fakeStyle);

const request = mockUrlRequest('/v1/styles/labels.json', `?tileMatrix=NZTM2000Quad`, Api.header);
const res = await handler.router.handle(request);
assert.equal(res.status, 200, res.statusDescription);
const style = JSON.parse(Buffer.from(res.body, 'base64').toString()) as StyleJson;
assert.equal(style.layers[0].minzoom, 3);
assert.equal(style.layers[0].maxzoom, 3);
assert.equal(style.terrain?.exaggeration, 4.4);
assert.deepEqual(style.layers[0].layout, {
'line-width': {
stops: [
[14, 0.75],
[22, 1.5],
],
},
});
assert.deepEqual(style.layers[0].paint, {
'line-width': {
stops: [
[14, 0.75],
[22, 1.5],
],
},
});
});
});
Loading

0 comments on commit 960b926

Please sign in to comment.