From 927ff297cdf031054a1a76b392a509e186c5abae Mon Sep 17 00:00:00 2001 From: Norman Rzepka Date: Sun, 12 Aug 2018 18:36:25 +0200 Subject: [PATCH] moar buffer --- Dockerfile | 2 +- README.md | 13 +++++++++---- index.js | 17 +++++++++++++---- package.json | 1 + yarn.lock | 4 ++++ 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index cefae5b..d67ff74 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,6 +6,6 @@ RUN yarn install ENV TILE_SET_CACHE 128 ENV TILE_SET_PATH /app/data -ENV MAX_POST_SIZE 500kb +ENV MAX_POST_SIZE 700kb CMD ["yarn", "run", "start"] diff --git a/README.md b/README.md index 1d09ff0..694ee45 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,12 @@ Elevation service that works with the [Terrain data provided on Amazon AWS S3](h Inspired by: -* https://github.com/perliedman/elevation-service -* https://github.com/perliedman/node-hgt +- https://github.com/perliedman/elevation-service +- https://github.com/perliedman/node-hgt ## API usage -The service has a very simple API. Just post your latitude-longitude pairs as a JSON array to the service and receive an array of elevations as response. Maximum post payload is by default 500 KB. + +The service has a very simple API. Just post your latitude-longitude pairs as a JSON array to the service and receive an array of elevations as response. Maximum post payload is by default 700 KB (which fits roughly 10,000 points). ```bash # > [[lat, lng], ...] @@ -19,11 +20,13 @@ curl -d '[[51.3, 13.4], [51.4, 13.3]]' -XPOST -H 'Content-Type: application/json ## Usage with pre-downloaded data Download data (ca. 200 GB): + ```bash aws s3 cp --recursive s3://elevation-tiles-prod/skadi /path/to/data/folder ``` Run the docker container: + ```bash docker run --rm -v/path/to/data/folder:/app/data -p3000:3000 normanrz/elevation-service ``` @@ -31,9 +34,11 @@ docker run --rm -v/path/to/data/folder:/app/data -p3000:3000 normanrz/elevation- ## Usage with S3-hosted data Run the docker container: + ```bash docker run --rm -eTILE_SET_PATH=s3:// -p3000:3000 normanrz/elevation-service ``` ## License -MIT \ No newline at end of file + +MIT diff --git a/index.js b/index.js index b71e57d..1e19f81 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,11 @@ const { json, send } = require("micro"); +const limitedMap = require("limited-map"); const { FileTileSet, S3TileSet } = require("./tileset"); const cacheSize = process.env.TILE_SET_CACHE || 128; const tileFolder = process.env.TILE_SET_PATH || __dirname; const maxPostSize = process.env.MAX_POST_SIZE || "500kb"; +const maxParallelProcessing = 500; const tiles = tileFolder.startsWith("s3://") ? new S3TileSet({ cacheSize }) @@ -14,11 +16,18 @@ module.exports = async (req, res) => { return send(res, 405, { error: "Only POST allowed" }); } - const geojson = await json(req, { limit: maxPostSize }); - if (!geojson || Object.keys(geojson).length === 0) { - return send(res, 400, { error: "Invalid GeoJSON" }); + const payload = await json(req, { limit: maxPostSize }); + if (!payload || Object.keys(payload).length === 0) { + return send(res, 400, { + error: + "Invalid Payload. Expected a JSON array with latitude-longitude pairs: [[lat, lng], ...]" + }); } - const result = await Promise.all(geojson.map(ll => tiles.getElevation(ll))); + const result = await limitedMap( + payload, + ll => tiles.getElevation(ll), + maxParallelProcessing + ); return result; }; diff --git a/package.json b/package.json index dac4ebe..56e2c33 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "author": "Norman Rzepka ", "license": "MIT", "dependencies": { + "limited-map": "^0.0.1", "lru-memoize": "^1.0.2", "micro": "^9.3.2" }, diff --git a/yarn.lock b/yarn.lock index 268fdcd..9e58f37 100644 --- a/yarn.lock +++ b/yarn.lock @@ -71,6 +71,10 @@ is-stream@1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44" +limited-map@^0.0.1: + version "0.0.1" + resolved "https://registry.yarnpkg.com/limited-map/-/limited-map-0.0.1.tgz#78a24fd73b0fb58312f7d639ca572b1bfd3f0bf0" + lru-memoize@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/lru-memoize/-/lru-memoize-1.0.2.tgz#f5ae84d288e7d55fec8388ec0bd725621bc815d0"