-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathnodejs-parallel-batch-geocoding.js
More file actions
214 lines (200 loc) · 5.72 KB
/
nodejs-parallel-batch-geocoding.js
File metadata and controls
214 lines (200 loc) · 5.72 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* This example demonstrates how to batch geocode addresses using OpenCage Data Geocoder.
*
* Create a file file_to_geocode.csv
* id,address
* 1,"Madrid,Spain"
* 2,"Milan,Italy"
* 3,"Berlin,Germany"
* ...
*
* The batch file runs asynchronously with parallel workers, so it is important to have unique id so the results can be matched.
*
* create a .env file with your API KEY
* OPENCAGE_API_KEY=<YOUR API KEY>
*
* npm install opencage-api-client async csv-parser csv-stringify
*
* node nodejs-parallel-batch-geocoding.js
*
* Adjust CONCURRENCY value to increase the throughput. Check the <https://caolan.github.io/async/v3/docs.html#queue>documentation</a>
*
*/
// NodeJS builtin
const fs = require('fs');
// Dependencies
const { geocode } = require('opencage-api-client');
const async = require('async');
const csv = require('csv-parser');
const stringify = require('csv-stringify');
// --------------------------------------
//
const CONCURRENCY = 2;
//
const LANGUAGE = 'en';
//
const INFILE = 'file_to_geocode.csv';
const OUTFILE = 'file_geocoded.csv';
//
const NO_RS = {
id: '',
input: '',
latitude: 0,
longitude: 0,
// Any of these components might be empty :
country: '',
county: '',
city: '',
postcode: '',
road: '',
house_number: '',
//
confidence: -1,
formatted: '',
};
// output file columns
const stringifierOptions = {
columns: [
'id',
'input',
'latitude',
'longitude',
'country',
'county',
'city',
'postcode',
'road',
'house_number',
'confidence',
'formatted',
],
};
// write queue ensures sequential file writes (concurrency: 1)
// csv-stringify library quotes fields containing special characters, which provides
// basic protection against CSV injection (so values starting with = aren't interpreted
// as formulas by Excel or such)
const writeQueue = async.queue((task, callback) => {
stringify([task.data], stringifierOptions, (error, content) => {
if (error) {
console.error('error stringifying the result');
return callback(error);
}
fs.appendFile(OUTFILE, content, (err) => {
if (err) console.error('error writing line', err);
callback(err);
});
});
}, 1);
const outputResult = (data) => {
return new Promise((resolve, reject) => {
writeQueue.push({ data }, (err) => {
if (err) reject(err);
else resolve();
});
});
};
const gracefulExit = (code) => {
writeQueue.drain(() => {
process.exit(code);
});
};
const MAX_ADDRESS_LENGTH = 200;
const isValidAddress = (address) => {
if (!address || typeof address !== 'string') return false;
const trimmed = address.trim();
if (trimmed.length === 0 || trimmed.length > MAX_ADDRESS_LENGTH) return false;
return true;
};
const geocodeAddress = async ({ id, address }) => {
console.log(`geocoding "${address}"`);
try {
if (isValidAddress(address)) {
const apiResult = await geocode({
q: address,
limit: 1,
no_annotations: 1,
language: LANGUAGE,
});
// console.log(apiResult);
// NodeJS<14 use : if(apiResult && apiResult.results && apiResult.results.length > 0)
if (apiResult?.results?.length > 0) {
const geocoded = apiResult.results[0];
const result = {
id,
input: address,
latitude: geocoded.geometry.lat,
longitude: geocoded.geometry.lng,
// Any of these components might be empty :
country: geocoded.components.country || '',
county: geocoded.components.county || '',
city: geocoded.components.city || '',
postcode: geocoded.components.postcode || '',
road: geocoded.components.road || '',
house_number: geocoded.components.house_number || '',
//
confidence: geocoded.confidence,
formatted: geocoded.formatted,
};
// console.log(geocoded.formatted);
return outputResult(result);
}
}
return outputResult({ ...NO_RS, id, input: address });
} catch (error) {
const statusCode = error.status?.code;
switch (statusCode) {
case 401:
console.error('Invalid API key. Check your OPENCAGE_API_KEY.');
gracefulExit(401);
break;
case 402:
console.error('Daily limit reached. Signup for a paid plan or upgrade your plan.');
gracefulExit(402);
break;
case 403:
console.error('API key suspended or access forbidden.');
gracefulExit(403);
break;
case 429:
console.error('Rate limit exceeded. Reduce CONCURRENCY or add delays.');
gracefulExit(429);
break;
default:
console.error(`Geocoding error for "${address}":`, error.message || error);
return outputResult({ ...NO_RS, id, input: address });
}
}
};
const processFileStream = async (queue) => {
fs.createReadStream(INFILE)
.on('error', (err) => {
console.error(`Error reading input file: ${err.message}`);
gracefulExit(1);
})
.pipe(csv(['id', 'address']))
.on('data', (data) => {
// console.log(`Line from file: ${JSON.stringify(data)}`);
console.log(`Line from file: ${data.id}, ${data.address}`);
queue.push({
id: data.id,
address: data.address,
});
})
.on('end', () => {
console.log('Csv file fully parsed');
});
};
const run = async () => {
// check input file exists
if (!fs.existsSync(INFILE)) {
console.error(`Input file '${INFILE}' not found.`);
process.exit(1);
}
// clear output file before starting
fs.writeFileSync(OUTFILE, '');
// create a queue object with concurrency
const queue = async.queue(geocodeAddress, CONCURRENCY);
await processFileStream(queue);
};
console.log('Running Batch Geocoding');
run();