| 1 | #!/usr/local/bin/perl -w | ||
| 2 | # | ||
| 3 | # convert WDB textual GIS data into Mercator projected | ||
| 4 | # binary data for quicker loading and data compression | ||
| 5 | # | ||
| 6 | use Geo::Mercator; | ||
| 7 | use Pod::Usage; | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | |||
| 12 | pod2usage(-exitval => 1 ) if $ARGV[0] && | ||
| 13 | (($ARGV[0] eq '-h') || ($ARGV[0] eq '-help')); | ||
| 14 | pod2usage(-msg => 'No input path specified.', -exitval => 1 ) unless $ARGV[0]; | ||
| 15 | pod2usage(-msg => 'No output path specified.', -exitval => 1 ) unless $ARGV[1]; | ||
| 16 | my $path = $ARGV[0]; | ||
| 17 | my $out = $ARGV[1]; | ||
| 18 | |||
| 19 | my @srcs = qw( | ||
| 20 | africa-bdy | ||
| 21 | africa-cil | ||
| 22 | africa-riv | ||
| 23 | asia-bdy | ||
| 24 | asia-cil | ||
| 25 | asia-riv | ||
| 26 | europe-bdy | ||
| 27 | europe-cil | ||
| 28 | europe-riv | ||
| 29 | namer-bdy | ||
| 30 | namer-cil | ||
| 31 | namer-pby | ||
| 32 | namer-riv | ||
| 33 | samer-bdy | ||
| 34 | samer-cil | ||
| 35 | samer-riv | ||
| 36 | asia-bdy | ||
| 37 | asia-riv | ||
| 38 | ); | ||
| 39 | |||
| 40 | $| = 1; | ||
| 41 | read_wdb($_) | ||
| 42 | foreach (@srcs); | ||
| 43 | |||
| 44 | sub read_wdb { | ||
| 45 | my $src = shift; | ||
| 46 | print STDERR "Skipping $path/$src.txt: $!\n" and | ||
| 47 | return undef | ||
| 48 | unless open INF, "$path/$src.txt"; | ||
| 49 | |||
| 50 | open BINF, '>', "$out/$src.bin" or die "Cannot open $out/$src.bin: $!"; | ||
| 51 | binmode BINF; | ||
| 52 | my $oldfd = select BINF; | ||
| 53 | $| = 1; | ||
| 54 | select $oldfd; | ||
| 55 | |||
| 56 | my @wdb = (); | ||
| 57 | my ($minlat, $minlong, $maxlat, $maxlong) = (10000, 100000, -100000, -100000); | ||
| 58 | # | ||
| 59 | # add parsing code here | ||
| 60 | # | ||
| 61 | print "Loading $path/$src.txt\n"; | ||
| 62 | my $seg = ''; | ||
| 63 | my @mercs = (); | ||
| 64 | my ($segno, $rank, $pts); | ||
| 65 | my $msg; | ||
| 66 | while (<INF>) { | ||
| 67 | s/^\s+//; | ||
| 68 | unless (substr($_, 0, 7) eq 'segment') { | ||
| 69 | my ($lat, $long) = split /\s+/; | ||
| 70 | # | ||
| 71 | # we've got some dirty data!!! | ||
| 72 | # | ||
| 73 | print STDERR "\n**** Bad data point in $path/$src.txt segment $segno: ", | ||
| 74 | $lat, ', ', $long, "\n" and | ||
| 75 | next | ||
| 76 | unless ($lat >= -90) && ($lat <= 90) && ($long >= -192) && ($long <= 192); | ||
| 77 | push @mercs, mercate($lat, $long); | ||
| 78 | $minlat = $lat | ||
| 79 | if ($minlat > $lat); | ||
| 80 | $maxlat = $lat | ||
| 81 | if ($maxlat < $lat); | ||
| 82 | $minlong = $long | ||
| 83 | if ($minlong > $long); | ||
| 84 | $maxlong = $long | ||
| 85 | if ($maxlong < $long); | ||
| 86 | next; | ||
| 87 | } | ||
| 88 | # | ||
| 89 | # set total record length (after recordlength) | ||
| 90 | # then pack all the floats into the record | ||
| 91 | # | ||
| 92 | if (@mercs > 0) { | ||
| 93 | $msg = join('', | ||
| 94 | "\r", | ||
| 95 | "Packing segment $segno got ", scalar @mercs, " coords, expected ", | ||
| 96 | $pts * 2); | ||
| 97 | $msg .= ' ' x (75 - length($msg)); | ||
| 98 | print $msg; | ||
| 99 | $seg .= pack('LLLL', 12 + (8 * 2 * $pts), $segno, $rank, $pts); | ||
| 100 | $pts *= 2; | ||
| 101 | $seg .= pack("d$pts", @mercs); | ||
| 102 | print BINF $seg; | ||
| 103 | @mercs = (); | ||
| 104 | $seg = ''; | ||
| 105 | } | ||
| 106 | ($segno, $rank, $pts) = (/^\s*segment\s+(\d+)\s+rank\s+(\d+)\s+points\s+(\d+)/); | ||
| 107 | $msg = "\rStarting segment $segno"; | ||
| 108 | $msg .= ' ' x (75 - length($msg)); | ||
| 109 | print $msg; | ||
| 110 | } | ||
| 111 | close INF; | ||
| 112 | if (@mercs > 0) { | ||
| 113 | $msg = join('', | ||
| 114 | "\rPacking segment $segno got ", scalar @mercs, " coords, expected ", | ||
| 115 | $pts * 2); | ||
| 116 | $msg .= ' ' x (75 - length($msg)); | ||
| 117 | print $msg; | ||
| 118 | $seg .= pack('LLLL', 12 + (8 * 2 * $pts), $segno, $rank, $pts); | ||
| 119 | $pts *= 2; | ||
| 120 | $seg .= pack("d$pts", @mercs); | ||
| 121 | print BINF $seg; | ||
| 122 | } | ||
| 123 | |||
| 124 | print "\n$src bounding box is $minlat, $minlong to $maxlat,$maxlong\n"; | ||
| 125 | close BINF; | ||
| 126 | return 1; | ||
| 127 | } | ||
| 128 | |||
| 129 | =pod | ||
| 130 | |||
| 131 | =head1 NAME | ||
| 132 | |||
| 133 | wdb2merc - Convert Textual WDB datasets to binary Mercator Projected files | ||
| 134 | |||
| 135 | =head1 SYNOPSIS | ||
| 136 | |||
| 137 | wdb2merc.pl <WDB-path> <output-path> | ||
| 138 | wdb2merc.pl -h|-help | ||
| 139 | |||
| 140 | =head1 DESCRIPTION | ||
| 141 | |||
| 142 | Reads the latitude/longitude coordinate values from textual WDB dataset | ||
| 143 | files I(available at> L<http://www.evl.uic.edu/pape/data/WDB/>) and converts | ||
| 144 | them to Mercator projected distance values (in meters), writing the | ||
| 145 | the values out in a packed binary format that is faster to load, and | ||
| 146 | compresses the data approx. 35%. | ||
| 147 | |||
| 148 | The datasets are also scrubbed of some bad data (I checked my globe | ||
| 149 | several times, but couldn't find longitude -544 degrees...). | ||
| 150 | |||
| 151 | The resulting binary files are used by L<GD::Map::Mercator> to | ||
| 152 | render map images. | ||
| 153 | |||
| 154 | =cut |