|
| 1 | +import { Config, Sources, StyleJson, TileSetType } from '@basemaps/config'; |
| 2 | +import { Env, fsa } from '@basemaps/shared'; |
| 3 | +import { HttpHeader, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda'; |
| 4 | +import { convertRelativeUrl } from '../routes/tile.style.json.js'; |
| 5 | +import { Etag } from '../util/etag.js'; |
| 6 | +import { NotFound, NotModified } from '../util/response.js'; |
| 7 | +import { Validate } from '../util/validate.js'; |
| 8 | + |
| 9 | +interface StyleGet { |
| 10 | + Params: { |
| 11 | + tileSet: string; |
| 12 | + }; |
| 13 | +} |
| 14 | + |
| 15 | +function tileserverUrl(tileSet: string, apiKey: string): string { |
| 16 | + const host = Env.get(Env.PublicUrlBase) ?? ''; |
| 17 | + const url = `/v1/arcgis/rest/services/${tileSet}/VectorTileServer`; |
| 18 | + const fullUrl = new URL(fsa.join(host, url)); |
| 19 | + fullUrl.searchParams.set('api', apiKey); |
| 20 | + fullUrl.searchParams.set('f', 'json'); |
| 21 | + return fullUrl.toString().replace(/%7B/g, '{').replace(/%7D/g, '}'); |
| 22 | +} |
| 23 | + |
| 24 | +function convertStyleJson(tileSet: string, style: StyleJson, apiKey: string): StyleJson { |
| 25 | + const sources: Sources = JSON.parse(JSON.stringify(style.sources)); |
| 26 | + // Only keep the vector layer and update the source url |
| 27 | + for (const [key, value] of Object.entries(sources)) { |
| 28 | + if (value.type === 'vector') { |
| 29 | + value.url = tileserverUrl(tileSet, apiKey); |
| 30 | + sources[key] = value; |
| 31 | + } else { |
| 32 | + delete sources[key]; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // Remove all the not vector layers. |
| 37 | + const layers = []; |
| 38 | + for (const layer of style.layers) { |
| 39 | + if (layer.source != null && !sources.hasOwnProperty(layer.source)) continue; |
| 40 | + layers.push(layer); |
| 41 | + } |
| 42 | + |
| 43 | + return { |
| 44 | + version: 8, |
| 45 | + id: style.id, |
| 46 | + name: style.name, |
| 47 | + sources, |
| 48 | + layers, |
| 49 | + metadata: style.metadata ?? {}, |
| 50 | + glyphs: convertRelativeUrl(style.glyphs), |
| 51 | + sprite: convertRelativeUrl(style.sprite), |
| 52 | + } as StyleJson; |
| 53 | +} |
| 54 | + |
| 55 | +export async function arcgisStyleJsonGet(req: LambdaHttpRequest<StyleGet>): Promise<LambdaHttpResponse> { |
| 56 | + const apiKey = Validate.apiKey(req); |
| 57 | + const tileSet = await Config.TileSet.get(Config.TileSet.id(req.params.tileSet)); |
| 58 | + if (tileSet?.type !== TileSetType.Vector) return NotFound(); |
| 59 | + |
| 60 | + const style = req.query.get('style'); |
| 61 | + const styleName = style ? style : 'topographic'; // Defalut to topographic style |
| 62 | + |
| 63 | + // Get style Config from db |
| 64 | + const dbId = Config.Style.id(styleName); |
| 65 | + const styleConfig = await Config.Style.get(dbId); |
| 66 | + if (styleConfig == null) return NotFound(); |
| 67 | + |
| 68 | + // Prepare sources and add linz source |
| 69 | + const styleJson = convertStyleJson(tileSet.name, styleConfig.style, apiKey); |
| 70 | + const data = Buffer.from(JSON.stringify(styleJson)); |
| 71 | + |
| 72 | + const cacheKey = Etag.key(data); |
| 73 | + if (Etag.isNotModified(req, cacheKey)) return NotModified(); |
| 74 | + |
| 75 | + const response = new LambdaHttpResponse(200, 'ok'); |
| 76 | + response.header(HttpHeader.ETag, cacheKey); |
| 77 | + response.header(HttpHeader.CacheControl, 'no-store'); |
| 78 | + response.buffer(data, 'application/json'); |
| 79 | + req.set('bytes', data.byteLength); |
| 80 | + return response; |
| 81 | +} |
0 commit comments