From a586ff4f239e231ffc0c1c252f0693b68f027f37 Mon Sep 17 00:00:00 2001 From: Norman Rzepka Date: Sat, 4 Aug 2018 14:14:39 +0200 Subject: [PATCH] s3 tileset --- index.js | 4 +- test/{ => N51}/N51E013.hgt.gz | Bin tileset.js | 73 +++++++++++++++++++++++++++------- 3 files changed, 61 insertions(+), 16 deletions(-) rename test/{ => N51}/N51E013.hgt.gz (100%) diff --git a/index.js b/index.js index b6bf3df..2f7f13e 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,11 @@ const { json, send } = require("micro"); -const TileSet = require("./tileset"); +const { 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 tiles = new TileSet(tileFolder, { cacheSize }); +const tiles = new S3TileSet({ cacheSize }); module.exports = async (req, res) => { if (req.method !== "POST") { diff --git a/test/N51E013.hgt.gz b/test/N51/N51E013.hgt.gz similarity index 100% rename from test/N51E013.hgt.gz rename to test/N51/N51E013.hgt.gz diff --git a/tileset.js b/tileset.js index 42f7b71..3c43a9a 100644 --- a/tileset.js +++ b/tileset.js @@ -1,44 +1,89 @@ const path = require("path"); -const fs = require("fs"); +const { createReadStream } = require("fs"); +const { get } = require("https"); +const { promisify } = require("util"); const { createGunzip } = require("zlib"); const memoize = require("lru-memoize").default; const HGT = require("./hgt"); class TileSet { - constructor(folder, options) { + constructor(options) { this.options = Object.assign( {}, { - cacheSize: 128, - gzip: true + cacheSize: 128 }, options ); this.getTile = memoize(this.options.cacheSize)(this._getTile.bind(this)); + } + + getFilePath(lat, lng) { + const latFileName = `${lat < 0 ? "S" : "N"}${String(Math.abs(lat)).padStart( + 2, + "0" + )}`; + const lngFileName = `${lng < 0 ? "W" : "E"}${String(Math.abs(lng)).padStart( + 3, + "0" + )}`; + const fileName = `${latFileName}${lngFileName}.hgt.gz`; + return `${latFileName}/${fileName}`; + } + + async getElevation(latLng) { + const tile = await this.getTile( + Math.floor(latLng[0]), + Math.floor(latLng[1]) + ); + return tile.getElevation(latLng); + } +} + +class FileTileSet extends TileSet { + constructor(folder, options) { + super( + Object.assign( + {}, + { + cacheSize: 128, + gzip: true + }, + options + ) + ); this._folder = folder; } async _getTile(lat, lng) { - const fileName = - `${lat < 0 ? "S" : "N"}${String(Math.abs(lat)).padStart(2, "0")}` + - `${lng < 0 ? "W" : "E"}${String(Math.abs(lng)).padStart(3, "0")}.hgt.gz`; - - let stream = fs.createReadStream(path.join(this._folder, fileName)); + let stream = fs.createReadStream( + path.join(this._folder, this.getFilePath(lat, lng)) + ); if (this.options.gzip) { stream = stream.pipe(createGunzip()); } const tile = await HGT.loadStream(stream, [lat, lng]); return tile; } +} - async getElevation(latLng) { - const tile = await this.getTile( - Math.floor(latLng[0]), - Math.floor(latLng[1]) +class S3TileSet extends TileSet { + async _getTile(lat, lng) { + console.log(`${S3TileSet.baseUrl}/${this.getFilePath(lat, lng)}`); + let stream = await new Promise(resolve => + get(`${S3TileSet.baseUrl}/${this.getFilePath(lat, lng)}`, resolve) ); - return tile.getElevation(latLng); + if (this.options.gzip) { + stream = stream.pipe(createGunzip()); + } + const tile = await HGT.loadStream(stream, [lat, lng]); + return tile; } } +S3TileSet.baseUrl = "https://elevation-tiles-prod.s3.amazonaws.com/skadi"; + +TileSet.S3TileSet = S3TileSet; +TileSet.FileTileSet = FileTileSet; module.exports = TileSet;