-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
48 lines (46 loc) · 1.37 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
var request = require('request');
var geojson = require('geojson');
var geocoder = require('geocoder');
var fs = require('fs');
request({
url: 'http://www.walkabout.sg/ajax/company-all',
json: true
}, function(err, resp, body){
var promises = [];
Object.keys(body).forEach(function(key){
var val = body[key];
var p = new Promise(function(resolve, reject){
if (val.long < 90){ // Only for those with invalid coords
geocoder.geocode(val.address, function(e, d){
console.log('Geocoding', key, val.address);
if (e){
console.log('FAIL', e);
resolve(val);
};
var location = d.results[0].geometry.location;
console.log('Result', location.lat, location.lng);
val.lat = location.lat;
val.long = location.lng;
resolve(val);
});
} else {
resolve(val);
}
});
promises.push(p);
});
Promise.all(promises).then(function(points){
var data = geojson.parse(points.map(function(point){
return {
name: point.companyName,
website: point.websiteURL,
address: point.address,
lat: point.lat,
lng: point.long
}
}), { Point: ['lat', 'lng'] });
fs.writeFile('sg-companies.geojson', JSON.stringify(data), function(){
console.log('sg-companies.geojson generated!');
});
});
});