#!/usr/bin/perl

=pod

=head1 NAME 

updatemap.pl - update map of regions for Club register

=head1 SYNOPSIS

updatemap.pl

=head1 DESCRIPTION

The script searches for latitudes and longitudes of CHGK regions
and outputs them superimposed on the world map. The world map
is taken from the gnuplot distribution. Then the script outputs
an include file for register.cgi(1)

=head1 AUTHOR

Boris Veytsman

=head1 DATE

$Date: 2001/10/13 23:39:44 $

=head1 REVISION

$Revision: 1.2 $

=cut

    use strict;
use DBI;
my $dbh;
$dbh = DBI->connect("DBI:mysql:chgk", "piataev", "") or 
    die ($dbh->errstr);
my $width=640;
my $height=320;
my $offset=160;
#######################################################
# Opening the files
#######################################################
open (INCLUDE, ">regions.html");
open (GNU, ">regions.gnu");
open (DAT, ">regions.dat");

print GNU <<END;
 set term png medium color
 set ou "regions.png"
 set noborder
 set nokey
 set size ratio 0.5 1, 1
 set tmargin 0
 set bmargin 0
 set lmargin 0
 set rmargin 0
 set noyzeroaxis
 set noxtics
 set noytics
 set xrange [-180:180]
 set yrange [-90:90]
END
print INCLUDE <<END;
 <map name="regions">
END

##########################################################
# Writing clubs coordinates
##########################################################

my $sth = $dbh->prepare ("
SELECT rid,Name,Lon,Lat from Regions 
where NOT ISNULL(Lat) and NOT ISNULL(Lon)");
$sth->execute;
while (my ($rid,$Name,$Lon,$Lat) = $sth->fetchrow_array) {
    print DAT <<END;
 $Lon, $Lat
END
    my $x=int($width*($Lon+180)/360);
    my $y=int($height*(90-$Lat)/180);
    print INCLUDE <<END;
 <area shape="circle" coords="$x, $y, 3" 
 href="/znatoki/cgi-bin/register.cgi?rid=$rid&clubs=1&level=10000" 
 alt="$Name">
END
}

##########################################################
# Finishing up
##########################################################
print INCLUDE <<END;
 </map>
 <img usemap="#regions" src="../images/regions.gif"
 alt="Карта клубов" width=$width height=$height>
END
print GNU <<END;
 plot 'world.dat'  with lines 3 4, 'regions.dat' with points 1
END
close GNU;
close INCLUDE;
close DAT;
`gnuplot regions.gnu`;
my $dim=$width.'x'.$height.'+0+'.$offset;
`convert -crop $dim regions.png gif:regions.tmp`;
`giftrans -t '#ffffff' regions.tmp> regions.gif`;
exit 0;
