colouring-montreal/app/src/tiles/rendererDefinition.ts

91 lines
3.2 KiB
TypeScript
Raw Normal View History

import { parseBooleanExact } from '../helpers';
import { getAllLayerNames, getBuildingLayerNames, getDataConfig, getLayerVariables } from "./dataDefinition";
2019-11-07 02:48:51 -05:00
import { createBlankTile } from "./renderers/createBlankTile";
import { getTileWithCaching } from "./renderers/getTileWithCaching";
2019-11-07 02:48:51 -05:00
import { renderDataSourceTile } from "./renderers/renderDataSourceTile";
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";
/**
* A list of all tilesets handled by the tile server
*/
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;
/**
* 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]
*/
const EXTENT_BBOX: BoundingBox = [-61149.622628, 6667754.851372, 28128.826409, 6744803.375884];
2019-09-17 13:03:20 -04: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;
} else if(!dataLayersCacheSwitch) {
// 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,
{
tilesets: getBuildingLayerNames(),
2019-09-17 13:03:20 -04:00
minZoom: 9,
maxZoom: 19,
2019-09-17 13:03:20 -04:00
scales: [1, 2]
},
shouldCacheFn,
// 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
);
const renderBuildingTile = (t: TileParams, d: any) => renderDataSourceTile(t, d, getDataConfig, getLayerVariables);
2019-09-17 13:03:20 -04:00
function cacheOrCreateBuildingTile(tileParams: TileParams, dataParams: any): Promise<Tile> {
return getTileWithCaching(tileParams, dataParams, tileCache, stitchOrRenderBuildingTile);
}
2019-09-17 13:03:20 -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
function renderTile(tileParams: TileParams, dataParams: any): Promise<Tile> {
if (isOutsideExtent(tileParams, EXTENT_BBOX)) {
return createBlankTile();
}
2019-09-17 13:03:20 -04:00
if (tileParams.tileset === 'highlight') {
return renderBuildingTile(tileParams, dataParams);
}
2019-09-17 13:03:20 -04:00
return cacheOrCreateBuildingTile(tileParams, dataParams);
}
2019-09-17 13:03:20 -04:00
export {
allTilesets,
renderTile,
2019-09-17 13:03:20 -04:00
tileCache
};