-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathairspace_check.pl.new
executable file
·308 lines (259 loc) · 8.63 KB
/
airspace_check.pl.new
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
#!/usr/bin/perl
#
# Check to see if a track violates airspace
#
# Needs to be quick/pruned somehow?
# Only check if 'nearby' / every 30 seconds?
#
# Geoff Wong 2008
#
require DBD::mysql;
use Math::Trig;
use Data::Dumper;
use POSIX qw(ceil floor);
use TrackLib qw(:all);
my $dbh;
my $max_height = 3048; # 10000' limit in oz
#
# Read an abbreviated tracklog from the database
#
sub read_short_track
{
my ($traPk) = @_;
my %track;
my @coords;
my %awards;
my $ref;
my $c1;
$sth = $dbh->prepare("select *, floor(trlTime/60) as trlMinTime, max(trlAltitude) as maxAlt from tblTrackLog where traPk=$traPk group by floor(trlTime/60) order by trlTime");
$sth->execute();
while ($ref = $sth->fetchrow_hashref())
{
#print "Found a row: time = $ref->{'trlTime'}\n";
my %coord;
$coord{'dlat'} = $ref->{'trlLatDecimal'};
$coord{'dlong'} = $ref->{'trlLongDecimal'};
# radians
$coord{'lat'} = $ref->{'trlLatDecimal'} * $pi / 180;
$coord{'long'} = $ref->{'trlLongDecimal'} * $pi / 180;
# alt / time / pressure?
$coord{'alt'} = $ref->{'maxAlt'};
$coord{'time'} = $ref->{'trlTime'};
# and cartesian ..
$c1 = polar2cartesian(\%coord);
$coord{'cart'} = $c1;
push @coords, \%coord;
}
$track{'coords'} = \@coords;
return \%track;
}
sub airspace_check
{
my ($traPk, $airspaces) = @_;
my $flight;
my $violation;
my $dst;
$violation = 0;
$flight = read_short_track($traPk);
$full = $flight->{'coords'};
foreach $space (@$airspaces)
{
print "Checking airspace ", $space->{'name'}, " with base=", $space->{'base'}, "\n";
#print Dumper($space);
for my $coord ( @$full )
{
# add dist check on centre of space too ...distance($coord, $space->{'centre'}) < $space->{'radius'}
# fix: create an angle to current point .. compare with radius
# and if between the angles of the "edge" points of (outer) radius
#print "check ", $space->{'name'}, "\n";
# stupid 10000ft check for oz pilots
if ($coord->{'alt'} > $max_height)
{
if ($coord->{'alt'} - $space->{'base'} > $violation)
{
$violation = $coord->{'alt'} - $max_height;
}
}
if ($coord->{'alt'} > $space->{'base'})
{
# potential violation ..
if ($space->{'shape'} eq 'circle')
{
$dst = distance($coord, $space->{'centre'});
if ($dst < $space->{'radius'})
{
print "PV: alt=", $coord->{'alt'}, " dlat=", $coord->{'dlat'}, " dlong=", $coord->{'dlong'}, "\n";
$violation = $coord->{'alt'} - $space->{'base'};
}
}
else if (in_polygon($coord, $space->{'points'}))
{
if ($coord->{'alt'} - $space->{'base'} > $violation)
{
print "PV: alt=", $coord->{'alt'}, " dlat=", $coord->{'dlat'}, " dlong=", $coord->{'dlong'}, "\n";
$violation = $coord->{'alt'} - $space->{'base'};
}
}
}
}
}
return $violation;
}
#int nvert, float *vertx, float *verty, float testx, float testy)
# for (i = 0, j = nvert-1; i < nvert; j = i++)
# {
# if ( ((verty[i]>$wpt->{'y'}) != (verty[j]>$wpt->{'y'})) &&
# (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
# c = !c;
#
# for (i = 0, j = npol-1; i < npol; j = i++) {
# if ((((yp[i] <= y) && (y < yp[j])) ||
# ((yp[j] <= y) && (y < yp[i]))) &&
# (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))
# c = !c;
# }
sub in_polygon
{
my ($wpt, $poly) = @_;
my ($i, $j);
my $c = 0;
my $nvert;
$nvert = scalar @$poly;
# print "wpt=", Dumper($wpt);
# print "poly=", Dumper($poly);
for ($i = 0, $j = $nvert-1; $i < $nvert; $j = $i++)
{
# if ( (($poly->[$i]->{'cart'}->{'y'} > $wpt->{'cart'}->{'y'})
# != ($poly->[$j]->{'cart'}->{'y'} > $wpt->{'cart'}->{'y'})) &&
# ($wpt->{'cart'}->{'x'} < ($poly->[$j]->{'cart'}->{'x'}-$poly->[$i]->{'cart'}->{'x'}) * ($wpt->{'cart'}->{'y'} - $poly->[$i]->{'cart'}->{'y'}) /
# ($poly->[$j]->{'cart'}->{'y'} - $poly->[$i]->{'cart'}->{'y'}) + $poly->[$i]->{'cart'}->{'x'}) )
if (( (($poly->[$i]->{'cart'}->{'y'} <= $wpt->{'cart'}->{'y'}) &&
($wpt->{'cart'}->{'y'} < $poly->[$j]->{'cart'}->{'y'})) ||
(($poly->[$j]->{'cart'}->{'y'} <= $wpt->{'cart'}->{'y'}) &&
($wpt->{'cart'}->{'y'} < $poly->[$i]->{'cart'}->{'y'}))) &&
($wpt->{'cart'}->{'x'} < ($poly->[$j]->{'cart'}->{'x'} -
$poly->[$i]->{'cart'}->{'x'}) * ($wpt->{'cart'}->{'y'} -
$poly->[$i]->{'cart'}->{'y'}) /
($poly->[$j]->{'cart'}->{'y'} - $poly->[$i]->{'cart'}->{'y'}) + $poly->[$i]->{'cart'}->{'x'}) )
{
#print "cross\n";
$c++;
}
}
return ($c % 2);
}
#
# Don't bother with great circle for checking
# Calculate angle described by a line made by two points
# $p1 = origin
sub cart_angle
{
my ($p1, $p2);
my ($x,$y);
$x = $p2->{'cart'}->{'x'} - $p1->{'cart'}->{'x'};
$y = $p2->{'cart'}->{'y'} - $p1->{'cart'}->{'y'};
return atan2($x,$y);
}
#
# Returns a list of airspace that is centred 'near' the track
# within traDistance+XXX)
#
sub find_nearby_airspace
{
my ($regPk, $dist) = @_;
my @allair;
my ($sth,$ref);
my %nearair;
my $airPk;
my @points;
my $centre;
# Get regPk centre ...
#$sth = $dbh->prepare("select * from tblAirspace A, tblAirspaceWaypoint AW where A.airCentreWP = AW.awpPk and AW.awpLatDecimal between (X,Y) and AW.awpLonDecimal between (X,Y)");
# Get it all for now ...
$sth = $dbh->prepare("select * from tblAirspace A, tblAirspaceWaypoint AW where A.airPk=AW.airPk order by A.airPk,AW.airOrder");
$sth->execute();
$airPk = 0;
while ($ref = $sth->fetchrow_hashref())
{
my %coord;
if ($ref->{'airPk'} != $airPk)
{
if ($airPk == 0)
{
$airPk = $ref->{'airPk'};
}
else
{
my @pts;
my %na;
@pts = @points;
$nearair{'points'} = \@pts;
%na = %nearair;
push @allair, \%na;
@points = ();
%nearair = ();
$airPk = $ref->{'airPk'};
}
}
$nearair{'name'} = $ref->{'airName'};
$nearair{'class'} = $ref->{'airClass'};
$nearair{'base'} = $ref->{'airBase'};
$nearair{'tops'} = $ref->{'airTops'};
$nearair{'shape'} = $ref->{'airShape'};
$nearair{'radius'} = $ref->{'airRadius'};
$coord{'dlat'} = $ref->{'awpLatDecimal'};
$coord{'dlong'} = $ref->{'awpLongDecimal'};
$coord{'lat'} = $ref->{'awpLatDecimal'} * $pi / 180;
$coord{'long'} = $ref->{'awpLongDecimal'} * $pi / 180;
$coord{'cart'} = polar2cartesian(\%coord);
#print "coord=",Dumper($coord), "\n";
push @points, \%coord;
}
$nearair{'points'} = \@points;
# Assuming only one point for circular regions
$nearair{'centre'} = $points[0];
push @allair, \%nearair;
return \@allair;
}
sub get_all_tracks
{
my ($tasPk) = @_;
my ($sth,$ref);
my %ret;
$sth = $dbh->prepare("select CTT.traPk, P.* from tblComTaskTrack CTT, tblTrack T, tblPilot P where CTT.traPk=T.traPk and P.pilPk=T.pilPk and CTT.tasPk=$tasPk");
$sth->execute();
while ($ref = $sth->fetchrow_hashref())
{
$ret{$ref->{'traPk'}} = $ref;
}
return \%ret;
}
#
# Verify an entire task ...
#
my $tasPk = 0 + $ARGV[0];
my $tracks;
my $airspace;
my $dist;
my $name;
$dbh = db_connect();
$tracks = get_all_tracks($tasPk);
#$tracks = [ 821, 823, 865 ];
$airspace = find_nearby_airspace($regPk, 100000.0);
#print Dumper($airspace);
# Go through all the tracks ..
# might be more useful to print the names :-)
for my $track (keys %$tracks)
{
print "Airspace check for track: $track\n";
$dist = 0;
$name = $tracks->{$track}->{'pilFirstName'} . " " . $tracks->{$track}->{'pilLastName'};
if (($dist = airspace_check($track,$airspace)) > 0)
{
print " Maximum violation of $dist metres ($name).\n";
}
else
{
print " No violation ($name).\n";
}
}