This repository was archived by the owner on May 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
193 lines (157 loc) · 5.06 KB
/
index.js
File metadata and controls
193 lines (157 loc) · 5.06 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const axios = require('axios');
const download = require('download');
const fs = require('fs');
const { exec } = require('child_process');
const locations = require('./assets/locations');
let config;
// Make sure config file exists
try {
config = require('./config/config');
// set axios defaults
axios.defaults.baseURL = config.exportAPI.url;
axios.defaults.headers.common['Authorization'] = `Bearer ${config.exportAPI.token}`;
} catch (e) {
console.error(`=>=>=> ERROR: Missing configuration file in root (config.json). See config-example.json`);
// kill script
process.exit(1);
}
axios.defaults.headers.post['Content-Type'] = 'application/json';
/**
* Select random country from each region in locations.json file
* @param locations
*/
const randomCountrySelector = (locations) => {
let selections = [];
let randomNumber;
for (let region in locations) {
// random number between 0-# of countries in region
randomNumber = Math.floor(Math.random() * locations[region].length);
// grab location @ random number index
let location = locations[region][randomNumber];
location['label'] = `${location.country} - ${location.cities[0].name}`
selections.push(locations[region][randomNumber]);
}
console.log(`Test set:\n` + selections.map(s=>`${s.label}`).join(`\n`))
return selections;
};
/**
* Create download w/ Export API /jobs route
* @param selections
*/
const downloadExportPbfs = (selections) => {
let requests = [];
selections.forEach(selection => {
const location = selection.cities[0];
const postData = {};
postData['feature_selection'] = config.exportAPI.allTagsQuery;
postData['export_formats'] = ['bundle'];
// postData['description'] = '';
postData['name'] = selection.label;
postData['the_geom'] = getBoundsGeojson(location.bounds);
requests.push(axios.post(`${config.exportAPI.url}/api/jobs`, postData))
})
// Promise.all
axios.all(requests)
.then(response => {
// send array of responses
pingExportAPIjobs(response.map(r => r.data));
})
.catch(error => {
console.error(error.response);
})
}
/**
* Ping Export API /runs route until job is complete
* @param jobs
*/
const pingExportAPIjobs = (jobs) => {
jobs.forEach(job => {
// post cool osm bounding box visualization link
console.log(`View export - ` + job.osma_link);
// ping jobs api every 10 seconds
let ping = setInterval(()=> checkJobStatus(ping,job), 15000);
})
}
/**
* Fetch Export API /runs for job status
* @param ping
* @param job
*/
const checkJobStatus = (ping, job) => {
// fetch job status
axios.get(`${config.exportAPI.url}/api/runs?job_uid=${job.uid}`)
.then(resp => {
let response = resp.data[0];
switch (response.status){
case 'COMPLETED':
let downloadUrl = response.tasks[0].download_urls[0].download_url;
clearInterval(ping);
console.log(`Job ${job.uid} complete.`);
writeUrlToDisk(downloadUrl);
break;
case 'RUNNING':
console.log(`Ping ${job.uid}`);
break;
case 'FAILED':
clearInterval(ping);
break;
default:
console.log(`Unhandled status: ${response.status}`);
break;
}
})
.catch(error => {
console.error(error);
})
}
/**
* Takes in bounds and returns a geojson feature
* @param bounds
* @returns {{}}
*/
const getBoundsGeojson = (bounds) => {
const feature = {};
const w = bounds[0];
const s = bounds[1];
const e = bounds[2];
const n = bounds[3];
feature['type'] = 'Polygon';
feature['coordinates'] = [[[w, n], [w, s], [e, s], [e, n], [w, n]]];
return feature;
}
/**
* Write file to disk
* @param downloadUrl
*/
const writeUrlToDisk = (downloadUrl) => {
let filename = `${new Date().valueOf()}.tar.gz`
download(`${config.exportAPI.url}/${downloadUrl}`, `downloads`, { filename: filename })
.then(() => {
console.log(`Download ${downloadUrl} complete.`);
unpackTarGz(`downloads/${filename}`);
})
.catch(error => {
console.error(`Error downloading ${downloadUrl}. ${error}c`);
})
}
/**
* Unzip tar into root
* @param path
*/
const unpackTarGz = (path) => {
exec(`tar -xvzf ${path}`, (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
console.error(err);
return;
}
// file housekeeping
exec(`mv osm/* downloads/`)
exec(`rm manifest.json`)
exec(`rm ${path}`)
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
}
downloadExportPbfs(randomCountrySelector(locations));