#!/usr/bin/perl

use Imager;
use strict;

my ($in, $out) = @ARGV;
die "Usage: $0 <input.jpg> <output.jpg>\n" unless $in and $out;

my ($top, $left)     = (  90, -180 );
my ($bottom, $right) = ( -90,  180 );

my $map = Imager->new;
$map->open( file => $in ) or die $map->errstr;

my $x_scale = $map->getwidth  / ($right - $left);
my $y_scale = $map->getheight / ($bottom - $top);

while (<STDIN>) {
    my ($lat, $lon) = /(-?\d+\.?\d*)/gos;
    next if not defined $lat or not defined $lon 
	or $lon > $right or $lon < $left or $lat < $bottom or $lat > $top;

    my $x = ($lon - $left) * $x_scale;
    my $y = ($lat - $top)  * $y_scale;

    $map->circle( color => "white", r => 5, x => $x, y => $y );
    $map->circle( color => "red",   r => 4, x => $x, y => $y );
}

$map->write( file => $out ) or die $map->errstr;
