-
Notifications
You must be signed in to change notification settings - Fork 10
/
verifyGeoreference.pl
executable file
·3189 lines (2543 loc) · 99.8 KB
/
verifyGeoreference.pl
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
# Verify the georeferencing of plates
# This is a terrible, but functional, GUI for georeferencing IAP and APD files
# Copyright (C) 2014 Jesse McGraw (jlmcgraw@gmail.com)
#
#--------------------------------------------------------------------------------------------------------------------------------------------
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
#-------------------------------------------------------------------------------------------------------------------------------------------
#To-do
#Make auto-routine run only on added,changed IAPs, APDs
#Use status area of GUI
#Have auto-apd code save/restore GCP hash
#Done
#Start using processFaa2 database
#Show chosen GCP coordinate
#Check the squareness of the "graticule"
use 5.010;
use strict;
use warnings;
use autodie;
#Standard libraries
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
use File::Basename;
use Getopt::Std;
use Carp;
use List::Util qw[min max];
use Storable;
use Math::Trig;
use Math::Trig qw(great_circle_distance deg2rad great_circle_direction rad2deg);
use POSIX;
#Allow use of locally installed libraries in conjunction with Carton
use FindBin '$Bin';
use lib "$FindBin::Bin/local/lib/perl5";
#Non-standard libraries
use DBI;
use Math::Round;
use Params::Validate qw(:all);
use Gtk3 '-init';
use Glib 'TRUE', 'FALSE';
use GD;
use GD::Polyline;
#Some subroutines
use GeoReferencePlatesSubroutines;
use AffineTransform;
use Parse::FixedLength;
#Some other constants
# use constant COLUMN_FIXED => 0;
# use constant COLUMN_NUMBER => 1;
# use constant COLUMN_SEVERITY => 2;
# use constant COLUMN_DESCRIPTION => 3;
use constant COLUMN_NAME => 0;
use constant COLUMN_TYPE => 1;
use constant COLUMN_LONGITUDE => 2;
use constant COLUMN_LATITUDE => 3;
use constant COLUMN_DISTANCE => 4;
#DPI of the output PNG
our $pngDpi = 300;
#A hash to collect statistics
our %statistics = ();
use vars qw/ %opt /;
#Define the valid command line options
my $opt_string = 'va:i:';
my $arg_num = scalar @ARGV;
#Whether to draw various features
our $shouldDrawRunways = 1;
our $shouldDrawNavaids = 1;
our $shouldDrawNavaidsNames = 0;
our $shouldDrawFixes = 0;
our $shouldDrawFixesNames = 0;
our $shouldDrawObstacles = 0;
our $shouldDrawObstaclesHeights = 0;
our $shouldDrawGcps = 1;
our $shouldDrawGraticules = 1;
#We need at least one argument (the cycle number)
if ( $arg_num < 1 ) {
usage();
exit(1);
}
#This will fail if we receive an invalid option
unless ( getopts( "$opt_string", \%opt ) ) {
usage();
exit(1);
}
#Get the cycle from command line options
our $cycle = $ARGV[0];
our ($dtppDirectory) = "./dtpp-$cycle/";
if ( !-e ($dtppDirectory) ) {
say "Target dTpp directory $dtppDirectory doesn't exist";
exit(1);
}
#Default to all airports for the SQL query
our $airportId = "%";
if ( $opt{a} ) {
#If something provided on the command line use it instead
$airportId = $opt{a};
say "Supplied airport ID: $airportId";
}
#Default to all states for the SQL query
our $stateId = "%";
if ( $opt{i} ) {
#If something provided on the command line use it instead
$stateId = $opt{i};
say "Supplied state ID: $stateId";
}
# our $shouldNotOverwriteVrt = $opt{c};
# our $shouldOutputStatistics = $opt{s};
# our $shouldSaveMarkedPdf = $opt{p};
our $debug = $opt{v};
# our $shouldRecreateOutlineFiles = $opt{o};
# our $shouldSaveBadRatio = $opt{b};
# our $shouldUseMultipleObstacles = $opt{m};
#database of metadata for dtpp
my $dtppDbh = DBI->connect( "dbi:SQLite:dbname=./dtpp-$cycle.db",
"", "", { RaiseError => 1 } )
or croak $DBI::errstr;
#CIFP database
my $cifpDbh = DBI->connect( "dbi:SQLite:dbname=./cifp-$cycle.db",
"", "", { RaiseError => 1 } )
or croak $DBI::errstr;
#The 56 day NASR database
our $nasrDbh =
DBI->connect( "dbi:SQLite:dbname=./nasr.sqlite", "", "", { RaiseError => 1 } )
or croak $DBI::errstr;
#-----------------------------------------------
#Open the locations database
our $dbh;
my $sth;
# $dbh = DBI->connect( "dbi:SQLite:dbname=./locationinfo.db",
# "", "", { RaiseError => 1 } )
# or croak $DBI::errstr;
$dtppDbh->do("PRAGMA page_size=4096");
$dtppDbh->do("PRAGMA synchronous=OFF");
#a reference to an array of charts with no longitude or latitude info
my $_platesNotMarkedManually = findPlatesNotMarkedManually();
my $indexIntoPlatesWithNoLonLat = 0;
#a reference to an array of charts marked Changed
my $_platesMarkedChanged = findPlatesMarkedChanged();
my $indexIntoPlatesMarkedChanged = 0;
#a reference to an array of charts marked bad
my $_platesMarkedBad = findPlatesMarkedBad();
my $indexIntoPlatesMarkedBad = 0;
#a reference to an array of all IAP and APD charts
my $_allPlates = allIapAndApdCharts();
my $indexIntoAllPlates = 0;
our (
$currentGcpName, $currentGcpLon, $currentGcpLat,
$currentGcpPdfX, $currentGcpPdfY, $currentGcpPngX,
$currentGcpPngY, $currentGcpPixbufX, $currentGcpPixbufY
);
#Lat/Lon of current airport
our ( $airportLatitudeDec, $airportLongitudeDec );
#Create the UI
my $builder = Gtk3::Builder->new();
$builder->add_from_file('./verifyPlatesUI.glade');
our $pixbuf;
#Connect our handlers
$builder->connect_signals(undef);
my $window = $builder->get_object('applicationwindow1');
$window->set_screen( $window->get_screen() );
$window->signal_connect( destroy => sub { Gtk3->main_quit } );
#Various UI elements we populate programmmatically
our $plate = $builder->get_object('image2');
our $plateSw = $builder->get_object('viewport1');
our $runwayBox = $builder->get_object('scrolledwindow2');
our $navaidBox = $builder->get_object('scrolledwindow4');
our $fixesBox = $builder->get_object('scrolledwindow5');
our $obstaclesBox = $builder->get_object('scrolledwindow6');
our $gcpBox = $builder->get_object('scrolledwindow3');
our $lonLatTextEntry = $builder->get_object('lonLatTextEntry');
# our $statusBar = $builder->get_object('statusbar1');
# our $context_id = $statusBar->get_context_id("Statusbar");
our $textview1 = $builder->get_object('textview1');
our $comboboxtext1 = $builder->get_object('comboboxtext1');
# my $textviewBuffer = $textview1->get_buffer;
# my $iter = $textviewBuffer->get_iter_at_offset (0);
# $textviewBuffer->insert ($iter, "The text widget can display text with all kinds of nifty attributes. It also supports multiple views of the same buffer; this demo is showing the same buffer in two places.\n\n");
$window->show_all();
#Set the initial plate
activateNewPlate( @$_allPlates[0] );
#Start the main GUI loop
Gtk3->main();
# #Close the charts database
# $dtppSth->finish();
$dtppDbh->disconnect();
#Close the locations database
# $sth->finish();
$nasrDbh->disconnect();
exit;
sub findAirportLatitudeAndLongitude {
#Returns the lat/lon of the airport for the plate we're working on
#Validate and set input parameters to this function
my ($FAA_CODE) = validate_pos( @_, { type => SCALAR }, );
my $_airportLatitudeDec;
my $_airportLongitudeDec;
#Query the database for airport
my $sth = $main::nasrDbh->prepare( "
SELECT
location_identifier
,apt_latitude
,apt_longitude
,official_facility_name
FROM
apt_apt
WHERE
location_identifier = '$FAA_CODE'
"
);
$sth->execute();
my $_allSqlQueryResults = $sth->fetchall_arrayref();
foreach my $_row (@$_allSqlQueryResults) {
my ( $airportFaaId, $airportname );
(
$airportFaaId, $_airportLatitudeDec, $_airportLongitudeDec,
$airportname
) = @$_row;
# if ($debug) {
# say "Airport ID: $airportFaaId";
# say "Airport Latitude: $_airportLatitudeDec";
# say "Airport Longitude: $_airportLongitudeDec";
# say "Airport Name: $airportname";
# }
}
if ( $_airportLatitudeDec && $_airportLongitudeDec ) {
say
"FAA_CODE: $FAA_CODE -> Lon:$_airportLongitudeDec Lat:$_airportLatitudeDec";
my $textviewBuffer = $main::textview1->get_buffer;
my $iter = $textviewBuffer->get_iter_at_offset(0);
$textviewBuffer->insert( $iter,
"FAA_CODE: $FAA_CODE -> Lon:$_airportLongitudeDec Lat:$_airportLatitudeDec\n\n"
);
return ( $_airportLatitudeDec, $_airportLongitudeDec );
}
else {
return ( 0, 0 );
}
}
sub getPngSize {
#Calculate the raster size ourselves
my $_pngXSize = ceil( ( $main::pdfXSize / 72 ) * $pngDpi );
my $_pngYSize = ceil( ( $main::pdfYSize / 72 ) * $pngDpi );
#This calls the file utility to determine raster size
# #Find the dimensions of the PNG
# my $fileoutput = qx(file $targetpng );
# my $_retval = $? >> 8;
# die "No output from file. Is it installed? Return code was $_retval"
# if ( $fileoutput eq "" || $_retval != 0 );
# foreach my $line ( split /[\r\n]+/, $fileoutput ) {
# ## Regular expression magic to grab what you want
# if ( $line =~ /([-\.0-9]+)\s+x\s+([-\.0-9]+)/ ) {
# $_pngXSize = $1;
# $_pngYSize = $2;
# }
# }
#Calculate the ratios of the PNG/PDF coordinates
my $_scaleFactorX = $_pngXSize / $main::pdfXSize;
my $_scaleFactorY = $_pngYSize / $main::pdfYSize;
my $_pngXYRatio = $_pngXSize / $_pngYSize;
if ($debug) {
say "PNG size: " . $_pngXSize . "x" . $_pngYSize;
say "Scalefactor PDF->PNG X: " . $_scaleFactorX;
say "Scalefactor PDF->PNG Y: " . $_scaleFactorY;
say "PNG X/Y Ratio: " . $_pngXYRatio;
}
return ( $_pngXSize, $_pngYSize, $_scaleFactorX, $_scaleFactorY,
$_pngXYRatio );
}
sub findObstaclesNearAirport {
#Validate and set input parameters to this function
my ( $airportLongitude, $airportLatitude ) =
validate_pos( @_, { type => SCALAR }, { type => SCALAR }, );
# # my $radius = ".2";
# my $minimumAgl = "0";
#How far away from the airport to look for feature
my $radiusNm = 30;
#Calculate radius for the airport's latitude
my ( $radiusDegreesLatitude, $radiusDegreesLongitude ) =
radiusGivenLatitude( $radiusNm, $airportLatitude );
my $dtppSth = $nasrDbh->prepare(
"SELECT
amsl_ht
,obstacle_latitude
,obstacle_longitude
FROM
OBSTACLE_OBSTACLE
WHERE
agl_ht > 200
AND
(obstacle_latitude BETWEEN $airportLatitude - $radiusDegreesLatitude AND $airportLatitude + $radiusDegreesLatitude )
and
(obstacle_longitude BETWEEN $airportLongitude - $radiusDegreesLongitude AND $airportLongitude + $radiusDegreesLongitude )
ORDER BY
amsl_ht ASC"
);
$dtppSth->execute();
my %unique_obstacles_from_db;
my $all = $dtppSth->fetchall_arrayref();
foreach my $_row (@$all) {
my ( $heightMsl, $lat, $lon ) = @$_row;
# if ( exists $unique_obstacles_from_db{$heightMsl} ) {
#
# #This is a duplicate obstacle
# $lat = $lon = 0;
# }
#
# #Populate variables from our database lookup
#
# $unique_obstacles_from_db{$heightMsl}{"Name"} = $heightMsl;
# $unique_obstacles_from_db{$heightMsl}{"Lat"} = $lat;
# $unique_obstacles_from_db{$heightMsl}{"Lon"} = $lon;
#
# }
# if ( exists $unique_obstacles_from_db{$lon . $lat} ) {
#
# #This is a duplicate obstacle
# $lat = $lon = 0;
# }
#Populate variables from our database lookup. Key starts with $heightMsl so hash will be sortable by height
$unique_obstacles_from_db{ $heightMsl . $lon . $lat }{"Name"} =
$heightMsl;
$unique_obstacles_from_db{ $heightMsl . $lon . $lat }{"Lat"} = $lat;
$unique_obstacles_from_db{ $heightMsl . $lon . $lat }{"Lon"} = $lon;
}
return ( \%unique_obstacles_from_db );
}
sub convertPdfToPng {
#Validate and set input parameters to this function
my ( $targetPdf, $targetpng ) =
validate_pos( @_, { type => SCALAR }, { type => SCALAR }, );
#---------------------------------------------------
#Convert the PDF to a PNG
my $pdfToPpmOutput;
if ( -e $targetpng ) {
return;
}
$pdfToPpmOutput = qx(pdftoppm -png -r $pngDpi $targetPdf > $targetpng);
my $retval = $? >> 8;
die "Error from pdftoppm. Return code is $retval" if $retval != 0;
return;
}
sub findFixesFromCifpForAirport {
#Returns only fixes mentioned in CIFP database for this airport
#Validate and set input parameters to this function
my ( $airportLongitude, $airportLatitude ) =
validate_pos( @_, { type => SCALAR }, { type => SCALAR }, );
my %fixes_from_db;
#Query the database for fixes in IAP
my $sth = $cifpDbh->prepare(
"select distinct
iap.FixIdentifier
,fix.waypointLatitude
,fix.waypointLongitude
from
\"primary_P_F_base_Airport - Approach Procedures\" as IAP
JOIN
\"primary_E_A_base_Enroute - Grid Waypoints\" as FIX
ON
iap.FixIdentifier = fix.waypointIdentifier
WHERE
LandingFacilityIcaoIdentifier like '%$main::FAA_CODE%' ;"
);
$sth->execute();
my $allSqlQueryResults = $sth->fetchall_arrayref();
#Add each of them to our overall FIX hash
foreach my $_row (@$allSqlQueryResults) {
my ( $fixname, $lat, $lon ) = @$_row;
my @A = NESW(
coordinateToDecimalCifpFormat($lon),
coordinateToDecimalCifpFormat($lat)
);
my @B = NESW( $airportLongitude, $airportLatitude );
# Last number is radius of earth in whatever units (eg 6378.137 is kilometers
my $km = great_circle_distance( @A, @B, 6378.137 );
my $nm = great_circle_distance( @A, @B, 3443.89849 );
$fixes_from_db{$fixname}{"Name"} = $fixname;
$fixes_from_db{$fixname}{"Lat"} = coordinateToDecimalCifpFormat($lat);
$fixes_from_db{$fixname}{"Lon"} = coordinateToDecimalCifpFormat($lon);
$fixes_from_db{$fixname}{"Type"} = '$fixtype';
$fixes_from_db{$fixname}{"Distance"} = $nm;
}
#Query the database for terminal fixes for IAP
$sth = $cifpDbh->prepare(
"select distinct
iap.FixIdentifier
,fix.waypointLatitude
,fix.waypointLongitude
from
\"primary_P_F_base_Airport - Approach Procedures\" as IAP
JOIN
\"primary_P_C_base_Airport - Terminal Waypoints\" as FIX
ON
iap.FixIdentifier = fix.waypointIdentifier
WHERE
LandingFacilityIcaoIdentifier like '%$main::FAA_CODE%' ;"
);
$sth->execute();
$allSqlQueryResults = $sth->fetchall_arrayref();
#Add each of them to our overall FIX hash
foreach my $_row (@$allSqlQueryResults) {
my ( $fixname, $lat, $lon ) = @$_row;
my @A = NESW(
coordinateToDecimalCifpFormat($lon),
coordinateToDecimalCifpFormat($lat)
);
my @B = NESW( $airportLongitude, $airportLatitude );
# Last number is radius of earth in whatever units (eg 6378.137 is kilometers
my $km = great_circle_distance( @A, @B, 6378.137 );
my $nm = great_circle_distance( @A, @B, 3443.89849 );
$fixes_from_db{$fixname}{"Name"} = $fixname;
$fixes_from_db{$fixname}{"Lat"} = coordinateToDecimalCifpFormat($lat);
$fixes_from_db{$fixname}{"Lon"} = coordinateToDecimalCifpFormat($lon);
$fixes_from_db{$fixname}{"Type"} = '$fixtype';
$fixes_from_db{$fixname}{"Distance"} = $nm;
}
return ( \%fixes_from_db );
}
sub findFixesNearAirport {
#Validate and set input parameters to this function
my ( $airportLongitude, $airportLatitude ) =
validate_pos( @_, { type => SCALAR }, { type => SCALAR }, );
# my $radius = .5;
my $radiusNm = 50;
#Calculate radius for the airport's latitude
my ( $radiusDegreesLatitude, $radiusDegreesLongitude ) =
radiusGivenLatitude( $radiusNm, $airportLatitude );
#Query the database for fixes within our $radius
my $sth = $nasrDbh->prepare( "
SELECT
national_airspace_system_nas_identifier_for_the_fix_usually_5_c
,latitude
,longitude
,fix_use
FROM
fix_fix1
WHERE
(latitude BETWEEN $airportLatitude - $radiusDegreesLatitude AND $airportLatitude + $radiusDegreesLatitude )
and
(longitude BETWEEN $airportLongitude - $radiusDegreesLongitude AND $airportLongitude + $radiusDegreesLongitude ) "
);
$sth->execute();
my $allSqlQueryResults = $sth->fetchall_arrayref();
my %fixes_from_db;
foreach my $_row (@$allSqlQueryResults) {
my ( $fixname, $lat, $lon, $fixtype ) = @$_row;
my @A = NESW( $lon, $lat );
my @B = NESW( $airportLongitude, $airportLatitude );
# Last number is radius of earth in whatever units (eg 6378.137 is kilometers
my $km = great_circle_distance( @A, @B, 6378.137 );
my $nm = great_circle_distance( @A, @B, 3443.89849 );
$fixes_from_db{$fixname}{"Name"} = $fixname;
$fixes_from_db{$fixname}{"Lat"} = $lat;
$fixes_from_db{$fixname}{"Lon"} = $lon;
$fixes_from_db{$fixname}{"Type"} = $fixtype;
$fixes_from_db{$fixname}{"Distance"} = $nm;
}
return ( \%fixes_from_db );
}
sub radiusGivenLatitude {
#Validate and set input parameters to this function
my ( $radiusNm, $airportLatitude ) =
validate_pos( @_, { type => SCALAR }, { type => SCALAR }, );
#Convert to degrees of Longitude and Latitude for the latitude of our airport
my $radiusDegreesLatitude = $radiusNm / 60;
my $radiusDegreesLongitude =
abs( ( $radiusNm / 60 ) / cos( deg2rad($airportLatitude) ) );
return ( $radiusDegreesLatitude, $radiusDegreesLongitude );
}
# sub NESW {
#
# #Validate and set input parameters to this function
# my ( $airportLongitude, $airportLatitude ) =
# validate_pos( @_, { type => SCALAR }, { type => SCALAR }, );
#
# # Notice the 90 - latitude: phi zero is at the North Pole.
# return deg2rad($airportLongitude), deg2rad( 90 - $airportLatitude );
# }
sub findNavaidsNearAirport {
#Validate and set input parameters to this function
my ( $airportLongitude, $airportLatitude ) =
validate_pos( @_, { type => SCALAR }, { type => SCALAR }, );
#How far away from the airport to look for feature
my $radiusNm = 30;
#Calculate radius for the airport's latitude
my ( $radiusDegreesLatitude, $radiusDegreesLongitude ) =
radiusGivenLatitude( $radiusNm, $airportLatitude );
my $sth = $main::nasrDbh->prepare( "
SELECT
navaid_facility_identifier
,latitude
,longitude
,navaid_facility_type_see_description
FROM
nav_nav1
WHERE
(latitude BETWEEN $airportLatitude - $radiusDegreesLatitude AND $airportLatitude + $radiusDegreesLatitude )
and
(longitude BETWEEN $airportLongitude - $radiusDegreesLongitude AND $airportLongitude + $radiusDegreesLongitude )
"
);
$sth->execute();
my $allSqlQueryResults = $sth->fetchall_arrayref();
my %navaids_from_db;
foreach my $_row (@$allSqlQueryResults) {
my ( $navaidName, $lat, $lon, $navaidType ) = @$_row;
#Ignore VOTs
next if ( $navaidType =~ /^VOT$/i );
my @A = NESW( $lon, $lat );
my @B = NESW( $airportLongitude, $airportLatitude );
# Last number is radius of earth in whatever units (eg 6378.137 is kilometers
my $km = great_circle_distance( @A, @B, 6378.137 );
my $nm = great_circle_distance( @A, @B, 3443.89849 );
$navaids_from_db{ $navaidName . $navaidType }{"Name"} = $navaidName;
$navaids_from_db{ $navaidName . $navaidType }{"Lat"} = $lat;
$navaids_from_db{ $navaidName . $navaidType }{"Lon"} = $lon;
$navaids_from_db{ $navaidName . $navaidType }{"Type"} = $navaidType;
$navaids_from_db{ $navaidName . $navaidType }{"Distance"} = $nm;
}
return ( \%navaids_from_db );
}
sub findRunwaysAtAirport {
#
#Validate and set input parameters to this function
my ($FAA_CODE) =
validate_pos( @_, { type => SCALAR } );
my $sth = $main::nasrDbh->prepare( "
SELECT
rwy. base_end_identifier
,rwy.base_latitude
,rwy.base_longitude
,rwy.base_runway_end_true_alignment
,rwy.reciprocal_end_identifier
,rwy.reciprocal_latitude
,rwy.reciprocal_longitude
,rwy.reciprocal_runway_end_true_alignment
,rwy.runway_identification
FROM
apt_apt AS apt
JOIN
apt_rwy AS rwy
ON
apt.landing_facility_site_number=rwy.landing_facility_site_number
WHERE
apt.location_identifier = '$FAA_CODE'
;
"
);
$sth->execute();
my $all = $sth->fetchall_arrayref();
# #How many rows did this search return
# my $_rows = $sth->rows();
# say "Found $_rows runways for $main::airportId" if $debug;
my %runwaysFromDatabase;
foreach my $_row (@$all) {
my (
$LEName, $LELatitude, $LELongitude, $LEHeading,
$HEName, $HELatitude, $HELongitude, $HEHeading
) = @$_row;
#Skip helipads or waterways
next if ( $LEName =~ /[HW]/i );
next
unless (
$LEName
&& $LELatitude
&& $LELongitude
&& $HEName
&& $HELatitude
&& $HELongitude
);
#$runwaysFromDatabase{$LEName}{} = $trueHeading;
$runwaysFromDatabase{ $LEName . $HEName }{'LELatitude'} = $LELatitude;
$runwaysFromDatabase{ $LEName . $HEName }{'LELongitude'} = $LELongitude;
$runwaysFromDatabase{ $LEName . $HEName }{'LEHeading'} = $LEHeading;
$runwaysFromDatabase{ $LEName . $HEName }{'LEName'} = $LEName;
$runwaysFromDatabase{ $LEName . $HEName }{'HELatitude'} = $HELatitude;
$runwaysFromDatabase{ $LEName . $HEName }{'HELongitude'} = $HELongitude;
$runwaysFromDatabase{ $LEName . $HEName }{'HEHeading'} = $HEHeading;
$runwaysFromDatabase{ $LEName . $HEName }{'HEName'} = $HEName;
}
return ( \%runwaysFromDatabase );
}
sub usage {
say "Usage: $0 <options> <cycle>";
say " <cycle> The cycle number, eg. 1410";
say "-v debug";
say "-a<FAA airport ID> To specify an airport ID";
say "-i<2 Letter state ID> To specify a specific state";
return;
}
sub findPlatesNotMarkedManually {
#Charts not marked MANUAL
my $dtppSth = $dtppDbh->prepare( "
SELECT
D.TPP_VOLUME
,D.FAA_CODE
,D.CHART_SEQ
,D.CHART_CODE
,D.CHART_NAME
,D.USER_ACTION
,D.PDF_NAME
,D.FAANFD18_CODE
,D.MILITARY_USE
,D.COPTER_USE
,D.STATE_ID
,ABS( CAST (DG.targetLonLatRatio AS FLOAT) - CAST(DG.lonLatRatio AS FLOAT)) AS Difference
,DG.upperLeftLon
,DG.upperLeftLat
,DG.lowerRightLon
,DG.lowerRightLat
,DG.xMedian
,DG.yMedian
,DG.xPixelSkew
,DG.yPixelSkew
FROM
dtpp as D
JOIN
dtppGeo as DG
ON
D.PDF_NAME=DG.PDF_NAME
WHERE
(
CHART_CODE = 'IAP'
OR
CHART_CODE = 'APD'
)
AND
FAA_CODE LIKE '$main::airportId'
AND
STATE_ID LIKE '$main::stateId'
AND
DG.PDF_NAME NOT LIKE '%DELETED%'
-- AND
-- DG.STATUS LIKE '%ADDEDCHANGED%'
-- AND
-- DG.STATUS NOT LIKE '%NOGEOREF%'
AND
DG.STATUS NOT LIKE '%MANUAL%'
-- AND
-- D.MILITARY_USE != 'M'
ORDER BY
D.FAA_CODE ASC
;"
);
$dtppSth->execute();
#An array of all APD and IAP charts with no lon/lat
return ( $dtppSth->fetchall_arrayref() );
}
sub allIapAndApdCharts {
#Query the dtpp database for IAP and APD charts
my $dtppSth = $dtppDbh->prepare( "
SELECT
D.TPP_VOLUME
,D.FAA_CODE
,D.CHART_SEQ
,D.CHART_CODE
,D.CHART_NAME
,D.USER_ACTION
,D.PDF_NAME
,D.FAANFD18_CODE
,D.MILITARY_USE
,D.COPTER_USE
,D.STATE_ID
,ABS( CAST (DG.targetLonLatRatio AS FLOAT) - CAST(DG.lonLatRatio AS FLOAT)) AS Difference
,DG.upperLeftLon
,DG.upperLeftLat
,DG.lowerRightLon
,DG.lowerRightLat
,DG.xMedian
,DG.yMedian
,DG.xPixelSkew
,DG.yPixelSkew
FROM
dtpp as D
JOIN
dtppGeo as DG
ON
D.PDF_NAME=DG.PDF_NAME
WHERE
( CHART_CODE = 'IAP' OR CHART_CODE = 'APD' )
AND
FAA_CODE LIKE '$main::airportId'
AND
STATE_ID LIKE '$main::stateId'
AND
DG.PDF_NAME NOT LIKE '%DELETED%'
-- AND
-- DG.STATUS NOT LIKE '%MANUAL%'
-- AND
-- DG.STATUS NOT LIKE '%NOGEOREF%'
-- CAST (DG.upperLeftLon AS FLOAT) = '0'
-- AND
-- CAST (DG.xScaleAvgSize as FLOAT) > 1
-- AND
-- Difference > .08
ORDER BY
D.FAA_CODE ASC
;"
);
$dtppSth->execute();
#An array of all APD and IAP charts
return ( $dtppSth->fetchall_arrayref() );
}
sub findPlatesMarkedBad {
#Charts marked bad
my $dtppSth = $dtppDbh->prepare( "
SELECT
D.TPP_VOLUME
,D.FAA_CODE
,D.CHART_SEQ
,D.CHART_CODE
,D.CHART_NAME
,D.USER_ACTION
,D.PDF_NAME
,D.FAANFD18_CODE
,D.MILITARY_USE
,D.COPTER_USE
,D.STATE_ID
,ABS( CAST (DG.targetLonLatRatio AS FLOAT) - CAST(DG.lonLatRatio AS FLOAT)) AS Difference
,DG.upperLeftLon
,DG.upperLeftLat
,DG.lowerRightLon
,DG.lowerRightLat
,DG.xMedian
,DG.yMedian
,DG.xPixelSkew
,DG.yPixelSkew
FROM
dtpp as D
JOIN
dtppGeo as DG
ON
D.PDF_NAME=DG.PDF_NAME
WHERE
( CHART_CODE = 'IAP'
OR
CHART_CODE = 'APD' )
AND
FAA_CODE LIKE '$main::airportId'
AND
STATE_ID LIKE '$main::stateId'
AND
DG.PDF_NAME NOT LIKE '%DELETED%'
AND
DG.STATUS LIKE '%BAD%'
-- AND
-- DG.STATUS NOT LIKE '%NOGEOREF%'
-- BUG TODO: Civilian charts only for now
AND
D.MILITARY_USE != 'M'
ORDER BY
D.FAA_CODE ASC
;"
);
$dtppSth->execute();
#Return the arraryRef
return ( $dtppSth->fetchall_arrayref() );
}
sub findPlatesMarkedChanged {
#Charts marked ADDEDCHANGED
my $dtppSth = $dtppDbh->prepare( "
SELECT
D.TPP_VOLUME
,D.FAA_CODE
,D.CHART_SEQ
,D.CHART_CODE
,D.CHART_NAME
,D.USER_ACTION
,D.PDF_NAME
,D.FAANFD18_CODE
,D.MILITARY_USE
,D.COPTER_USE
,D.STATE_ID
,ABS( CAST (DG.targetLonLatRatio AS FLOAT) - CAST(DG.lonLatRatio AS FLOAT)) AS Difference
,DG.upperLeftLon
,DG.upperLeftLat
,DG.lowerRightLon
,DG.lowerRightLat
,DG.xMedian
,DG.yMedian
,DG.xPixelSkew
,DG.yPixelSkew
FROM
dtpp as D
JOIN
dtppGeo as DG
ON
D.PDF_NAME=DG.PDF_NAME
WHERE
(
D.USER_ACTION = 'C'
OR
D.USER_ACTION = 'A'
)
AND
(
CHART_CODE = 'IAP'
OR
CHART_CODE = 'APD'
)
AND
FAA_CODE LIKE '$main::airportId'
AND
STATE_ID LIKE '$main::stateId'
AND
DG.PDF_NAME NOT LIKE '%DELETED%'
AND
DG.STATUS LIKE '%ADDEDCHANGED%'
-- AND
-- DG.STATUS NOT LIKE '%NOGEOREF%'
-- AND
-- DG.STATUS NOT LIKE '%MANUALGOOD%'
ORDER BY
D.FAA_CODE ASC
;"
);
$dtppSth->execute();
#Return the arraryRef
return ( $dtppSth->fetchall_arrayref() );
}