2021-01-28 21:40:11 -05:00
|
|
|
import { parseBooleanExact } from '../helpers';
|
|
|
|
import { getAllLayerNames, getBuildingLayerNames, getDataConfig, getLayerVariables } from "./dataDefinition";
|
2019-11-07 02:48:51 -05:00
|
|
|
import { createBlankTile } from "./renderers/createBlankTile";
|
2019-10-07 08:34:22 -04:00
|
|
|
import { getTileWithCaching } from "./renderers/getTileWithCaching";
|
2019-11-07 02:48:51 -05:00
|
|
|
import { renderDataSourceTile } from "./renderers/renderDataSourceTile";
|
2019-10-07 08:34:22 -04:00
|
|
|
import { stitchTile } from "./renderers/stitchTile";
|
2019-11-07 02:48:51 -05:00
|
|
|
import { TileCache } from "./tileCache";
|
|
|
|
import { BoundingBox, Tile, TileParams } from "./types";
|
|
|
|
import { isOutsideExtent } from "./util";
|
2019-09-17 13:35:05 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of all tilesets handled by the tile server
|
|
|
|
*/
|
2019-10-07 12:27:22 -04:00
|
|
|
const allTilesets = getAllLayerNames();
|
2019-09-17 13:03:20 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Zoom level when we switch from rendering direct from database to instead composing tiles
|
|
|
|
* from the zoom level below - gets similar effect, with much lower load on Postgres
|
|
|
|
*/
|
|
|
|
const STITCH_THRESHOLD = 12;
|
|
|
|
|
2019-10-07 08:34:22 -04:00
|
|
|
/**
|
|
|
|
* Hard-code extent so we can short-circuit rendering and return empty/transparent tiles outside the area of interest
|
|
|
|
* bbox in CRS epsg:3857 in form: [w, s, e, n]
|
|
|
|
*/
|
2021-05-11 19:06:42 -04:00
|
|
|
const EXTENT_BBOX: BoundingBox = [-61149.622628, 6667754.851372, 37183, 6744803.375884];
|
2019-09-17 13:03:20 -04:00
|
|
|
|
2021-01-28 21:40:11 -05:00
|
|
|
const allLayersCacheSwitch = parseBooleanExact(process.env.CACHE_TILES) ?? true;
|
|
|
|
const dataLayersCacheSwitch = parseBooleanExact(process.env.CACHE_DATA_TILES) ?? true;
|
|
|
|
let shouldCacheFn: (t: TileParams) => boolean;
|
|
|
|
|
|
|
|
if(!allLayersCacheSwitch) {
|
|
|
|
shouldCacheFn = t => false;
|
2021-02-24 02:21:44 -05:00
|
|
|
} else if(dataLayersCacheSwitch) {
|
2021-01-28 21:40:11 -05:00
|
|
|
// cache age data and base building outlines for more zoom levels than other layers
|
|
|
|
shouldCacheFn = ({ tileset, z }: TileParams) =>
|
|
|
|
(tileset === 'date_year' && z <= 16) ||
|
|
|
|
(['base_light', 'base_night'].includes(tileset) && z <= 17) ||
|
|
|
|
z <= 13;
|
|
|
|
} else {
|
|
|
|
shouldCacheFn = ({ tileset, z }: TileParams) =>
|
|
|
|
['base_light', 'base_night'].includes(tileset) && z <= 17;
|
|
|
|
}
|
|
|
|
|
2019-09-17 13:03:20 -04:00
|
|
|
const tileCache = new TileCache(
|
|
|
|
process.env.TILECACHE_PATH,
|
|
|
|
{
|
2019-10-07 12:27:22 -04:00
|
|
|
tilesets: getBuildingLayerNames(),
|
2019-09-17 13:03:20 -04:00
|
|
|
minZoom: 9,
|
2019-10-08 12:40:30 -04:00
|
|
|
maxZoom: 19,
|
2019-09-17 13:03:20 -04:00
|
|
|
scales: [1, 2]
|
|
|
|
},
|
2021-01-28 21:40:11 -05:00
|
|
|
shouldCacheFn,
|
2019-10-07 12:27:22 -04:00
|
|
|
|
|
|
|
// don't clear base_light and base_night on bounding box cache clear
|
|
|
|
(tileset: string) => tileset !== 'base_light' && tileset !== 'base_night'
|
2019-09-17 13:03:20 -04:00
|
|
|
);
|
|
|
|
|
2021-01-28 21:40:11 -05:00
|
|
|
const renderBuildingTile = (t: TileParams, d: any) => renderDataSourceTile(t, d, getDataConfig, getLayerVariables);
|
2019-09-17 13:03:20 -04:00
|
|
|
|
2019-10-07 08:34:22 -04:00
|
|
|
function cacheOrCreateBuildingTile(tileParams: TileParams, dataParams: any): Promise<Tile> {
|
|
|
|
return getTileWithCaching(tileParams, dataParams, tileCache, stitchOrRenderBuildingTile);
|
|
|
|
}
|
2019-09-17 13:03:20 -04:00
|
|
|
|
2019-10-07 08:34:22 -04:00
|
|
|
function stitchOrRenderBuildingTile(tileParams: TileParams, dataParams: any): Promise<Tile> {
|
|
|
|
if (tileParams.z <= STITCH_THRESHOLD) {
|
|
|
|
// stitch tile, using cache recursively
|
|
|
|
return stitchTile(tileParams, dataParams, cacheOrCreateBuildingTile);
|
|
|
|
} else {
|
|
|
|
return renderBuildingTile(tileParams, dataParams);
|
|
|
|
}
|
|
|
|
}
|
2019-09-17 13:03:20 -04:00
|
|
|
|
2019-10-07 08:34:22 -04:00
|
|
|
function renderTile(tileParams: TileParams, dataParams: any): Promise<Tile> {
|
|
|
|
if (isOutsideExtent(tileParams, EXTENT_BBOX)) {
|
|
|
|
return createBlankTile();
|
|
|
|
}
|
2019-09-17 13:03:20 -04:00
|
|
|
|
2019-10-07 08:34:22 -04:00
|
|
|
if (tileParams.tileset === 'highlight') {
|
2021-01-28 21:40:11 -05:00
|
|
|
return renderBuildingTile(tileParams, dataParams);
|
2019-10-07 08:34:22 -04:00
|
|
|
}
|
2019-09-17 13:03:20 -04:00
|
|
|
|
2019-10-07 08:34:22 -04:00
|
|
|
return cacheOrCreateBuildingTile(tileParams, dataParams);
|
|
|
|
}
|
2019-09-17 13:03:20 -04:00
|
|
|
|
|
|
|
export {
|
2019-09-17 13:35:05 -04:00
|
|
|
allTilesets,
|
2019-10-07 08:34:22 -04:00
|
|
|
renderTile,
|
2019-09-17 13:03:20 -04:00
|
|
|
tileCache
|
2019-09-17 13:35:05 -04:00
|
|
|
};
|