Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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' ); } }

gryphon
code('Perl') || die;


In reply to Build Wall Map from Google Map Tiles by gryphon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-19 20:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found