-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
317 lines (294 loc) · 8.44 KB
/
index.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
const superagent = require('superagent')
const convert = require('xml-js')
const cheerio = require('cheerio')
const baseURI = 'https://tfr.faa.gov/save_pages/'
// Main method / shortcut to fetch
const tfrs = (module.exports = (...args) => {
return tfrs.fetch(...args)
})
/**
* Verify a date string matches the format mm/dd/yyyy
*/
const isDate = text => {
const [ mm, dd, yyyy ] = text.split('/')
if (!mm || mm.length !== 2 || !parseInt(mm)) {
return false
}
if (!dd || dd.length !== 2 || !parseInt(dd)) {
return false
}
if (!yyyy || yyyy.length !== 4 || !parseInt(yyyy)) {
return false
}
return true
}
/**
* List all TFRs available on https://tfr.faa.gov
*/
tfrs.list = async () => {
const response = await superagent
.get('https://tfr.faa.gov/tfr2/list.jsp')
.buffer()
const $ = cheerio.load(response.text)
const listingTable = $('body').find('table table')[2]
const rows = $(listingTable)
.find('tr')
.toArray()
const results = rows.map(row => {
const columns = $(row)
.find('a')
.toArray()
const date = $(columns[0]).text()
if (!isDate(date)) {
return
}
return {
date,
notam: $(columns[1]).text(),
facility: $(columns[2]).text(),
state: $(columns[3]).text(),
type: $(columns[4]).text(),
description: $(columns[5])
.text()
.replace(/\n|\r/g, ''),
links: {
details: $(columns[1])
.attr('href')
.replace('..', 'https://tfr.faa.gov')
.replace(/\n|\r/g, ''),
zoom: `https://tfr.faa.gov${$(columns[6]).attr('href')}`,
xml: $(columns[1])
.attr('href')
.replace('..', 'https://tfr.faa.gov')
.replace('html', 'xml')
.replace(/\n|\r/g, '')
}
}
})
return results.filter(r => !!r)
}
/**
* Extract the NotUid fields
*/
const extractNotUid = notUid => ({
accountableFacility: notUid.txtNameAcctFac._text,
indexYear: notUid.dateIndexYear._text,
sequenceNumber: notUid.noSeqNo._text,
localName: notUid.txtLocalName._text,
guid: notUid.codeGUID._text
})
/**
* Extract the airspace schedule fields
*/
const extractAirspaceSchedule = ScheduleGroup => {
if (
!ScheduleGroup ||
(!ScheduleGroup.isTimeSeparate &&
!ScheduleGroup.dateEffective &&
!ScheduleGroup.dateExpire)
) {
return null
}
return {
isTimeSeparate: ScheduleGroup.isTimeSeparate
? ScheduleGroup.isTimeSeparate._text
: null,
dateEffective: ScheduleGroup.dateEffective
? ScheduleGroup.dateEffective._text
: null,
dateExpire: ScheduleGroup.dateExpire ? ScheduleGroup.dateExpire._text : null
}
}
/**
* Extract the airspace boundary data
*/
const extractBoundary = TFRAreaGroup => {
if (!TFRAreaGroup.abdMergedArea) {
return null
}
return {
airspaceType: TFRAreaGroup.abdMergedArea.AbdUid
? TFRAreaGroup.abdMergedArea.AbdUid.AseUid.codeType._text
: null,
airspaceId: TFRAreaGroup.abdMergedArea.AbdUid
? TFRAreaGroup.abdMergedArea.AbdUid.AseUid.codeId._text
: null,
remark: TFRAreaGroup.abdMergedArea.txtRmk._text,
datum: TFRAreaGroup.abdMergedArea.Avx[0].codeDatum._text,
type: TFRAreaGroup.abdMergedArea.Avx[0].codeType._text,
vertices: TFRAreaGroup.abdMergedArea.Avx.map(avx => {
let lat = avx.geoLat._text
if (lat.includes('S')) {
lat = -1 * parseFloat(lat.replace('S', ''))
} else {
lat = parseFloat(lat.replace('N', ''))
}
let long = avx.geoLong._text
if (long.includes('W')) {
long = -1 * parseFloat(long.replace('W', ''))
} else {
long = parseFloat(long.replace('E', ''))
}
return [ lat, long ]
})
}
}
/**
* Extract a single TFRAreaGroup/Airspace group
*/
const extractAirspaceGroup = TFRAreaGroup => {
return {
airspaceType: TFRAreaGroup.aseTFRArea.AseUid.codeType._text,
airspaceId: TFRAreaGroup.aseTFRArea.AseUid.codeId._text,
name: TFRAreaGroup.aseTFRArea.txtName
? TFRAreaGroup.aseTFRArea.txtName._text
: null,
distVerUpperCode: TFRAreaGroup.aseTFRArea.codeDistVerUpper
? TFRAreaGroup.aseTFRArea.codeDistVerUpper._text
: null,
distVerUpperValue: TFRAreaGroup.aseTFRArea.valDistVerUpper
? TFRAreaGroup.aseTFRArea.valDistVerUpper._text
: null,
distVerUpperUnit: TFRAreaGroup.aseTFRArea.uomDistVerUpper
? TFRAreaGroup.aseTFRArea.uomDistVerUpper._text
: null,
distVerLowerCode: TFRAreaGroup.aseTFRArea.codeDistVerLower
? TFRAreaGroup.aseTFRArea.codeDistVerLower._text
: null,
distVerLowerValue: TFRAreaGroup.aseTFRArea.valDistVerLower
? TFRAreaGroup.aseTFRArea.valDistVerLower._text
: null,
distVerLowerUnit: TFRAreaGroup.aseTFRArea.uomDistVerLower
? TFRAreaGroup.aseTFRArea.uomDistVerLower._text
: null,
airspaceTimesheetWorkHr: TFRAreaGroup.aseTFRArea.Att.codeWorkHr._text,
excludeVerUpper: TFRAreaGroup.aseTFRArea.codeExclVerUpper._text,
excludeVerLower: TFRAreaGroup.aseTFRArea.codeExclVerLower._text,
isScheduledTfrArea: TFRAreaGroup.aseTFRArea.isScheduledTfrArea._text,
schedule: extractAirspaceSchedule(TFRAreaGroup.aseTFRArea.ScheduleGroup),
boundary: extractBoundary(TFRAreaGroup),
incFRD: TFRAreaGroup.codeIncFRD._text,
shpPrt: TFRAreaGroup.codeShpPrt._text,
localTime: TFRAreaGroup.codeLclTime._text,
authATC: TFRAreaGroup.codeAuthATC._text
}
}
/**
* Extract one or more airspace groups
*/
const extractAirspaceGroups = tfrNot => {
if (Array.isArray(tfrNot.TFRAreaGroup)) {
return tfrNot.TFRAreaGroup.map(extractAirspaceGroup)
}
if (typeof tfrNot.TFRAreaGroup === 'object') {
return extractAirspaceGroup(tfrNot.TFRAreaGroup)
}
return null
}
/**
* Extract the TfrNot fields
*/
const extractTfrNot = tfrNot => {
return {
type: tfrNot.codeType._text,
airspace: extractAirspaceGroups(tfrNot)
// aac: tfrNot.TFRAreaGroup.Aac,
// aseShapes: tfrNot.TFRAreaGroup.aseShapes
}
}
/**
* Extract POC/point-of-contact fields
*/
const extractPoc = not => {
if (!not.notTxtNamePOC && !not.txtAddrPOCPhone) {
return null
}
return {
name: not.txtNamePOC ? not.txtNamePOC._text : null,
phone: not.txtAddrPOCPhone ? not.txtAddrPOCPhone._text : null
}
}
/**
* Extract all fields from the transformed XML
*/
const extractNot = not => ({
...extractNotUid(not.NotUid),
dailyOperations: not.codeDailyOper._text,
dateIssued: not.NotUid.dateIssued._text,
dateEffective: not.dateEffective ? not.dateEffective._text : null,
dateExpires: not.dateExpire ? not.dateExpire._text : null,
timezone: not.codeTimeZone._text,
expirationTimezone: not.codeExpirationTimeZone._text,
facility: {
id: not.codeFacility._text,
type: not.codeCoordFacilityType._text,
location: {
city: not.AffLocGroup.txtNameCity
? not.AffLocGroup.txtNameCity._text
: null,
state: not.AffLocGroup.txtNameUSState._text
}
},
poc: extractPoc(not),
...extractTfrNot(not.TfrNot),
description: {
freeFormText: not.codeFreeformText._text,
usns: not.txtDescrUSNS._text,
traditional: not.txtDescrTraditional._text
}
})
/**
* Fetch the XML version of the TFR and transform it into a simpler JSON format
*/
const fetchJson = async id => {
const response = await superagent
.get(`${baseURI}/detail_${id.split('/').join('_')}.xml`)
.buffer()
const result = parse(
response.text.replace(/<txtDescrModern[\s\S]*txtDescrModern>/g, '')
)
return {
id,
created: result['XNOTAM-Update']._attributes.created,
...extractNot(result['XNOTAM-Update'].Group.Add.Not)
}
}
/**
* Default fetch() options
*/
const defaultOptions = {
format: 'json'
}
/**
* Main fetching method used to get TFR details
*/
tfrs.fetch = async (id, options = defaultOptions) => {
try {
if (options.format === 'json') {
return fetchJson(id)
}
if (options.format === 'xml') {
const response = await superagent
.get(`${baseURI}/detail_${id.split('/').join('_')}.xml`)
.buffer()
return response.text
}
if (options.format === 'aixm50') {
const response = await superagent
.get(`${baseURI}/detail_${id.split('/').join('_')}.aixm50`)
.buffer()
return response.text
}
} catch (err) {
console.error(`Error fetching TFR values from ${baseURI}`, err)
}
}
/**
* Convert the XML version of the TFR into JSON for further processing
*/
const parse = xml => {
const js = convert.xml2js(xml, {
compact: true
})
return js
}