#!/usr/bin/perl use strict; use warnings; use File::Slurp; use GD; GD::Image->trueColor( 1 ); use DBI; use Image::Size; use LWP::Simple; use Readonly; my $dbh = DBI->connect('dbi:SQLite:metoffice.db', undef, undef, {RaiseError => 1, AutoCommit => 0 } ); # Map: http://www.evl.uic.edu/pape/data/Earth/ Readonly::Scalar my $MAP_TEMPLATE => 'etopo-landmask-blue.png'; Readonly::Scalar my $MAP_OUTPUT => 'cru-stations-map.png'; my $gd = GD::Image->new( $MAP_TEMPLATE ) or die "Cannot load map template: '$MAP_TEMPLATE'\n"; my ($lon_mapper, $lat_mapper); { my ($w, $h) = imgsize $MAP_TEMPLATE; $lon_mapper = sub { ( $w / 2 ) - $_[0] * ( $w / 360 ) } ; $lat_mapper = sub { ( $h / 2 ) - $_[0] * ( $h / 180 ) } ; } my $marker_color = $gd->colorAllocate( 0xff, 0xff, 0x00 ); my $circle_color = $gd->colorAllocate( 0xff, 0x00, 0x00 ); my $coords = $dbh->selectall_arrayref( q{SELECT Station_Long, Station_Lat FROM meta} ); for my $coord ( @$coords ) { my $x = $lon_mapper->( $coord->[0] ); my $y = $lat_mapper->( $coord->[1] ); $gd->filledEllipse( $x, $y, 8, 8, $circle_color ); $gd->line( $x, $y - 4, $x, $y + 4, $marker_color ); $gd->line( $x - 4, $y, $x + 4, $y, $marker_color ); } $dbh->disconnect; write_file $MAP_OUTPUT, { binmode => ':raw' }, \$gd->png; __END__