#!/usr/bin/perl

# generated from http://openstreetmap.gryph.de/bigmap.cgi/
# permalink for this map: http://openstreetmap.gryph.de/bigmap.cgi?xmin=24141&xmax=24162&ymin=13749&ymax=13770&zoom=15&scale=256&baseurl=http%3A%2F%2Ftile.openstreetmap.org%2F!z%2F!x%2F!y.png
#
use strict;
use LWP;
use GD;

my $img = GD::Image->new(5632, 5632, 1);
my $white = $img->colorAllocate(248,248,248);
$img->filledRectangle(0,0,5632,5632,$white);
my $ua = LWP::UserAgent->new();
$ua->env_proxy;
for (my $x=0;$x<22;$x++)
{
    for (my $y=0;$y<22;$y++)
    {
        my $xx = $x + 24141;
        my $yy = $y + 13749;
        foreach my $base(split(/\|/, "http://tile.openstreetmap.org/15/!x/!y.png"))
	{
		my $url = $base;
                $url =~ s/!x/$xx/g;
                $url =~ s/!y/$yy/g;
		print STDERR "$url... ";
		my $resp = $ua->get($url);
		print STDERR $resp->status_line;
		print STDERR "\n";
		next unless $resp->is_success;
		my $tile = GD::Image->new($resp->content);
		next if ($tile->width == 1);
		if ($base =~ /seamark/) {
		my $black=$tile->colorClosest(0,0,0);
		$tile->transparent($black);
		}
		$img->copy($tile, $x*256,$y*256,0,0,256,256);
	}
    }
}
binmode STDOUT;
print $img->png();
