User:Wereon/gbthumb.pl


 * 1) !/usr/bin/perl


 * 1) gbthumb.pl
 * 2) written August 9, 2006 by User:Wereon
 * 3) public domain

use Geography::NationalGrid::GB;


 * 1) Latitude and longitude - commented out
 * 2) $latd=51; $latm=10; $lats=43.87; $latns="N";
 * 3) $lond=1; $lonm=49; $lons=35.07; $lonwe="W";

$gr="SP1955";
 * 1) Grid reference

$p=new Geography::NationalGrid::GB(GridReference=>$gr);

$north=10670.104; $west=17098.18; $south=12256.114; $east=18202.09;
 * 1) The image Gb4dot.svg is a cropped part of a world map.
 * 2) The original co-ordinates of its corners are given below.

$imgw=180; $imgh=259; $cr=4;
 * 1) The width and height of the small image, and the radius of the dot.

$pi=3.14159265358979;

$lim=85.0511287798; $h=18000;
 * 1) The northernmost latitude on the original world map, and half its height.


 * 1) code proper starts here


 * 1) Make latitudes and longitudes decimal - commented out
 * 2) $lat=$latd+($latm/60)+($lats/3600); if (uc($latns) eq "S") { $lat=-$lat; }
 * 3) $lon=$lond+($lonm/60)+($lons/3600); if (uc($lonwe) eq "W") { $lon=-$lon; }

$lat=$p->latitude; $lon=$p->longitude;
 * 1) Get lat. and lon. from grid reference

$x=(LonToX($lon)-$west)/($east-$west); $y=(LatToY($lat)-$north)/($south-$north);
 * 1) Calculate co-ordinates of circle centre as fractions

$x=int($x*$imgw)-$cr; $y=int($y*$imgh)-$cr;
 * 1) And put them on small map

print "\n";
 * 1) print "$x,$y\n";

sub LonToX { my ($x)=@_; $x=(180+$x)*100; return $x; }

sub XToLon { my ($x)=@_; $x=($x/100)-180; return $x; }

sub LatToY { my ($y)=@_; if ($y>$lim) { $y=$lim; } if ($y<-$lim) { $y=-$lim; } $y=$y*$pi/180; $y=log((sin($y)/cos($y))+(1/cos($y))); $y=(1-($y/$pi))*$h; return $y; }

sub YToLat { my ($y)=@_; $y=$pi*(1-($y/$h)); $y=(2*atan2(exp($y),1))-($pi/2); $y=$y*180/$pi; return $y; }