-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (46 loc) · 1.21 KB
/
index.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
require('dotenv').config();
const util = require('util');
const request = util.promisify(require('request'));
const fs = require('fs');
const yaml = require('js-yaml');
const fileContents = fs.readFileSync('./offices.yml', 'utf8');
const { offices } = yaml.safeLoad(fileContents);
const geoJson = {
type: 'FeatureCollection',
features: [],
};
const promises = offices.map((o) => {
// NOTE: filter statement is akin to _.compact
const address = [
o.street,
o.zip,
]
.filter(Boolean)
.join(', ');
return request({
url: 'https://maps.googleapis.com/maps/api/geocode/json',
qs: {
address,
key: process.env.GOOGLE_GEOCODER_API_KEY,
},
});
});
Promise.all(promises)
.then((results) => {
results.forEach(({ body }, i) => {
const { results: [ { geometry: { location } } ]} = JSON.parse(body);
const o = offices[i];
geoJson.features.push({
type: 'Feature',
properties: Object.assign({}, o),
geometry: {
type: "Point",
coordinates: [
location.lng,
location.lat,
]
},
});
});
fs.writeFileSync('offices.geojson', JSON.stringify(geoJson, null, 2), 'utf8');
});