-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
59 lines (56 loc) · 2 KB
/
index.ts
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
import axios from "axios";
import * as sharedstreetsPbf from "sharedstreets-pbf";
export type Tile = number[];
export type Layer = "geometry" | "intersection" | "metadata" | "reference";
export type Output = "json" | "pbf";
export type Extensions = "pbf";
export type Protocols = "https" | "http";
/**
* Download Tile
*
* @param {Array<number>} tile Tile [x, y, z]
* @param {string} layer Layer (geometry|intersection|metadata|reference)
* @param {Object} [options={}] Optional parameter
* @param {string} [options.output="pbf"] Output (json|pbf)
* @param {string} [options.protocol="https"] Protocol (https|http)
* @param {string} [options.domain="tiles.sharedstreets.io"] Domain
* @param {string} [options.extension="pbf"] Extension
* @returns {Promise<Buffer>} PBF Buffer
* @example
* const tile = [1186, 1466, 12];
* const layer = "geometry";
*
* sharedstreetsApi.downloadTile(tile, layer).then(data => {
* data // => PBF Buffer
* })
*/
export function downloadTile(tile: Tile, layer: Layer, options: {
output?: Output,
protocol?: Protocols,
domain?: string,
extension?: Extensions,
} = {}) {
// Default parameters
const output = options.output || "pbf";
const protocol = options.protocol || "https";
const domain = options.domain || "tiles.sharedstreets.io";
const extension = options.extension || "pbf";
const [x, y, z] = tile;
const url = `${protocol}://${domain}/${z}-${x}-${y}.${layer}.${extension}`;
// Perform HTTP connection to SharedStreets API Server
return axios.get(url).then((response) => {
const data = response.data;
// Handle outputs in JSON
if (options.output === "json") {
switch (layer) {
case "geometry": return sharedstreetsPbf.geometry(data);
case "intersection": return sharedstreetsPbf.intersection(data);
case "metadata": return sharedstreetsPbf.metadata(data);
case "reference": return sharedstreetsPbf.reference(data);
default: throw new Error("invalid layer");
}
}
// Data in PBF
return data;
});
}