WWW-StreetMap

 view release on metacpan or  search on metacpan

lib/WWW/StreetMap.pm  view on Meta::CPAN

=head1 NAME

WWW::StreetMap - Interface to http://www.streetmap.co.uk/

=head1 SYNOPSIS

 use WWW::StreetMap;

=head1 DESCRIPTION

Interface to http://www.streetmap.co.uk/

Please respect the terms and conditions of the excellent streetmap website: 
http://www.streetmap.co.uk/disclaimer.htm

=cut



package WWW::StreetMap;



# pragmata
use strict;
use vars qw($VERSION);

# Standard Perl Library and CPAN modules
use File::Temp qw(tempfile);
use Image::Magick;
use IO::All;
use IO::All::LWP;
use OpenOffice::OODoc;


$VERSION = '0.18';

=head1 CLASS METHODS

=head2 new

 new(url => $url)

=cut

sub new {
	my($class, %options) = @_;

	die "No URL specified\n" unless $options{url};

	my $self = {
		url => $options{url},
	};

	bless $self, $class;
	return $self;
}

=head1 OBJECT METHODS

=head2 build_map_jpg

 build_map_jpg

Download the map from streetmap and join it into a single jpeg image.

=cut

sub build_map_jpg {
	my($self, $filename) = @_;

	return unless $filename;

	# I guess could use WWW::Mechanize and its get_all_links instead of this....
	my @lines = io($self->{url})->slurp;

	my @image_urls = grep (/image.dll/, @lines);
	

	map {  s!  ^.*SRC="([^"]+)".*$ !$1!x } @image_urls;
	
	my @filenames = ();
	my $image = Image::Magick->new;
	foreach my $url (@image_urls) {
		my ($fh, $filename) = tempfile(SUFFIX=>'gif');
		chomp $url;
		io($url) > io($filename);
		$image->Read($filename);
		close $fh;  # auto deletes
	}
	
	my $map = $image->Montage(geometry=>'200x200', tile => '3x3');
	$map->Write($filename);
}

=head2 create_oo_doc

 create_oo_doc($filename, $map_filename)

Create an OpenOffice Writer document containg the map.

If you do not specify a pre-existing map then $map_filename will default to
$filename with the .jpg extension added.

=cut


sub create_oo_doc {
	my($self, $filename, $map_filename) = @_;

	return unless $filename;

	unless($map_filename) {
		$map_filename = "$filename.jpg";
		$self->build_map_jpg($map_filename);
	}

	# create the File object
	my $oofile = ooFile($filename, create => 'text') or die "Something was wrong !\n";

	# get the current local time in OpenOffice.org-compliant format
	my $oodate = ooLocaltime;
	# get access to its metadata
	my $metadata = ooMeta(archive => $oofile);

	# set the current time as the creation date
	$metadata->creation_date($oodate);
	# set the current time as modification date
	$metadata->date($oodate);
	# set the title, if provided
	$metadata->title("Map");
	# saving (before here, the file didn't exist)
	$oofile->save;


	my $document =  OpenOffice::OODoc::Document->new(file => $filename);

	$document->createImageElement(
		"Map",
		import  => $map_filename,
		size    => "10cm, 10cm",
	);



( run in 0.686 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )