-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
80 lines (63 loc) · 2.25 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
const INPUT_CSV = 'input/test.csv'; // location of the input csv file
const OUTPUT_CSV = 'output/test.csv'; // location where to store the result
const ADDRESS_COLUMN = 'adresse'; // the name of the field in the csv that stores the address string
const DELIMITER = ';'; // the csv file delimiter
const GOOGLE_API_KEY = 'YOUR_API_KEY'; // your google geocoder api key: https://developers.google.com/maps/documentation/geocoding/start?hl=de#get-a-key
const ADD_CLEAN_ADRESS_ROW = false;
const fs = require('fs');
const path = require('path');
const dsv = require('d3-dsv');
const axios = require('axios');
const idx = require('idx');
const inputPath = path.resolve(__dirname, INPUT_CSV);
const outputPath = path.resolve(__dirname, OUTPUT_CSV);
const csvParser = dsv.dsvFormat(DELIMITER);
const data = csvParser.parse(fs.readFileSync(inputPath).toString());
async function geocodeRow(row) {
return new Promise((resolve, reject) => {
const address = row[ADDRESS_COLUMN];
row.lat = '';
row.lng = '';
if (ADD_CLEAN_ADRESS_ROW) {
row.address_clean = '';
}
if (!address) {
return resolve(row);
}
console.log('geocoding address:', address);
const cleanedAddress = address
.replace(/\s/g, '+')
.replace(/ß/g, 'ss')
.replace(/ö|Ö/g, 'oe')
.replace(/ü|Ü/g, 'ue')
.replace(/ä|Ä/g, 'ae');
axios.get(`https://maps.googleapis.com/maps/api/geocode/json?address=${cleanedAddress}&key=${GOOGLE_API_KEY}`)
.then(res => {
row.lat = idx(res.data, _ => _.results[0].geometry.location.lat) || '';
row.lng = idx(res.data, _ => _.results[0].geometry.location.lng) || '';
if (ADD_CLEAN_ADRESS_ROW) {
row.address_clean = idx(res.data, _ => _.results[0].formatted_address) || '';
}
resolve(row);
})
.catch(err => {
resolve(row);
})
});
}
(async () => {
let successCounter = 0;
for (let row of data) {
row = await geocodeRow(row);
if (row.lat && row.lng) {
successCounter++;
}
}
const output = csvParser.format(data);
fs.writeFile(outputPath, output, err => {
if (err) {
throw err;
}
console.log(`${successCounter} of ${data.length} rows geocoded and written to: ${OUTPUT_CSV}`);
});
})();