@basemaps/lambda-tiler 7.3.0 → 7.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import { ConfigTileSetRaster, Layer, Sources, StyleJson, TileSetType } from '@basemaps/config';
2
- import { GoogleTms, TileMatrixSets } from '@basemaps/geo';
2
+ import { GoogleTms, TileMatrixSet, TileMatrixSets } from '@basemaps/geo';
3
3
  import { Env, toQueryString } from '@basemaps/shared';
4
4
  import { HttpHeader, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';
5
5
  import { URL } from 'url';
@@ -15,8 +15,14 @@ import { Validate } from '../util/validate.js';
15
15
  * @param apiKey ApiKey to append with ?api= if required
16
16
  * @returns Updated Url or empty string if url is empty
17
17
  */
18
- export function convertRelativeUrl(url?: string, apiKey?: string, config?: string | null): string {
18
+ export function convertRelativeUrl(
19
+ url?: string,
20
+ tileMatrix?: TileMatrixSet,
21
+ apiKey?: string,
22
+ config?: string | null,
23
+ ): string {
19
24
  if (url == null) return '';
25
+ if (tileMatrix) url = url.replace('{tileMatrix}', tileMatrix.identifier);
20
26
  const host = Env.get(Env.PublicUrlBase) ?? '';
21
27
  if (!url.startsWith('/')) return url; // Not relative ignore
22
28
  const fullUrl = new URL(url, host);
@@ -31,29 +37,39 @@ export function convertRelativeUrl(url?: string, apiKey?: string, config?: strin
31
37
  * @param apiKey api key to inject
32
38
  * @returns new stylejson
33
39
  */
34
- export function convertStyleJson(style: StyleJson, apiKey: string, config: string | null, layers?: Layer[]): StyleJson {
40
+ export function convertStyleJson(
41
+ style: StyleJson,
42
+ tileMatrix: TileMatrixSet,
43
+ apiKey: string,
44
+ config: string | null,
45
+ layers?: Layer[],
46
+ ): StyleJson {
35
47
  const sources: Sources = JSON.parse(JSON.stringify(style.sources));
36
48
  for (const [key, value] of Object.entries(sources)) {
37
49
  if (value.type === 'vector') {
38
- value.url = convertRelativeUrl(value.url, apiKey, config);
39
- } else if (value.type === 'raster' && Array.isArray(value.tiles)) {
50
+ if (tileMatrix !== GoogleTms) throw new Error(`TileMatrix is not supported for the vector source ${value.url}.`);
51
+ value.url = convertRelativeUrl(value.url, tileMatrix, apiKey, config);
52
+ } else if ((value.type === 'raster' || value.type === 'raster-dem') && Array.isArray(value.tiles)) {
40
53
  for (let i = 0; i < value.tiles.length; i++) {
41
- value.tiles[i] = convertRelativeUrl(value.tiles[i], apiKey, config);
54
+ value.tiles[i] = convertRelativeUrl(value.tiles[i], tileMatrix, apiKey, config);
42
55
  }
43
56
  }
44
57
  sources[key] = value;
45
58
  }
46
59
 
47
- return {
60
+ const styleJson: StyleJson = {
48
61
  version: 8,
49
62
  id: style.id,
50
63
  name: style.name,
51
64
  sources,
52
65
  layers: layers ? layers : style.layers,
53
- metadata: style.metadata ?? {},
54
- glyphs: convertRelativeUrl(style.glyphs, undefined, config),
55
- sprite: convertRelativeUrl(style.sprite, undefined, config),
56
- } as StyleJson;
66
+ };
67
+
68
+ if (style.metadata) styleJson.metadata = style.metadata;
69
+ if (style.glyphs) styleJson.glyphs = convertRelativeUrl(style.glyphs, undefined, undefined, config);
70
+ if (style.sprite) styleJson.sprite = convertRelativeUrl(style.sprite, undefined, undefined, config);
71
+
72
+ return styleJson;
57
73
  }
58
74
 
59
75
  export interface StyleGet {
@@ -65,10 +81,9 @@ export interface StyleGet {
65
81
  export async function tileSetToStyle(
66
82
  req: LambdaHttpRequest<StyleGet>,
67
83
  tileSet: ConfigTileSetRaster,
84
+ tileMatrix: TileMatrixSet,
68
85
  apiKey: string,
69
86
  ): Promise<LambdaHttpResponse> {
70
- const tileMatrix = TileMatrixSets.find(req.query.get('tileMatrix') ?? GoogleTms.identifier);
71
- if (tileMatrix == null) return new LambdaHttpResponse(400, 'Invalid tile matrix');
72
87
  const [tileFormat] = Validate.getRequestedFormats(req) ?? ['webp'];
73
88
  if (tileFormat == null) return new LambdaHttpResponse(400, 'Invalid image format');
74
89
 
@@ -104,11 +119,9 @@ export async function tileSetToStyle(
104
119
  export async function tileSetOutputToStyle(
105
120
  req: LambdaHttpRequest<StyleGet>,
106
121
  tileSet: ConfigTileSetRaster,
122
+ tileMatrix: TileMatrixSet,
107
123
  apiKey: string,
108
124
  ): Promise<LambdaHttpResponse> {
109
- const tileMatrix = TileMatrixSets.find(req.query.get('tileMatrix') ?? GoogleTms.identifier);
110
- if (tileMatrix == null) return new LambdaHttpResponse(400, 'Invalid tile matrix');
111
-
112
125
  const configLocation = ConfigLoader.extract(req);
113
126
  const query = toQueryString({ config: configLocation, api: apiKey });
114
127
 
@@ -178,6 +191,8 @@ export async function styleJsonGet(req: LambdaHttpRequest<StyleGet>): Promise<La
178
191
  const styleName = req.params.styleName;
179
192
  const excludeLayers = req.query.getAll('exclude');
180
193
  const excluded = new Set(excludeLayers.map((l) => l.toLowerCase()));
194
+ const tileMatrix = TileMatrixSets.find(req.query.get('tileMatrix') ?? GoogleTms.identifier);
195
+ if (tileMatrix == null) return new LambdaHttpResponse(400, 'Invalid tile matrix');
181
196
 
182
197
  // Get style Config from db
183
198
  const config = await ConfigLoader.load(req);
@@ -188,13 +203,14 @@ export async function styleJsonGet(req: LambdaHttpRequest<StyleGet>): Promise<La
188
203
  const tileSet = await config.TileSet.get(config.TileSet.id(styleName));
189
204
  if (tileSet == null) return NotFound();
190
205
  if (tileSet.type !== TileSetType.Raster) return NotFound();
191
- if (tileSet.outputs) return tileSetOutputToStyle(req, tileSet, apiKey);
192
- else return tileSetToStyle(req, tileSet, apiKey);
206
+ if (tileSet.outputs) return tileSetOutputToStyle(req, tileSet, tileMatrix, apiKey);
207
+ else return tileSetToStyle(req, tileSet, tileMatrix, apiKey);
193
208
  }
194
209
 
195
210
  // Prepare sources and add linz source
196
211
  const style = convertStyleJson(
197
212
  styleConfig.style,
213
+ tileMatrix,
198
214
  apiKey,
199
215
  ConfigLoader.extract(req),
200
216
  styleConfig.style.layers.filter((f) => !excluded.has(f.id.toLowerCase())),