#!/usr/bin/perl

use Imager;
use Imager::Fill;
use Geo::Dymaxion;
use strict;
use warnings;

my $image_file  = "dymaxion.jpg";
my @dot_color  = (255, 0, 0);
my $dot_size   = 5;
my @coord;

$|++;

scalar <>; # throw away 1st line
while (<>) {
    y/"//d;
    s/\b([\d.]+)\W[WS]\b/-$1/g;
    s/\b([\d.]+)\W[NE]\b/$1/g;
    s/\s+$//g;
    s/&#(\d+);/sprintf("\\u%04x", $1)/eg;
    split "\t";
    push @coord, [@_[3,4]];
}

my $map = Imager->new;
$map->open( file => $image_file );

my $dymax = Geo::Dymaxion->new( $map->getwidth, $map->getheight );

my $dot  = Imager::Color->new( @dot_color );
my $fill = Imager::Fill->new( solid => $dot );
for (@coord) {
    my ($x, $y) = $dymax->plot( @$_ );
    $map->circle( fill => $fill, r => $dot_size, x => $x, y => $y );
    # $map->setpixel( x => $x, y => $y, color => $dot );
}

$map->write( type => "jpeg", fd => fileno(STDOUT) )
    or die $map->errstr;
