Created
June 5, 2010 00:06
-
-
Save eikes/426114 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# configure here: | |
# first top left (north west) | |
my $startlon = 13.40991; | |
my $startlat = 52.48905; | |
# then bottom right (south east) | |
my $endlon = 13.42209; | |
my $endlat = 52.47982; | |
#zoom level: | |
my $zoom = 16; | |
# end of configuration | |
use strict; | |
use LWP; | |
use GD; | |
use Math::Trig; | |
sub getTileNumber { | |
my ($lat,$lon,$zoom) = @_; | |
my $xtile = int( ($lon+180)/360 *2**$zoom ) ; | |
my $ytile = int( (1 - log(tan(deg2rad($lat)) + sec(deg2rad($lat)))/pi)/2 *2**$zoom ) ; | |
return ($xtile, $ytile); | |
} | |
(my $xstart, my $ystart) = getTileNumber($startlat, $startlon, $zoom); | |
(my $xend, my $yend) = getTileNumber($endlat, $endlon, $zoom); | |
my $tilesize = 256; | |
# calc image size | |
my $xsize = ($xend-$xstart+1) * $tilesize; | |
my $ysize = ($yend-$ystart+1) * $tilesize; | |
my $img = GD::Image->new($xsize, $ysize, 1); | |
my $white = $img->colorAllocate(248,248,248); | |
$img->filledRectangle(0,0,$xsize,$ysize,$white); | |
my $ua = LWP::UserAgent->new(); | |
$ua->env_proxy; | |
for (my $x=$xstart;$x<=$xend;$x++) { | |
for (my $y=$ystart;$y<=$yend;$y++) { | |
my $url = sprintf("http://c.tile.openstreetmap.org/%d/%d/%d.png", $zoom, $x, $y); | |
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); | |
$img->copy($tile, ($x-$xstart)*256,($y-$ystart)*256,0,0,256,256); | |
} | |
} | |
binmode STDOUT; | |
print $img->png(); |
The resulting png file is pretty large. I tried to reduce the size using:
$img->trueColorToPalette(0, 255);
print $img->png(9);
which works fine, but I think the original files are still smaller then the files created by this script. Any ideas how to get smaller files without corrupting colours?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I take it this is a modified version of the perl script generated by "BigMap" ( http://wiki.openstreetmap.org/wiki/Bigmap )
I also modified the BigMap script. Take a look at bigmapmod.pl linked from here: http://harrywood.co.uk/maps/christchurch/ This includes various tricks to make the tile scraping script more friendly to the OpenStreetMap server (important when re-running regularly)
Also various code tidying to make it more configurable, although I haven't done the lat/lon to tile number conversion you've done here. That could be handy