forked from vatpac-technology/vatsim-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atc.js
353 lines (329 loc) · 12.5 KB
/
atc.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
import { getLineFeatures, getXMLtoJS, getVatsimAFV, getVatsimData } from './client.js';
import config from 'config';
import * as turf from '@turf/turf';
import { iso2dec } from './iso2dec.js';
import rgb2hex from 'rgb2hex';
// sector:
// properties:
// name: ARA
// callsign: BN-ARA_CTR
// frequency: 133.700
// fullname: Arafura
// volumes: FeatureCollection
// responsibleSectors: [ sectors ]
// standard_position: boolean
// controller: VatsimController
export async function getCoastline(){
var coastline = await getLineFeatures(config.get("data.vatsys.coastlineUrl"));
return turf.featureCollection(coastline);
}
export async function getColours(){
var colours = [];
var coloursXml = await getXMLtoJS(config.get("data.vatsys.coloursUrl"));
coloursXml.Colours.Colour.forEach(function(e){
var [r,g,b] = [e.R._text, e.G._text, e.B._text];
var hex = rgb2hex(`rgb(${r},${g},${b})`);
var colour = {
id: e._attributes.id,
use: e.Use._text,
name: e.Name._text,
hex: hex.hex
}
colours.push(colour);
});
return colours;
}
export async function getATCSectors() {
var sectors = [];
// Get volume GeoJSON
// var volumeBoundaryFeatures = await getLineFeatures(config.get("data.vatsys.volumesUrl"));
// Boundary = volume.properties.Name
var volumesXml = await getXMLtoJS(config.get("data.vatsys.volumesUrl"));
// volume
// properties
// Name
// Get sector details
var sectorsXml = await getXMLtoJS(config.get("data.vatsys.sectorsUrl"));
var sectors = [];
sectorsXml.Sectors.Sector.forEach(function(s){
var volumes = []
if("Volumes" in s){
// Get Volumes
var v = s.Volumes._text.split(',')
v.forEach(function(e){
var volume = getVolume(e, volumesXml);
volumes.push(volume);
})
}
var sector = {
...s._attributes,
standard_position: false,
volumes: volumes,
responsibleSectors: ("ResponsibleSectors" in s ? s.ResponsibleSectors._text.split(',') : [])
};
if (config.get("map.sectors.standard").includes(sector.Callsign)){
sector.standard_position = true;
}
if (config.get("map.sectors.border").includes(sector.Callsign)){
sector.border_position = true;
}
sectors.push(sector);
});
return sectors;
}
function getBoundary(boundaryName, volumesXml){
// Find Boundary in XML
var boundary = volumesXml.Volumes.Boundary.find(e => {
if(e._attributes.Name === boundaryName){
return e;
}
});
// Transform boundary to polygon
var lines = boundary._text.split('/');
lines.forEach(function(line, index) {
var dec = iso2dec(line);
this[index] = [dec.longitude, dec.latitude];
}, lines);
return turf.lineToPolygon(turf.lineString(lines),{mutate: true, properties: boundary._attributes });
};
function getVolume(volumeName, volumesXml){
// Find Volume in XML
var volume = volumesXml.Volumes.Volume.find(e => {
if(e._attributes.Name === volumeName){
return e;
}
});
var newVol = {
...volume._attributes
}
// Get Volume Boundaries
var boundaryFeatures = []
var boundaries = volume.Boundaries._text.split(',');
boundaries.forEach(function(e){
var boundary = getBoundary(e, volumesXml);
boundaryFeatures.push(boundary);
})
newVol.Boundaries = boundaryFeatures;
return newVol;
};
function uniq(a) {
return Array.from(new Set(a));
}
function getSectorByName(sectorName, sectors){
var sector = sectors.find(e => {
if(e.Name === sectorName){
return e;
};
});
return sector;
}
function getSectorByCallsign(sectorName, sectors){
var sector = sectors.find(e => {
if(e.Callsign === sectorName){
return e;
};
});
return sector;
}
function mergeSectors(sector, sectors, json){
let mergedSector;
// FSS don't have sector volumes, only responsible sectors.
if(sector.volumes.length > 0){
mergedSector = mergeBoundaries(sector);
}
if(sectors.length > 0){
var sectorVolumes = [];
// Union all sector volumes into a polygon
sectors.forEach(function(e){
var sector = getSectorByName(e, json);
var poly = mergeBoundaries(sector);
sectorVolumes.push(poly);
});
// Add self and union all sector volumes
sectorVolumes.push(mergeBoundaries(sector));
var mergedPoly = unionArray(sectorVolumes);
mergedSector = mergedPoly;
}
if(mergedSector != undefined){
mergedSector.properties = {...sector};
delete mergedSector.properties.responsibleSectors
delete mergedSector.properties.volumes
return mergedSector;
}
}
function mergeBoundaries(sector) {
var features = sector.volumes.map((volume) => volume.Boundaries.map((boundary) => boundary)).flat();
if (features.length > 1) {
var union = unionArray(features);
union.properties = { ...sector };
delete union.properties.responsibleSectors;
delete union.properties.volumes;
return union;
} else if (features.length == 1) {
features[0].properties = { ...sector };
delete features[0].properties.responsibleSectors;
delete features[0].properties.volumes;
return features[0];
} else {
return false;
}
}
function unionArray(array){
var featureCollection = turf.featureCollection(array);
if (featureCollection.length === 0) {
return null
}
let ret = featureCollection.features[0];
turf.featureEach(featureCollection, function (currentFeature, featureIndex) {
if (featureIndex > 0) {
ret = turf.union(ret, currentFeature);
}
});
// Remove any holes added in union
ret.geometry.coordinates.length = 1;
return ret;
};
function isAdjacentSector(sectorFrequency, primarySector, sectors){
var adjacentSector = false;
try{
// Get sectors with matching frequency
var sectorsWithFreq = sectors.filter(function cb(sector){
if(parseFloat(sector.Frequency) === parseFloat(sectorFrequency)){
return sector;
};
});
sectorsWithFreq.forEach(function(sectorFrequencyMatch){
var sfmFreq = parseFloat(sectorFrequencyMatch.Frequency)
var sfFreq = parseFloat(sectorFrequency)
// Match freqs where not own sector freq
if(sfmFreq === sfFreq && sectorFrequencyMatch.Callsign != primarySector.properties.Callsign){
var sectorsIntersect = booleanIntersects(primarySector, mergeBoundaries(sectorFrequencyMatch));
if(sectorsIntersect){
// Return only sectors sharing a border
adjacentSector = sectorFrequencyMatch;
}
}
});
}finally{
return adjacentSector;
}
};
export async function getOnlineControllers(){
// Get the sector boundary information for the region.
var sectors = await getATCSectors();
// Get the vatsim data.
var data = await getVatsimData();
// Initate array for output.
var onlineControllers = [];
// Loop through each online controller to see if they should be included.
data.controllers.forEach(function(controller) {
// Check if they are FSS, CTR, APP or TWR.
if (!controller.callsign.toUpperCase().includes("FSS") &&
!controller.callsign.toUpperCase().includes("CTR") &&
!controller.callsign.toUpperCase().includes("APP") &&
!controller.callsign.toUpperCase().includes("TWR"))
{
return;
}
// Check if they are controller for the right region.
if (!sectors.some(sector => sector.Callsign == controller.callsign.toUpperCase()))
{
return;
}
// OK to add to the output.
onlineControllers.push(controller);
});
return onlineControllers;
}
export async function getOnlinePositions() {
// Initiate variables to use.
var onlineSectors = [];
var sectorWithSubsectors = false;
// Get the required data.
var sectors = await getATCSectors();
var stations = await getVatsimAFV();
var onlineControllers = await getOnlineControllers();
// If someone of the data missing, abort.
if (!sectors || !stations || !onlineControllers) return;
// Loop through each online controller to see if they should be included.
onlineControllers.forEach(function(onlineController) {
// Get the AFV stations for the controller.
var station = stations.find(station => station.callsign == onlineController.callsign);
// If no stations, move to the next controller.
if (!station) return;
// Get the sector for the position.
var sector = sectors.find(sector => sector.Callsign == onlineController.callsign);
// If no sector, move to the next controller.
if (!sector) return;
// Check std sectors and load sub sectors.
if (sector.standard_position === true && sector.responsibleSectors.length > 0) {
sectorWithSubsectors = mergeSectors(sector, sector.responsibleSectors,sectors);
onlineSectors.push(sectorWithSubsectors);
} else {
onlineSectors.push(mergeBoundaries(sector));
}
// Check if controller is extending.
var activeFrequencies = [];
station.transceivers.forEach(function(transceiver){
// Convert Hertz to Megahurts.
var convertedFrequency = (transceiver.frequency/1000000).toFixed(3);
// Check if already added.
if (activeFrequencies.some(frequency => frequency == convertedFrequency)) return;
// Add it to the list of active frequencies.
activeFrequencies.push(convertedFrequency);
});
// if only 1 frequency, nothing else to do.
if (activeFrequencies.length <= 1) return;
//
activeFrequencies.forEach(function(activeFrequency) {
// Get all the sectors that use that frequency (but only the same position type as the primary position).
var frequencySectors = sectors.filter(sector => sector.Frequency == activeFrequency
&& sector.volumes.length > 0 && sector.Callsign.toUpperCase().endsWith(onlineController.callsign.toUpperCase().slice(-3)));
// If nothing found, continue.
if (!frequencySectors) return;
var extendedSector;
// Loops through each sector.
frequencySectors.forEach(frequencySector => {
// If the primary sector frequency, continue.
if (frequencySector.Callsign == sector.Callsign) return;
if (!extendedSector)
{
extendedSector = frequencySector;
return;
}
if (getDistanceInKm(sector, extendedSector) > getDistanceInKm(sector, frequencySector))
{
extendedSector = frequencySector;
}
});
if (!extendedSector) return;
// Remove the existing entry.
var existingIndex = onlineSectors.indexOf(onlineSectors.Callsign == extendedSector.Callsign);
if (existingIndex > -1) {
onlineSectors.splice(existingIndex, 1);
}
// Insert the updated entry.
onlineSectors.push(mergeSectors(extendedSector, extendedSector.responsibleSectors, sectors));
})
});
return turf.featureCollection(uniq(onlineSectors));
}
function getDistanceInKm(sector1, sector2) {
var lon1 = sector1.volumes[0].Boundaries[0].geometry.coordinates[0][0][0];
var lat1 = sector1.volumes[0].Boundaries[0].geometry.coordinates[0][0][1];
var lon2 = sector2.volumes[0].Boundaries[0].geometry.coordinates[0][0][0];
var lat2 = sector2.volumes[0].Boundaries[0].geometry.coordinates[0][0][1];
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)
}