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

72 lines
1.8 KiB
TypeScript
Raw Normal View History

2018-09-30 14:50:09 -04:00
/**
2019-04-27 08:36:17 -04:00
* Tileserver
* - routes for Express app
2019-09-17 13:11:42 -04:00
* - see rendererDefinition for actual rules of rendering
2018-09-30 14:50:09 -04:00
*/
2018-09-10 05:44:32 -04:00
import express from 'express';
2019-02-24 07:17:59 -05:00
import { strictParseInt } from '../parse';
2019-09-17 13:11:42 -04:00
import { TileParams } from './types';
import { mainRenderer } from './rendererDefinition';
import asyncController from '../api/routes/asyncController';
const handleTileRequest = asyncController(async function (req: express.Request, res: express.Response) {
try {
var tileParams = parseTileParams(req.params);
var dataParams = req.query;
} catch(err) {
console.error(err);
return res.status(400).send({error: err.message});
}
try {
const im = await mainRenderer.getTile(tileParams, dataParams);
res.writeHead(200, { 'Content-Type': 'image/png' });
res.end(im);
} catch(err) {
console.error(err);
res.status(500).send({ error: err });
}
});
2018-09-10 05:44:32 -04:00
// tiles router
const router = express.Router()
2019-09-17 13:11:42 -04:00
router.get('/:tileset/:z/:x/:y(\\d+):scale(@\\dx)?.png', handleTileRequest);
2018-09-10 05:44:32 -04:00
2019-09-17 13:11:42 -04:00
function parseTileParams(params: any): TileParams {
const { tileset, z, x, y, scale } = params;
2018-09-10 05:44:32 -04:00
2019-05-27 13:26:29 -04:00
const intZ = strictParseInt(z);
2019-09-17 13:11:42 -04:00
if (isNaN(intZ)) throw new Error('Invalid value for z');
2018-09-10 05:44:32 -04:00
2019-09-17 13:11:42 -04:00
const intX = strictParseInt(x);
if (isNaN(intX)) throw new Error('Invalid value for x');
2019-09-17 13:11:42 -04:00
const intY = strictParseInt(y);
if (isNaN(intY)) throw new Error('Invalid value for y');
2019-09-17 13:11:42 -04:00
let intScale: number;
if (scale === '@2x') {
intScale = 2;
} else if (scale === '@1x' || scale == undefined) {
intScale = 1;
} else {
2019-09-17 13:11:42 -04:00
throw new Error('Invalid value for scale');
}
2019-09-17 13:11:42 -04:00
return {
tileset,
z: intZ,
x: intX,
y: intY,
scale: intScale
};
}
2019-09-17 13:11:42 -04:00
router.use((req, res) => {
return res.status(404).send('Tile not found');
});
2018-09-10 05:44:32 -04:00
export default router;