-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateLatLongs.js
124 lines (110 loc) · 4.71 KB
/
generateLatLongs.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
const fetch = require('node-fetch');
var parser = require('xml2json');
const csvtojson = require('csvtojson');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const APP_CODE = "uVpeMH7xQCESU28Te7NHvw";
const APP_ID = "fcCDTt5BOM4w825JePpQ";
async function generateLatLongs(options) {
try {
// extract csv file records into array
let csvRecords = [];
let csvFile = "output_" + options.timestamp + ".csv";
await csvtojson()
.fromFile(csvFile)
.then((data) => {
csvRecords = data;
});
// extract and prepare addresses for sending to geocoder api
let geocoderData = "recId|searchText\n";
for (let i = 0; i < csvRecords.length; i++) {
geocoderData += (i+1) + "|" + '"' + csvRecords[i].location + '"' +"\n";
}
// get request id from geocoder api for checking status of results and getting results
let requestId = await getRequestId(geocoderData);
// check status of resutls
console.log("Generating latitude and longigude...");
let status = await checkStatus(requestId);
while (status !== "completed") {
console.log("Status: " + status);
await new Promise(resolve => setTimeout(resolve, 5000)); // wait 5 seconds
status = await checkStatus(requestId);
}
console.log("Status: " + status);
// when status is completed get results and convert to json
console.log("Fetching Results...");
let geoCoderApiResults = await getData(requestId);
await csvtojson(geoCoderApiResults)
.fromString(geoCoderApiResults)
.then((data) => {
geoCoderApiResults = data;
});
// remove duplicate results
for (let i = 0; i < geoCoderApiResults.length - 1; i++) {
if (geoCoderApiResults[i].recId === geoCoderApiResults[i+1].recId) {
geoCoderApiResults.splice(i+1, 1);
i = i - 1;
}
}
// add latitude and longitude to each csv record
for (let i = 0; i < csvRecords.length; i++) {
csvRecords[i].latitude = geoCoderApiResults[i].latitude;
csvRecords[i].longitude = geoCoderApiResults[i].longitude;
}
// write records to new csv file
const csvWriter = createCsvWriter({
path: `final_output_${options.timestamp}.csv`,
header: [
{ id: 'platform', title: 'platform' },
{ id: 'title', title: 'title' },
{ id: 'description', title: 'description' },
{ id: 'contactPhone', title: 'contactPhone' },
{ id: 'startDate', title: 'startDate' },
{ id: 'endDate', title: 'endDate' },
{ id: 'photoUrl', title: 'photoUrl' },
{ id: 'eventUrl', title: 'eventUrl' },
{ id: 'contactEmail', title: 'contactEmail' },
{ id: 'location', title: 'location' },
{ id: 'latitude', title: 'latitude' },
{ id: 'longitude', title: 'longitude' },
]
});
csvWriter
.writeRecords(csvRecords)
.then(() => {
console.log('Latitude and longitude of all addresses are generated.');
});
} catch (error) {
console.log(error);
}
}
function getRequestId(data){
const options = {
method: 'POST',
body: data,
headers: {'Content-Type': 'text/plain', 'charset':'UTF-8'}
}
return fetch(`https://batch.geocoder.api.here.com/6.2/jobs?app_code=${APP_CODE}&app_id=${APP_ID}&mode=retrieveAddresses&action=run&header=true&inDelim=|&outDelim=,&outCols=recId,latitude,longitude,locationLabel&outputcombined=true&language=de-DE`, options)
.then(res => res.text())
.then(body => {
let data = parser.toJson(body, { object: true });
let RequestId = data["ns2:SearchBatch"].Response.MetaInfo.RequestId
return RequestId;
});
}
function checkStatus(requestId) {
return fetch(`https://batch.geocoder.api.here.com/6.2/jobs/${requestId}?action=status&app_id=${APP_ID}&app_code=${APP_CODE}`)
.then(res => res.text())
.then(body => {
let data = parser.toJson(body, { object: true });
let status = data["ns2:SearchBatch"].Response.Status;
return status;
});
}
function getData(requestId) {
return fetch(`https://batch.geocoder.api.here.com/6.2/jobs/${requestId}/result?outputcompressed=false&app_id=fcCDTt5BOM4w825JePpQ&app_code=uVpeMH7xQCESU28Te7NHvw`)
.then(res => res.text())
.then(body => {
return body;
});
}
module.exports = generateLatLongs;