-
Notifications
You must be signed in to change notification settings - Fork 10
/
rasterize_and_tile_plates_by_airport.pl
executable file
·512 lines (405 loc) · 15.4 KB
/
rasterize_and_tile_plates_by_airport.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
#!/usr/bin/perl
# Given a database of charts and georef data, create a directory of IAP and APD .PNGs and
# associated world files
#
# Copyright (C) 2013 Jesse McGraw ([email protected])
#
#-------------------------------------------------------------------------------
# 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/].
#-------------------------------------------------------------------------------
#TODO
# Finding 12826 vs 12837 airports when joining on CIFP id
# Make sure upperLeftLon/upperLeftLat are used if no CIFP airport coordinate
use 5.010;
use strict;
use warnings;
use autodie;
#Standard libraries
use Carp;
use File::Copy;
use File::Slurp qw(read_file write_file read_dir);
use File::Path qw(make_path remove_tree);
use Getopt::Std;
use vars qw/ %opt /;
#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 Params::Validate qw(:all);
use Parse::FixedLength;
#Call the main routine and exit with its return code
exit main(@ARGV);
#-------------------------------------------------------------------------------
sub main {
#Define the valid command line options
my $opt_string = 'tmoa:s:';
my $arg_num = scalar @ARGV;
if ( $arg_num < 1 ) {
usage();
}
#This will fail if we receive an invalid option
unless ( getopts( "$opt_string", \%opt ) ) {
usage();
}
#Whether to perform various functions
my $shouldCreateTiles = $opt{t};
my $shouldCreateMbtiles = $opt{m};
my $shouldOptimizeTiles = $opt{o};
#Either of these options implies making tiles first
if ( $shouldCreateMbtiles || $shouldOptimizeTiles ) {
$shouldCreateTiles = 'True';
}
#Default to all airports for the SQL query
my $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
my $stateId = "%";
if ( $opt{s} ) {
#If something provided on the command line use it instead
$stateId = $opt{s};
say "Supplied state ID: $stateId";
}
#Get the cycle from command line options
my $cycle = $ARGV[0];
if ( !( $cycle =~ /^\d\d\d\d$/ ) ) {
say "Cycle must be a 4 digit number";
say "eg: $0 1413";
exit(1);
}
say "Cycle: $cycle";
#Connect to our databases
my $dtppDatabase = "./dtpp-$cycle.sqlite";
my $cifpDatabase = "./cifp-$cycle.sqlite";
#database of metadata for dtpp
my $dtppDbh = DBI->connect( "dbi:SQLite:dbname=$dtppDatabase",
"", "", { RaiseError => 1 } )
or croak $DBI::errstr;
$dtppDbh->do("PRAGMA page_size=4096");
$dtppDbh->do("PRAGMA synchronous=OFF");
#Also attach our CIFP database
$dtppDbh->do("attach database '$cifpDatabase' as cifp");
#Query the dtpp database for charts
my $dtppSth = $dtppDbh->prepare(
"SELECT
D.PDF_NAME
,D.FAA_CODE
,D.CHART_NAME
,D.MILITARY_USE
,DG.upperLeftLon
,DG.upperLeftLat
,DG.xMedian
,DG.yMedian
,DG.xPixelSkew
,DG.yPixelSkew
,C.AirportReferencePtLongitude
,C.AirportReferencePtLatitude
FROM
dtpp as D
JOIN
dtppGeo as DG
,cifp.'primary_P_A_base_Airport - Reference Points' as C
ON
D.PDF_NAME=DG.PDF_NAME
and
D.FAA_CODE=C.ATAIATADesignator
WHERE
(
CHART_CODE = 'IAP'
OR
CHART_CODE = 'APD'
)
AND
DG.PDF_NAME NOT LIKE '%DELETED%'
AND
DG.STATUS LIKE '%MANUALGOOD%'
AND
D.FAA_CODE LIKE '$airportId'
AND
D.STATE_ID LIKE '$stateId'
;"
);
$dtppSth->execute();
my $_allSqlQueryResults = $dtppSth->fetchall_arrayref();
my $_rows = $dtppSth->rows;
unless ($_rows) {
say "No charts found in database";
exit(1);
}
say "Processing $_rows charts";
my $completedCount = 0;
#Where the PDFs are for this cycle
my $inputPathRoot = "./dtpp-$cycle/";
#Where to store output
my $outputPathRoot = "./byAirportWorldFile-$cycle/";
#Make the output directory if it doesn't already exist
if ( !-e "$outputPathRoot" ) {
make_path("$outputPathRoot");
}
#Process each plate returned by our query
foreach my $_row (@$_allSqlQueryResults) {
my (
$PDF_NAME, $FAA_CODE,
$CHART_NAME, $MILITARY_USE,
$upperLeftLon, $upperLeftLat,
$xPixelSize, $yPixelSize,
$xPixelSkew, $yPixelSkew,
$AirportReferencePtLongitude, $AirportReferencePtLatitude
) = @$_row;
say "$FAA_CODE ----------------------------------------------------";
#Make the airport directory if it doesn't already exist
if ( !-e "$outputPathRoot" . "$FAA_CODE/" ) {
make_path( "$outputPathRoot" . "$FAA_CODE/" );
}
my ($chartBasename) = $PDF_NAME =~ m/(\w+)\.PDF/i;
my $pngName = $chartBasename . '.png';
my $worldFileName = $chartBasename . '.wld';
my $vrtFileName = $chartBasename . '.vrt';
my $numberFormat = "%.10f";
#Create the .png for this procedure if it doesn't already exist
if ( !-e "$outputPathRoot" . "$FAA_CODE/" . $pngName ) {
#say "No .png ($pngName) found for $FAA_CODE";
say "Create PNG: "
. $inputPathRoot
. $PDF_NAME . "->"
. $outputPathRoot
. "$FAA_CODE/"
. $pngName;
convertPdfToPng( $inputPathRoot . $PDF_NAME,
$outputPathRoot . "$FAA_CODE/" . $pngName );
say "Optimize: $outputPathRoot" . "$FAA_CODE/" . $pngName;
my $pngQuantCommand =
"pngquant -s2 -q 100 --ext=.png --force $outputPathRoot"
. "$FAA_CODE/"
. $pngName;
executeAndReport($pngQuantCommand);
}
#If this plate is georeferenced (which it should be due to our query parameters
if ( $upperLeftLon && $upperLeftLat ) {
my $worldfilePath =
"$outputPathRoot" . "$FAA_CODE/" . "$worldFileName";
#Create the world file
open( my $fh, '>', $worldfilePath )
or die "Could not open file '$worldfilePath' $!";
if ( $yPixelSize > 0 ) {
say "Converting $yPixelSize to negative";
$yPixelSize = -($yPixelSize);
}
#Write out the world file parameters
say $fh sprintf( $numberFormat, $xPixelSize );
say $fh sprintf( $numberFormat, $yPixelSkew );
say $fh sprintf( $numberFormat, $xPixelSkew );
say $fh sprintf( $numberFormat, $yPixelSize );
say $fh sprintf( $numberFormat, $upperLeftLon );
say $fh sprintf( $numberFormat, $upperLeftLat );
close $fh;
#Make a .vrt for this .png, using the .wld file we just created
say "Translate: $outputPathRoot$FAA_CODE/$pngName";
my $gdal_translateCommand =
"gdal_translate -of VRT -strict -a_srs EPSG:4326"
. " $outputPathRoot$FAA_CODE/$pngName"
. " $outputPathRoot$FAA_CODE/$vrtFileName";
executeAndReport($gdal_translateCommand);
#Create tiles if the user asked to
if ($shouldCreateTiles) {
say "Tile: $outputPathRoot$FAA_CODE/$vrtFileName";
my $tileCommand =
"./tilers_tools/gdal_tiler.py "
. ' --profile=tms'
. ' --release'
. ' --paletted'
. ' --dest-dir="'
. $outputPathRoot
. $FAA_CODE . '"'
. " $outputPathRoot"
. $FAA_CODE . '/'
. $vrtFileName;
executeAndReport($tileCommand);
#Copy the viewer
copy( "./leaflet_template.html",
"$outputPathRoot$FAA_CODE/$chartBasename.tms/leaflet.html"
);
#Get the sort list of directories in the tiles folder and use first entry as min zoom
#and last as maxZoom
# TODO HACK
my $tilesDirectory =
"$outputPathRoot$FAA_CODE/$chartBasename.tms";
my @zoom_levels =
sort { $a <=> $b }
grep { -d "$tilesDirectory/$_" } read_dir($tilesDirectory);
my $minNativeZoom = $zoom_levels[0];
my $maxNativeZoom = $zoom_levels[-1];
#Calculate WGS84 decimal coordinates of the airport
my $airportLongitudeWgs84 =
coordinateToDecimalCifpFormat($AirportReferencePtLongitude);
my $airportLatitudeWgs84 =
coordinateToDecimalCifpFormat($AirportReferencePtLatitude);
#If these coordinates aren't defined use the upper left ones
$airportLongitudeWgs84 //= $upperLeftLon;
$airportLatitudeWgs84 //= $upperLeftLat;
#Adjust the template for this particular plate
#Fix up the parameters in the leaflet
#These are simple hacks for now
my $filename =
"$outputPathRoot$FAA_CODE/$chartBasename.tms/leaflet.html";
my $data = read_file $filename, { binmode => ':utf8' };
$data =~
s|<title>Tiled Chart</title>|<title>$FAA_CODE $CHART_NAME</title>|ig;
$data =~
s|this._div.innerHTML = "Merged Chart";|this._div.innerHTML = "$FAA_CODE $CHART_NAME";|ig;
$data =~
s|center: \[44.966667,-103.766667\],|center: [$airportLatitudeWgs84, $airportLongitudeWgs84],|ig;
$data =~ s|zoom: 4,|zoom: $minNativeZoom,|ig;
$data =~
s|maxNativeZoom: 12345|maxNativeZoom: $maxNativeZoom|ig;
write_file $filename, { binmode => ':utf8' }, $data;
}
#Optimize all of the tiles if the user asked to
if ($shouldOptimizeTiles) {
say "Optimize: $outputPathRoot$FAA_CODE/$chartBasename.tms";
my $pngQuantCommand =
"./pngquant_all_files_in_directory.sh $outputPathRoot$FAA_CODE/$chartBasename.tms";
executeAndReport($pngQuantCommand);
}
#Create mbtiles if the user asked to
if ($shouldCreateMbtiles) {
say "Mbtile: $outputPathRoot$FAA_CODE/$chartBasename.tms";
my $mbtileCommand =
"python ./mbutil/mb-util"
. ' --scheme=tms'
. " $outputPathRoot$FAA_CODE/$chartBasename.tms"
. " $outputPathRoot$FAA_CODE/$chartBasename.mbtiles";
executeAndReport($mbtileCommand);
}
++$completedCount;
}
}
}
sub executeAndReport {
#Validate and set input parameters to this function
my ($command) =
validate_pos( @_, { type => SCALAR } );
my $output = qx($command);
my $retval = $? >> 8;
#Did we succeed?
if ( $retval != 0 ) {
say $output;
carp "Error from $command. Return code is $retval";
}
return $retval;
}
sub convertPdfToPng {
#Validate and set input parameters to this function
my ( $targetPdf, $targetPng ) =
validate_pos( @_, { type => SCALAR }, { type => SCALAR } );
#DPI of the output PNG
my $pngDpi = 300;
#Convert the PDF to a PNG
my $pdfToPpmOutput;
#Return if the png already exists
if ( -e $targetPng ) {
return;
}
$pdfToPpmOutput = qx(pdftoppm -png -r $pngDpi $targetPdf > $targetPng);
my $retval = $? >> 8;
#Did we succeed?
if ( $retval != 0 ) {
#Delete the possibly bad png
unlink $targetPng;
carp "Error from pdftoppm. Return code is $retval";
}
return $retval;
}
sub usage {
say "Usage: $0 <options> <cycle>";
say " <cycle> The cycle number, eg. 1513";
say " -t Make tiles";
say " -m Make mbtiles";
say " -o Optimze tile size";
say " -a FAA airport ID";
say " -s Two letter state code";
exit 1;
}
sub coordinateToDecimalCifpFormat {
#Convert a latitude or longitude in CIFP format to its decimal equivalent
my ($coordinate) = shift;
my ( $deg, $min, $sec, $signedDegrees, $declination, $secPostDecimal );
my $data;
#First parse the common information for a record to determine which more specific parser to use
my $parser_latitude = Parse::FixedLength->new(
[
qw(
Declination:1
Degrees:2
Minutes:2
Seconds:2
SecondsPostDecimal:2
)
]
);
my $parser_longitude = Parse::FixedLength->new(
[
qw(
Declination:1
Degrees:3
Minutes:2
Seconds:2
SecondsPostDecimal:2
)
]
);
#Get the first character of the coordinate and parse accordingly
$declination = substr( $coordinate, 0, 1 );
given ($declination) {
when (/[NS]/) {
$data = $parser_latitude->parse_newref($coordinate);
die "Bad input length on parser_latitude"
if ( $parser_latitude->length != 9 );
#Latitude is invalid if less than -90 or greater than 90
# $signedDegrees = "" if ( abs($signedDegrees) > 90 );
}
when (/[EW]/) {
$data = $parser_longitude->parse_newref($coordinate);
die "Bad input length on parser_longitude"
if ( $parser_longitude->length != 10 );
#Longitude is invalid if less than -180 or greater than 180
# $signedDegrees = "" if ( abs($signedDegrees) > 180 );
}
default {
return 0;
}
}
$declination = $data->{Declination};
$deg = $data->{Degrees};
$min = $data->{Minutes};
$sec = $data->{Seconds};
$secPostDecimal = $data->{SecondsPostDecimal};
# print Dumper($data);
$deg = $deg / 1;
$min = $min / 60;
#Concat the two portions of the seconds field with a decimal between
$sec = ( $sec . "." . $secPostDecimal );
# say "Sec: $sec";
$sec = ($sec) / 3600;
$signedDegrees = ( $deg + $min + $sec );
#Make coordinate negative if necessary
if ( ( $declination eq "S" ) || ( $declination eq "W" ) ) {
$signedDegrees = -($signedDegrees);
}
# say "Coordinate: $coordinate to $signedDegrees"; #if $debug;
# say "Decl:$declination Deg: $deg, Min:$min, Sec:$sec"; #if $debug;
return ($signedDegrees);
}