@basemaps/lambda-tiler 6.19.0 → 6.20.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.
Files changed (157) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/LICENSE +2 -2
  3. package/build/__test__/tile.style.json.test.d.ts +2 -0
  4. package/build/__test__/tile.style.json.test.d.ts.map +1 -0
  5. package/build/__test__/tile.style.json.test.js +33 -0
  6. package/build/__test__/xyz.test.js +24 -23
  7. package/build/cli/dump.js +2 -2
  8. package/build/index.d.ts.map +1 -1
  9. package/build/index.js +2 -0
  10. package/build/routes/esri/rest.d.ts +10 -0
  11. package/build/routes/esri/rest.d.ts.map +1 -0
  12. package/build/routes/esri/rest.js +87 -0
  13. package/build/routes/imagery.d.ts +2 -0
  14. package/build/routes/imagery.d.ts.map +1 -1
  15. package/build/routes/imagery.js +3 -1
  16. package/build/routes/response.d.ts +4 -0
  17. package/build/routes/response.d.ts.map +1 -0
  18. package/build/routes/response.js +3 -0
  19. package/build/routes/tile.d.ts +0 -20
  20. package/build/routes/tile.d.ts.map +1 -1
  21. package/build/routes/tile.js +11 -161
  22. package/build/routes/tile.json.d.ts +10 -0
  23. package/build/routes/tile.json.d.ts.map +1 -0
  24. package/build/routes/tile.json.js +31 -0
  25. package/build/routes/tile.style.json.d.ts +10 -0
  26. package/build/routes/tile.style.json.d.ts.map +1 -0
  27. package/build/routes/tile.style.json.js +74 -0
  28. package/build/routes/tile.wmts.d.ts +9 -0
  29. package/build/routes/tile.wmts.d.ts.map +1 -0
  30. package/build/routes/tile.wmts.js +57 -0
  31. package/build/routes/tile.xyz.d.ts +13 -0
  32. package/build/routes/tile.xyz.d.ts.map +1 -0
  33. package/build/routes/tile.xyz.js +28 -0
  34. package/build/tile.set.raster.d.ts +2 -0
  35. package/build/tile.set.raster.d.ts.map +1 -1
  36. package/build/tile.set.raster.js +3 -1
  37. package/build/tile.set.vector.js +1 -1
  38. package/dist/index.js +10545 -9583
  39. package/dist/node_modules/color/README.md +7 -7
  40. package/dist/node_modules/color/index.js +173 -158
  41. package/dist/node_modules/color/package.json +18 -16
  42. package/dist/node_modules/color-convert/conversions.js +281 -310
  43. package/dist/node_modules/color-convert/index.js +27 -24
  44. package/dist/node_modules/color-convert/package.json +16 -14
  45. package/dist/node_modules/color-convert/route.js +22 -22
  46. package/dist/node_modules/color-name/package.json +17 -14
  47. package/dist/node_modules/color-string/package.json +5 -5
  48. package/dist/node_modules/decompress-response/index.d.ts +14 -21
  49. package/dist/node_modules/decompress-response/index.js +34 -16
  50. package/dist/node_modules/decompress-response/license +1 -1
  51. package/dist/node_modules/decompress-response/package.json +23 -17
  52. package/dist/node_modules/decompress-response/readme.md +2 -6
  53. package/dist/node_modules/detect-libc/package.json +0 -1
  54. package/dist/node_modules/mimic-response/index.d.ts +2 -2
  55. package/dist/node_modules/mimic-response/index.js +41 -2
  56. package/dist/node_modules/mimic-response/package.json +17 -17
  57. package/dist/node_modules/mimic-response/readme.md +22 -1
  58. package/dist/node_modules/node-abi/.circleci/config.yml +63 -0
  59. package/dist/node_modules/node-abi/.releaserc.json +9 -0
  60. package/dist/node_modules/node-abi/abi_registry.json +39 -4
  61. package/dist/node_modules/node-abi/index.js +5 -2
  62. package/dist/node_modules/node-abi/package.json +17 -16
  63. package/dist/node_modules/node-abi/test/index.js +7 -15
  64. package/dist/node_modules/node-addon-api/README.md +2 -2
  65. package/dist/node_modules/node-addon-api/except.gypi +20 -11
  66. package/dist/node_modules/node-addon-api/napi-inl.h +734 -196
  67. package/dist/node_modules/node-addon-api/napi.h +420 -164
  68. package/dist/node_modules/node-addon-api/noexcept.gypi +21 -11
  69. package/dist/node_modules/node-addon-api/package.json +43 -14
  70. package/dist/node_modules/node-addon-api/tools/clang-format.js +18 -17
  71. package/dist/node_modules/node-addon-api/tools/eslint-format.js +71 -0
  72. package/dist/node_modules/prebuild-install/CHANGELOG.md +24 -7
  73. package/dist/node_modules/prebuild-install/README.md +24 -4
  74. package/dist/node_modules/prebuild-install/asset.js +10 -10
  75. package/dist/node_modules/prebuild-install/bin.js +13 -13
  76. package/dist/node_modules/prebuild-install/download.js +22 -22
  77. package/dist/node_modules/prebuild-install/log.js +4 -4
  78. package/dist/node_modules/prebuild-install/node_modules/detect-libc/LICENSE +201 -0
  79. package/dist/node_modules/prebuild-install/node_modules/detect-libc/README.md +160 -0
  80. package/dist/node_modules/prebuild-install/node_modules/detect-libc/index.d.ts +11 -0
  81. package/dist/node_modules/prebuild-install/node_modules/detect-libc/lib/detect-libc.js +178 -0
  82. package/dist/node_modules/prebuild-install/node_modules/detect-libc/lib/process.js +16 -0
  83. package/dist/node_modules/prebuild-install/node_modules/detect-libc/package.json +71 -0
  84. package/dist/node_modules/prebuild-install/package.json +18 -19
  85. package/dist/node_modules/prebuild-install/proxy.js +10 -10
  86. package/dist/node_modules/prebuild-install/rc.js +12 -12
  87. package/dist/node_modules/prebuild-install/util.js +14 -14
  88. package/dist/node_modules/semver/package.json +1 -0
  89. package/dist/node_modules/sharp/README.md +2 -2
  90. package/dist/node_modules/sharp/binding.gyp +12 -9
  91. package/dist/node_modules/sharp/build/Release/sharp-linux-x64.node +0 -0
  92. package/dist/node_modules/sharp/install/dll-copy.js +6 -6
  93. package/dist/node_modules/sharp/install/libvips.js +4 -8
  94. package/dist/node_modules/sharp/lib/channel.js +11 -7
  95. package/dist/node_modules/sharp/lib/colour.js +42 -1
  96. package/dist/node_modules/sharp/lib/constructor.js +18 -31
  97. package/dist/node_modules/sharp/lib/input.js +45 -3
  98. package/dist/node_modules/sharp/lib/is.js +19 -5
  99. package/dist/node_modules/sharp/lib/libvips.js +4 -19
  100. package/dist/node_modules/sharp/lib/operation.js +28 -5
  101. package/dist/node_modules/sharp/lib/output.js +147 -16
  102. package/dist/node_modules/sharp/lib/sharp.js +31 -0
  103. package/dist/node_modules/sharp/lib/utility.js +3 -2
  104. package/dist/node_modules/sharp/package.json +32 -23
  105. package/dist/node_modules/sharp/src/common.cc +67 -11
  106. package/dist/node_modules/sharp/src/common.h +25 -5
  107. package/dist/node_modules/sharp/src/libvips/cplusplus/VConnection.cpp +0 -26
  108. package/dist/node_modules/sharp/src/libvips/cplusplus/VImage.cpp +54 -16
  109. package/dist/node_modules/sharp/src/libvips/cplusplus/VInterpolate.cpp +0 -13
  110. package/dist/node_modules/sharp/src/libvips/cplusplus/vips-operators.cpp +185 -1
  111. package/dist/node_modules/sharp/src/metadata.cc +14 -0
  112. package/dist/node_modules/sharp/src/metadata.h +1 -0
  113. package/dist/node_modules/sharp/src/operations.cc +29 -3
  114. package/dist/node_modules/sharp/src/operations.h +13 -2
  115. package/dist/node_modules/sharp/src/pipeline.cc +103 -35
  116. package/dist/node_modules/sharp/src/pipeline.h +23 -3
  117. package/dist/node_modules/sharp/src/utilities.cc +1 -1
  118. package/dist/node_modules/sharp/vendor/{8.10.6 → 8.11.3/linux-x64}/THIRD-PARTY-NOTICES.md +2 -3
  119. package/dist/node_modules/sharp/vendor/{8.10.6 → 8.11.3/linux-x64}/lib/libvips-cpp.so.42 +0 -0
  120. package/dist/node_modules/sharp/vendor/{8.10.6 → 8.11.3/linux-x64}/platform.json +0 -0
  121. package/dist/node_modules/sharp/vendor/8.11.3/linux-x64/versions.json +30 -0
  122. package/dist/node_modules/simple-get/.github/dependabot.yml +15 -0
  123. package/dist/node_modules/simple-get/.github/workflows/ci.yml +23 -0
  124. package/dist/node_modules/simple-get/README.md +17 -3
  125. package/dist/node_modules/simple-get/index.js +9 -0
  126. package/dist/node_modules/simple-get/package.json +27 -13
  127. package/dist/package-lock.json +51 -51
  128. package/dist/package.json +9 -9
  129. package/package.json +10 -10
  130. package/src/__test__/tile.style.json.test.ts +40 -0
  131. package/src/__test__/xyz.test.ts +32 -30
  132. package/src/cli/dump.ts +2 -2
  133. package/src/index.ts +2 -0
  134. package/src/routes/esri/rest.ts +90 -0
  135. package/src/routes/imagery.ts +3 -1
  136. package/src/routes/response.ts +4 -0
  137. package/src/routes/tile.json.ts +44 -0
  138. package/src/routes/tile.style.json.ts +77 -0
  139. package/src/routes/tile.ts +11 -183
  140. package/src/routes/tile.wmts.ts +59 -0
  141. package/src/routes/tile.xyz.ts +30 -0
  142. package/src/tile.set.raster.ts +4 -1
  143. package/src/tile.set.vector.ts +1 -1
  144. package/tsconfig.tsbuildinfo +1 -1
  145. package/dist/node_modules/color-name/.eslintrc.json +0 -43
  146. package/dist/node_modules/color-name/test.js +0 -7
  147. package/dist/node_modules/node-abi/.travis.yml +0 -17
  148. package/dist/node_modules/node-abi/node_modules/semver/CHANGELOG.md +0 -39
  149. package/dist/node_modules/node-abi/node_modules/semver/LICENSE +0 -15
  150. package/dist/node_modules/node-abi/node_modules/semver/README.md +0 -412
  151. package/dist/node_modules/node-abi/node_modules/semver/bin/semver +0 -160
  152. package/dist/node_modules/node-abi/node_modules/semver/package.json +0 -60
  153. package/dist/node_modules/node-abi/node_modules/semver/range.bnf +0 -16
  154. package/dist/node_modules/node-abi/node_modules/semver/semver.js +0 -1483
  155. package/dist/node_modules/node-addon-api/CHANGELOG.md +0 -722
  156. package/dist/node_modules/sharp/build/Release/sharp.node +0 -0
  157. package/dist/node_modules/sharp/vendor/8.10.6/versions.json +0 -31
@@ -0,0 +1,90 @@
1
+ import { GoogleTms, TileMatrixSet } from '@basemaps/geo';
2
+ import { tileXyzFromPath } from '@basemaps/shared';
3
+ import { LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';
4
+ import { Router } from '../../router.js';
5
+ import { TileSets } from '../../tile.set.cache.js';
6
+ import { NotFound } from '../response.js';
7
+
8
+ export async function vectorTileServer(
9
+ req: LambdaHttpRequest,
10
+ layerId: string,
11
+ tms: TileMatrixSet,
12
+ ): Promise<LambdaHttpResponse> {
13
+ if (tms.identifier !== GoogleTms.identifier) return NotFound;
14
+ const extent = {
15
+ xmin: tms.extent.x,
16
+ ymin: tms.extent.y,
17
+ xmax: tms.extent.right,
18
+ ymax: tms.extent.bottom,
19
+ // TODO where is wkid from
20
+ spatialReference: { wkid: 102100, latestWkid: tms.projection.code },
21
+ };
22
+
23
+ const res = new LambdaHttpResponse(200, 'ok');
24
+ res.json({
25
+ currentVersion: 10.4,
26
+ name: layerId,
27
+ capabilities: 'TilesOnly',
28
+ type: 'indexedVector',
29
+ tileMap: 'tilemap',
30
+ defaultStyles: 'resources/styles',
31
+ tiles: ['tiles/{z}/{x}/{y}.pbf'],
32
+ exportTilesAllowed: false,
33
+ maxExportTilesCount: 0,
34
+ initialExtent: extent,
35
+ fullExtent: extent,
36
+ minScale: tms.zooms[0].scaleDenominator,
37
+ maxScale: tms.zooms[tms.zooms.length - 1].scaleDenominator,
38
+ tileInfo: {
39
+ // TODO are all the pbf 256x256?
40
+ rows: 256,
41
+ cols: 256,
42
+ dpi: 96,
43
+ format: 'pbf',
44
+ origin: { x: tms.extent.x, y: tms.extent.bottom },
45
+ spatialReference: { wkid: 102100, latestWkid: tms.projection.code },
46
+ lods: tms.zooms.map((c, i) => {
47
+ return {
48
+ level: i,
49
+ scale: c.scaleDenominator,
50
+ resolution: c.scaleDenominator * 0.28e-3,
51
+ };
52
+ }),
53
+ },
54
+ resourceInfo: {
55
+ styleVersion: 8,
56
+ tileCompression: 'gzip',
57
+ cacheInfo: { storageInfo: { packetSize: 128, storageFormat: 'compactV2' } },
58
+ },
59
+ });
60
+ return res;
61
+ }
62
+
63
+ /**
64
+ * /v1/esri/services/:layerId/VectorTileServer
65
+ *
66
+ * @example http://localhost:5000/v1/esri/services/topographic/VectorTileServer
67
+ */
68
+ export async function Esri(req: LambdaHttpRequest): Promise<LambdaHttpResponse> {
69
+ const { rest } = Router.action(req);
70
+ if (rest[0] !== 'services') return NotFound;
71
+ const layerId = rest[1];
72
+ if (layerId == null) return NotFound;
73
+
74
+ const serviceId = rest[2];
75
+ if (serviceId !== 'VectorTileServer') return NotFound;
76
+ if (rest.length === 3) return vectorTileServer(req, layerId, GoogleTms);
77
+
78
+ if (rest[rest.length - 1].endsWith('.pbf')) {
79
+ const generatedPath = [layerId, GoogleTms.identifier, ...rest.slice(rest.length - 3)];
80
+ const xyz = tileXyzFromPath(generatedPath);
81
+ if (xyz == null) return NotFound;
82
+ req.timer.start('tileset:load');
83
+ const tileSet = await TileSets.get(xyz.name, xyz.tileMatrix);
84
+ req.timer.end('tileset:load');
85
+ if (tileSet == null) return NotFound;
86
+ return await tileSet.tile(req, xyz);
87
+ }
88
+
89
+ return new LambdaHttpResponse(200, 'ok');
90
+ }
@@ -5,8 +5,8 @@ import { createHash } from 'crypto';
5
5
  import { promisify } from 'util';
6
6
  import { gzip } from 'zlib';
7
7
  import { Router } from '../router.js';
8
+ import { NotModified } from './response.js';
8
9
  import { TileEtag } from './tile.etag.js';
9
- import { NotModified } from './tile.js';
10
10
 
11
11
  const gzipP = promisify(gzip);
12
12
 
@@ -18,6 +18,8 @@ function isAllowedFile(f: string): boolean {
18
18
 
19
19
  /**
20
20
  * Get metadata around the imagery such as the source bounding box or the bounding box of the COGS
21
+ *
22
+ * @example
21
23
  * - /v1/imagery/:imageryId/source.geojson
22
24
  * - /v1/imagery/:imageryId/covering.geojson
23
25
  */
@@ -0,0 +1,4 @@
1
+ import { LambdaHttpResponse } from '@linzjs/lambda';
2
+
3
+ export const NotFound = new LambdaHttpResponse(404, 'Not Found');
4
+ export const NotModified = new LambdaHttpResponse(304, 'Not modified');
@@ -0,0 +1,44 @@
1
+ import { Env } from '@basemaps/shared';
2
+ import { HttpHeader, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';
3
+ import { createHash } from 'crypto';
4
+ import { Router } from '../router.js';
5
+ import { NotModified } from './response.js';
6
+ import { TileEtag } from './tile.etag.js';
7
+
8
+ export interface TileJson {
9
+ tiles: string[];
10
+ minzoom: number;
11
+ maxzoom: number;
12
+ format: string;
13
+ tilejson: string;
14
+ }
15
+
16
+ export async function tileJson(req: LambdaHttpRequest): Promise<LambdaHttpResponse> {
17
+ const { version, rest, name } = Router.action(req);
18
+ const apiKey = Router.apiKey(req);
19
+ const host = Env.get(Env.PublicUrlBase) ?? '';
20
+ const tileUrl = `${host}/${version}/${name}/${rest[0]}/${rest[1]}/{z}/{x}/{y}.pbf?api=${apiKey}`;
21
+
22
+ const tileJson: TileJson = {
23
+ tiles: [tileUrl],
24
+ minzoom: 0,
25
+ maxzoom: 15,
26
+ format: 'pbf',
27
+ tilejson: '2.0.0',
28
+ };
29
+
30
+ const json = JSON.stringify(tileJson);
31
+
32
+ const data = Buffer.from(json);
33
+
34
+ const cacheKey = createHash('sha256').update(data).digest('base64');
35
+
36
+ if (TileEtag.isNotModified(req, cacheKey)) return NotModified;
37
+
38
+ const response = new LambdaHttpResponse(200, 'ok');
39
+ response.header(HttpHeader.ETag, cacheKey);
40
+ response.header(HttpHeader.CacheControl, 'max-age=120');
41
+ response.buffer(data, 'application/json');
42
+ req.set('bytes', data.byteLength);
43
+ return response;
44
+ }
@@ -0,0 +1,77 @@
1
+ import { Sources, StyleJson } from '@basemaps/config';
2
+ import { Config, Env } from '@basemaps/shared';
3
+ import { fsa } from '@chunkd/fs';
4
+ import { HttpHeader, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';
5
+ import { createHash } from 'crypto';
6
+ import { URL } from 'url';
7
+ import { Router } from '../router.js';
8
+ import { NotFound, NotModified } from './response.js';
9
+ import { TileEtag } from './tile.etag.js';
10
+
11
+ /**
12
+ * Convert relative URLS into a full hostname url
13
+ * @param url possible url to update
14
+ * @param apiKey ApiKey to append with ?api= if required
15
+ * @returns Updated Url or empty string if url is empty
16
+ */
17
+ export function convertRelativeUrl(url?: string, apiKey?: string): string {
18
+ if (url == null) return '';
19
+ const host = Env.get(Env.PublicUrlBase) ?? '';
20
+ if (!url.startsWith('/')) return url; // Not relative ignore
21
+ const fullUrl = new URL(fsa.join(host, url));
22
+ if (apiKey) fullUrl.searchParams.set('api', apiKey);
23
+ return fullUrl.toString().replace(/%7B/g, '{').replace(/%7D/g, '}');
24
+ }
25
+
26
+ export async function styleJson(req: LambdaHttpRequest, fileName: string): Promise<LambdaHttpResponse> {
27
+ const apiKey = Router.apiKey(req);
28
+ if (apiKey == null) return new LambdaHttpResponse(400, 'Invalid API Key.');
29
+ const styleName = fileName.split('.json')[0];
30
+
31
+ // Get style Config from db
32
+ const dbId = Config.Style.id(styleName);
33
+ const styleConfig = await Config.Style.get(dbId);
34
+ if (styleConfig == null) return NotFound;
35
+
36
+ // Prepare sources and add linz source
37
+ const style = styleConfig.style;
38
+ const sources: Sources = {};
39
+ for (const [key, value] of Object.entries(style.sources)) {
40
+ if (value.type === 'vector') {
41
+ value.url = convertRelativeUrl(value.url, apiKey);
42
+ } else if (value.type === 'raster' && Array.isArray(value.tiles)) {
43
+ for (let i = 0; i < value.tiles.length; i++) {
44
+ value.tiles[i] = convertRelativeUrl(value.tiles[i], apiKey);
45
+ }
46
+ }
47
+ sources[key] = value;
48
+ }
49
+
50
+ // prepare Style.json
51
+ const styleJson: StyleJson = {
52
+ /** Style.json version 8 */
53
+ version: 8,
54
+ id: style.id,
55
+ name: style.name,
56
+ sources,
57
+ layers: style.layers,
58
+ metadata: style.metadata || {},
59
+ glyphs: convertRelativeUrl(style.glyphs),
60
+ sprite: convertRelativeUrl(style.sprite),
61
+ };
62
+
63
+ const json = JSON.stringify(styleJson);
64
+
65
+ const data = Buffer.from(json);
66
+
67
+ const cacheKey = createHash('sha256').update(data).digest('base64');
68
+
69
+ if (TileEtag.isNotModified(req, cacheKey)) return NotModified;
70
+
71
+ const response = new LambdaHttpResponse(200, 'ok');
72
+ response.header(HttpHeader.ETag, cacheKey);
73
+ response.header(HttpHeader.CacheControl, 'no-store');
74
+ response.buffer(data, 'application/json');
75
+ req.set('bytes', data.byteLength);
76
+ return response;
77
+ }
@@ -1,185 +1,13 @@
1
- import { Sources, StyleJson } from '@basemaps/config';
2
- import { TileMatrixSet } from '@basemaps/geo';
3
- import { HttpHeader, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';
4
- import { Config, Env, setNameAndProjection, TileSetName, tileWmtsFromPath, tileXyzFromPath } from '@basemaps/shared';
5
- import { TileMakerSharp } from '@basemaps/tiler-sharp';
6
- import { createHash } from 'crypto';
1
+ import { LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';
7
2
  import { isValidApiKey } from '../api.key.js';
8
- import { TileSets } from '../tile.set.cache.js';
9
- import { TileSetRaster } from '../tile.set.raster.js';
10
- import { WmtsCapabilities } from '../wmts.capability.js';
11
- import { attribution } from './attribution.js';
12
- import { TileEtag } from './tile.etag.js';
13
3
  import { Router } from '../router.js';
14
- import { ValidateTilePath } from '../validate.js';
15
- import { URL } from 'url';
16
- import { fsa } from '@chunkd/fs';
17
-
18
- export const TileComposer = new TileMakerSharp(256);
19
-
20
- export const NotFound = new LambdaHttpResponse(404, 'Not Found');
21
- export const NotModified = new LambdaHttpResponse(304, 'Not modified');
22
-
23
- export interface TileJson {
24
- tiles: string[];
25
- minzoom: number;
26
- maxzoom: number;
27
- format: string;
28
- tilejson: string;
29
- }
30
-
31
- export const TileRoute = {
32
- async tile(req: LambdaHttpRequest): Promise<LambdaHttpResponse> {
33
- const action = Router.action(req);
34
- const xyzData = tileXyzFromPath(action.rest);
35
- if (xyzData == null) return NotFound;
36
- ValidateTilePath.validate(req, xyzData);
37
-
38
- req.timer.start('tileset:load');
39
- const tileSet = await TileSets.get(xyzData.name, xyzData.tileMatrix);
40
- req.timer.end('tileset:load');
41
- if (tileSet == null) return NotFound;
42
-
43
- const res = await tileSet.tile(req, xyzData);
44
- return res;
45
- },
46
-
47
- async wmtsLoadTileSets(name: string, tileMatrix: TileMatrixSet | null): Promise<TileSetRaster[]> {
48
- if (tileMatrix != null) {
49
- const ts = await TileSets.get(name, tileMatrix);
50
- if (ts == null || ts.isVector()) return [];
51
- return [ts];
52
- }
53
- if (name === '') name = TileSetName.aerial;
54
- return (await TileSets.getAll(name, tileMatrix)).filter((f) => f.type === 'raster') as TileSetRaster[];
55
- },
56
-
57
- async wmts(req: LambdaHttpRequest): Promise<LambdaHttpResponse> {
58
- const action = Router.action(req);
59
- const wmtsData = tileWmtsFromPath(action.rest);
60
- if (wmtsData == null) return NotFound;
61
- setNameAndProjection(req, wmtsData);
62
- const host = Env.get(Env.PublicUrlBase) ?? '';
63
-
64
- req.timer.start('tileset:load');
65
- const tileSets = await TileRoute.wmtsLoadTileSets(wmtsData.name, wmtsData.tileMatrix);
66
- req.timer.end('tileset:load');
67
- if (tileSets.length === 0) return NotFound;
68
-
69
- const providerId = Config.Provider.id('linz');
70
- const provider = await Config.Provider.get(providerId);
71
- if (provider == null) return NotFound;
72
-
73
- const apiKey = Router.apiKey(req);
74
- const xml = WmtsCapabilities.toXml(host, provider, tileSets, apiKey);
75
- if (xml == null) return NotFound;
76
-
77
- const data = Buffer.from(xml);
78
-
79
- const cacheKey = createHash('sha256').update(data).digest('base64');
80
- if (TileEtag.isNotModified(req, cacheKey)) return NotModified;
81
-
82
- const response = new LambdaHttpResponse(200, 'ok');
83
- response.header(HttpHeader.ETag, cacheKey);
84
- response.header(HttpHeader.CacheControl, 'max-age=0');
85
- response.buffer(data, 'text/xml');
86
- req.set('bytes', data.byteLength);
87
- return response;
88
- },
89
-
90
- async tileJson(req: LambdaHttpRequest): Promise<LambdaHttpResponse> {
91
- const { version, rest, name } = Router.action(req);
92
- const apiKey = Router.apiKey(req);
93
- const host = Env.get(Env.PublicUrlBase) ?? '';
94
- const tileUrl = `${host}/${version}/${name}/${rest[0]}/${rest[1]}/{z}/{x}/{y}.pbf?api=${apiKey}`;
95
-
96
- const tileJson: TileJson = {
97
- tiles: [tileUrl],
98
- minzoom: 0,
99
- maxzoom: 15,
100
- format: 'pbf',
101
- tilejson: '2.0.0',
102
- };
103
-
104
- const json = JSON.stringify(tileJson);
105
-
106
- const data = Buffer.from(json);
107
-
108
- const cacheKey = createHash('sha256').update(data).digest('base64');
109
-
110
- if (TileEtag.isNotModified(req, cacheKey)) return NotModified;
111
-
112
- const response = new LambdaHttpResponse(200, 'ok');
113
- response.header(HttpHeader.ETag, cacheKey);
114
- response.header(HttpHeader.CacheControl, 'max-age=120');
115
- response.buffer(data, 'application/json');
116
- req.set('bytes', data.byteLength);
117
- return response;
118
- },
119
-
120
- async styleJson(req: LambdaHttpRequest, fileName: string): Promise<LambdaHttpResponse> {
121
- const apiKey = Router.apiKey(req);
122
- if (apiKey == null) return new LambdaHttpResponse(400, 'Invalid API Key.');
123
- const styleName = fileName.split('.json')[0];
124
- const host = Env.get(Env.PublicUrlBase) ?? '';
125
-
126
- // Get style Config from db
127
- const dbId = Config.Style.id(styleName);
128
- const styleConfig = await Config.Style.get(dbId);
129
- if (styleConfig == null) return NotFound;
130
-
131
- // Prepare sources and add linz source
132
- const style = styleConfig.style;
133
- const sources: Sources = {};
134
- for (const [key, value] of Object.entries(style.sources)) {
135
- if (value.type === 'vector') {
136
- if (value.url.startsWith('/')) {
137
- const url = new URL(fsa.join(host, value.url));
138
- url.searchParams.set('api', apiKey);
139
- value.url = url.toString().replace(/%7B/g, '{').replace(/%7D/g, '}');
140
- }
141
- } else if (value.type === 'raster' && Array.isArray(value.tiles)) {
142
- for (let i = 0; i < value.tiles.length; i++) {
143
- const tile = value.tiles[i];
144
- if (tile.startsWith('/')) {
145
- const url = new URL(fsa.join(host, tile));
146
- url.searchParams.set('api', apiKey);
147
- value.tiles[i] = url.toString().replace(/%7B/g, '{').replace(/%7D/g, '}');
148
- }
149
- }
150
- }
151
- sources[key] = value;
152
- }
153
-
154
- // prepare Style.json
155
- const styleJson: StyleJson = {
156
- /** Style.json version 8 */
157
- version: 8,
158
- id: style.id,
159
- name: style.name,
160
- sources,
161
- layers: style.layers,
162
- metadata: style.metadata || {},
163
- glyphs: style.glyphs || '',
164
- sprite: style.sprite || '',
165
- };
166
-
167
- const json = JSON.stringify(styleJson);
168
-
169
- const data = Buffer.from(json);
170
-
171
- const cacheKey = createHash('sha256').update(data).digest('base64');
172
-
173
- if (TileEtag.isNotModified(req, cacheKey)) return NotModified;
4
+ import { attribution } from './attribution.js';
5
+ import { NotFound } from './response.js';
6
+ import { tileJson } from './tile.json.js';
7
+ import { styleJson } from './tile.style.json.js';
8
+ import { wmts } from './tile.wmts.js';
9
+ import { tileXyz } from './tile.xyz.js';
174
10
 
175
- const response = new LambdaHttpResponse(200, 'ok');
176
- response.header(HttpHeader.ETag, cacheKey);
177
- response.header(HttpHeader.CacheControl, 'no-store');
178
- response.buffer(data, 'application/json');
179
- req.set('bytes', data.byteLength);
180
- return response;
181
- },
182
- };
183
11
  export async function Tiles(req: LambdaHttpRequest): Promise<LambdaHttpResponse> {
184
12
  const { rest } = Router.action(req);
185
13
  if (rest.length < 1) return NotFound;
@@ -188,8 +16,8 @@ export async function Tiles(req: LambdaHttpRequest): Promise<LambdaHttpResponse>
188
16
 
189
17
  const fileName = rest[rest.length - 1].toLowerCase();
190
18
  if (fileName === 'attribution.json') return attribution(req);
191
- if (fileName === 'wmtscapabilities.xml') return TileRoute.wmts(req);
192
- if (fileName === 'tile.json') return TileRoute.tileJson(req);
193
- if (fileName.endsWith('json') && rest[rest.length - 2] === 'style') return TileRoute.styleJson(req, fileName);
194
- return TileRoute.tile(req);
19
+ if (fileName === 'wmtscapabilities.xml') return wmts(req);
20
+ if (fileName === 'tile.json') return tileJson(req);
21
+ if (fileName.endsWith('json') && rest[rest.length - 2] === 'style') return styleJson(req, fileName);
22
+ return tileXyz(req);
195
23
  }
@@ -0,0 +1,59 @@
1
+ import { Config } from '@basemaps/config';
2
+ import { TileMatrixSet } from '@basemaps/geo';
3
+ import { Env, TileSetName, tileWmtsFromPath } from '@basemaps/shared';
4
+ import { HttpHeader, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';
5
+ import { createHash } from 'crypto';
6
+ import { Router } from '../router.js';
7
+ import { TileSets } from '../tile.set.cache.js';
8
+ import { TileSetRaster } from '../tile.set.raster.js';
9
+ import { WmtsCapabilities } from '../wmts.capability.js';
10
+ import { NotFound, NotModified } from './response.js';
11
+ import { TileEtag } from './tile.etag.js';
12
+
13
+ /**
14
+ * Serve a WMTS request
15
+ *
16
+ * /v1/tiles/:tileSet/:tileMatrixSet/WMTSCapabilities.xml
17
+ * @example `/v1/tiles/aerial/NZTM2000Quad/WMTSCapabilities.xml`
18
+ */
19
+ export async function wmts(req: LambdaHttpRequest): Promise<LambdaHttpResponse> {
20
+ const action = Router.action(req);
21
+ const wmtsData = tileWmtsFromPath(action.rest);
22
+ if (wmtsData == null) return NotFound;
23
+ const host = Env.get(Env.PublicUrlBase) ?? '';
24
+
25
+ req.timer.start('tileset:load');
26
+ const tileSets = await wmtsLoadTileSets(wmtsData.name, wmtsData.tileMatrix);
27
+ req.timer.end('tileset:load');
28
+ if (tileSets.length === 0) return NotFound;
29
+
30
+ const providerId = Config.Provider.id('linz');
31
+ const provider = await Config.Provider.get(providerId);
32
+ if (provider == null) return NotFound;
33
+
34
+ const apiKey = Router.apiKey(req);
35
+ const xml = WmtsCapabilities.toXml(host, provider, tileSets, apiKey);
36
+ if (xml == null) return NotFound;
37
+
38
+ const data = Buffer.from(xml);
39
+
40
+ const cacheKey = createHash('sha256').update(data).digest('base64');
41
+ if (TileEtag.isNotModified(req, cacheKey)) return NotModified;
42
+
43
+ const response = new LambdaHttpResponse(200, 'ok');
44
+ response.header(HttpHeader.ETag, cacheKey);
45
+ response.header(HttpHeader.CacheControl, 'max-age=0');
46
+ response.buffer(data, 'text/xml');
47
+ req.set('bytes', data.byteLength);
48
+ return response;
49
+ }
50
+
51
+ async function wmtsLoadTileSets(name: string, tileMatrix: TileMatrixSet | null): Promise<TileSetRaster[]> {
52
+ if (tileMatrix != null) {
53
+ const ts = await TileSets.get(name, tileMatrix);
54
+ if (ts == null || ts.isVector()) return [];
55
+ return [ts];
56
+ }
57
+ if (name === '') name = TileSetName.aerial;
58
+ return (await TileSets.getAll(name, tileMatrix)).filter((f) => f.type === 'raster') as TileSetRaster[];
59
+ }
@@ -0,0 +1,30 @@
1
+ import { tileXyzFromPath } from '@basemaps/shared';
2
+ import { LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';
3
+ import { Router } from '../router.js';
4
+ import { TileSets } from '../tile.set.cache.js';
5
+ import { ValidateTilePath } from '../validate.js';
6
+ import { NotFound } from './response.js';
7
+
8
+ /**
9
+ * Serve a tile
10
+ *
11
+ * /v1/tiles/:tileSet/:tileMatrixSet/:z/:x/:y.:tileType
12
+ *
13
+ * @example
14
+ * Vector Tile `/v1/tiles/topographic/EPSG:3857/2/1/1.pbf`
15
+ * Raster Tile `/v1/tiles/aerial/EPSG:3857/6/0/38.webp`
16
+ * @returns
17
+ */
18
+ export async function tileXyz(req: LambdaHttpRequest): Promise<LambdaHttpResponse> {
19
+ const action = Router.action(req);
20
+ const xyzData = tileXyzFromPath(action.rest);
21
+ if (xyzData == null) return NotFound;
22
+ ValidateTilePath.validate(req, xyzData);
23
+
24
+ req.timer.start('tileset:load');
25
+ const tileSet = await TileSets.get(xyzData.name, xyzData.tileMatrix);
26
+ req.timer.end('tileset:load');
27
+ if (tileSet == null) return NotFound;
28
+
29
+ return await tileSet.tile(req, xyzData);
30
+ }
@@ -2,17 +2,20 @@ import { ConfigImagery, ConfigLayer, ConfigTileSetRaster, TileSetNameParser, Til
2
2
  import { Bounds, Epsg, Tile, TileMatrixSet, TileMatrixSets } from '@basemaps/geo';
3
3
  import { Config, Env, fsa, LogType, TileDataXyz, titleizeImageryName, VectorFormat } from '@basemaps/shared';
4
4
  import { Tiler } from '@basemaps/tiler';
5
+ import { TileMakerSharp } from '@basemaps/tiler-sharp';
5
6
  import { CogTiff } from '@cogeotiff/core';
6
7
  import { HttpHeader, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';
7
8
  import { Metrics } from '@linzjs/metrics';
8
9
  import pLimit from 'p-limit';
10
+ import { NotFound, NotModified } from './routes/response.js';
9
11
  import { TileEtag } from './routes/tile.etag.js';
10
- import { NotFound, NotModified, TileComposer } from './routes/tile.js';
11
12
  import { TiffCache } from './tiff.cache.js';
12
13
  import { TileSetHandler } from './tile.set.js';
13
14
 
14
15
  const LoadingQueue = pLimit(Env.getNumber(Env.TiffConcurrency, 5));
15
16
 
17
+ export const TileComposer = new TileMakerSharp(256);
18
+
16
19
  export interface TileSetResponse {
17
20
  buffer: Buffer;
18
21
  metrics: Metrics;
@@ -2,7 +2,7 @@ import { ConfigTileSetVector, TileSetType } from '@basemaps/config';
2
2
  import { fsa, TileDataXyz, VectorFormat } from '@basemaps/shared';
3
3
  import { Cotar } from '@cotar/core';
4
4
  import { HttpHeader, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';
5
- import { NotFound } from './routes/tile.js';
5
+ import { NotFound } from './routes/response.js';
6
6
  import { TileSetHandler } from './tile.set.js';
7
7
 
8
8
  class CotarCache {