-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
158 lines (146 loc) · 5.01 KB
/
app.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
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
var fs = require('fs');
var request = require('request-promise');
var mysql = require('mysql');
var mssql = require('mssql');
// Setup exit conditions
process.on('SIGINT', function() {
console.log('==== Geocoded ' + logged + ' rows successfully ====');
process.exit();
});
process.on('exit', function() {
console.log('==== Geocoded ' + logged + ' rows successfully ====');
});
// Load settings
var SETTINGS = JSON.parse(fs.readFileSync('settings.json', 'utf8').trim());
if (SETTINGS.apikey == "") {
console.log("API key was not set in settings.json. Exiting.");
}
// Setup helper functions
var logged = 0;
function printRow(postcode, latitude, longitude) {
var output = postcode;
output += ',';
if (latitude) output += latitude;
output += ',';
if (longitude) output += longitude;
fs.appendFileSync('./output.csv', output + '\n');
logged++;
if (logged % 100 == 0) console.log("Completed " + logged + " rows.");
}
function printError(postcode, latitude, longitude) {
var output = postcode;
output += ',';
if (latitude) output += latitude;
output += ',';
if (longitude) output += longitude;
fs.appendFileSync('./error.csv', output + '\n');
logged++;
}
function insertLookupMysql(postcode, latitude, longitude) {
connection.query(SETTINGS.database.setPostcodeSql, [latitude, longitude, postcode], function(err, result) {
if (err) {
console.log(err);
printError(postcode, country, prov);
}
console.log('Inserted row '+ postcode + ','+latitude+ ','+longitude);
printRow(postcode, latitude, longitude);
});
}
function insertLookupMssql(postcode, latitude, longitude) {
return mssqlPool.request()
.input('postcode', mssql.NVarChar, postcode)
.input('latitude', mssql.NVarChar, latitude)
.input('longitude', mssql.NVarChar, longitude)
.query(SETTINGS.database.setPostcodeSql)
.then(result => {
console.log('Inserted row '+ postcode + ','+latitude+ ','+longitude);
printRow(postcode, latitude, longitude);
});
}
// Main script logic
if (SETTINGS.database.type === "mysql") {
var connection = mysql.createConnection(SETTINGS.database.connectionDetails);
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected to db as id ' + connection.threadId);
});
var selectAllSql = SETTINGS.database.selectAllPostcodesSql;
connection.query(selectAllSql, function(error, results, fields) {
if (error) return console.log(error);
console.log("Processing " + results.length + " rows");
var batches = []
while (results.length) {
batches.push(results.splice(0, 100));
}
for (var i = 0; i<batches.length; i++) {
getLocation(batches[i], insertLookupMysql)
}
});
} else {
var mssqlPool = null;
mssql.connect(SETTINGS.database.connectionDetails)
.then(pool => {
mssqlPool = pool;
return pool.request()
.query(SETTINGS.database.selectAllPostcodesSql);
})
.then(result => {
console.log("Processing " + result.recordset.length + " rows");
var batches = [];
while (result.recordset.length) {
batches.push(result.recordset.splice(0, 100));
}
var batchPromises = [];
for (var i = 0; i<batches.length; i++) {
batchPromises.push(getLocation(batches[i], insertLookupMssql));
}
return Promise.all(batchPromises);
})
.then(() => {
console.log("All postcodes have been geocoded. Exiting...");
process.exit();
})
.catch(err => {
console.log("An error occurred.");
console.log(err);
});
}
function getLocation(batch, insertLookup) {
var url = 'http://www.mapquestapi.com/geocoding/v1/batch?key=' + SETTINGS.apikey;
for (var i = 0; i<batch.length; i++) {
url += '&location=' + batch[i]["postcode"] + ',Canada'
}
var options = {
uri: url,
json: true
}
return request(options)
.then(function(body) {
if (!body.results) return;
if (!body.results.length) return;
var insertLookupPromises = [];
for (var i = 0; i < body.results.length; i++) {
var postcode;
var latitude;
var longitude;
if (body.results[i].providedLocation) {
postcode = body.results[i].providedLocation.location.split(',')[0]
} else {
continue;
}
if (body.results[i].locations[0]) {
if (body.results[i].locations[0].latLng.lat) latitude = body.results[i].locations[0].latLng.lat;
if (body.results[i].locations[0].latLng.lng) longitude = body.results[i].locations[0].latLng.lng;
}
insertLookupPromises.push(insertLookup(postcode, latitude, longitude));
}
return Promise.all(insertLookupPromises);
})
.catch(function(error) {
console.log("Error with request: " + options.uri);
console.log(error);
});
}