Greetings fellow monks,
Inspired by Google Maps kung fu and the lack of any decorations on a few of the long hallways at work (plus the fact that I tire of giving people directions), I decided to throw together a script that fetches a bunch of map tiles from Google Maps, merges them into a single, uber map, then splits that map into graphics big enough to fill an 8.5x11 sheet of paper.
The goal was mostly to play with Image::Magick (because I've never played with it before). I added a "sleep" period in the code to keep from spamming Google Maps too badly.
Note: This image scraping is probably not a blessed activity by the folks at Google. I couldn't find any terms of use on the beta site (not that I really looked all that hard). Anyway, be nice and don't flood Google please.
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use LWP::Simple;
use Image::Magick;
my %config = (
zoom => 3, # Google Maps zoom level (1 == smallest resol
+ution)
x_start => -2387, # Upper-left map tile "x" CGI GET parameter
y_start => -1095, # Upper-left map tile "y" CGI GET parameter
rows => 108, # Number of rows (i.e. y-axis or height) to f
+etch
cols => 47, # Number of columns (i.e. x-axis or width) to
+ fetch
file => 'map.gif', # Filename to save the merged tiles into
s_secs => 5, # Number of seconds to sleep in each fetch in
+terval
s_every => 18, # Number of tiles to fetch in a given interva
+l
get_est => 0.21, # Estimated number of seconds to fetch a tile
width => 768, # Desired width for cut image plates
height => 1008 # Desired height for cut image plates
);
my ($tiles_count, $tiles_total) = (0, $config{rows} * $config{cols});
my $eta = $config{rows} * $config{cols} / $config{s_every} * $config{s
+_secs}
+ $config{rows} * $config{cols} * $config{get_est};
my $start_time = time;
for (my $y = 0; $y < $config{rows}; $y++) {
for (my $x = 0; $x < $config{cols}; $x++) {
$tiles_count++;
getstore(
'http://mt.google.com/mt?' . 'x=' . ( $config{x_start} + $x ) .
'&y=' . ( $config{y_start} + $y ) . '&zoom=' . $config{zoom},
join('_', 'plate', $x, $y) . '.gif'
);
sleep $config{s_secs}
if (int($tiles_count/$config{s_every}) == $tiles_count/$config{s_
+every});
}
}
my $plates = new Image::Magick;
for (my $y = 0; $y < $config{rows}; $y++) {
for (my $x = 0; $x < $config{cols}; $x++) {
$plates->Read('plate_' . $x . '_' . $y . '.gif');
}
}
my $width = $plates->[0]->Get('columns');
my $height = $plates->[0]->Get('rows');
my $single_map = $plates->Montage(
geometry => $width . 'x' . $height . '+0+0',
tile => $config{cols}
);
$single_map->Write($config{file});
for (my $y = 0; $y < $config{rows}; $y++) {
for (my $x = 0; $x < $config{cols}; $x++) {
unlink 'plate_' . $x . '_' . $y . '.gif';
}
}
$single_map->Rotate(degrees => 90);
$width = $single_map->[0]->Get('columns');
$height = $single_map->[0]->Get('rows');
my $x_count = int($width / $config{width});
my $y_count = int($height / $config{height});
for (my $x = 0; $x <= $x_count; $x++) {
for (my $y = 0; $y <= $y_count; $y++) {
my ($w, $h) = ($config{width}, $config{height});
$w = $width - $x_count * $config{width} if ($x == $x_count);
$h = $height - $y_count * $config{height} if ($y == $y_count);
my $geometry = $w . 'x' . $h .
'+' . $config{width} * $x . '+' . $config{height} * $y;
my $plate = $single_map->Clone;
$plate->Crop(geometry => $geometry);
$plate->Montage(geometry => $w . 'x' . $h . '+0+0')->Write(
'plate_' . $x . '_' . $y . '.gif'
);
}
}