-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtilelive-aws.js
83 lines (66 loc) · 2.07 KB
/
tilelive-aws.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"use strict";
const { Buffer } = require("buffer");
const url = require("url");
const {
Location
} = require("@aws-sdk/client-location");
module.exports = function (tilelive) {
const client = new Location();
// Usage: node_modules/.bin/tl copy -r tilelive-aws -z 0 -Z 2 aws:///assetTracker file://./tiles?filetype=mvt
// npm install tl tilelive-file
class AwsSource {
constructor(uri, callback) {
if (uri.path == null) {
throw new Error("Usage: aws:///<MapResource>");
}
// aws:///MapName
// this uses the path component rather than the host because URL normalization will downcase the host and resource names are case-sensitive
this.mapName = uri.path.slice(1);
return client.getMapStyleDescriptor({
MapName: this.mapName
}, (err, { Blob }) => {
if (err) {
return callback(err);
}
const style = JSON.parse(Buffer.from(Blob));
const maxzoom = Object.values(style.sources).map(({ maxzoom }) => maxzoom).pop();
this.info = {
bounds: [-180, -85.0511, 180, 85.0511],
minzoom: 0,
maxzoom,
// TODO support non-MVT sources (type !== "vector")
format: "pbf",
};
return callback(null, this);
});
}
async getTile(z, x, y, callback) {
try {
// for raster tiles, this will always fetch the 256x256 version
const tile = await client.getMapTile({
MapName: this.mapName,
Z: z,
X: x,
Y: y,
});
return callback(null, Buffer.from(tile.Blob), {
// TODO in future, tile contents may be compressed
"Content-Encoding": "identity"
});
} catch (err) {
console.log('ERROR');
console.log(err);
return callback(err);
}
}
getInfo(callback) {
return setImmediate(callback, null, this.info);
}
close(callback) {
callback = callback || function () {};
return callback();
}
}
tilelive.protocols["aws:"] = AwsSource;
return AwsSource;
};