forked from Agilefreaks/test_oop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoffeeApp.js
92 lines (76 loc) · 2.58 KB
/
CoffeeApp.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
const fs = require('fs');
const csv = require('fast-csv');
var obj = csv();
const readline = require('readline');
const rdl= readline.createInterface(process.stdin, process.stdout);
var longi;
var lati;
var file;
var latitude = [];
let longitude = [];
let location = [];
var ct = 0;
rdl.question("Datele: ", function(data){
let elementss = data.split(',');
longi = elementss[0];
lati = elementss[1];
file = elementss[2];
let value;
function interpretation(value) {
location=[...location, `${value[0]}`];
latitude=[...latitude, `${value[1]}`];
longitude=[...longitude, `${value[2]}`];
}
let lat1, lon1, lat2, lon2;
function getDistance(lat1,lon1,lat2,lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d;
}
function deg2rad(deg) {
return deg * (Math.PI/180)
}
var sort_by = function(field, reverse, primer){
var key = primer ?
function(x) {return primer(x[field])} :
function(x) {return x[field]};
reverse = !reverse ? 1 : -1;
return function (a, b) {
return a = key(a), b = key(b), reverse * ((a > b) - (b > a));
}
}
function showLocations() {
let result = [];
let result1= [];
let distance = [];
for(let i=0; i<location.length; i++){
distance= [...distance, getDistance(longi,lati,latitude[i],longitude[i])];
}
for(let i=0; i<location.length; i++){
result.push({location: `${location[i]}`, distance: `${distance[i]}`})
}
resultSorted= result.sort(sort_by('distance', false, parseInt));
for(let i=0;i<3;i++){
console.log(`${resultSorted[i]['location']}, ${resultSorted[i]['distance']}`);
}
}
fs.createReadStream(`${file}`)
.pipe(csv())
.on('data', (row) => {
interpretation(row);
ct++;
if ( ct === 6 ) {
showLocations();
}
})
.on('end', () => {
console.log('CSV file successfully processed!');
});
});