User:Wereon/gbthumb.pl
#!/usr/bin/perl # gbthumb.pl # written August 9, 2006 by User:Wereon # public domain use Geography::NationalGrid::GB; # Latitude and longitude - commented out #$latd=51; $latm=10; $lats=43.87; $latns="N"; #$lond=1; $lonm=49; $lons=35.07; $lonwe="W"; # Grid reference $gr="SP1955"; $p=new Geography::NationalGrid::GB(GridReference=>$gr); # The image Gb4dot.svg is a cropped part of a world map. # The original co-ordinates of its corners are given below. $north=10670.104; $west=17098.18; $south=12256.114; $east=18202.09; # The width and height of the small image, and the radius of the dot. $imgw=180; $imgh=259; $cr=4; $pi=3.14159265358979; # The northernmost latitude on the original world map, and half its height. $lim=85.0511287798; $h=18000; # code proper starts here # Make latitudes and longitudes decimal - commented out #$lat=$latd+($latm/60)+($lats/3600); if (uc($latns) eq "S") { $lat=-$lat; } #$lon=$lond+($lonm/60)+($lons/3600); if (uc($lonwe) eq "W") { $lon=-$lon; } # Get lat. and lon. from grid reference $lat=$p->latitude; $lon=$p->longitude; # Calculate co-ordinates of circle centre as fractions $x=(LonToX($lon)-$west)/($east-$west); $y=(LatToY($lat)-$north)/($south-$north); # And put them on small map $x=int($x*$imgw)-$cr; $y=int($y*$imgh)-$cr; #print "$x,$y\n"; print "{{GBthumb|$x|$y|$gr}}\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; }