-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (51 loc) · 1.53 KB
/
index.js
File metadata and controls
65 lines (51 loc) · 1.53 KB
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
const fs = require('fs');
const parser = require('xml2json');
let xmlData = fs.readFileSync('./stations.xml');
var options = {
object: true,
reversible: false,
coerce: false,
sanitize: true,
trim: true,
arrayNotation: false,
alternateTextNode: false
};
var stationList = parser.toJson( xmlData, options );
var points = buildTree(stationList.StationList.Station);
points = findDistance(points, {lat:52.284, lon:-1.559});
var answer = points.sort(compareDistance);
console.log(JSON.stringify(answer));
console.log(JSON.stringify(answer.slice(0,3)));
function distance(a, b){
return Math.pow(a.lat - b.lat, 2) + Math.pow(a.lon - b.lon, 2);
}
function findDistance(points, target) {
points.forEach(function(station){
station.distance = distance(station, target);
//console.log(station.station.name + " " + station.distance);
});
return points;
}
function buildTree(stationArray) {
points = [];
stationArray.forEach(function(station){
points.push({
lat: parseFloat(station.Latitude),
lon: parseFloat(station.Longitude),
station: {name: station.Name, crs: station.CrsCode}
});
});
return points;
}
function compareDistance(a, b) {
if (a.distance < b.distance)
return -1;
if (a.distance > b.distance)
return 1;
return 0;
}
//stationList.StationList.Station.forEach(function(station){
// console.log(station.Name);
// });
//let data = JSON.stringify(jsonObj);
//fs.writeFileSync('stations.json', data); points