Do more work in render_tile, test if should check cache

This commit is contained in:
Tom Russell 2019-01-22 22:37:44 +00:00
parent 7ca7fb59ac
commit d05e125810
2 changed files with 32 additions and 29 deletions

View File

@ -34,17 +34,7 @@ const mercator = new SphericalMercator({
size: TILE_SIZE size: TILE_SIZE
}); });
function get_bbox(params){ function get_bbox(int_z, int_x, int_y){
const { z, x, y } = params
const int_z = strictParseInt(z);
const int_x = strictParseInt(x);
const int_y = strictParseInt(y);
if (isNaN(int_x) || isNaN(int_y) || isNaN(int_z)){
console.error("Missing x or y or z")
return {error:'Bad parameter'}
}
return mercator.bbox( return mercator.bbox(
int_x, int_x,
int_y, int_y,
@ -54,7 +44,28 @@ function get_bbox(params){
); );
} }
function render_tile(bbox, table_def, style_def, cb){ function should_try_cache(style, int_z) {
if (style === 'base_light' || style === 'base_light') {
// cache for higher zoom levels (unlikely to change)
return int_z <= 15
}
// else cache for lower zoom levels (change slowly)
return int_z <= 12
}
function render_tile(params, table_def, style_def, cb){
const { z, x, y } = params
const int_z = strictParseInt(z);
const int_x = strictParseInt(x);
const int_y = strictParseInt(y);
if (isNaN(int_x) || isNaN(int_y) || isNaN(int_z)){
console.error("Missing x or y or z")
return {error:'Bad parameter'}
}
const bbox = get_bbox(int_z, int_x, int_y)
// const should_cache = should_try_cache(style_def[0], int_z)
const map = new mapnik.Map(TILE_SIZE, TILE_SIZE, PROJ4_STRING); const map = new mapnik.Map(TILE_SIZE, TILE_SIZE, PROJ4_STRING);
map.bufferSize = TILE_BUFFER_SIZE; map.bufferSize = TILE_BUFFER_SIZE;

View File

@ -4,7 +4,7 @@
*/ */
import express from 'express'; import express from 'express';
import { get_bbox, render_tile } from './tile'; import { render_tile } from './tile';
import { strictParseInt } from './parse'; import { strictParseInt } from './parse';
// tiles router // tiles router
@ -12,7 +12,6 @@ const router = express.Router()
// basic geometry tiles // basic geometry tiles
router.get('/base_light/:z/:x/:y.png', function(req, res) { router.get('/base_light/:z/:x/:y.png', function(req, res) {
const bbox = get_bbox(req.params)
const table_def = `( const table_def = `(
SELECT SELECT
b.location_number as location_number, b.location_number as location_number,
@ -24,7 +23,7 @@ router.get('/base_light/:z/:x/:y.png', function(req, res) {
g.geometry_id = b.geometry_id g.geometry_id = b.geometry_id
) as outline` ) as outline`
const style_def = ['base_light'] const style_def = ['base_light']
render_tile(bbox, table_def, style_def, function(err, im) { render_tile(req.params, table_def, style_def, function(err, im) {
if (err) throw err if (err) throw err
res.writeHead(200, {'Content-Type': 'image/png'}) res.writeHead(200, {'Content-Type': 'image/png'})
@ -34,7 +33,6 @@ router.get('/base_light/:z/:x/:y.png', function(req, res) {
// dark theme // dark theme
router.get('/base_night/:z/:x/:y.png', function(req, res) { router.get('/base_night/:z/:x/:y.png', function(req, res) {
const bbox = get_bbox(req.params)
const table_def = `( const table_def = `(
SELECT SELECT
b.location_number as location_number, b.location_number as location_number,
@ -46,7 +44,7 @@ router.get('/base_night/:z/:x/:y.png', function(req, res) {
g.geometry_id = b.geometry_id g.geometry_id = b.geometry_id
) as outline` ) as outline`
const style_def = ['base_night'] const style_def = ['base_night']
render_tile(bbox, table_def, style_def, function(err, im) { render_tile(req.params, table_def, style_def, function(err, im) {
if (err) throw err if (err) throw err
res.writeHead(200, {'Content-Type': 'image/png'}) res.writeHead(200, {'Content-Type': 'image/png'})
@ -62,7 +60,6 @@ router.get('/highlight/:z/:x/:y.png', function(req, res) {
res.status(400).send({error:'Bad parameter'}) res.status(400).send({error:'Bad parameter'})
return return
} }
const bbox = get_bbox(req.params)
const table_def = `( const table_def = `(
SELECT SELECT
g.geometry_id = ${geometry_id} as focus, g.geometry_id = ${geometry_id} as focus,
@ -74,7 +71,7 @@ router.get('/highlight/:z/:x/:y.png', function(req, res) {
g.geometry_id = b.geometry_id g.geometry_id = b.geometry_id
) as highlight` ) as highlight`
const style_def = ['highlight'] const style_def = ['highlight']
render_tile(bbox, table_def, style_def, function(err, im) { render_tile(req.params, table_def, style_def, function(err, im) {
if (err) throw err if (err) throw err
res.writeHead(200, {'Content-Type': 'image/png'}) res.writeHead(200, {'Content-Type': 'image/png'})
@ -84,7 +81,6 @@ router.get('/highlight/:z/:x/:y.png', function(req, res) {
// date_year choropleth // date_year choropleth
router.get('/date_year/:z/:x/:y.png', function(req, res) { router.get('/date_year/:z/:x/:y.png', function(req, res) {
const bbox = get_bbox(req.params)
// const table_def = 'geometries' // const table_def = 'geometries'
const table_def = `( const table_def = `(
SELECT SELECT
@ -97,7 +93,7 @@ router.get('/date_year/:z/:x/:y.png', function(req, res) {
g.geometry_id = b.geometry_id g.geometry_id = b.geometry_id
) as outline` ) as outline`
const style_def = ['date_year'] const style_def = ['date_year']
render_tile(bbox, table_def, style_def, function(err, im) { render_tile(req.params, table_def, style_def, function(err, im) {
if (err) throw err if (err) throw err
res.writeHead(200, {'Content-Type': 'image/png'}) res.writeHead(200, {'Content-Type': 'image/png'})
@ -107,7 +103,6 @@ router.get('/date_year/:z/:x/:y.png', function(req, res) {
// date_year choropleth // date_year choropleth
router.get('/size_storeys/:z/:x/:y.png', function(req, res) { router.get('/size_storeys/:z/:x/:y.png', function(req, res) {
const bbox = get_bbox(req.params)
// const table_def = 'geometries' // const table_def = 'geometries'
const table_def = `( const table_def = `(
SELECT SELECT
@ -123,7 +118,7 @@ router.get('/size_storeys/:z/:x/:y.png', function(req, res) {
g.geometry_id = b.geometry_id g.geometry_id = b.geometry_id
) as outline` ) as outline`
const style_def = ['size_storeys'] const style_def = ['size_storeys']
render_tile(bbox, table_def, style_def, function(err, im) { render_tile(req.params, table_def, style_def, function(err, im) {
if (err) throw err if (err) throw err
res.writeHead(200, {'Content-Type': 'image/png'}) res.writeHead(200, {'Content-Type': 'image/png'})
@ -133,7 +128,6 @@ router.get('/size_storeys/:z/:x/:y.png', function(req, res) {
// location information depth // location information depth
router.get('/location/:z/:x/:y.png', function(req, res) { router.get('/location/:z/:x/:y.png', function(req, res) {
const bbox = get_bbox(req.params)
const table_def = `( const table_def = `(
SELECT SELECT
( (
@ -156,7 +150,7 @@ router.get('/location/:z/:x/:y.png', function(req, res) {
g.geometry_id = b.geometry_id g.geometry_id = b.geometry_id
) as location` ) as location`
const style_def = ['location_info_count'] const style_def = ['location_info_count']
render_tile(bbox, table_def, style_def, function(err, im) { render_tile(req.params, table_def, style_def, function(err, im) {
if (err) throw err if (err) throw err
res.writeHead(200, {'Content-Type': 'image/png'}) res.writeHead(200, {'Content-Type': 'image/png'})
@ -167,7 +161,6 @@ router.get('/location/:z/:x/:y.png', function(req, res) {
// likes // likes
router.get('/likes/:z/:x/:y.png', function(req, res) { router.get('/likes/:z/:x/:y.png', function(req, res) {
const bbox = get_bbox(req.params)
const table_def = `( const table_def = `(
SELECT SELECT
g.geometry_geom, g.geometry_geom,
@ -180,7 +173,7 @@ router.get('/likes/:z/:x/:y.png', function(req, res) {
AND b.likes_total > 0 AND b.likes_total > 0
) as location` ) as location`
const style_def = ['likes'] const style_def = ['likes']
render_tile(bbox, table_def, style_def, function(err, im) { render_tile(req.params, table_def, style_def, function(err, im) {
if (err) throw err if (err) throw err
res.writeHead(200, {'Content-Type': 'image/png'}) res.writeHead(200, {'Content-Type': 'image/png'})
@ -191,7 +184,6 @@ router.get('/likes/:z/:x/:y.png', function(req, res) {
// conservation status // conservation status
router.get('/conservation_area/:z/:x/:y.png', function(req, res) { router.get('/conservation_area/:z/:x/:y.png', function(req, res) {
const bbox = get_bbox(req.params)
const table_def = `( const table_def = `(
SELECT SELECT
g.geometry_geom g.geometry_geom
@ -203,7 +195,7 @@ router.get('/conservation_area/:z/:x/:y.png', function(req, res) {
AND b.planning_in_conservation_area = true AND b.planning_in_conservation_area = true
) as conservation_area` ) as conservation_area`
const style_def = ['planning_in_conservation_area'] const style_def = ['planning_in_conservation_area']
render_tile(bbox, table_def, style_def, function(err, im) { render_tile(req.params, table_def, style_def, function(err, im) {
if (err) throw err if (err) throw err
res.writeHead(200, {'Content-Type': 'image/png'}) res.writeHead(200, {'Content-Type': 'image/png'})