Skip to content

Commit

Permalink
s3 tileset
Browse files Browse the repository at this point in the history
  • Loading branch information
normanrz committed Aug 4, 2018
1 parent 6c695a4 commit a586ff4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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") {
Expand Down
File renamed without changes.
73 changes: 59 additions & 14 deletions tileset.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit a586ff4

Please sign in to comment.