Global-Rainbow-Map

 view release on metacpan or  search on metacpan

lib/Global/Rainbow/Map.pm  view on Meta::CPAN

	
	foreach my $country ($self->countries)
	{
		my $colour = $self->colour_for_country($country)->svg;
		$str .= <<"CSS";
.${country} {
	fill: $colour;
	opacity: 1;
}
CSS
	}
	
	return $str;
}

sub libxml_document
{
	my ($self) = @_;
	my $class  = (ref $self // $self);
	my $doc    = $class->clone_template;
	my $styles = $doc->findnodes($class->stylesheet_xpath)->get_node(1);
	$styles->appendText($self->generate_css);
	return $doc;
}

sub svg
{
	my ($self) = @_;
	$self->libxml_document->toString;
}

# This just doesn't seem to work. :-(
our $png =
q{
	use File::Temp 0          ();
	use SVG::Parser 0         ();
	use SVG::Parser::Expat 0  ();
	use SVG::Parser::SAX 0    ();
	use SVG::Rasterize 0      ();
	
	my ($self, $width, $height) = @_;
	$width  ||= 640;
	$height ||= 480;
	
	my $temp_file = File::Temp->new(UNLINK => 1);
	
	state $parser = SVG::Parser->new;
	my $svg = $parser->parse($self->svg);
	
	my $rasterize = SVG::Rasterize->new(
		svg     => $svg,
		width   => $width,
		height  => $height,
	);
	$rasterize->rasterize;
	$rasterize->write(
		type      => 'png',
		file_name => $temp_file->filename,
	);
	
	return do { local(@ARGV, $/) = $temp_file->filename; <> };
};


=encoding utf8

=head1 NAME

Global::Rainbow::Map - make the world look like a rainbow, but usefully

=head1 SYNOPSIS

 my $map = Global::Rainbow::Map->new(
   gb => 'red',
   ca => 'orange',
   in => 'yellow',
   nz => 'green',
   au => 'blue',
   lk => 'purple',
 );
 print $map->svg;

=head1 DESCRIPTION

Global::Rainbow::Map generates a map of the world (Robinson projection)
with each country shaded whatever colour you like. In fact, you can even
choose colours that don't appear in the rainbow... like magenta!

The possibilities for such a module are clearly endless. If you had a
table listing per-capita alcohol consumption, you could create a map of
the world in different colours to illustrate per-capita alcohol
consumption. Or if you had a table of which countries had won the most
Olympic medals for swimming, then you could create a map of the world
in different colours to illustrate which countries had won the most
Olympic medals for swimming.

The template map used is Wikipedia's public domain blank world map
L<http://en.wikipedia.org/wiki/File:BlankMap-World6.svg>, the 14 Feb
2012 revision.

=head2 Constructor

=over

=item C<< new(%colours) >>

Creates a new map. The hash is a list of country code to colour pairings.

Country codes are typically ISO 3166-1 two letter codes. If you don't
know the code for a particular country, L<Locale::Country> can help
you out. There are a number of additional codes like "eu" (the whole
European Union) and "aq" (Antarctica). These codes are all lower case.

Colours can be hexadecimal codes like '#ff0000' or named colours from
the SVG or X11 palettes. Countries not coloured will remain a pale
grey.

Hash keys which do not match the regular expression
C<< /^[a-z][a-z0-9-]*[a-z0-9]$/ >>, in particular hash keys starting
with a hyphen are reserved for future use.



( run in 1.145 second using v1.01-cache-2.11-cpan-b9db842bd85 )