-
Notifications
You must be signed in to change notification settings - Fork 5
/
parser.js
379 lines (377 loc) · 12 KB
/
parser.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Import dependencies
var xml2js = require('xml2js'),
fs = require('fs'),
colors = require('colors/safe'),
request = require('request'),
Q = require('q'),
GeoJSON = require('geojson');
// Declare global vars
var airports = [],
runways = [],
airspaces = [],
navaids = [],
hotspots = [],
pathInput = "./input/",
pathOutput = "./output/",
points = [],
polygons = [],
fileTypes = ['airports', 'airspace', 'hotspot', 'navaid'],
geoBlocks = ['airports', 'airspaces', 'hotspots', 'navaids', 'runways'],
fileStatus = ['listed', 'loaded', 'analysed', 'checked', 'finished'],
allFiles = 0,
datas = [];
var readDir = Q.denodeify(fs.readdir);
var readFile = Q.denodeify(fs.readFile);
init()
.then(readDir)
.then(createFileList)
.then(getDataFromFiles);/*
.then(fnTest)
.catch(function(error){
console.error(colors.red("ERRROR :\n"+error));
})
.done(function(value){
console.log(colors.cyan("DONE :"));
console.log(colors.cyan(JSON.stringify(value)));
});*/
function init(){
// Shows the main menu used by this little script.
console.log(colors.green("################################################################################"));
console.log(colors.green("# #"));
console.log(colors.green("# OpenAIP to GeoJSON converter #"));
console.log(colors.green("# #"));
console.log(colors.green("################################################################################"));
console.log("\n");
console.log(colors.green("This parser will analyse all datas in folder [input], and will analyse if there \nis any duplicates entries.\n"));
console.log(colors.green("Be sure all your files in the \"input\" folder.\n"));
console.log(colors.green(">>> reading [input] folder"));
var deferred = Q.defer();
deferred.resolve(pathInput);
return deferred.promise;
}
function fnTest(response){
var deferred = Q.defer();
console.log(colors.gray("TEST :\n"));
console.log(colors.gray(JSON.stringify(response)));
deferred.resolve(response);
return deferred.promise;
}
function createFileList(list){
console.log(colors.green(">>> Create the list of files to analyse"));
var fileList = [];
for(var i = 0; i < list.length; i++){
var item = {
type: list[i].split("_")[1],
ext : list[i].slice(-3),
url : pathInput+list[i]
};
switch(item.ext){
case 'aip' : fileList.push(item); break;
}
}
allFiles = fileList.length;
console.log(colors.green(">>> "+fileList.length +" files will be analysed"));
return fileList;
}
function getDataFromFiles(list){
var deferred = Q.defer();
console.log(colors.green(">>> Getting datas from files"));
for(var i = 0; i < list.length; i++){
console.log(colors.blue(">>> Parsing datas from xml for " + list[i].url));
readFile(list[i].url, 'utf8')
.then(parseXMLdatas)
.then(analyseParsedData)
.then(secondPartOfTheJob)
.then(checkDuplicatesInArray)
.then(produceGeoJSON);
}
deferred.resolve(datas);
return deferred.promise;
}
function parseXMLdatas(data){
var deferred = Q.defer();
xml2js.parseString(data, function(err, result){
if(err) {
deferred.reject(new Error(err));
} else {
deferred.resolve(result);
}
});
return deferred.promise;
}
function analyseParsedData(data){
var deferred = Q.defer();
var count = 0;
if( data.OPENAIP.HOTSPOTS !== undefined){
count = data.OPENAIP.HOTSPOTS[0].HOTSPOT.length;
for(var i = 0; i < count; i++){
createHotspot(data.OPENAIP.HOTSPOTS[0].HOTSPOT[i])
.done(function(result){
hotspots.push(result);
});
}
allFiles--;
} else if( data.OPENAIP.AIRSPACES !== undefined){
count = data.OPENAIP.AIRSPACES[0].ASP.length;
for(var i = 0; i < count ; i++){
createAirspace(data.OPENAIP.AIRSPACES[0].ASP[i])
.done(function(result){
airspaces.push(result);
});
}
allFiles--;
} else if( data.OPENAIP.NAVAIDS !== undefined){
count = data.OPENAIP.NAVAIDS[0].NAVAID.length;
for(var i = 0; i < count ; i++){
createNavaid(data.OPENAIP.NAVAIDS[0].NAVAID[i])
.done(function(result){
navaids.push(result);
});
}
allFiles--;
} else if( data.OPENAIP.WAYPOINTS !== undefined){
count = data.OPENAIP.WAYPOINTS[0].AIRPORT.length;
for(var i = 0; i < count ; i++){
createAirport(data.OPENAIP.WAYPOINTS[0].AIRPORT[i])
.done(function(result){
airports.push(result);
});
}
allFiles--;
} else {
throw new Error("There are other data types than airports, airspaces, navaids or hotspots");
}
console.log(colors.yellow(">>> Found " + count +" items to work on."));
deferred.resolve(datas);
return deferred.promise;
}
function createHotspot(data){
var deferred = Q.defer();
var hotspot = {
guid: "",
aeronautical : "hotspot",
type: data.$.TYPE,
country: data.COUNTRY[0],
name: data.NAME[0],
latitude : parseFloat(data.GEOLOCATION[0].LAT[0]),
longitude : parseFloat(data.GEOLOCATION[0].LON[0]),
elev : parseFloat(data.GEOLOCATION[0].ELEV[0]._),
reliability: parseFloat(data.RELIABILITY[0]),
occurrence: data.OCCURRENCE[0],
conditions:[]
};
// Working on conditions.
if(data.CONDITIONS != undefined){
for(var i = 0; i < data.CONDITIONS.length; i++){
var tmpCond = data.CONDITIONS[i];
var cond = {
type: tmpCond.$.TYPE,
timeofday: [],
wind:[]
};
// Work on times
if(tmpCond.TIMEOFDAY != undefined){
for(var j=0; j < tmpCond.TIMEOFDAY[0].TIME.length; j++){
cond.timeofday.push(tmpCond.TIMEOFDAY[0].TIME[j]);
}
}
// Work on winds
if(tmpCond.WIND != undefined){
for(var j = 0; j < tmpCond.WIND[0].DIRECTION.length; j++){
cond.wind.push(tmpCond.WIND[0].DIRECTION[j]);
}
}
hotspot.conditions.push(cond);
}
}
deferred.resolve(hotspot);
return deferred.promise;
}
function createAirspace(data){
var deferred = Q.defer();
var airspace = {
guid: "",
aeronautical : "airspace",
category: data.$.CATEGORY,
version: data.VERSION[0],
id: data.ID[0],
country: data.COUNTRY[0],
name: data.NAME[0],
alt_limits:{
top:{
ref:data.ALTLIMIT_TOP[0].$.REFERENCE,
value: data.ALTLIMIT_TOP[0].ALT[0].$.UNIT +" "+ data.ALTLIMIT_TOP[0].ALT[0]._
},
bottom:{
ref:data.ALTLIMIT_BOTTOM[0].$.REFERENCE,
value:data.ALTLIMIT_BOTTOM[0].ALT[0].$.UNIT +" "+ data.ALTLIMIT_BOTTOM[0].ALT[0]._
}
},
geometry:[]
};
// Generate vertexes for airspaces geometry
// A GeoJSON polygon is polygon : [ [ [Coordinates 1] ] ]
var strGeomArr = data.GEOMETRY[0].POLYGON[0].split(', ');
var vertexes = [];
for(var g = 0; g < strGeomArr.length; g++){
var tmpVertex = strGeomArr[g].split(" ");
var vertex = [ parseFloat(tmpVertex[0]), parseFloat(tmpVertex[1])];
vertexes.push(vertex);
}
airspace.geometry = [ vertexes ];
deferred.resolve(airspace);
return deferred.promise;
}
function createNavaid(data){
var deferred = Q.defer();
var navaid = {
guid: "",
aeronautical : "navaid",
type: data.$.TYPE,
country: data.COUNTRY[0],
id: data.ID[0],
latitude : parseFloat(data.GEOLOCATION[0].LAT[0]),
longitude : parseFloat(data.GEOLOCATION[0].LON[0]),
elev : parseFloat(data.GEOLOCATION[0].ELEV[0]._),
radios:[],
params:{
range: (data.PARAMS[0].RANGE != undefined) ? parseFloat(data.PARAMS[0].RANGE[0]._) : null,
declination : parseFloat(data.PARAMS[0].DECLINATION[0]),
alignedtotruenorth : !(data.PARAMS[0].ALIGNEDTOTRUENORTH[0] === "FALSE")
}
};
// Work on radio
if(data.RADIO != undefined){
for(var j =0; j < data.RADIO.length; j++){
var radio = {
frequency: parseFloat(data.RADIO[j].FREQUENCY[0]),
channel: (data.RADIO[j].CHANNEL != undefined) ? data.RADIO[j].CHANNEL[0] : ""
};
navaid.radios.push(radio);
}
}
deferred.resolve(navaid);
return deferred.promise;
}
function createAirport(data){
var deferred = Q.defer();
var airport = {
guid: "",
aeronautical : "airport",
name: data.NAME[0],
type: data.$.TYPE,
country: data.COUNTRY[0],
icao : (data.ICAO != undefined ) ? data.ICAO[0] : "",
radios: [],
latitude : parseFloat(data.GEOLOCATION[0].LAT[0]),
longitude : parseFloat(data.GEOLOCATION[0].LON[0]),
elev : parseFloat(data.GEOLOCATION[0].ELEV[0]._)
};
// Work on radios
if(data.RADIO != undefined){
for(var r = 0; r < data.RADIO.length; r++){
var tempRadio = data.RADIO[r];
var radio = {
category: tempRadio.$.CATEGORY,
frequency: parseFloat(tempRadio.FREQUENCY[0]),
type: tempRadio.TYPE[0],
spec: (tempRadio.TYPESPEC != undefined ) ? tempRadio.TYPESPEC[0] : "",
description: (tempRadio.DESCRIPTION != undefined) ? tempRadio.DESCRIPTION[0] : ""
};
airport.radios.push(radio);
}
}
// Work on runways
if(data.RWY != undefined){
for(var r = 0; r < data.RWY.length; r++){
createRunway(data.RWY[r], airport)
.done(function(result){
runways.push(result);
});
}
}
deferred.resolve(airport);
return deferred.promise;
}
function createRunway(data, airport){
var deferred = Q.defer();
var runway = {
guid: "",
aeronautical : "navaid",
operations : data.$.OPERATIONS,
airport : airport.name,
name : data.NAME[0],
sfc : data.SFC[0],
latitude : airport.latitude,
longitude : airport.longitude,
elev : airport.elev,
length : parseFloat(data.LENGTH[0]._),
width : parseFloat(data.WIDTH[0]._),
directions: []
};
if( data.DIRECTION != undefined ){
for(var z = 0; z < data.DIRECTION.length; z++){
runway.directions.push(data.DIRECTION[z].$.TC);
}
}
deferred.resolve(runway);
return deferred.promise;
}
function secondPartOfTheJob(){
var deferred = Q.defer();
if(allFiles == 0){
var datas = {
airports : airports,
airspaces : airspaces,
hotspots : hotspots,
navaids : navaids,
runways : runways
};
deferred.resolve(datas);
}
return deferred.promise;
}
function checkDuplicatesInArray(datas){
var deferred = Q.defer();
console.log(colors.magenta(">>> Safety : Checking for duplicates entries."))
var duplicatesCounter = 0;
for(block in datas){
// Creating a temporary array
var temporaryArray = [];
console.log(colors.magenta(">>> Safety : Checking " + block + " ("+ block.length +" items)."));
for(item in datas[block]){
// In this block, check for duplicates.
// So, for each entry, loop on all next ones, and check if they are identical.
if(temporaryArray.length == 0){
// First item, we add it directly to our temporaryArray.
temporaryArray.push(item);
} else {
// Other items, need to check if it's already copied.
var existsInArray = false;
for(var j = 0; j < temporaryArray.length; j++){
if(temporaryArray[j] == item){
existsInArray = true;
duplicatesCounter++;
break;
}
}
if(existsInArray === false){
temporaryArray.push(item);
}
}
}
// Changing the actual array.
datas[block] = temporaryArray;
}
console.log(colors.magenta(">>> Safety : " + duplicatesCounter + " duplicates found and removed"))
deferred.resolve(datas);
return deferred.promise;
}
function produceGeoJSON(datas){
console.log(colors.white(">>> Generating the GeoJSON Files"));
for(block in datas){
console.log(colors.white(">>> Generating the GeoJSON for " + block + " ("+ block.length +" items)."));
}
}
function createFile(datas, filename){
}