-
Notifications
You must be signed in to change notification settings - Fork 5
/
bbox.js
100 lines (79 loc) · 2.7 KB
/
bbox.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env node
'use strict';
const request = require('request');
const prompt = require('prompt');
const validateBbox = require('../util/validateBbox');
const auth = require('../util/get_auth');
const HTTPError = require('../util/errors').HTTPError;
class Query {
constructor(api) {
this.api = api;
}
help() {
console.error('');
console.error('Fetch raw data from the server using the bbox API');
console.error('');
console.error('usage: cli.js bbox');
console.error('');
}
cli(options = {}) {
const self = this;
if (!options) options = {};
if (options.script) return run(options);
prompt.message = '$';
prompt.start({
stdout: process.stderr
});
let args = [{
name: 'bbox',
message: 'bbox to download',
required: true,
type: 'string',
default: options.bbox
}];
if (this.api.auth_rules && this.api.auth_rules.feature.get !== 'public') {
args = args.concat(auth(this.api.user));
}
prompt.get(args, (err, argv) => {
prompt.stop();
if (argv.username) {
this.api.user = {
username: argv.username,
password: argv.password
};
}
return run(argv);
});
function run(argv) {
self.main(argv, (err, res) => {
if (err) throw err;
console.log(res);
});
}
}
/**
* Queries hecate /api/data/features endpoint
* Currently supports downloading features by bbox
*
* @param {!Object} options Options for making a request to the hecate /api/data/features endpoint
* @param {Array|string} [options.bbox] Bounding box of features to download from hecate
*/
// TODO: handle queries to the features endpoint other than bbox
main(options = {}, cb) {
if (!options) options = {};
if (!options.bbox) return cb(new Error('options.bbox required'));
// Validate options.bbox. Will throw an error it bbox is invalid
if (options.bbox) validateBbox(options.bbox);
// TODO this should return a stream or else we will quickly run out of memory
request({
method: 'GET',
url: `http://${this.api.url}:${this.api.port}/api/data/features?bbox=${options.bbox}`,
auth: this.api.user
}, (err, res) => {
if (err) return cb(err);
if (res.statusCode !== 200) return cb(new HTTPError(res.body, res));
return cb(null, res.body);
});
}
}
module.exports = Query;