-
Notifications
You must be signed in to change notification settings - Fork 0
/
from_latlong_to_streetname.js
79 lines (68 loc) · 2.77 KB
/
from_latlong_to_streetname.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
/*
get file list -- https://medium.com/stackfame/get-list-of-all-files-in-a-directory-in-node-js-befd31677ec5
open file (json) -- https://stackabuse.com/reading-and-writing-json-files-with-node-js
get required elements
push into array
save array into csv
*/
//requiring path and fs modules
const path = require('path');
const fs = require('fs');
//joining path of directory
const directoryPath = path.join(__dirname, '..\\json');
const All_Addresses = []
//passsing directoryPath and callback function
fs.readdir(directoryPath, function (err, files) {
//handling error
if (err) {
return console.log('Unable to scan directory: ' + err);
}
//listing all files using forEach
files.forEach(function (file) {
// Do whatever you want to do with the file
//console.log(file);
let rawdata = fs.readFileSync('D:\\ONE DRIVE - PESSOAL\\OneDrive\\Site_Doutoramento\\LATLONG_STREET\\json\\' + file);
let address = JSON.parse(rawdata);
if (!address.error) {
const address_info = {
Filename : file,
Address_Line1: address.features[0].properties.address_line1,
Address_Line2: address.features[0].properties.address_line2,
Formatted: address.features[0].properties.formatted,
Street: address.features[0].properties.street,
Housenumber : address.features[0].properties.housenumber
}
/*
console.log(address.features[0].properties.address_line1);
console.log(address.features[0].properties.address_line2);
console.log(address.features[0].properties.formatted);
console.log(address.features[0].properties.street);
console.log(address.features[0].properties.housenumber);
*/
All_Addresses.push(address_info);
}
});
//console.table(All_Addresses);
writeToCSVFile(All_Addresses);
});
//address.features[0].properties.address_line1
//address.features[0].properties.address_line2
//address.features[0].properties.formatted
//address.features[0].properties.street
function writeToCSVFile(addresses) {
const filename = '..\\output-address-info.csv';
fs.writeFile(filename, extractAsCSV(addresses), err => {
if (err) {
console.log('Error writing to csv file', err);
} else {
console.log(`saved as ${filename}`);
}
});
}
function extractAsCSV(addresses) {
const header = ["Filename|Address_Line1|Address_Line2|Formatted|Street|Housenumber"];
const rows = addresses.map(address =>
`${address.Filename}|${address.Address_Line1}|${address.Address_Line2}|${address.Formatted}|${address.Street}|${address.Housenumber}`
);
return header.concat(rows).join("\n");
}