-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtopsky.php
More file actions
161 lines (135 loc) · 4.86 KB
/
topsky.php
File metadata and controls
161 lines (135 loc) · 4.86 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
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
<?php
function decimalToDms(float $value, string $type): string
{
$hemisphere = match ($type) {
'lat' => $value >= 0 ? 'N' : 'S',
'lon' => $value >= 0 ? 'E' : 'W',
default => throw new InvalidArgumentException("Invalid type")
};
$value = abs($value);
// 1 degree = 3600 seconds
$totalSeconds = round($value * 3600, 3);
// Decompose
$deg = floor($totalSeconds / 3600);
$remaining = $totalSeconds - ($deg * 3600);
$min = floor($remaining / 60);
$sec = $remaining - ($min * 60);
return sprintf(
'%s%03d.%02d.%06.3f',
$hemisphere,
$deg,
$min,
$sec
);
}
function geojsonToTopSkyArea($geojson, $areaNameprefix, $isModeS = true): array
{
$data = json_decode(file_get_contents($geojson), true);
$multiPolygon = $data['features'];
$topsky = [];
if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException('JSON decode error: ' . json_last_error_msg());
} else {
foreach ($multiPolygon as $featureIndex => $feature) {
foreach ($feature['geometry']['coordinates'] as $polygonIndex => $polygon) {
foreach (array_reverse($polygon) as $ringIndex => $ring) {
if (($ringIndex==count($polygon)-1)==$isModeS) {
$topsky[] = "AREA:".$areaNameprefix.($polygonIndex>0 ? '_'.$polygonIndex+1 : '')."\n";
$topsky[] = "MODE_S\n";
} else {
$topsky[] = "AREA:".$areaNameprefix."_EXCLUSION".($polygonIndex>0 ? '_'.$polygonIndex+1 : '').('_'.$ringIndex+1)."\n";
}
foreach ($ring as [$lon, $lat]) {
$topsky[] = decimalToDms($lat, 'lat')." ".decimalToDms($lon, 'lon')."\n";
}
$topsky[] = "\n";
}
}
}
}
return $topsky;
}
/**
* Build filtered prefixes and include unmatched candidates.
*
* @param string[] $candidates Codes used to generate prefix candidates
* @param string[] $airports Codes used to invalidate prefixes
* @return string[]
*/
function buildFilteredPrefixes(array $candidates, array $airports): array
{
$prefixes = fn(array $codes, int $len) =>
array_values(array_unique(array_map(
fn($c) => substr($c, 0, $len),
$codes
)));
// Prefix candidates
$p2 = $prefixes($candidates, 2);
$p3 = $prefixes($candidates, 3);
// Used prefixes from airports
$used2 = [];
$used3 = [];
foreach ($airports as $a) {
$used2[substr($a, 0, 2)] = true;
$used3[substr($a, 0, 3)] = true;
}
// Filtered 2-letter prefixes
$f2 = array_values(array_diff($p2, array_keys($used2)));
// Filtered 3-letter prefixes
$f3 = array_values(array_filter(
$p3,
fn($p) =>
!isset($used3[$p]) &&
!array_filter($f2, fn($p2) => str_starts_with($p, $p2))
));
// Add candidates that start with neither f2 nor f3
$unmatchedCandidates = array_values(array_filter(
$candidates,
function (string $c) use ($f2, $f3): bool {
foreach ($f2 as $p2) {
if (str_starts_with($c, $p2)) {
return false;
}
}
foreach ($f3 as $p3) {
if (str_starts_with($c, $p3)) {
return false;
}
}
return true;
}
));
// Combine and sort
$result = array_merge($f2, $f3, $unmatchedCandidates);
sort($result, SORT_STRING);
return $result;
}
// TopSky plugin Mode S area definitions
$topsky = array_merge(geojsonToTopSkyArea(__DIR__.'/data/geojson/Boundaries_dissolved.geojson', 'SIERRA'), geojsonToTopSkyArea(__DIR__.'/data/geojson/Encompassing.geojson', 'SIERRA_ENCOMPASSING', false));
// TopSky plugin Mode S airport inclusion and exclusion lists
foreach (file(__DIR__.'/data/VATSpy.dat', FILE_IGNORE_NEW_LINES) as $vatspy) {
$parts = explode('|', $vatspy);
if (count($parts)==7 && substr($vatspy, 0, 1)!=';') {
$vatspy_apt[] = $parts;
}
}
$filter = file(__DIR__.'/data/config.txt', FILE_IGNORE_NEW_LINES);
foreach ($vatspy_apt as $apt) {
if ($apt[6]) {
// pseudo airport, exclude
} else if (preg_match('/^'.$filter[1].'/', $apt[0])) {
$match['ireg'][] = $apt[0];
} else if (preg_match('/^'.$filter[0].'/', $apt[0])) {
$match['reg'][] = $apt[0];
} else {
$match['nreg'][] = $apt[0];
}
}
if (!empty($apt_regm = buildFilteredPrefixes($match['reg'], $match['nreg']))) {
$topsky[] = "MODE_S_AIRPORTS:".implode(',', $apt_regm)."\n\n";
}
if (!empty($apt_iregm = buildFilteredPrefixes($match['ireg'], $match['reg']))) {
$topsky[] = "MODE_S_AIRPORTS_EXCLUDE:".implode(',', $apt_iregm)."\n\n";
}
file_put_contents(__DIR__.'/data/topsky/modes.txt', $topsky);
?>