-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
521 lines (421 loc) Β· 18.8 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/* eslint-disable no-console */
/* eslint-disable no-restricted-syntax */
/* eslint-disable guard-for-in */
/* eslint-disable no-await-in-loop */
/* eslint-disable no-continue */
const XLSX = require('xlsx');
const fs = require('fs');
const nodeCleanup = require('node-cleanup');
const express = require('express');
const { argv } = require('yargs');
const path = require('path');
const get = require('lodash.get');
const NodeGeocoder = require('./geocoder');
const inPolygon = require('./utils/in-polygon');
// ALTAY KRAY
// const REGIONS = require('./regions/altay-kray-regions.json');
// const POLYGONS = require('./regions/altay-kray-polygons');
// const RENAMES = require('./regions/altay-kray-renames');
// ALTAY REPUBLIC
// const REGIONS = require('./regions/altay-republic-regions.json');
// const POLYGONS = require('./regions/altay-republic-polygons');
// const RENAMES = require('./regions/altay-republic-renames');
// BASHKIRIA
// const REGIONS = require('./regions/bashkiria-regions.json');
// const POLYGONS = require('./regions/bashkiria-polygons');
// const RENAMES = require('./regions/bashkiria-renames');
// GORKY
// const REGIONS = require('./regions/gorky-oblast-regions.json');
// const POLYGONS = require('./regions/gorky-oblast-polygons');
// const RENAMES = require('./regions/gorky-oblast-renames');
// // KARELIA
// const REGIONS = require('./regions/karelian-assr-regions.json');
// const POLYGONS = require('./regions/karelian-assr-polygons');
// const RENAMES = require('./regions/karelian-assr-renames');
// NORTH OSETIA
const REGIONS = require('./regions/north-osetia-regions.json');
const POLYGONS = require('./regions/north-osetia-polygons');
const RENAMES = require('./regions/north-osetia-renames');
// consts
const API_KEYS = [
'13932715-051b-41fb-a1e2-e18d40c4ca96',
'dd3d890a-1093-430b-a7b2-9d77264c8222',
'a8eae0a1-44d2-47b6-839e-9f445a0ca5ab'
];
const PERSON_ID_FIELD = 'ID Memorial DB';
const DISTRICT_FIELD = 'DistrictName';
const Mode = {
STDOUT: 'stdout',
XLSX: 'xlsx'
};
// settings
const INPUT_DATA = argv.data || path.join('.', 'data', fs.readdirSync('./data').find((f) => !['.DS_Store'].includes(f)));
const PORT = argv.port || 8080;
const REDUCE_BY = Number(argv.reduce) ? Number(argv.reduce) : 50;
const HEAT_MAP = Boolean(argv.hm);
const IDS = Boolean(argv.ids) && String(argv.ids).split(',').map(Number);
const IDS_SOURCE = Boolean(argv['ids-src']) && require(argv['ids-src']); // eslint-disable-line import/no-dynamic-require, global-require
const FROM_ID = Number(argv['from-id']);
const MODE = argv.mode === Mode.STDOUT ? Mode.STDOUT : Mode.XLSX;
function getArea(rawResult, index) {
return get(rawResult, `response.GeoObjectCollection.featureMember[${index}].GeoObject.metaDataProperty.GeocoderMetaData.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName`);
}
function getEdgeIds(output) {
return `${output[0].personId}-${output[output.length - 1].personId}`;
}
function prepareAddress(address) {
let result = address;
RENAMES.forEach((rename) => {
rename.from.forEach((ruleFrom) => {
const reg = new RegExp(ruleFrom, 'ig');
if (reg.test(address)) {
result = result.replace(reg, rename.to);
}
});
});
return result;
}
function extractdistrictID(address) {
return Object.keys(REGIONS).find((districtID) => (
REGIONS[districtID]
.matches
.find((m) => new RegExp(m, 'ig').test(address))
));
}
function enrichGeocoderResultWithDistrict({ geocoderResult }) {
return geocoderResult.map((geocoderItem) => {
const polygonIdForGeoCoderItem = Object.keys(POLYGONS)
.find((polygonId) => inPolygon({
polygon: POLYGONS[polygonId],
lat: geocoderItem.latitude,
lon: geocoderItem.longitude
}));
if (polygonIdForGeoCoderItem) {
const region = REGIONS[polygonIdForGeoCoderItem];
return {
...geocoderItem,
inPolygon: true,
districtName: region ? region.districtName : polygonIdForGeoCoderItem,
districtId: region ? region.districtID : polygonIdForGeoCoderItem
};
}
return {
...geocoderItem,
inPolygon: false
};
});
}
(async () => {
const output = [];
const coordinates = [];
const geocoder = NodeGeocoder({ apikey: API_KEYS[2] });
function saveResults() {
if (output.length === 0) {
console.log('=> NO GEOCODE DATA');
return;
}
if (MODE === Mode.XLSX) {
console.log('=> WRITING FILES');
const outputWs = XLSX.utils.json_to_sheet(output);
const outputWb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(outputWb, outputWs, 'Sheet 1');
const outputsLength = fs.readdirSync('./result').filter((fileName) => /^output/.test(fileName)).length;
XLSX.writeFile(outputWb, `./result/output-${outputsLength + 1}-${getEdgeIds(output)}.xlsx`);
console.log('=> π XLSX FILES READY');
}
if (MODE === Mode.STDOUT) {
console.log('=> π OUTPUT READY');
console.log(output);
}
if (HEAT_MAP) {
console.log('=> PREPEARING HEATMAP...');
fs.writeFileSync('./result/data.js', `const data = ${JSON.stringify(coordinates)};`);
console.log('=> STARTING HEATMAP SERVER');
const server = express();
server.use(express.static(__dirname));
server.listen(PORT, () => {
console.log('=> π SERVER STARTED');
console.log(`Go to http://localhost:${PORT}/heatmap.html`);
console.log('Press Ctrl + C to stop server');
});
}
}
function appendResultToOutput(geocoderResult, calculatedResult, flags) {
const safeResults = geocoderResult.length > 0 ? geocoderResult : [{}];
const enrichedResults = enrichGeocoderResultWithDistrict({ geocoderResult: safeResults });
enrichedResults.forEach((r, i) => {
output.push({
...calculatedResult,
latitude: r.latitude,
longitude: r.longitude,
city: r.city,
state: r.state,
area: getArea(geocoderResult.raw, i),
streetName: r.streetName,
streetNumber: r.streetNumber,
formattedAddress: r.formattedAddress,
matchedPolygon: r.inPolygon ? r.districtName : undefined,
...flags
});
coordinates.push([r.latitude, r.longitude]);
});
}
nodeCleanup((exitCode, signal) => {
if (signal) {
saveResults();
process.kill(process.pid, signal);
nodeCleanup.uninstall();
process.exit(exitCode);
}
return false;
});
console.log(`reading ${INPUT_DATA}`);
const workBook = XLSX.readFile(INPUT_DATA);
const targetSheetName = (workBook.SheetNames || [''])[0];
const workSheet = workBook.Sheets[targetSheetName];
const json = XLSX.utils.sheet_to_json(workSheet, { raw: true });
let reducedJson = json;
if (IDS || IDS_SOURCE) {
reducedJson = json
.filter((person) => (IDS || IDS_SOURCE)
.includes(person[PERSON_ID_FIELD]));
} else if (FROM_ID) {
let fromIndex = 0;
for (let i = 0; i < json.length; i += 1) {
if (json[i][PERSON_ID_FIELD] === FROM_ID) {
fromIndex = i;
break;
}
}
reducedJson = json.slice(fromIndex);
} else if (REDUCE_BY > 0) {
reducedJson = json.filter((_, index) => index % REDUCE_BY === 0);
}
console.log('=> ALL DATA LENGTH');
console.log(json.length);
console.log('=> REDUCED DATA LENGTH');
console.log(reducedJson.length);
console.log('=> STARTING GEOCODING\n');
try {
for (const index in reducedJson) {
/**
* Output flags:
*
* DISTRICT_EXTRACTED - we attributed district name from address name
* COORDINATES_GAINED - Geocoder returned more than one result
* COORDINATES_IN_DISTRICT - Geocoder returned coordinates of a settlement inside polygon of an attributed district
* COORDINATES_IN_REGION - Geocoder returned coordinates of a settlement inside polygon of the region
* MULTIPLE_RESULTS - Geocoder returned more than 1 result inside polygons
* RESOLUTION - Text description of results
*/
const person = reducedJson[index];
const personId = person[PERSON_ID_FIELD];
const originalAddress = `${person.Region} ${person.residence}`;
const preparedAddress = `${person.Region} ${prepareAddress(person.residence)}`;
const districtNameFromInputTable = person[DISTRICT_FIELD];
console.log(`${Number(index) + 1}/${reducedJson.length}: Geocoding ${personId} ${originalAddress}...`);
const districtIDExtractedFromAddress = extractdistrictID(
districtNameFromInputTable || originalAddress
);
const districtName = districtIDExtractedFromAddress
&& REGIONS[districtIDExtractedFromAddress].districtName;
const geocoderResult = await geocoder.geocode(preparedAddress);
const calculatedResult = {
personId,
originalAddress,
preparedAddress,
districtID: districtIDExtractedFromAddress,
districtName,
geocoderRawResultsCount: geocoderResult.length
};
const flags = {
DISTRICT_EXTRACTED: false,
COORDINATES_GAINED: false,
COORDINATES_IN_DISTRICT: false,
COORDINATES_IN_REGION: false,
MULTIPLE_RESULTS: false,
RESOLUTION: ''
};
// Geocoder did not return any result and failed to attribute district
if (!districtIDExtractedFromAddress && geocoderResult.length === 0) {
const resolution = 'β Not found at all';
appendResultToOutput(geocoderResult, calculatedResult, {
...flags,
RESOLUTION: resolution
});
console.log(`${resolution}\n`);
continue;
}
// Geocoder did not return any result, but district was attributed
if (districtIDExtractedFromAddress && geocoderResult.length === 0) {
const resolution = `β Πatched only by adress string in district "${districtName}"`;
appendResultToOutput(geocoderResult, calculatedResult, {
...flags,
DISTRICT_EXTRACTED: true,
RESOLUTION: resolution
});
console.log(`${resolution}\n`);
continue;
}
// Geocoder returned one result, and it was inside the attributed district
if (districtIDExtractedFromAddress && geocoderResult.length === 1) {
console.log(districtIDExtractedFromAddress);
const resultInRegionPolygon = geocoderResult.filter((r) => inPolygon({
polygon: POLYGONS[districtIDExtractedFromAddress],
lat: r.latitude,
lon: r.longitude
}));
const resultInAreaPolygon = geocoderResult.filter(
(r) => Object.values(POLYGONS)
.find((polygon) => inPolygon({
polygon,
lat: r.latitude,
lon: r.longitude
}))
);
if (resultInRegionPolygon.length > 0) {
const resolution = `π Found 1 result in polygon for "${districtName}"`;
appendResultToOutput(geocoderResult, calculatedResult, {
...flags,
DISTRICT_EXTRACTED: true,
COORDINATES_GAINED: true,
COORDINATES_IN_DISTRICT: true,
COORDINATES_IN_REGION: true,
MULTIPLE_RESULTS: resultInRegionPolygon.length > 1,
RESOLUTION: resolution
});
console.log(`${resolution}\n`);
continue;
}
if (resultInAreaPolygon.length > 0) {
const resolution = `π½ Found 1 result in the whole region but not in "${districtName}"`;
appendResultToOutput(geocoderResult, calculatedResult, {
...flags,
DISTRICT_EXTRACTED: true,
COORDINATES_GAINED: true,
COORDINATES_IN_REGION: true,
MULTIPLE_RESULTS: resultInAreaPolygon.length > 1,
RESOLUTION: resolution
});
console.log(`${resolution}\n`);
continue;
}
const resolution = `π΅ Not found any results in polygon for "${districtName}" and whole region`;
appendResultToOutput([{}], calculatedResult, {
...flags,
DISTRICT_EXTRACTED: true,
COORDINATES_GAINED: true,
RESOLUTION: resolution
});
console.log(`${resolution}\n`);
continue;
}
// Geocoder returned multiple results, but found one inside the attributed district
if (districtIDExtractedFromAddress && geocoderResult.length > 1) {
const resultInRegionPolygon = geocoderResult.filter((r) => inPolygon({
polygon: POLYGONS[districtIDExtractedFromAddress],
lat: r.latitude,
lon: r.longitude
}));
const resultInAreaPolygon = geocoderResult.filter(
(r) => Object.values(POLYGONS)
.find((polygon) => inPolygon({
polygon,
lat: r.latitude,
lon: r.longitude
}))
);
if (resultInRegionPolygon.length > 0) {
const resolution = `π Found multiple results but specified by polygon for "${districtName}"`;
appendResultToOutput(resultInRegionPolygon, calculatedResult, {
...flags,
DISTRICT_EXTRACTED: true,
COORDINATES_GAINED: true,
COORDINATES_IN_DISTRICT: true,
COORDINATES_IN_REGION: true,
MULTIPLE_RESULTS: resultInRegionPolygon.length > 1,
RESOLUTION: resolution
});
console.log(`${resolution}\n`);
continue;
}
if (resultInAreaPolygon.length > 0) {
const resolution = `π½ Found multiple results in whole region but not in "${districtName}"`;
appendResultToOutput(resultInAreaPolygon, calculatedResult, {
...flags,
DISTRICT_EXTRACTED: true,
COORDINATES_IN_REGION: true,
COORDINATES_GAINED: true,
MULTIPLE_RESULTS: resultInAreaPolygon.length > 1,
RESOLUTION: resolution
});
console.log(`${resolution}\n`);
continue;
}
const resolution = `π΅ Did not find any results in polygon for "${districtName}" and whole region`;
appendResultToOutput([{}], calculatedResult, {
...flags,
MULTIPLE_RESULTS: true,
DISTRICT_EXTRACTED: true,
COORDINATES_GAINED: true,
RESOLUTION: resolution
});
console.log(`${resolution}\n`);
continue;
}
// Geocoder returned results inside the whole region
if (!districtIDExtractedFromAddress && geocoderResult.length > 0) {
const resultInAreaPolygon = geocoderResult.filter(
(r) => Object.values(POLYGONS)
.find((polygon) => inPolygon({
polygon,
lat: r.latitude,
lon: r.longitude
}))
);
if (resultInAreaPolygon.length === 1) {
let resolution;
if (geocoderResult.length === 1) {
resolution = 'π Found one result and it`s inside polygon for whole region';
} else {
resolution = 'π Found multiple results and specified by polygon for whole region';
}
appendResultToOutput(resultInAreaPolygon, calculatedResult, {
...flags,
COORDINATES_GAINED: true,
COORDINATES_IN_REGION: true,
RESOLUTION: resolution
});
console.log(`${resolution}\n`);
continue;
}
if (resultInAreaPolygon.length > 1) {
const resolution = 'π½ Found multiple results in the whole region';
appendResultToOutput(resultInAreaPolygon, calculatedResult, {
...flags,
COORDINATES_GAINED: true,
COORDINATES_IN_REGION: true,
MULTIPLE_RESULTS: true,
RESOLUTION: resolution
});
console.log(`${resolution}\n`);
continue;
}
// Geocoder did not return any results inside the whole region
const resolution = 'π΅ Did not found any results in polygon for the whole region';
appendResultToOutput(geocoderResult, calculatedResult, {
...flags,
COORDINATES_GAINED: true,
MULTIPLE_RESULTS: geocoderResult.length > 1,
RESOLUTION: resolution
});
console.log(`${resolution}\n`);
continue;
}
}
saveResults();
} catch (e) {
console.log(e);
saveResults();
}
})();