import { parseBooleanExact } from '../helpers' ; import { getAllLayerNames , getBuildingLayerNames , getDataConfig , getLayerVariables } from "./dataDefinition" ; import { createBlankTile } from "./renderers/createBlankTile" ; import { getTileWithCaching } from "./renderers/getTileWithCaching" ; import { renderDataSourceTile } from "./renderers/renderDataSourceTile" ; import { stitchTile } from "./renderers/stitchTile" ; 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 ( ) ; /**
 * 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 = [ - 61149.622628 , 6667754.851372 , 37183 , 6744803.375884 ] ; const allLayersCacheSwitch = parseBooleanExact ( process . env . CACHE_TILES ) ?? true ; const dataLayersCacheSwitch = parseBooleanExact ( process . env . CACHE_DATA_TILES ) ?? true ; let shouldCacheFn ; if ( ! allLayersCacheSwitch ) { shouldCacheFn = t => false ; } else if ( dataLayersCacheSwitch ) { shouldCacheFn = ( { tileset , z } ) => ( tileset === 'date_year' && z <= 16 ) || ( [ 'base_light' , 'base_night' ] . includes ( tileset ) && z <= 17 ) || z <= 13 ; } else { shouldCacheFn = ( { tileset , z } ) => [ 'base_light' , 'base_night' ] . includes ( tileset ) && z <= 17 ; } const tileCache = new TileCache ( process . env . TILECACHE_PATH , { tilesets : getBuildingLayerNames ( ) , minZoom : 9 , maxZoom : 19 , scales : [ 1 , 2 ] } , shouldCacheFn , ( tileset ) => tileset !== 'base_light' && tileset !== 'base_night' ) ; const renderBuildingTile = ( t , d ) => renderDataSourceTile ( t , d , getDataConfig , getLayerVariables ) ; function cacheOrCreateBuildingTile ( tileParams , dataParams ) { return getTileWithCaching ( tileParams , dataParams , tileCache , stitchOrRenderBuildingTile ) ; } function stitchOrRenderBuildingTile ( tileParams , dataParams ) { if ( tileParams . z <= STITCH_THRESHOLD ) { return stitchTile ( tileParams , dataParams , cacheOrCreateBuildingTile ) ; } else { return renderBuildingTile ( tileParams , dataParams ) ; } } function renderTile ( tileParams , dataParams ) { if ( isOutsideExtent ( tileParams , EXTENT_BBOX ) ) { return createBlankTile ( ) ; } if ( tileParams . tileset === 'highlight' ) { return renderBuildingTile ( tileParams , dataParams ) ; } return cacheOrCreateBuildingTile ( tileParams , dataParams ) ; } export { allTilesets , renderTile , tileCache } ;