#!/usr/bin/perl

# small script that takes the output of unwayedsegs.pl (assumed to
# reside in unwayedsegs.out) and writes, to stdout, a web page with
# the top 100 z-12 tiles.
#
# there's a filter expression in this script that ignores all 
# netherlands tiles (and perhaps a bit more around that).

use strict;
use Math::Trig;

open (BLA, "sort -rn< unwayedsegs.out|");

print <<EOF;
<html>
<head>
</head>
<body>
<h1>Top 100 Level-12 tiles with Unwayed Segments</h1>
<hr>

Based on latest planet file. Click on tile to view on ifw.org. <p>
(The Netherlands not listed as there are lots of unwayed segments due to ongoing AND import; some Netherlands tiles may have slipped in nonetheless, ignore them.)
<p>

<table>
<tr>
<th># of segments</th>
<th>on this z12 tile</th>
<th>co-ordinates</th>
<th>that's here (z7)</th>
</tr>
EOF
my $c;
while(<BLA>)
{
    my ($seg,$x, $y)=split(" ");
    # ignore NL
    if ($x<2080 or $x>2127 or $y<1318 or $y>1400)
    {
    last if ($c++==100);
    printf "<tr><td align='right'>%d</td><td><a href='http://informationfreeway.org/?lat=%6.3f&lon=%6.3f&zoom=12&layers=0000F0B0'><img border'0' src='http://tile.openstreetmap.org/12/%d/%d.png'></a></td><td>lat=%6.3f<br>lon=%6.3f<br>tile x=%d y=%d</td><td><a href=''http://informationfreeway.org/?lat=%6.3f&lon=%6.3f&zoom=7&layers=0000F0B0'><img border='0' src='http://tile.openstreetmap.org/7/%d/%d.png'></tr>\n",
        $seg,
        tileYToLat($y),
        tileXToLon($x),
        $x,
        $y,
        tileYToLat($y),
        tileXToLon($x),
        $x,
        $y,
        tileYToLat($y),
        tileXToLon($x),
        $x/32,
        $y/32;
    }
}
print "</table></body></html>\n";

sub tileYToLat() {
    my ($y)=@_;
    return Math::Trig::atan(Math::Trig::sinh(pi - (pi*$y / (1 << 11)))) * 180 / pi;
}
sub tileXToLon() {
    my ($x)=@_;
    return $x * 45.0 / (1 << 9) - 180.0;
}

