| 1 | package Geo::Coordinates::OSGB; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 10 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use 5.008; # at least Perl 5.08 please | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 10 | our $VERSION = '2.19'; | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 10 | our %EXPORT_TAGS = ( | |||||||||||||||||||||||||||||||||||||||||
| 11 | all => [ | ||||||||||||||||||||||||||||||||||||||||||
| 12 | qw( | ||||||||||||||||||||||||||||||||||||||||||
| 13 | ll_to_grid | ||||||||||||||||||||||||||||||||||||||||||
| 14 | grid_to_ll | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | ll_to_grid_helmert | ||||||||||||||||||||||||||||||||||||||||||
| 17 | grid_to_ll_helmert | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | get_ostn02_shift_pair | ||||||||||||||||||||||||||||||||||||||||||
| 20 | set_default_shape | ||||||||||||||||||||||||||||||||||||||||||
| 21 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 22 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 10 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{all} } ); | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 10 | use constant ELLIPSOIDS => { | |||||||||||||||||||||||||||||||||||||||||
| 28 | WGS84 => [ 6_378_137.000, 6_356_752.31424518, 298.257223563, 0.006694379990141316996137233540 ], | ||||||||||||||||||||||||||||||||||||||||||
| 29 | ETRS89 => [ 6_378_137.000, 6_356_752.314140, 298.257222101, 0.006694380022900787625359114703 ], | ||||||||||||||||||||||||||||||||||||||||||
| 30 | GRS80 => [ 6_378_137.000, 6_356_752.314140, 298.257222101, 0.006694380022900787625359114703 ], | ||||||||||||||||||||||||||||||||||||||||||
| 31 | OSGB36 => [ 6_377_563.396, 6_356_256.909, 299.3249612665, 0.0066705400741492318211148938735613129751683486352306 ], | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 10 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 10 | my $default_shape = 'WGS84'; | |||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | sub set_default_shape { | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | ▶ | my $s = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_default_shape: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | ▶ | croak "Unknown shape: $s" if !exists ELLIPSOIDS->{$s}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 2 | $default_shape = $s; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 10 | *get_ostn02_shift_pair = \&_find_OSTN02_shifts_at; | |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | # constants for OSGB mercator projection | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 10 | ▶ | use constant ORIGIN_LONGITUDE => -2 / 57.29577951308232087679815481410517; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 10 | ▶ | use constant ORIGIN_LATITUDE => 49 / 57.29577951308232087679815481410517; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 10 | ▶ | use constant ORIGIN_EASTING => 400_000; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 10 | ▶ | use constant ORIGIN_NORTHING => -100_000; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 10 | ▶ | use constant CONVERGENCE_FACTOR => 0.9996012717; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | # constants for OSTN02 data | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 10 | ▶ | use constant MIN_X_SHIFT => 86000; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 10 | ▶ | use constant MIN_Y_SHIFT => -82000; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 10 | ▶ | use constant TENTH_MM => 0.0001; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 10 | ▶ | use constant HUNDREDTH_MM => 0.00001; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | # load all the data from below | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 10 | my @ostn_data; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 10 | ▶ | while (<DATA>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 12290 | push @ostn_data, $_; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 10 | close DATA; | |||||||||||||||||||||||||||||||||||||||||
| 65 | # closing the DATA file handle supresses annoying additions to any error messages | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 10 | my %ostn_shifts_for; | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | sub _llh_to_cartesian { | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 29 | ▶ | my ( $lat, $lon, $H, $shape ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_llh_to_cartesian: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 29 | my ( $a, $b, $f, $ee ) = @{ ELLIPSOIDS->{$shape} }; | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 29 | my $phi = $lat / 57.29577951308232087679815481410517; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 29 | my $sp = sin $phi; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 29 | my $cp = cos $phi; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 29 | my $lam = $lon / 57.29577951308232087679815481410517; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 29 | my $sl = sin $lam; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 29 | my $cl = cos $lam; | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 29 | my $nu = $a / sqrt( 1 - $ee * $sp * $sp ); | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 29 | my $x = ( $nu + $H ) * $cp * $cl; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 29 | my $y = ( $nu + $H ) * $cp * $sl; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 29 | my $z = ( ( 1 - $ee ) * $nu + $H ) * $sp; | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 29 | return ( $x, $y, $z ); | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | sub _cartesian_to_llh { | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 29 | ▶ | my ( $x, $y, $z, $shape ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cartesian_to_llh: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 29 | my ( $a, $b, $f, $ee ) = @{ ELLIPSOIDS->{$shape} }; | |||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 29 | my $p = sqrt($x*$x+$y*$y); | |||||||||||||||||||||||||||||||||||||||||
| 96 | 29 | my $lam = atan2 $y, $x; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 29 | my $phi = atan2 $z, $p*(1-$ee); | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 29 | my ( $nu, $oldphi, $sp ); | |||||||||||||||||||||||||||||||||||||||||
| 100 | 29 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 101 | 87 | $sp = sin $phi; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 87 | $nu = $a / sqrt(1 - $ee*$sp*$sp); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 87 | $oldphi = $phi; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 87 | $phi = atan2 $z+$ee*$nu*$sp, $p; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 87 | ▶ | last if abs($oldphi-$phi) < 1E-12; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 29 | my $lat = $phi * 57.29577951308232087679815481410517; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 29 | my $lon = $lam * 57.29577951308232087679815481410517; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 29 | my $H = $p / cos($phi) - $nu; | |||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 29 | return ( $lat, $lon, $H ); | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | sub _small_Helmert_transform_for_OSGB { | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 29 | ▶ | my ($direction, $xa, $ya, $za) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_small_Helmert_transform_for_OSGB: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 29 | my $tx = $direction * -446.448; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 29 | my $ty = $direction * +125.157; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 29 | my $tz = $direction * -542.060; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 29 | my $sp = $direction * 0.0000204894 + 1; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 29 | my $rx = ($direction * -0.1502/3600) / 57.29577951308232087679815481410517; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 29 | my $ry = ($direction * -0.2470/3600) / 57.29577951308232087679815481410517; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 29 | my $rz = ($direction * -0.8421/3600) / 57.29577951308232087679815481410517; | |||||||||||||||||||||||||||||||||||||||||
| 124 | 29 | my $xb = $tx + $sp*$xa - $rz*$ya + $ry*$za; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 29 | my $yb = $ty + $rz*$xa + $sp*$ya - $rx*$za; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 29 | my $zb = $tz - $ry*$xa + $rx*$ya + $sp*$za; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 29 | return ($xb, $yb, $zb); | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | sub _shift_ll_from_osgb36_to_wgs84 { | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 11 | ▶ | my ($lat, $lon) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_shift_ll_from_osgb36_to_wgs84: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 11 | my ($xa, $ya, $za) = _llh_to_cartesian($lat, $lon, 0, 'OSGB36' ); | |||||||||||||||||||||||||||||||||||||||||
| 133 | 11 | my ($xb, $yb, $zb) = _small_Helmert_transform_for_OSGB(-1,$xa, $ya, $za); | |||||||||||||||||||||||||||||||||||||||||
| 134 | 11 | my ($latx, $lonx, $junk) = _cartesian_to_llh($xb, $yb, $zb, 'WGS84'); | |||||||||||||||||||||||||||||||||||||||||
| 135 | 11 | return ($latx, $lonx); | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | sub _shift_ll_from_wgs84_to_osgb36 { | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 18 | ▶ | my ($lat, $lon) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_shift_ll_from_wgs84_to_osgb36: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 18 | my ($xa, $ya, $za) = _llh_to_cartesian($lat, $lon, 0, 'WGS84'); | |||||||||||||||||||||||||||||||||||||||||
| 141 | 18 | my ($xb, $yb, $zb) = _small_Helmert_transform_for_OSGB(+1,$xa, $ya, $za); | |||||||||||||||||||||||||||||||||||||||||
| 142 | 18 | my ($latx, $lonx, $junk) = _cartesian_to_llh($xb, $yb, $zb, 'OSGB36'); | |||||||||||||||||||||||||||||||||||||||||
| 143 | 18 | return ($latx, $lonx); | |||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | sub ll_to_grid { | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 214 | ▶ | my ( $lat, $lon, $options ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ll_to_grid: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | # we might have been passed a hash as the first argument | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 214 | ▶ | if (ref $lat && defined $lat->{lat} && defined $lat->{lon}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
ref $lat and defined $lat->{"lat"} and defined $lat->{"lon"}
ref $lat and defined $lat->{"lat"}
MC/DC33%1/3
ref $lat and defined $lat->{"lat"} and defined $lat->{"lon"}
ref $lat
defined $lat->{"lat"}
defined $lat->{"lon"}
Truth table50%2/4
A and B and C
A= ref $latB= defined $lat->{"lat"}C= defined $lat->{"lon"}
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 1 | $options = $lat; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 1 | $lat = $options->{lat}; | |||||||||||||||||||||||||||||||||||||||||
| 154 | 1 | $lon = $options->{lon}; | |||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | # correct reversed arguments, this is always valid in OSGB area | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 214 | ▶ | if ($lat < $lon) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 1 | ($lat, $lon) = ($lon, $lat) | |||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 214 | ▶ | my $shape = exists $options->{shape} ? $options->{shape} : $default_shape; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 214 | ▶ | croak "Unknown shape: $shape" if !exists ELLIPSOIDS->{$shape}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 214 | my ($e,$n) = _project_onto_grid($lat, $lon, $shape); | |||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 214 | my @out; | |||||||||||||||||||||||||||||||||||||||||
| 168 | # If we were using LL from OS maps, then we are done | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 214 | ▶ | if ($shape eq 'OSGB36') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 12 | @out = map { sprintf '%.3f', $_ } ($e, $n); | |||||||||||||||||||||||||||||||||||||||||
| 171 | 12 | ▶ | return wantarray ? @out : "@out"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | # now shape is WGS84 etc so we must adjust | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 202 | my ($dx, $dy) = _find_OSTN02_shifts_at($e,$n); | |||||||||||||||||||||||||||||||||||||||||
| 176 | 202 | ▶ | if ($dx) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 188 | @out = map { sprintf '%.3f', $_ } ($e + $dx, $n + $dy); | |||||||||||||||||||||||||||||||||||||||||
| 178 | 188 | ▶ | return wantarray ? @out : "@out"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | # still here? Then do Helmert shift into OSGB36 and re-project | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 14 | return ll_to_grid_helmert($lat, $lon) | |||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | sub ll_to_grid_helmert { | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 18 | ▶ | my ($lat, $lon) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ll_to_grid_helmert: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 18 | my @out = map { sprintf '%.0f', $_ } # round to metres | |||||||||||||||||||||||||||||||||||||||||
| 188 | _project_onto_grid( _shift_ll_from_wgs84_to_osgb36($lat, $lon), 'OSGB36' ); | ||||||||||||||||||||||||||||||||||||||||||
| 189 | 18 | ▶ | return wantarray ? @out : "@out"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | sub _project_onto_grid { | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | 232 | ▶ | my ( $lat, $lon, $shape ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_project_onto_grid: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 232 | my ($a,$b,$f,$e2) = @{ ELLIPSOIDS->{$shape} }; | |||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 232 | my $n = ($a-$b)/($a+$b); | |||||||||||||||||||||||||||||||||||||||||
| 199 | 232 | my $af = $a * CONVERGENCE_FACTOR; | |||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | 232 | my $phi = $lat / 57.29577951308232087679815481410517; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 232 | my $lam = $lon / 57.29577951308232087679815481410517; | |||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 232 | my $cp = cos $phi; my $sp = sin $phi; | |||||||||||||||||||||||||||||||||||||||||
| 205 | 232 | my $sp2 = $sp*$sp; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 232 | my $tp = $sp/$cp; # cos phi cannot be zero in GB | |||||||||||||||||||||||||||||||||||||||||
| 207 | 232 | my $tp2 = $tp*$tp; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 232 | my $tp4 = $tp2*$tp2; | |||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | 232 | my $splat = 1 - $e2 * $sp2; | |||||||||||||||||||||||||||||||||||||||||
| 211 | 232 | my $sqrtsplat = sqrt $splat; | |||||||||||||||||||||||||||||||||||||||||
| 212 | 232 | my $nu = $af / $sqrtsplat; | |||||||||||||||||||||||||||||||||||||||||
| 213 | 232 | my $rho = $af * (1 - $e2) / ($splat*$sqrtsplat); | |||||||||||||||||||||||||||||||||||||||||
| 214 | 232 | my $eta2 = $nu/$rho - 1; | |||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | 232 | my $p_plus = $phi + ORIGIN_LATITUDE; | |||||||||||||||||||||||||||||||||||||||||
| 217 | 232 | my $p_minus = $phi - ORIGIN_LATITUDE; | |||||||||||||||||||||||||||||||||||||||||
| 218 | 232 | my $M = $b * CONVERGENCE_FACTOR * ( | |||||||||||||||||||||||||||||||||||||||||
| 219 | (1 + $n * (1 + 5/4*$n*(1 + $n)))*$p_minus | ||||||||||||||||||||||||||||||||||||||||||
| 220 | - 3*$n*(1+$n*(1+7/8*$n)) * sin( $p_minus) * cos( $p_plus) | ||||||||||||||||||||||||||||||||||||||||||
| 221 | + (15/8*$n * ($n*(1+$n))) * sin(2*$p_minus) * cos(2*$p_plus) | ||||||||||||||||||||||||||||||||||||||||||
| 222 | - 35/24*$n**3 * sin(3*$p_minus) * cos(3*$p_plus) | ||||||||||||||||||||||||||||||||||||||||||
| 223 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | 232 | my $I = $M + ORIGIN_NORTHING; | |||||||||||||||||||||||||||||||||||||||||
| 226 | 232 | my $II = $nu/2 * $sp * $cp; | |||||||||||||||||||||||||||||||||||||||||
| 227 | 232 | my $III = $nu/24 * $sp * $cp**3 * (5-$tp2+9*$eta2); | |||||||||||||||||||||||||||||||||||||||||
| 228 | 232 | my $IIIA = $nu/720* $sp * $cp**5 *(61-58*$tp2+$tp4); | |||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | 232 | my $IV = $nu*$cp; | |||||||||||||||||||||||||||||||||||||||||
| 231 | 232 | my $V = $nu/6 * $cp**3 * ($nu/$rho-$tp2); | |||||||||||||||||||||||||||||||||||||||||
| 232 | 232 | my $VI = $nu/120 * $cp**5 * (5-18*$tp2+$tp4+14*$eta2-58*$tp2*$eta2); | |||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 232 | my $dl = $lam - ORIGIN_LONGITUDE; | |||||||||||||||||||||||||||||||||||||||||
| 235 | 232 | my $north = $I + ( $II + ( $III + $IIIA * $dl * $dl ) * $dl * $dl ) * $dl * $dl; | |||||||||||||||||||||||||||||||||||||||||
| 236 | 232 | my $east = ORIGIN_EASTING + ( $IV + ( $V + $VI * $dl * $dl ) * $dl * $dl ) * $dl; | |||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | 232 | return ($east, $north); | |||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | sub _find_OSTN02_shifts_at { | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | 818 | ▶ | my ($x, $y) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_OSTN02_shifts_at: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | 818 | ▶ | return if $x < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 816 | ▶ | return if $y < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 816 | my $e_index = int $x/1000; | |||||||||||||||||||||||||||||||||||||||||
| 249 | 816 | my $n_index = int $y/1000; | |||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 816 | ▶ | return if $n_index+1 > $#ostn_data; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | 814 | my $row_size = 701; | |||||||||||||||||||||||||||||||||||||||||
| 254 | 814 | my $key = $e_index + $row_size * $n_index; | |||||||||||||||||||||||||||||||||||||||||
| 255 | 814 | ▶ | if ( ! exists $ostn_shifts_for{$key} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 297 | $ostn_shifts_for{$key} = _get_ostn_pair_reference($e_index, $n_index); | |||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 814 | ▶ | return if ! $ostn_shifts_for{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | 802 | my ($se0,$sn0,$se1,$sn1) = @{$ostn_shifts_for{$key}}; | |||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | 802 | $key += $row_size; | |||||||||||||||||||||||||||||||||||||||||
| 263 | 802 | ▶ | if ( ! exists $ostn_shifts_for{$key} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 291 | $ostn_shifts_for{$key} = _get_ostn_pair_reference($e_index, $n_index+1); | |||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 802 | ▶ | return if ! $ostn_shifts_for{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | 796 | my ($se2,$sn2,$se3,$sn3) = @{$ostn_shifts_for{$key}}; | |||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | 796 | my $t = $x/1000 - $e_index; # offset within square | |||||||||||||||||||||||||||||||||||||||||
| 271 | 796 | my $u = $y/1000 - $n_index; | |||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | 796 | my $f0 = (1-$t)*(1-$u); | |||||||||||||||||||||||||||||||||||||||||
| 274 | 796 | my $f1 = $t *(1-$u); | |||||||||||||||||||||||||||||||||||||||||
| 275 | 796 | my $f2 = (1-$t)* $u ; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 796 | my $f3 = $t * $u ; | |||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | 796 | my $se = $f0*$se0 + $f1*$se1 + $f2*$se2 + $f3*$se3; | |||||||||||||||||||||||||||||||||||||||||
| 279 | 796 | my $sn = $f0*$sn0 + $f1*$sn1 + $f2*$sn2 + $f3*$sn3; | |||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | 796 | return ($se, $sn); | |||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | sub _d32 { | ||||||||||||||||||||||||||||||||||||||||||
| 285 | 2304 | ▶ | my ($a, $b, $c) = unpack 'CCC', $_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_d32: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | 2304 | return ($a << 10) + ($b << 5) + $c - 50736; | |||||||||||||||||||||||||||||||||||||||||
| 287 | # the magic number here is (48<<10)+(48<<5)+48 to allow for us | ||||||||||||||||||||||||||||||||||||||||||
| 288 | # using a character set based at chr(48) = 0. | ||||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | sub _get_ostn_pair_reference { | ||||||||||||||||||||||||||||||||||||||||||
| 292 | 588 | ▶ | my $x = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_ostn_pair_reference: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | 588 | my $y = shift; | |||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | 588 | my $leading_zeros = substr $ostn_data[$y], 0, 3; | |||||||||||||||||||||||||||||||||||||||||
| 296 | 588 | ▶ | return if $x < $leading_zeros; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | 584 | my $start = 3 + 6 * ( $x - $leading_zeros ); | |||||||||||||||||||||||||||||||||||||||||
| 299 | 584 | ▶ | return if $start + 12 > length $ostn_data[$y]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | 579 | my $data = substr $ostn_data[$y], $start, 12; | |||||||||||||||||||||||||||||||||||||||||
| 302 | 579 | ▶ | return if -1 < index $data, '000'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | 576 | my @shifts = map { _d32 } unpack "A3A3A3A3", $data; | |||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | 576 | $shifts[0] += MIN_X_SHIFT; | |||||||||||||||||||||||||||||||||||||||||
| 307 | 576 | $shifts[1] += MIN_Y_SHIFT; | |||||||||||||||||||||||||||||||||||||||||
| 308 | 576 | $shifts[2] += MIN_X_SHIFT; | |||||||||||||||||||||||||||||||||||||||||
| 309 | 576 | $shifts[3] += MIN_Y_SHIFT; | |||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | 576 | return [ map { $_ / 1000 } @shifts ]; | |||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | sub grid_to_ll { | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | 214 | ▶ | my ($e, $n, $options) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
grid_to_ll: called CC=13 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | 214 | ▶ | if (ref $e && defined $e->{e} && defined $e->{n}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
ref $e and defined $e->{"e"} and defined $e->{"n"}
ref $e and defined $e->{"e"}
MC/DC33%1/3
ref $e and defined $e->{"e"} and defined $e->{"n"}
ref $e
defined $e->{"e"}
defined $e->{"n"}
Truth table50%2/4
A and B and C
A= ref $eB= defined $e->{"e"}C= defined $e->{"n"}
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | 1 | $options = $e; | |||||||||||||||||||||||||||||||||||||||||
| 320 | 1 | $e = $options->{e}; | |||||||||||||||||||||||||||||||||||||||||
| 321 | 1 | $n = $options->{n}; | |||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | 214 | ▶ | my $shape = exists $options->{shape} ? $options->{shape} : $default_shape; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | 214 | ▶ | croak "Unknown shape: $shape" if !exists ELLIPSOIDS->{$shape}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | 214 | my ($os_lat, $os_lon) = _reverse_project_onto_ellipsoid($e, $n, 'OSGB36'); | |||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | # if we want OS map LL we are done | ||||||||||||||||||||||||||||||||||||||||||
| 331 | 214 | ▶ | if ($shape eq 'OSGB36') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | 7 | return ($os_lat, $os_lon) | |||||||||||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | # If we want WGS84 LL, we must adjust to pseudo grid if we can | ||||||||||||||||||||||||||||||||||||||||||
| 336 | 207 | my ($dx, $dy) = _find_OSTN02_shifts_at($e,$n); | |||||||||||||||||||||||||||||||||||||||||
| 337 | 207 | ▶ | if ($dx) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 338 | 200 | my $in_ostn02_polygon = 1; | |||||||||||||||||||||||||||||||||||||||||
| 339 | 200 | my ($x,$y) = ($e-$dx, $n-$dy); | |||||||||||||||||||||||||||||||||||||||||
| 340 | 200 | my ($last_dx, $last_dy) = ($dx, $dy); | |||||||||||||||||||||||||||||||||||||||||
| 341 | APPROX: | ||||||||||||||||||||||||||||||||||||||||||
| 342 | 200 | for (1..20) { | |||||||||||||||||||||||||||||||||||||||||
| 343 | 399 | ($dx, $dy) = _find_OSTN02_shifts_at($x,$y); | |||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | 399 | ▶ | if (!$dx) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 346 | # we have been shifted off the edge | ||||||||||||||||||||||||||||||||||||||||||
| 347 | 1 | $in_ostn02_polygon = 0; | |||||||||||||||||||||||||||||||||||||||||
| 348 | last APPROX | ||||||||||||||||||||||||||||||||||||||||||
| 349 | 1 | } | |||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | 398 | ($x,$y) = ($e-$dx, $n-$dy); | |||||||||||||||||||||||||||||||||||||||||
| 352 | 398 | ▶ | last APPROX if abs($dx-$last_dx) < TENTH_MM | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
abs $dx - $last_dx < "0.0001" and abs $dy - $last_dy < "0.0001"
MC/DC100%2/2
abs $dx - $last_dx < "0.0001" and abs $dy - $last_dy < "0.0001"
abs $dx - $last_dx < "0.0001"
abs $dy - $last_dy < "0.0001"
Truth table100%3/3
A and B
A= abs $dx - $last_dx < "0.0001"B= abs $dy - $last_dy < "0.0001"
| |||||||||||||||||||||||||||||||||||||||||||
| 353 | && abs($dy-$last_dy) < TENTH_MM; | ||||||||||||||||||||||||||||||||||||||||||
| 354 | 199 | ($last_dx, $last_dy) = ($dx, $dy); | |||||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||||||
| 356 | 200 | ▶ | if ($in_ostn02_polygon ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 357 | 199 | return _reverse_project_onto_ellipsoid($e-$dx, $n-$dy, 'WGS84') | |||||||||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | # If we get here, we must use the Helmert approx | ||||||||||||||||||||||||||||||||||||||||||
| 362 | 8 | return _shift_ll_from_osgb36_to_wgs84($os_lat, $os_lon) | |||||||||||||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | sub grid_to_ll_helmert { | ||||||||||||||||||||||||||||||||||||||||||
| 366 | 3 | ▶ | my ($e, $n) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
grid_to_ll_helmert: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 367 | 3 | my ($os_lat, $os_lon) = _reverse_project_onto_ellipsoid($e, $n, 'OSGB36'); | |||||||||||||||||||||||||||||||||||||||||
| 368 | 3 | return _shift_ll_from_osgb36_to_wgs84($os_lat, $os_lon) | |||||||||||||||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | sub _reverse_project_onto_ellipsoid { | ||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | 416 | ▶ | my ( $easting, $northing, $shape ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reverse_project_onto_ellipsoid: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | 416 | my ( $a, $b, $f, $e2 ) = @{ ELLIPSOIDS->{$shape} }; | |||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | 416 | my $n = ( $a - $b ) / ( $a + $b ); | |||||||||||||||||||||||||||||||||||||||||
| 378 | 416 | my $af = $a * CONVERGENCE_FACTOR; | |||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | 416 | my $dn = $northing - ORIGIN_NORTHING; | |||||||||||||||||||||||||||||||||||||||||
| 381 | 416 | my $de = $easting - ORIGIN_EASTING; | |||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||
| 383 | 416 | my $phi = ORIGIN_LATITUDE + $dn/$af; | |||||||||||||||||||||||||||||||||||||||||
| 384 | 416 | my $lam = ORIGIN_LONGITUDE; | |||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | 416 | my ($M, $p_plus, $p_minus); | |||||||||||||||||||||||||||||||||||||||||
| 387 | 416 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 388 | 1504 | $p_plus = $phi + ORIGIN_LATITUDE; | |||||||||||||||||||||||||||||||||||||||||
| 389 | 1504 | $p_minus = $phi - ORIGIN_LATITUDE; | |||||||||||||||||||||||||||||||||||||||||
| 390 | 1504 | $M = $b * CONVERGENCE_FACTOR * ( | |||||||||||||||||||||||||||||||||||||||||
| 391 | (1 + $n * (1 + 5/4*$n*(1 + $n)))*$p_minus | ||||||||||||||||||||||||||||||||||||||||||
| 392 | - 3*$n*(1+$n*(1+7/8*$n)) * sin( $p_minus) * cos( $p_plus) | ||||||||||||||||||||||||||||||||||||||||||
| 393 | + (15/8*$n * ($n*(1+$n))) * sin(2*$p_minus) * cos(2*$p_plus) | ||||||||||||||||||||||||||||||||||||||||||
| 394 | - 35/24*$n**3 * sin(3*$p_minus) * cos(3*$p_plus) | ||||||||||||||||||||||||||||||||||||||||||
| 395 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 396 | 1504 | ▶ | last if abs($dn-$M) < HUNDREDTH_MM; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 397 | 1088 | $phi = $phi + ($dn-$M)/$af; | |||||||||||||||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||
| 400 | 416 | my $cp = cos $phi; | |||||||||||||||||||||||||||||||||||||||||
| 401 | 416 | my $sp = sin $phi; | |||||||||||||||||||||||||||||||||||||||||
| 402 | 416 | my $tp = $sp / $cp; # cos phi cannot be zero in GB | |||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | 416 | my $splat = 1 - $e2 * $sp * $sp; | |||||||||||||||||||||||||||||||||||||||||
| 405 | 416 | my $sqrtsplat = sqrt $splat; | |||||||||||||||||||||||||||||||||||||||||
| 406 | 416 | my $nu = $af / $sqrtsplat; | |||||||||||||||||||||||||||||||||||||||||
| 407 | 416 | my $rho = $af * (1 - $e2) / ( $splat * $sqrtsplat ); | |||||||||||||||||||||||||||||||||||||||||
| 408 | 416 | my $eta2 = $nu / $rho - 1; | |||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | 416 | my $VII = $tp / (2 * $rho * $nu); | |||||||||||||||||||||||||||||||||||||||||
| 411 | 416 | my $VIII = $tp / (24 * $rho * $nu**3) * (5 + $eta2 + ( 3 - 9 * $eta2 ) * $tp * $tp ); | |||||||||||||||||||||||||||||||||||||||||
| 412 | 416 | my $IX = $tp / (720 * $rho * $nu**5) * (61 + ( 90 + 45 * $tp * $tp ) * $tp * $tp ); | |||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||
| 414 | 416 | my $secp = 1/$cp; | |||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||
| 416 | 416 | my $X = $secp / $nu; | |||||||||||||||||||||||||||||||||||||||||
| 417 | 416 | my $XI = $secp / ( 6 * $nu**3 ) * ( $nu / $rho + 2 * $tp * $tp ); | |||||||||||||||||||||||||||||||||||||||||
| 418 | 416 | my $XII = $secp / ( 120 * $nu**5 ) * ( 5 + ( 28 + 24 * $tp * $tp ) * $tp * $tp ); | |||||||||||||||||||||||||||||||||||||||||
| 419 | 416 | my $XIIA = $secp / ( 5040 * $nu**7 ) * ( 61 + ( 662 + ( 1320 + 720 * $tp * $tp ) * $tp * $tp ) * $tp * $tp ); | |||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||
| 421 | 416 | $phi = $phi + ( -$VII + ( $VIII - $IX * $de * $de ) * $de * $de) * $de * $de; | |||||||||||||||||||||||||||||||||||||||||
| 422 | 416 | $lam = $lam + ( $X + ( -$XI + ( $XII - $XIIA * $de * $de ) * $de * $de) * $de * $de) * $de; | |||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | # now put into degrees & return | ||||||||||||||||||||||||||||||||||||||||||
| 425 | 416 | return ($phi * 57.29577951308232087679815481410517, | |||||||||||||||||||||||||||||||||||||||||
| 426 | $lam * 57.29577951308232087679815481410517); | ||||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||
| 431 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | Geo::Coordinates::OSGB - Convert coordinates between Lat/Lon and the British National Grid | ||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | An implementation of co-ordinate conversion for England, Wales, and Scotland | ||||||||||||||||||||||||||||||||||||||||||
| 438 | based on formulae and data published by the Ordnance Survey of Great Britain. | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | 2.19 | ||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||
| 444 | =for HTML <a href="https://travis-ci.org/thruston/perl-geo-coordinates-osgb"> | ||||||||||||||||||||||||||||||||||||||||||
| 445 | <img src="https://travis-ci.org/thruston/perl-geo-coordinates-osgb.svg?branch=master"></a> | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | use Geo::Coordinates::OSGB qw(ll_to_grid grid_to_ll); | ||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | ($easting,$northing) = ll_to_grid($lat,$lon); | ||||||||||||||||||||||||||||||||||||||||||
| 452 | ($lat,$lon) = grid_to_ll($easting,$northing); | ||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||
| 454 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | These modules convert accurately between an OSGB national grid reference and | ||||||||||||||||||||||||||||||||||||||||||
| 457 | coordinates given in latitude and longitude. | ||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | In Version 2.10 and above, the default ellipsoid model used is the I<de facto> | ||||||||||||||||||||||||||||||||||||||||||
| 460 | international standard WGS84. This means that you can take latitude and | ||||||||||||||||||||||||||||||||||||||||||
| 461 | longitude readings from your GPS receiver, or read them from Wikipedia, or | ||||||||||||||||||||||||||||||||||||||||||
| 462 | Google Earth, or your car's sat-nav, and use this module to convert them to | ||||||||||||||||||||||||||||||||||||||||||
| 463 | accurate British National grid references for use with one of the Ordnance | ||||||||||||||||||||||||||||||||||||||||||
| 464 | Survey's paper maps. And I<vice versa>, of course. | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | The module is implemented purely in Perl, and should run on any platform with | ||||||||||||||||||||||||||||||||||||||||||
| 467 | Perl version 5.8 or better. | ||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | In this description, the abbreviations `OS' and `OSGB' mean `the Ordnance | ||||||||||||||||||||||||||||||||||||||||||
| 470 | Survey of Great Britain': the British government agency that produces the | ||||||||||||||||||||||||||||||||||||||||||
| 471 | standard maps of England, Wales, and Scotland. Any mention of `sheets' or | ||||||||||||||||||||||||||||||||||||||||||
| 472 | `maps' refers to one or more of the map sheets defined in the accompanying maps | ||||||||||||||||||||||||||||||||||||||||||
| 473 | module. | ||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||
| 475 | This code is written for the British national grid system. It is of no use | ||||||||||||||||||||||||||||||||||||||||||
| 476 | outside Britain. In fact it's only really useful in the areas covered by the | ||||||||||||||||||||||||||||||||||||||||||
| 477 | OS's main series of maps, which exclude the Channel Islands and Northern | ||||||||||||||||||||||||||||||||||||||||||
| 478 | Ireland. | ||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||
| 482 | The following functions can be exported from the | ||||||||||||||||||||||||||||||||||||||||||
| 483 | C<Geo::Coordinates::OSGB> module: | ||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||
| 485 | grid_to_ll ll_to_grid | ||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | Neither of these is exported by default. | ||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | =head2 Main subroutines | ||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||
| 491 | =head3 C<ll_to_grid(lat,lon)> | ||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||
| 493 | C<ll_to_grid> translates a latitude and longitude pair into a grid | ||||||||||||||||||||||||||||||||||||||||||
| 494 | easting and northing pair. | ||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | When called in a list context, C<ll_to_grid> returns the easting and | ||||||||||||||||||||||||||||||||||||||||||
| 497 | northing as a list of two. When called in a scalar context, it returns | ||||||||||||||||||||||||||||||||||||||||||
| 498 | a single string with the numbers separated by spaces. | ||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||
| 500 | The arguments should be supplied as real numbers representing | ||||||||||||||||||||||||||||||||||||||||||
| 501 | decimal degrees, like this | ||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||
| 503 | my ($e,$n) = ll_to_grid(51.5, -2.1); # (393154.801, 177900.605) | ||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||
| 505 | Following the normal mathematical convention, positive arguments mean North or | ||||||||||||||||||||||||||||||||||||||||||
| 506 | East, negative South or West. | ||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||
| 508 | If you have data with degrees, minutes and seconds, you can convert them | ||||||||||||||||||||||||||||||||||||||||||
| 509 | to decimals like this: | ||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | my ($e,$n) = ll_to_grid(51+25/60, 0-5/60-2/3600); | ||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||
| 513 | If you have trouble remembering the order of the arguments, or the returned | ||||||||||||||||||||||||||||||||||||||||||
| 514 | values, note that latitude comes before longitude in the alphabet too, as | ||||||||||||||||||||||||||||||||||||||||||
| 515 | easting comes before northing. However, since reasonable latitudes for the | ||||||||||||||||||||||||||||||||||||||||||
| 516 | OSGB are in the range 49 to 61, and reasonable longitudes in the range -9 to | ||||||||||||||||||||||||||||||||||||||||||
| 517 | +2, C<ll_to_grid> accepts the arguments in either order; if your longitude is | ||||||||||||||||||||||||||||||||||||||||||
| 518 | larger than your latitude, then the values of the arguments will be silently | ||||||||||||||||||||||||||||||||||||||||||
| 519 | swapped. | ||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||
| 521 | You can also supply the arguments as named keywords (but be sure to use | ||||||||||||||||||||||||||||||||||||||||||
| 522 | the curly braces so that you pass them as a reference): | ||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||
| 524 | my ($e,$n) = ll_to_grid( { lat => 51.5, lon => -2.1 } ); | ||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||
| 526 | The easting and northing will be returned as the orthogonal distances in metres | ||||||||||||||||||||||||||||||||||||||||||
| 527 | from the `false point of origin' of the British Grid (which is a point some way | ||||||||||||||||||||||||||||||||||||||||||
| 528 | to the south-west of the Scilly Isles). The returned pair refers to a point on | ||||||||||||||||||||||||||||||||||||||||||
| 529 | the usual OSGB grid, which extends from the Scilly Isles in the south west to | ||||||||||||||||||||||||||||||||||||||||||
| 530 | the Shetlands in the north. | ||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||
| 532 | my ($e,$n) = ll_to_grid(51.5, -2.1); # (393154.801, 177900.605) | ||||||||||||||||||||||||||||||||||||||||||
| 533 | my $s = ll_to_grid(51.5, -2.1); # "393154.801 177900.605" | ||||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||||
| 535 | If the coordinates you supply are in the area covered by the OSTN02 | ||||||||||||||||||||||||||||||||||||||||||
| 536 | transformation data, then the results will be rounded to 3 decimal places, | ||||||||||||||||||||||||||||||||||||||||||
| 537 | which corresponds to the nearest millimetre. If they are outside the coverage | ||||||||||||||||||||||||||||||||||||||||||
| 538 | (which normally means more than a few km off shore) then the conversion is | ||||||||||||||||||||||||||||||||||||||||||
| 539 | automagically done using a Helmert transformation instead of the OSTN02 data. | ||||||||||||||||||||||||||||||||||||||||||
| 540 | The results will be rounded to the nearest metre in this case, although you | ||||||||||||||||||||||||||||||||||||||||||
| 541 | probably should not rely on the results being more accurate than about 5m. | ||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||
| 543 | # A point in the sea, to the north-west of Coll | ||||||||||||||||||||||||||||||||||||||||||
| 544 | my $s = ll_to_grid(56.75,-7); # returns "94471 773206" | ||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||
| 546 | The numbers returned may be negative if your latitude and longitude are | ||||||||||||||||||||||||||||||||||||||||||
| 547 | far enough south and west, but beware that the transformation is less | ||||||||||||||||||||||||||||||||||||||||||
| 548 | and less accurate or useful the further you get from the British Isles. | ||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||
| 550 | If you want the result presented in a more traditional grid reference | ||||||||||||||||||||||||||||||||||||||||||
| 551 | format you should pass the results to one of the grid formatting | ||||||||||||||||||||||||||||||||||||||||||
| 552 | routines from L<Grid.pm|Geo::Coordinates::OSGB::Grid>. Like this. | ||||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||
| 554 | my $s = ll_to_grid(51.5, -2.1); # "393154.801 177900.605" | ||||||||||||||||||||||||||||||||||||||||||
| 555 | $s = format_grid(ll_to_grid(51.5,-2.1)); # "ST 931 779" | ||||||||||||||||||||||||||||||||||||||||||
| 556 | $s = format_grid_GPS(ll_to_grid(51.5,-2.1)); # "ST 93154 77900" | ||||||||||||||||||||||||||||||||||||||||||
| 557 | $s = format_grid_map(ll_to_grid(51.5,-2.1)); # "ST 931 779 on A:173, B:156, C:157" | ||||||||||||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||||||||||
| 559 | C<ll_to_grid()> also takes an optional argument that sets the ellipsoid | ||||||||||||||||||||||||||||||||||||||||||
| 560 | model to use. This defaults to `WGS84', the name of the normal model | ||||||||||||||||||||||||||||||||||||||||||
| 561 | for working with normal GPS coordinates, but if you want to work with | ||||||||||||||||||||||||||||||||||||||||||
| 562 | the traditional latitude and longitude values printed around the edges of | ||||||||||||||||||||||||||||||||||||||||||
| 563 | OS maps before 2015 then you should add an optional shape parameter like this: | ||||||||||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||||||||||
| 565 | my ($e, $n) = ll_to_grid(49,-2, {shape => 'OSGB36'}); | ||||||||||||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||||||||||||
| 567 | Incidentally, if you make this call above you will get back | ||||||||||||||||||||||||||||||||||||||||||
| 568 | (400000,-100000) which are the coordinates of the `true point of origin' | ||||||||||||||||||||||||||||||||||||||||||
| 569 | of the British grid. You should get back an easting of 400000 for any | ||||||||||||||||||||||||||||||||||||||||||
| 570 | point with longitude 2W since this is the central meridian used for the | ||||||||||||||||||||||||||||||||||||||||||
| 571 | OSGB projection. However you will get a slightly different value unless | ||||||||||||||||||||||||||||||||||||||||||
| 572 | you specify C<< {shape => 'OSGB36'} >> because the WGS84 meridians are not | ||||||||||||||||||||||||||||||||||||||||||
| 573 | quite the same as OSGB36. | ||||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||||
| 575 | =head3 C<grid_to_ll(e,n)> | ||||||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||||||
| 577 | The routine C<grid_to_ll()> takes an easting and northing pair | ||||||||||||||||||||||||||||||||||||||||||
| 578 | representing the distance in metres from the `false point of origin' of | ||||||||||||||||||||||||||||||||||||||||||
| 579 | the OSGB grid and returns a pair of real numbers representing the | ||||||||||||||||||||||||||||||||||||||||||
| 580 | equivalent longitude and latitude coordinates in the WGS84 model. | ||||||||||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||||||||||
| 582 | Following convention, positive results are North of the equator and East | ||||||||||||||||||||||||||||||||||||||||||
| 583 | of the prime meridian, negative numbers are South and West. The | ||||||||||||||||||||||||||||||||||||||||||
| 584 | fractional parts of the results represent decimal fractions of degrees. | ||||||||||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||||||||||
| 586 | No special processing is done in scalar context because there is no | ||||||||||||||||||||||||||||||||||||||||||
| 587 | obvious assumption about how to round the results. You will just get | ||||||||||||||||||||||||||||||||||||||||||
| 588 | the length of the list returned, which is 2. | ||||||||||||||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||||||||||||
| 590 | The arguments must be an (easting, northing) pair representing the | ||||||||||||||||||||||||||||||||||||||||||
| 591 | absolute grid reference in metres from the point of origin. You can get | ||||||||||||||||||||||||||||||||||||||||||
| 592 | these from a traditional grid reference string by calling | ||||||||||||||||||||||||||||||||||||||||||
| 593 | C<parse_grid()> first. | ||||||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||||||
| 595 | my ($lat, $lon) = grid_to_ll(parse_grid('SM 349 231')) | ||||||||||||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||||||||||||
| 597 | An optional last argument defines the ellipsoid model to use just as it | ||||||||||||||||||||||||||||||||||||||||||
| 598 | does for C<ll_to_grid()>. This is only necessary is you are working | ||||||||||||||||||||||||||||||||||||||||||
| 599 | with an ellipsoid model other than WGS84. Pass the argument as a hash | ||||||||||||||||||||||||||||||||||||||||||
| 600 | ref with a `shape' key. | ||||||||||||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||||||||||||
| 602 | my ($lat, $lon) = grid_to_ll(400000, 300000, {shape => 'OSGB36'}); | ||||||||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||||||||
| 604 | If you like named arguments then you can use a single hash ref for all | ||||||||||||||||||||||||||||||||||||||||||
| 605 | of them (this is strictly optional): | ||||||||||||||||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||||||||||||||||
| 607 | my ($lat, $lon) = grid_to_ll({ e => 400000, n => 300000, shape => 'OSGB36'}); | ||||||||||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||||||||||
| 609 | The results returned will be floating point numbers with the default | ||||||||||||||||||||||||||||||||||||||||||
| 610 | Perl precision. Unless you are running with long double precision | ||||||||||||||||||||||||||||||||||||||||||
| 611 | floats you will get 13 decimal places for latitude and 14 places for | ||||||||||||||||||||||||||||||||||||||||||
| 612 | longitude; but this does not mean that the calculations are accurate to | ||||||||||||||||||||||||||||||||||||||||||
| 613 | that many places. The OS online conversion tools return decimal degrees | ||||||||||||||||||||||||||||||||||||||||||
| 614 | to only 6 places. A difference of 1 in the sixth decimal place | ||||||||||||||||||||||||||||||||||||||||||
| 615 | represents a distance on the ground of about 10 cm. This is probably a | ||||||||||||||||||||||||||||||||||||||||||
| 616 | good rule of thumb for the reliability of these calculations, but all | ||||||||||||||||||||||||||||||||||||||||||
| 617 | the available decimal places are returned so that you can choose the | ||||||||||||||||||||||||||||||||||||||||||
| 618 | rounding that is appropriate for your application. Here's one way to do | ||||||||||||||||||||||||||||||||||||||||||
| 619 | that: | ||||||||||||||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||||||||||||
| 621 | my ($lat, $lon) = map { sprintf "%.6f", $_ } grid_to_ll(431234, 312653); | ||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||||||||||||
| 624 | =head2 Additional subroutines | ||||||||||||||||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||||||||||||||||
| 626 | =head3 C<set_default_shape(shape)> | ||||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||
| 628 | The default ellipsoid shape used for conversion to and from latitude and | ||||||||||||||||||||||||||||||||||||||||||
| 629 | longitude is `WGS84' as used in the international GPS system. This | ||||||||||||||||||||||||||||||||||||||||||
| 630 | default it set every time that you load the module. If you want to | ||||||||||||||||||||||||||||||||||||||||||
| 631 | process or produce a large number latitude and longitude coordinates in | ||||||||||||||||||||||||||||||||||||||||||
| 632 | the British Ordnance Survey system (as printed round the edges of OS | ||||||||||||||||||||||||||||||||||||||||||
| 633 | Landranger and Explorer maps before 2015) you can use C<< set_default_shape('OSGB36'); >> to | ||||||||||||||||||||||||||||||||||||||||||
| 634 | set the default shape to OSGB36. This saves you having to add C<< { | ||||||||||||||||||||||||||||||||||||||||||
| 635 | shape => 'OSGB36' } >> to every call of C<ll_to_grid> or C<grid_to_ll>. | ||||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||||
| 637 | You can use C<< set_default_shape('WGS84'); >> to set the default shape back | ||||||||||||||||||||||||||||||||||||||||||
| 638 | to WGS84 again when finished with OSGB36 coordinates. | ||||||||||||||||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||||||||||||||||
| 640 | =head3 C<ll_to_grid_helmert(lat, lon)> | ||||||||||||||||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||||||||||||||||
| 642 | You can use this function to do a quicker conversion from WGS84 lat/lon | ||||||||||||||||||||||||||||||||||||||||||
| 643 | to the OS grid without using the whole OSTN02 data set. The algorithm | ||||||||||||||||||||||||||||||||||||||||||
| 644 | used is known as a Helmert transformation. This is the usual coordinate | ||||||||||||||||||||||||||||||||||||||||||
| 645 | conversion algorithm implemented in most consumer-level GPS devices. It | ||||||||||||||||||||||||||||||||||||||||||
| 646 | is based on parameters supplied by the OS; they suggest that in most of | ||||||||||||||||||||||||||||||||||||||||||
| 647 | the UK this conversion is accurate to within about 5m. | ||||||||||||||||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||||||||||||||||
| 649 | my ($e, $n) = ll_to_grid_helmert(51.477811, -0.001475); # RO Greenwich | ||||||||||||||||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||||||||||||||||
| 651 | The input must be decimal degrees in the WGS84 model, with latitude | ||||||||||||||||||||||||||||||||||||||||||
| 652 | first and longitude second. The results are rounded to the nearest | ||||||||||||||||||||||||||||||||||||||||||
| 653 | whole metre. They can be used with C<format_grid> in the same way as | ||||||||||||||||||||||||||||||||||||||||||
| 654 | the results from C<ll_to_grid>. | ||||||||||||||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||||||||||||||
| 656 | This function is called automatically by C<ll_to_grid> if your | ||||||||||||||||||||||||||||||||||||||||||
| 657 | coordinates are WGS84 and lie outside the OSTN02 polygon. | ||||||||||||||||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||||||||||||||||
| 659 | =head3 C<grid_to_ll_helmert(e,n)> | ||||||||||||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||||||||||||
| 661 | You can use this function to do a quicker conversion from OS grid | ||||||||||||||||||||||||||||||||||||||||||
| 662 | references to WGS84 latitude and longitude coordinates without using the | ||||||||||||||||||||||||||||||||||||||||||
| 663 | whole OSTN02 data set. The algorithm used is known as a Helmert | ||||||||||||||||||||||||||||||||||||||||||
| 664 | transformation. This is the usual coordinate conversion algorithm | ||||||||||||||||||||||||||||||||||||||||||
| 665 | implemented in most consumer-level GPS devices. It is based on | ||||||||||||||||||||||||||||||||||||||||||
| 666 | parameters supplied by the OS; they suggest that in most of the UK this | ||||||||||||||||||||||||||||||||||||||||||
| 667 | conversion is accurate to within about 5m. | ||||||||||||||||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||||||||||||||||
| 669 | my ($lat, $lon) = grid_to_ll_helmert(538885, 177322); | ||||||||||||||||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||||||||||||||||
| 671 | The input must be in metres from false point of origin (as produced by | ||||||||||||||||||||||||||||||||||||||||||
| 672 | C<parse_grid>) and the results are in decimal degrees using the WGS84 | ||||||||||||||||||||||||||||||||||||||||||
| 673 | model. | ||||||||||||||||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||||||||||||||||
| 675 | The results are returned with the full Perl precision in the same way as | ||||||||||||||||||||||||||||||||||||||||||
| 676 | C<grid_to_ll> so that you can choose an appropriate rounding for your | ||||||||||||||||||||||||||||||||||||||||||
| 677 | needs. Four or five decimal places is probably appropriate in most | ||||||||||||||||||||||||||||||||||||||||||
| 678 | cases. This represents somewhere between 1 and 10 m on the ground. | ||||||||||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||||||||||
| 680 | This function is called automatically by C<grid_to_ll> if the grid | ||||||||||||||||||||||||||||||||||||||||||
| 681 | reference you supply lies outside the OSTN02 polygon. (Generally such | ||||||||||||||||||||||||||||||||||||||||||
| 682 | spots are in the sea). The results are only reliable close to mainland | ||||||||||||||||||||||||||||||||||||||||||
| 683 | Britain. | ||||||||||||||||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||||||||||||||||
| 685 | =head3 C<get_ostn02_shift_pair(e,n)> | ||||||||||||||||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||||||||||||||||
| 687 | This function provides an interface to the OSTN02 data. The input is an | ||||||||||||||||||||||||||||||||||||||||||
| 688 | easting, northing pair (in metres as usual), and the usual output is another | ||||||||||||||||||||||||||||||||||||||||||
| 689 | pair, also in metres, representing the easting and northing differences between | ||||||||||||||||||||||||||||||||||||||||||
| 690 | OSGB36 and WGS84 at this particular grid reference. The values returned are | ||||||||||||||||||||||||||||||||||||||||||
| 691 | the result of interpolation from the OSTN02 values at the four corners of the | ||||||||||||||||||||||||||||||||||||||||||
| 692 | relevant square kilometre. For more detail about OSTN02, see Background.pod. | ||||||||||||||||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||||||||||||||||
| 694 | These values are those used to do the accurate conversion at the heart of this | ||||||||||||||||||||||||||||||||||||||||||
| 695 | module. You don't normally need these, but this interface is provided (a) for | ||||||||||||||||||||||||||||||||||||||||||
| 696 | debugging and testing and (b) as a way to check whether a point is covered by | ||||||||||||||||||||||||||||||||||||||||||
| 697 | one of the OSTN02 polygons. | ||||||||||||||||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||||||||||||||||
| 699 | If a particular point is not in one of the polygons, this function will return | ||||||||||||||||||||||||||||||||||||||||||
| 700 | C<undef>. This feature is used by the by C<random_grid> function in | ||||||||||||||||||||||||||||||||||||||||||
| 701 | L<Geo::Coordinates::OSGB::Grid.pm> to make sure that the chosen random point is | ||||||||||||||||||||||||||||||||||||||||||
| 702 | well within the OSTN02 boundary. | ||||||||||||||||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||||||||||||||||
| 704 | If you are curious to see the boundaries, you should explore the | ||||||||||||||||||||||||||||||||||||||||||
| 705 | C<plot_maps.pl> script in the C<examples> directory. | ||||||||||||||||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||||||||||||||||
| 707 | =head3 Importing all the functions | ||||||||||||||||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||||||||||||||||
| 709 | You can import all the functions defined in C<OSGB.pm> with an C<:all> tag. | ||||||||||||||||||||||||||||||||||||||||||
| 710 | |||||||||||||||||||||||||||||||||||||||||||
| 711 | use Geo::Coordinates::OSGB ':all'; | ||||||||||||||||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||||||||||||||||
| 713 | =head1 EXAMPLES | ||||||||||||||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||||||||||||
| 715 | use Geo::Coordinates::OSGB qw/ll_to_grid grid_to_ll/; | ||||||||||||||||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||||||||||||||||
| 717 | # Latitude and longitude according to the WGS84 model | ||||||||||||||||||||||||||||||||||||||||||
| 718 | ($lat, $lon) = grid_to_ll($e, $n); | ||||||||||||||||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||||||||||||||||
| 720 | # and to go the other way | ||||||||||||||||||||||||||||||||||||||||||
| 721 | ($e, $n) = ll_to_grid($lat,$lon); | ||||||||||||||||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||||||||||||||||
| 723 | See the test files for more examples of usage. | ||||||||||||||||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||||||||||||||||
| 725 | =head1 BUGS AND LIMITATIONS | ||||||||||||||||||||||||||||||||||||||||||
| 726 | |||||||||||||||||||||||||||||||||||||||||||
| 727 | The formulae supplied by the OS and used for the conversion routines are | ||||||||||||||||||||||||||||||||||||||||||
| 728 | specifically designed to be close floating-point approximations rather | ||||||||||||||||||||||||||||||||||||||||||
| 729 | than exact mathematical equivalences. So after round-trips like these: | ||||||||||||||||||||||||||||||||||||||||||
| 730 | |||||||||||||||||||||||||||||||||||||||||||
| 731 | ($lat1,$lon1) = grid_to_ll(ll_to_grid($lat0,$lon0)); | ||||||||||||||||||||||||||||||||||||||||||
| 732 | ($e1,$n1) = ll_to_grid(grid_to_ll($e0,$n0)); | ||||||||||||||||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||||||||||||||||
| 734 | neither C<$lat1 == $lat0> nor C<$lon1 == $lon0> nor C<$e1 == $e0> nor | ||||||||||||||||||||||||||||||||||||||||||
| 735 | C<$n1 == $n0> exactly. However the differences should be very small. | ||||||||||||||||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||||||||||||||||
| 737 | The OS formulae were designed to give an accuracy of about 1 mm of | ||||||||||||||||||||||||||||||||||||||||||
| 738 | error. This means that you can rely on the third decimal place for grid | ||||||||||||||||||||||||||||||||||||||||||
| 739 | references and about the seventh or eighth for latitude and longitude | ||||||||||||||||||||||||||||||||||||||||||
| 740 | (although the OS themselves only provide six decimal places in their | ||||||||||||||||||||||||||||||||||||||||||
| 741 | results). | ||||||||||||||||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||||||||||||||||
| 743 | For all of England, Wales, Scotland, and the Isle of Man the error will | ||||||||||||||||||||||||||||||||||||||||||
| 744 | be tiny. All other areas, like Northern Ireland, the Channel Islands or | ||||||||||||||||||||||||||||||||||||||||||
| 745 | Rockall, and any areas of sea more than a few miles off shore, are | ||||||||||||||||||||||||||||||||||||||||||
| 746 | outside the coverage of OSTN02, so the simpler, less accurate | ||||||||||||||||||||||||||||||||||||||||||
| 747 | transformation is used. The OS state that this is accurate to about 5m | ||||||||||||||||||||||||||||||||||||||||||
| 748 | but that the parameters used are only valid in the reasonably close | ||||||||||||||||||||||||||||||||||||||||||
| 749 | vicinity of the British Isles. | ||||||||||||||||||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||||||||||||||||
| 751 | Not enough testing has been done. I am always grateful for the feedback | ||||||||||||||||||||||||||||||||||||||||||
| 752 | I get from users, but especially for problem reports that help me to | ||||||||||||||||||||||||||||||||||||||||||
| 753 | make this a better module. | ||||||||||||||||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||||||||||||||||
| 755 | =head1 DIAGNOSTICS | ||||||||||||||||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||||||||||||||||
| 757 | The only error message you will get from this module is about the | ||||||||||||||||||||||||||||||||||||||||||
| 758 | ellipsoid shape used for the transformation. If you try to set C<< | ||||||||||||||||||||||||||||||||||||||||||
| 759 | {shape => 'blah'} >> the module will croak with a message saying | ||||||||||||||||||||||||||||||||||||||||||
| 760 | C<Unknown shape: blah>. The shape should be one of the shapes defined: | ||||||||||||||||||||||||||||||||||||||||||
| 761 | WGS84 or OSGB36. | ||||||||||||||||||||||||||||||||||||||||||
| 762 | |||||||||||||||||||||||||||||||||||||||||||
| 763 | Should this software not do what you expect, then please first read this | ||||||||||||||||||||||||||||||||||||||||||
| 764 | documentation, secondly verify that you have installed it correctly and | ||||||||||||||||||||||||||||||||||||||||||
| 765 | that it passes all the installation tests on your set up, thirdly study | ||||||||||||||||||||||||||||||||||||||||||
| 766 | the source code to see what it's supposed to be doing, fourthly get in | ||||||||||||||||||||||||||||||||||||||||||
| 767 | touch to ask me about it. | ||||||||||||||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||||||||||||
| 769 | =head1 CONFIGURATION AND ENVIRONMENT | ||||||||||||||||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||||||||||||||||
| 771 | There is no configuration required either of these modules or your | ||||||||||||||||||||||||||||||||||||||||||
| 772 | environment. It should work on any recent version of Perl, on any | ||||||||||||||||||||||||||||||||||||||||||
| 773 | platform. | ||||||||||||||||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||||||||||||||||
| 775 | =head1 DEPENDENCIES | ||||||||||||||||||||||||||||||||||||||||||
| 776 | |||||||||||||||||||||||||||||||||||||||||||
| 777 | Perl 5.08 or better. | ||||||||||||||||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||||||||||||||||
| 779 | =head1 INCOMPATIBILITIES | ||||||||||||||||||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||||||||||||||||
| 781 | None known. | ||||||||||||||||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||||||||||||||||
| 783 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 784 | |||||||||||||||||||||||||||||||||||||||||||
| 785 | Copyright (C) 2002-2017 Toby Thurston | ||||||||||||||||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||||||||||||||||
| 787 | OSTN02 transformation data included in this module is freely available | ||||||||||||||||||||||||||||||||||||||||||
| 788 | from the Ordnance Survey but remains Crown Copyright (C) 2002 | ||||||||||||||||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||||||||||||||||
| 790 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 791 | under the terms of the GNU General Public License as published by the | ||||||||||||||||||||||||||||||||||||||||||
| 792 | Free Software Foundation; either version 2 of the License, or (at your | ||||||||||||||||||||||||||||||||||||||||||
| 793 | option) any later version. | ||||||||||||||||||||||||||||||||||||||||||
| 794 | |||||||||||||||||||||||||||||||||||||||||||
| 795 | This program is distributed in the hope that it will be useful, but | ||||||||||||||||||||||||||||||||||||||||||
| 796 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||||||||||||||||
| 797 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||||||||||||||||||||||||||||||||||||||||
| 798 | General Public License for more details. | ||||||||||||||||||||||||||||||||||||||||||
| 799 | |||||||||||||||||||||||||||||||||||||||||||
| 800 | You should have received a copy of the GNU General Public License along | ||||||||||||||||||||||||||||||||||||||||||
| 801 | with this program; if not, write to the Free Software Foundation, Inc., | ||||||||||||||||||||||||||||||||||||||||||
| 802 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||||||||||||||||||||||||||||||||||||||||||
| 803 | |||||||||||||||||||||||||||||||||||||||||||
| 804 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 805 | |||||||||||||||||||||||||||||||||||||||||||
| 806 | Toby Thurston -- 30 Jul 2017 | ||||||||||||||||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||||||||||||||||
| 808 | toby@cpan.org | ||||||||||||||||||||||||||||||||||||||||||
| 809 | |||||||||||||||||||||||||||||||||||||||||||
| 810 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 811 | |||||||||||||||||||||||||||||||||||||||||||
| 812 | See L<Geo::Coordinates::OSGB::Grid> for routines to format grid references. | ||||||||||||||||||||||||||||||||||||||||||
| 813 | |||||||||||||||||||||||||||||||||||||||||||
| 814 | The UK Ordnance Survey's explanations on their web pages. | ||||||||||||||||||||||||||||||||||||||||||
| 815 | |||||||||||||||||||||||||||||||||||||||||||
| 816 | See L<Geo::Coordinates::Convert> for a general approach (not based on the OSGB). | ||||||||||||||||||||||||||||||||||||||||||
| 817 | |||||||||||||||||||||||||||||||||||||||||||
| 818 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 819 | |||||||||||||||||||||||||||||||||||||||||||
| 820 | # leave blank line after cut so that pod2html works properly | ||||||||||||||||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||||||||||||||||
| 822 | __DATA__ | ||||||||||||||||||||||||||||||||||||||||||