@basemaps/lambda-tiler 6.43.0 → 6.44.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.
- package/CHANGELOG.md +14 -0
- package/build/cli/render.preview.d.ts +2 -0
- package/build/cli/render.preview.d.ts.map +1 -0
- package/build/cli/render.preview.js +38 -0
- package/build/cli/render.preview.js.map +1 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js +6 -0
- package/build/index.js.map +1 -1
- package/build/routes/__tests__/preview.index.test.d.ts +2 -0
- package/build/routes/__tests__/preview.index.test.d.ts.map +1 -0
- package/build/routes/__tests__/preview.index.test.js +82 -0
- package/build/routes/__tests__/preview.index.test.js.map +1 -0
- package/build/routes/preview.d.ts +58 -0
- package/build/routes/preview.d.ts.map +1 -0
- package/build/routes/preview.index.d.ts +23 -0
- package/build/routes/preview.index.d.ts.map +1 -0
- package/build/routes/preview.index.js +98 -0
- package/build/routes/preview.index.js.map +1 -0
- package/build/routes/preview.js +159 -0
- package/build/routes/preview.js.map +1 -0
- package/build/routes/tile.xyz.raster.d.ts +15 -0
- package/build/routes/tile.xyz.raster.d.ts.map +1 -1
- package/build/routes/tile.xyz.raster.js +45 -24
- package/build/routes/tile.xyz.raster.js.map +1 -1
- package/build/util/validate.d.ts +3 -1
- package/build/util/validate.d.ts.map +1 -1
- package/build/util/validate.js +10 -0
- package/build/util/validate.js.map +1 -1
- package/bundle.sh +1 -1
- package/package.json +8 -8
- package/src/cli/render.preview.ts +44 -0
- package/src/index.ts +7 -0
- package/src/routes/__tests__/preview.index.test.ts +94 -0
- package/src/routes/preview.index.ts +119 -0
- package/src/routes/preview.ts +234 -0
- package/src/routes/tile.xyz.raster.ts +53 -28
- package/src/util/validate.ts +10 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tile.xyz.raster.d.ts","sourceRoot":"","sources":["../../src/routes/tile.xyz.raster.ts"],"names":[],"mappings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
@@ -3,6 +3,7 @@ import { Bounds, Epsg, TileMatrixSet, TileMatrixSets, VectorFormat } from '@base
|
|
|
3
3
|
import { Env, fsa } from '@basemaps/shared';
|
|
4
4
|
import { Tiler } from '@basemaps/tiler';
|
|
5
5
|
import { TileMakerSharp } from '@basemaps/tiler-sharp';
|
|
6
|
+
import { CogTiff } from '@cogeotiff/core';
|
|
6
7
|
import { HttpHeader, LambdaHttpResponse } from '@linzjs/lambda';
|
|
7
8
|
import pLimit from 'p-limit';
|
|
8
9
|
import { ConfigLoader } from '../util/config.loader.js';
|
|
@@ -17,39 +18,52 @@ export function getTiffName(name) {
|
|
|
17
18
|
return name;
|
|
18
19
|
return `${name}.tiff`;
|
|
19
20
|
}
|
|
21
|
+
/** Check to see if a cloud archive is a Tiff or a Cotar */
|
|
22
|
+
export function isArchiveTiff(x) {
|
|
23
|
+
if (x instanceof CogTiff)
|
|
24
|
+
return true;
|
|
25
|
+
if (x.source.uri.endsWith('.tiff'))
|
|
26
|
+
return true;
|
|
27
|
+
if (x.source.uri.endsWith('.tif'))
|
|
28
|
+
return true;
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
20
31
|
export const TileComposer = new TileMakerSharp(256);
|
|
21
|
-
const DefaultResizeKernel = { in: 'lanczos3', out: 'lanczos3' };
|
|
22
|
-
const DefaultBackground = { r: 0, g: 0, b: 0, alpha: 0 };
|
|
32
|
+
export const DefaultResizeKernel = { in: 'lanczos3', out: 'lanczos3' };
|
|
33
|
+
export const DefaultBackground = { r: 0, g: 0, b: 0, alpha: 0 };
|
|
23
34
|
export const TileXyzRaster = {
|
|
24
|
-
async
|
|
35
|
+
async getAssetsForBounds(req, tileSet, tileMatrix, bounds, zoom, ignoreOverview = false) {
|
|
25
36
|
const config = await ConfigLoader.load(req);
|
|
26
|
-
const imagery = await getAllImagery(config, tileSet.layers, [
|
|
37
|
+
const imagery = await getAllImagery(config, tileSet.layers, [tileMatrix.projection]);
|
|
27
38
|
const filteredLayers = filterLayers(req, tileSet.layers);
|
|
28
39
|
const output = [];
|
|
29
|
-
const tileBounds = xyz.tileMatrix.tileToSourceBounds(xyz.tile);
|
|
30
40
|
// All zoom level config is stored as Google zoom levels
|
|
31
|
-
const filterZoom = TileMatrixSet.convertZoomLevel(
|
|
41
|
+
const filterZoom = TileMatrixSet.convertZoomLevel(zoom, tileMatrix, TileMatrixSets.get(Epsg.Google));
|
|
32
42
|
for (const layer of filteredLayers) {
|
|
33
43
|
if (layer.maxZoom != null && filterZoom > layer.maxZoom)
|
|
34
44
|
continue;
|
|
35
45
|
if (layer.minZoom != null && filterZoom < layer.minZoom)
|
|
36
46
|
continue;
|
|
37
|
-
const imgId = layer[
|
|
38
|
-
|
|
39
|
-
|
|
47
|
+
const imgId = layer[tileMatrix.projection.code];
|
|
48
|
+
// Imagery does not exist for this projection
|
|
49
|
+
if (imgId == null)
|
|
40
50
|
continue;
|
|
41
|
-
}
|
|
42
51
|
const img = imagery.get(imgId);
|
|
43
52
|
if (img == null) {
|
|
44
|
-
req.log.warn({ layer: layer.name, projection:
|
|
53
|
+
req.log.warn({ layer: layer.name, projection: tileMatrix.projection.code, imgId }, 'Failed to lookup imagery');
|
|
45
54
|
continue;
|
|
46
55
|
}
|
|
47
|
-
if (!
|
|
56
|
+
if (!bounds.intersects(Bounds.fromJson(img.bounds)))
|
|
48
57
|
continue;
|
|
49
58
|
for (const c of img.files) {
|
|
50
|
-
if (!
|
|
59
|
+
if (!bounds.intersects(Bounds.fromJson(c)))
|
|
51
60
|
continue;
|
|
52
|
-
|
|
61
|
+
// If there are overviews and they exist for this zoom range and we are not ignoring them
|
|
62
|
+
// lets use the overviews instead!
|
|
63
|
+
if (img.overviews &&
|
|
64
|
+
img.overviews.maxZoom >= filterZoom &&
|
|
65
|
+
img.overviews.minZoom <= filterZoom &&
|
|
66
|
+
ignoreOverview !== true) {
|
|
53
67
|
output.push(fsa.join(img.uri, img.overviews.path));
|
|
54
68
|
break;
|
|
55
69
|
}
|
|
@@ -59,16 +73,9 @@ export const TileXyzRaster = {
|
|
|
59
73
|
}
|
|
60
74
|
return output;
|
|
61
75
|
},
|
|
62
|
-
async
|
|
63
|
-
var _a, _b;
|
|
64
|
-
if (xyz.tileType === VectorFormat.MapboxVectorTiles)
|
|
65
|
-
return NotFound();
|
|
66
|
-
const assetPaths = await this.getAssetsForTile(req, tileSet, xyz);
|
|
67
|
-
const cacheKey = Etag.key(assetPaths);
|
|
68
|
-
if (Etag.isNotModified(req, cacheKey))
|
|
69
|
-
return NotModified();
|
|
76
|
+
async loadAssets(req, assets) {
|
|
70
77
|
const toLoad = [];
|
|
71
|
-
for (const assetPath of
|
|
78
|
+
for (const assetPath of assets) {
|
|
72
79
|
toLoad.push(LoadingQueue(() => {
|
|
73
80
|
if (assetPath.endsWith('.tar.co')) {
|
|
74
81
|
return CoSources.getCotar(assetPath).catch((error) => {
|
|
@@ -82,7 +89,21 @@ export const TileXyzRaster = {
|
|
|
82
89
|
});
|
|
83
90
|
}));
|
|
84
91
|
}
|
|
85
|
-
|
|
92
|
+
return (await Promise.all(toLoad)).filter((f) => f != null);
|
|
93
|
+
},
|
|
94
|
+
async getAssetsForTile(req, tileSet, xyz) {
|
|
95
|
+
const tileBounds = xyz.tileMatrix.tileToSourceBounds(xyz.tile);
|
|
96
|
+
return TileXyzRaster.getAssetsForBounds(req, tileSet, xyz.tileMatrix, tileBounds, xyz.tile.z);
|
|
97
|
+
},
|
|
98
|
+
async tile(req, tileSet, xyz) {
|
|
99
|
+
var _a, _b;
|
|
100
|
+
if (xyz.tileType === VectorFormat.MapboxVectorTiles)
|
|
101
|
+
return NotFound();
|
|
102
|
+
const assetPaths = await this.getAssetsForTile(req, tileSet, xyz);
|
|
103
|
+
const cacheKey = Etag.key(assetPaths);
|
|
104
|
+
if (Etag.isNotModified(req, cacheKey))
|
|
105
|
+
return NotModified();
|
|
106
|
+
const assets = await TileXyzRaster.loadAssets(req, assetPaths);
|
|
86
107
|
const tiler = new Tiler(xyz.tileMatrix);
|
|
87
108
|
const layers = await tiler.tile(assets, xyz.tile.x, xyz.tile.y, xyz.tile.z);
|
|
88
109
|
const res = await TileComposer.compose({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tile.xyz.raster.js","sourceRoot":"","sources":["../../src/routes/tile.xyz.raster.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAuB,MAAM,kBAAkB,CAAC;AACtE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,aAAa,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC1F,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"tile.xyz.raster.js","sourceRoot":"","sources":["../../src/routes/tile.xyz.raster.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAuB,MAAM,kBAAkB,CAAC;AACtE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,aAAa,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC1F,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE1C,OAAO,EAAE,UAAU,EAAqB,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACnF,OAAO,MAAM,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;A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ImageFormat, LatLon, TileMatrixSet, VectorFormat } from '@basemaps/geo';
|
|
2
2
|
import { LambdaHttpRequest } from '@linzjs/lambda';
|
|
3
3
|
import { TileXyzGet } from '../routes/tile.xyz';
|
|
4
4
|
export interface TileXyz {
|
|
@@ -26,6 +26,8 @@ export declare const Validate: {
|
|
|
26
26
|
/** Read in all image formats specified in the query parameters "format" or "tileFormat" */
|
|
27
27
|
getRequestedFormats(req: LambdaHttpRequest): ImageFormat[] | null;
|
|
28
28
|
getTileFormat(tileType: string): ImageFormat | VectorFormat | null;
|
|
29
|
+
/** Validate that a lat and lon are between -90/90 and -180/180 */
|
|
30
|
+
getLocation(lonIn: string, latIn: string): LatLon | null;
|
|
29
31
|
/**
|
|
30
32
|
* Validate that the tile request is somewhat valid
|
|
31
33
|
* - Valid projection
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../../src/util/validate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAc,aAAa,EAAkB,YAAY,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../../src/util/validate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,EAAc,aAAa,EAAkB,YAAY,EAAE,MAAM,eAAe,CAAC;AAG7G,OAAO,EAAE,iBAAiB,EAAsB,MAAM,gBAAgB,CAAC;AACvE,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAEhD,MAAM,WAAW,OAAO;IACtB,IAAI,EAAE;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC1C,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,aAAa,CAAC;IAC1B,QAAQ,EAAE,YAAY,GAAG,WAAW,CAAC;CACtC;AAED,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE;QAAE,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CACjC;AAED,eAAO,MAAM,QAAQ;IACnB;;;OAGG;gBACS,iBAAiB,GAAG,MAAM;2BAUf,MAAM,GAAG,aAAa,GAAG,IAAI;IAIpD,2FAA2F;6BAClE,iBAAiB,GAAG,WAAW,EAAE,GAAG,IAAI;4BAczC,MAAM,GAAG,WAAW,GAAG,YAAY,GAAG,IAAI;IAOlE,kEAAkE;uBAC/C,MAAM,SAAS,MAAM,GAAG,MAAM,GAAG,IAAI;IAOxD;;;;;;;;;OASG;aACM,kBAAkB,UAAU,CAAC,GAAG,OAAO;CAiCjD,CAAC"}
|
package/build/util/validate.js
CHANGED
|
@@ -44,6 +44,16 @@ export const Validate = {
|
|
|
44
44
|
return VectorFormat.MapboxVectorTiles;
|
|
45
45
|
return null;
|
|
46
46
|
},
|
|
47
|
+
/** Validate that a lat and lon are between -90/90 and -180/180 */
|
|
48
|
+
getLocation(lonIn, latIn) {
|
|
49
|
+
const lat = parseFloat(latIn);
|
|
50
|
+
const lon = parseFloat(lonIn);
|
|
51
|
+
if (isNaN(lon) || lon < -180 || lon > 180)
|
|
52
|
+
return null;
|
|
53
|
+
if (isNaN(lat) || lat < -90 || lat > 90)
|
|
54
|
+
return null;
|
|
55
|
+
return { lon, lat };
|
|
56
|
+
},
|
|
47
57
|
/**
|
|
48
58
|
* Validate that the tile request is somewhat valid
|
|
49
59
|
* - Valid projection
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate.js","sourceRoot":"","sources":["../../src/util/validate.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"validate.js","sourceRoot":"","sources":["../../src/util/validate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,UAAU,EAAiB,cAAc,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7G,OAAO,EAAE,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,kB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AG,EAAE,gBAAgB,CAAC,EAAE,CAAC,CAAC;QAEvG,MAAM,OAAO,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,OAAO,EAAE,GAAG,CAAC,MAAM,CAAC,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,CAAC;QACzF,GAAG,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;QAE7B,MAAM,MAAM,GAAG,UAAU,CAAC,kBAAkB,CAAC,UAAU,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;QACvE,GAAG,CAAC,GAAG,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;QAE5B,OAAO,OAAO,CAAC;IACjB,CAAC;CACF,CAAC"}
|
package/bundle.sh
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@basemaps/lambda-tiler",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.44.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/linz/basemaps.git",
|
|
@@ -22,11 +22,11 @@
|
|
|
22
22
|
"types": "./build/index.d.ts",
|
|
23
23
|
"license": "MIT",
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@basemaps/config": "^6.
|
|
26
|
-
"@basemaps/geo": "^6.
|
|
27
|
-
"@basemaps/shared": "^6.
|
|
28
|
-
"@basemaps/tiler": "^6.
|
|
29
|
-
"@basemaps/tiler-sharp": "^6.
|
|
25
|
+
"@basemaps/config": "^6.44.0",
|
|
26
|
+
"@basemaps/geo": "^6.44.0",
|
|
27
|
+
"@basemaps/shared": "^6.44.0",
|
|
28
|
+
"@basemaps/tiler": "^6.44.0",
|
|
29
|
+
"@basemaps/tiler-sharp": "^6.44.0",
|
|
30
30
|
"@chunkd/fs": "^10.0.2",
|
|
31
31
|
"@cogeotiff/core": "^7.2.0",
|
|
32
32
|
"@cotar/core": "^5.4.0",
|
|
@@ -50,12 +50,12 @@
|
|
|
50
50
|
"bundle": "./bundle.sh"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@basemaps/attribution": "^6.
|
|
53
|
+
"@basemaps/attribution": "^6.44.0",
|
|
54
54
|
"@chunkd/source-memory": "^10.0.0",
|
|
55
55
|
"@types/aws-lambda": "^8.10.75",
|
|
56
56
|
"@types/pixelmatch": "^5.0.0",
|
|
57
57
|
"@types/sharp": "^0.30.3",
|
|
58
58
|
"pretty-json-log": "^1.0.0"
|
|
59
59
|
},
|
|
60
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "025cc55d4e11c7e527ba2aec2f7655489be8d035"
|
|
61
61
|
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ConfigProviderMemory } from '@basemaps/config';
|
|
2
|
+
import { initConfigFromUrls } from '@basemaps/config/build/json/tiff.config.js';
|
|
3
|
+
import { ImageFormat, TileMatrixSet, TileMatrixSets } from '@basemaps/geo';
|
|
4
|
+
import { LogConfig, setDefaultConfig } from '@basemaps/shared';
|
|
5
|
+
import { fsa } from '@chunkd/fs';
|
|
6
|
+
import { LambdaHttpRequest, LambdaUrlRequest, UrlEvent } from '@linzjs/lambda';
|
|
7
|
+
import { Context } from 'aws-lambda';
|
|
8
|
+
import { pathToFileURL } from 'url';
|
|
9
|
+
import { renderPreview } from '../routes/preview.js';
|
|
10
|
+
|
|
11
|
+
const target = pathToFileURL(`/home/blacha/tmp/basemaps/bm-724/test-north-island_20230220_10m/`);
|
|
12
|
+
const location = { lat: -39.0852555, lon: 177.3998405 };
|
|
13
|
+
const z = 12;
|
|
14
|
+
|
|
15
|
+
const outputFormat = ImageFormat.Webp;
|
|
16
|
+
let tileMatrix: TileMatrixSet | null = null;
|
|
17
|
+
|
|
18
|
+
async function main(): Promise<void> {
|
|
19
|
+
const log = LogConfig.get();
|
|
20
|
+
const provider = new ConfigProviderMemory();
|
|
21
|
+
setDefaultConfig(provider);
|
|
22
|
+
const { tileSet, imagery } = await initConfigFromUrls(provider, [target]);
|
|
23
|
+
|
|
24
|
+
if (tileSet.layers.length === 0) throw new Error('No imagery found in path: ' + target);
|
|
25
|
+
log.info({ tileSet: tileSet.name, layers: tileSet.layers.length }, 'TileSet:Loaded');
|
|
26
|
+
|
|
27
|
+
for (const im of imagery) {
|
|
28
|
+
log.info({ url: im.uri, title: im.title, tileMatrix: im.tileMatrix, files: im.files.length }, 'Imagery:Loaded');
|
|
29
|
+
if (tileMatrix == null) {
|
|
30
|
+
tileMatrix = TileMatrixSets.find(im.tileMatrix);
|
|
31
|
+
log.info({ tileMatrix: im.tileMatrix }, 'Imagery:TileMatrix:Set');
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (tileMatrix == null) throw new Error('No tileMatrix found');
|
|
36
|
+
|
|
37
|
+
const req = new LambdaUrlRequest({ headers: {} } as UrlEvent, {} as Context, LogConfig.get()) as LambdaHttpRequest;
|
|
38
|
+
const res = await renderPreview(req, { tileMatrix, tileSet, location, z, outputFormat });
|
|
39
|
+
const previewFile = `./z${z}_${location.lon}_${location.lat}.${outputFormat}`;
|
|
40
|
+
await fsa.write(previewFile, Buffer.from(res.body, 'base64'));
|
|
41
|
+
log.info({ path: previewFile }, 'Tile:Write');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
main();
|
package/src/index.ts
CHANGED
|
@@ -18,6 +18,8 @@ import { versionGet } from './routes/version.js';
|
|
|
18
18
|
import { NotFound, OkResponse } from './util/response.js';
|
|
19
19
|
import { CoSources } from './util/source.cache.js';
|
|
20
20
|
import { St } from './util/source.tracer.js';
|
|
21
|
+
import { tilePreviewGet } from './routes/preview.js';
|
|
22
|
+
import { previewIndexGet } from './routes/preview.index.js';
|
|
21
23
|
|
|
22
24
|
export const handler = lf.http(LogConfig.get());
|
|
23
25
|
|
|
@@ -93,6 +95,11 @@ handler.router.get('/v1/tiles/:tileSet/:tileMatrix/tile.json', tileJsonGet);
|
|
|
93
95
|
// Tiles
|
|
94
96
|
handler.router.get('/v1/tiles/:tileSet/:tileMatrix/:z/:x/:y.:tileType', tileXyzGet);
|
|
95
97
|
|
|
98
|
+
// Preview
|
|
99
|
+
handler.router.get('/v1/preview/:tileSet/:tileMatrix/:z/:lon/:lat', tilePreviewGet);
|
|
100
|
+
handler.router.get('/v1/@:location', previewIndexGet);
|
|
101
|
+
handler.router.get('/@:location', previewIndexGet);
|
|
102
|
+
|
|
96
103
|
// Attribution
|
|
97
104
|
handler.router.get('/v1/tiles/:tileSet/:tileMatrix/attribution.json', tileAttributionGet);
|
|
98
105
|
handler.router.get('/v1/attribution/:tileSet/:tileMatrix/summary.json', tileAttributionGet);
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Env, LogConfig, V, fsa } from '@basemaps/shared';
|
|
2
|
+
import { LambdaAlbRequest, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';
|
|
3
|
+
import { ALBEvent, Context } from 'aws-lambda';
|
|
4
|
+
import o from 'ospec';
|
|
5
|
+
|
|
6
|
+
import { loadAndServeIndexHtml } from '../preview.index.js';
|
|
7
|
+
import { LocationUrl } from '@basemaps/geo';
|
|
8
|
+
import { FsMemory } from '@chunkd/source-memory';
|
|
9
|
+
|
|
10
|
+
o.spec('/@*', async () => {
|
|
11
|
+
o.specTimeout(1000);
|
|
12
|
+
const baseRequest: ALBEvent = {
|
|
13
|
+
requestContext: null as any,
|
|
14
|
+
httpMethod: 'get',
|
|
15
|
+
path: '/@-41.8900012,174.0492432,z5',
|
|
16
|
+
body: null,
|
|
17
|
+
isBase64Encoded: false,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const fsMem = new FsMemory();
|
|
21
|
+
let lastLocation: string | undefined;
|
|
22
|
+
o.beforeEach(() => {
|
|
23
|
+
fsa.register('memory://', fsMem);
|
|
24
|
+
lastLocation = process.env[Env.StaticAssetLocation];
|
|
25
|
+
});
|
|
26
|
+
o.afterEach(() => {
|
|
27
|
+
if (lastLocation == null) delete process.env[Env.StaticAssetLocation];
|
|
28
|
+
else process.env[Env.StaticAssetLocation] = lastLocation;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
o('Should redirect on failure to load', async () => {
|
|
32
|
+
const ctx: LambdaHttpRequest = new LambdaAlbRequest(baseRequest, {} as Context, LogConfig.get());
|
|
33
|
+
|
|
34
|
+
const res = await loadAndServeIndexHtml(ctx);
|
|
35
|
+
o(res.status).equals(302);
|
|
36
|
+
o(res.header('location')).equals('/?');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
o('Should redirect with querystring on failure to load', async () => {
|
|
40
|
+
const evt: ALBEvent = { ...baseRequest, queryStringParameters: { config: 'config-latest.json' } };
|
|
41
|
+
const ctx: LambdaHttpRequest = new LambdaAlbRequest(evt, {} as Context, LogConfig.get());
|
|
42
|
+
|
|
43
|
+
const res = await loadAndServeIndexHtml(ctx);
|
|
44
|
+
o(res.status).equals(302);
|
|
45
|
+
o(res.header('location')).equals('/?config=config-latest.json');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
o('Should redirect with querystring and location on failure to load', async () => {
|
|
49
|
+
const evt: ALBEvent = { ...baseRequest, queryStringParameters: { config: 'config-latest.json' } };
|
|
50
|
+
const loc = LocationUrl.fromSlug(evt.path);
|
|
51
|
+
const ctx: LambdaHttpRequest = new LambdaAlbRequest(evt, {} as Context, LogConfig.get());
|
|
52
|
+
|
|
53
|
+
const res = await loadAndServeIndexHtml(ctx, loc);
|
|
54
|
+
o(res.status).equals(302);
|
|
55
|
+
o(res.header('location')).equals('/?config=config-latest.json#@-41.8900012,174.0492432,z5');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
o('should redirect on failure to load index.html', async () => {
|
|
59
|
+
const ctx: LambdaHttpRequest = new LambdaAlbRequest(baseRequest, {} as Context, LogConfig.get());
|
|
60
|
+
process.env[Env.StaticAssetLocation] = 'memory://assets/';
|
|
61
|
+
|
|
62
|
+
const res = await loadAndServeIndexHtml(ctx);
|
|
63
|
+
o(res.status).equals(302);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
o('should redirect with new tags!', async () => {
|
|
67
|
+
const ctx = new LambdaAlbRequest(baseRequest, {} as Context, LogConfig.get());
|
|
68
|
+
process.env[Env.StaticAssetLocation] = 'memory://assets/';
|
|
69
|
+
|
|
70
|
+
const indexHtml = V('html', [
|
|
71
|
+
V('head', [
|
|
72
|
+
V('meta', { property: 'og:title', content: 'LINZ Basemaps' }),
|
|
73
|
+
V('meta', { property: 'og:image', content: '/basemaps-card.jepg' }),
|
|
74
|
+
V('meta', { name: 'viewport' }),
|
|
75
|
+
]),
|
|
76
|
+
]).toString();
|
|
77
|
+
|
|
78
|
+
await fsa.write('memory://assets/index.html', indexHtml);
|
|
79
|
+
|
|
80
|
+
// Pass back the body un altered
|
|
81
|
+
const res = await loadAndServeIndexHtml(ctx);
|
|
82
|
+
o(getBody(res)?.toString()).equals(indexHtml);
|
|
83
|
+
|
|
84
|
+
// Replace og:title with a <fake tag />
|
|
85
|
+
const resB = await loadAndServeIndexHtml(ctx, null, new Map([['og:title', '<fake tag />']]));
|
|
86
|
+
o(getBody(resB)?.toString().includes('<fake tag />')).equals(true);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
function getBody(res: LambdaHttpResponse): Buffer | null {
|
|
91
|
+
if (res._body == null) return null;
|
|
92
|
+
if (res.isBase64Encoded) return Buffer.from(res._body as string, 'base64');
|
|
93
|
+
return Buffer.from(res._body as string);
|
|
94
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { GoogleTms, LocationUrl, LonLatZoom, TileMatrixSets } from '@basemaps/geo';
|
|
2
|
+
import { HttpHeader, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';
|
|
3
|
+
import { ConfigLoader } from '../util/config.loader.js';
|
|
4
|
+
import { Env, fsa } from '@basemaps/shared';
|
|
5
|
+
import { isGzip } from '../util/cotar.serve.js';
|
|
6
|
+
import { gunzip } from 'node:zlib';
|
|
7
|
+
import { promisify } from 'node:util';
|
|
8
|
+
import { Etag } from '../util/etag.js';
|
|
9
|
+
|
|
10
|
+
const gunzipP = promisify(gunzip);
|
|
11
|
+
|
|
12
|
+
export interface PreviewIndexGet {
|
|
13
|
+
Params: {
|
|
14
|
+
location: string;
|
|
15
|
+
};
|
|
16
|
+
Query: {
|
|
17
|
+
config?: string;
|
|
18
|
+
style?: string;
|
|
19
|
+
tileMatrix?: string;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Load the `index.html` from the static bucket and replace `<meta />` tags with the preview open graph tags
|
|
25
|
+
*
|
|
26
|
+
* @param loc location of the request if valid
|
|
27
|
+
* @param tags tags to replace if they exist
|
|
28
|
+
*
|
|
29
|
+
* @returns response containing the output HTML
|
|
30
|
+
*/
|
|
31
|
+
export async function loadAndServeIndexHtml(
|
|
32
|
+
req: LambdaHttpRequest,
|
|
33
|
+
loc?: LonLatZoom | null,
|
|
34
|
+
tags?: Map<string, string>,
|
|
35
|
+
): Promise<LambdaHttpResponse> {
|
|
36
|
+
const locUrl = loc ? `#` + LocationUrl.toSlug(loc) : '';
|
|
37
|
+
// If the static location is given to us replace
|
|
38
|
+
const staticLocation = Env.get(Env.StaticAssetLocation);
|
|
39
|
+
// No static assets defined, just redirect back to the main page
|
|
40
|
+
if (staticLocation == null) {
|
|
41
|
+
return new LambdaHttpResponse(302, 'Invalid index.html', {
|
|
42
|
+
location: '/?' + req.query.toString() + locUrl,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
let indexHtml = await fsa.read(fsa.join(staticLocation, 'index.html'));
|
|
48
|
+
if (isGzip(indexHtml)) indexHtml = await gunzipP(indexHtml);
|
|
49
|
+
|
|
50
|
+
const res = new LambdaHttpResponse(200, 'ok');
|
|
51
|
+
// These index.html documents are refreshed frequently so only let them be cached for short durations
|
|
52
|
+
res.header(HttpHeader.CacheControl, 'public, max-age=30, stale-while-revalidate=60');
|
|
53
|
+
|
|
54
|
+
if (tags == null) {
|
|
55
|
+
res.header(HttpHeader.ETag, Etag.key(indexHtml));
|
|
56
|
+
res.buffer(indexHtml, `text/html; charset=utf-8`);
|
|
57
|
+
return res;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Replace open graph tags
|
|
61
|
+
const output = String(indexHtml)
|
|
62
|
+
.split('\n')
|
|
63
|
+
.map((f) => {
|
|
64
|
+
for (const [key, value] of tags.entries()) {
|
|
65
|
+
if (f.includes(key)) return value;
|
|
66
|
+
}
|
|
67
|
+
return f;
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const outHtml = output.join('\n');
|
|
71
|
+
res.header(HttpHeader.ETag, Etag.key(outHtml));
|
|
72
|
+
res.buffer(outHtml, `text/html; charset=utf-8`);
|
|
73
|
+
return res;
|
|
74
|
+
} catch (e) {
|
|
75
|
+
req.log.fatal({ e }, 'Index:Failed');
|
|
76
|
+
// If we fail to read transform the index, just redirect the user to the actual index.html
|
|
77
|
+
return new LambdaHttpResponse(302, 'Failed to render index.html', {
|
|
78
|
+
location: '/?' + req.query.toString() + locUrl,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function previewIndexGet(req: LambdaHttpRequest<PreviewIndexGet>): Promise<LambdaHttpResponse> {
|
|
84
|
+
const config = await ConfigLoader.load(req);
|
|
85
|
+
const loc = LocationUrl.fromSlug(req.params.location);
|
|
86
|
+
if (loc == null) return loadAndServeIndexHtml(req);
|
|
87
|
+
|
|
88
|
+
const query = LocationUrl.parseQuery(req.query);
|
|
89
|
+
|
|
90
|
+
req.timer.start('tileset:load');
|
|
91
|
+
const tileSet = await config.TileSet.get(config.TileSet.id(query.style));
|
|
92
|
+
req.timer.end('tileset:load');
|
|
93
|
+
if (tileSet == null) return loadAndServeIndexHtml(req, loc);
|
|
94
|
+
if (tileSet.type !== 'raster') return loadAndServeIndexHtml(req, loc);
|
|
95
|
+
|
|
96
|
+
let tileMatrix = TileMatrixSets.find(query.tileMatrix);
|
|
97
|
+
if (tileMatrix == null) tileMatrix = GoogleTms;
|
|
98
|
+
|
|
99
|
+
const short = LocationUrl.truncateLatLon(loc);
|
|
100
|
+
const shortLocation = [short.zoom, short.lon, short.lat].join('/');
|
|
101
|
+
|
|
102
|
+
// Include tile matrix name eg "[NZTM2000Quad]" in the title if its not WebMercatorQuad
|
|
103
|
+
const tileMatrixId = tileMatrix.identifier === GoogleTms.identifier ? '' : ` [${tileMatrix.identifier}]`;
|
|
104
|
+
// List of tags to replace in the index.html
|
|
105
|
+
const ogTags = new Map([
|
|
106
|
+
['og:title', `<meta name="twitter:title" property="og:title" content="LINZ Basemaps">`],
|
|
107
|
+
// TODO attribution could be used to get exactly what imagery is being looked at.
|
|
108
|
+
[
|
|
109
|
+
'og:description',
|
|
110
|
+
`<meta name="twitter:description" property="og:description" content="${tileSet.title}${tileMatrixId}" />`,
|
|
111
|
+
],
|
|
112
|
+
[
|
|
113
|
+
'og:image',
|
|
114
|
+
`<meta name="twitter:image" property="og:image" content="/v1/preview/${tileSet.name}/${tileMatrix.identifier}/${shortLocation}" />`,
|
|
115
|
+
],
|
|
116
|
+
]);
|
|
117
|
+
|
|
118
|
+
return loadAndServeIndexHtml(req, loc, ogTags);
|
|
119
|
+
}
|