blib/lib/Geo/Coordinates/RDNAP.pm

filter: Statementstmt 100% 101 / 101 Branchbran 62.5% 10 / 16 Conditioncond 36.3% 8 / 22 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 80.3% 139 / 173 CC 21 SCAR 18.3
CC
21
Cov
86%
from_rd
10.8
to_rd
10.6
deg
4.1
CRAP 6.3
↑ Summary
1 package Geo::Coordinates::RDNAP;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 $VERSION = '0.11';
8  
9 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Params::Validate qw/validate BOOLEAN SCALAR/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use vars qw/@ISA @EXPORT_OK/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 @ISA = qw/Exporter/;
15 3 @EXPORT_OK = qw/from_rd to_rd deg dms/;
16  
17 2 ▶sub deg {
Subroutine deg: called CC=4 SCAR=14.1
Pod documented
18 2 my (@in) = @_;
19 2 my @out;
20  
21 2 ▶ while (my($d, $m, $s) = splice (@in, 0, 3)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@in, 0, 3)) 3 2 5
22 3 ▶ push @out, $d + ($m||0)/60 + ($s||0)/3600;
Condition50%2/4
$m || 0
l!l
30
$s || 0
l!l
30
MC/DC0%0/1
$m || 0
$m
MC/DC0%0/1
$s || 0
$s
Truth table50%1/2
A
A= $m
Aresult
11
00
Truth table50%1/2
A
A= $s
Aresult
11
00
23 }
24 2 return @out;
25 }
26  
27 2 ▶sub dms {
Subroutine dms: called CC=1 SCAR=0.0
Pod documented
28 2 return map {int($_), int($_*60)%60, ($_-int($_*60)/60)*3600} @_;
29 }
30  
31 3 my %a = (
32 '01' => 3236.0331637,
33 20 => -32.5915821,
34 '02' => -0.2472814,
35 21 => -0.8501341,
36 '03' => -0.0655238,
37 22 => -0.0171137,
38 40 => 0.0052771,
39 23 => -0.0003859,
40 41 => 0.0003314,
41 '04' => 0.0000371,
42 42 => 0.0000143,
43 24 => -0.0000090,
44 );
45  
46 3 my %b = (
47 10 => 5261.3028966,
48 11 => 105.9780241,
49 12 => 2.4576469,
50 30 => -0.8192156,
51 31 => -0.0560092,
52 13 => 0.0560089,
53 32 => -0.0025614,
54 14 => 0.0012770,
55 50 => 0.0002574,
56 33 => -0.0000973,
57 51 => 0.0000293,
58 15 => 0.0000291,
59 );
60  
61 3 my %c = (
62 '01' => 190066.98903,
63 11 => -11830.85831,
64 21 => -114.19754,
65 '03' => -32.38360,
66 31 => -2.34078,
67 13 => -0.60639,
68 23 => 0.15774,
69 41 => -0.04158,
70 '05' => -0.00661,
71 );
72  
73 3 my %d = (
74 10 => 309020.31810,
75 '02' => 3638.36193,
76 12 => -157.95222,
77 20 => 72.97141,
78 30 => 59.79734,
79 22 => -6.43481,
80 '04' => 0.09351,
81 32 => -0.07379,
82 14 => -0.05419,
83 40 => -0.03444,
84 );
85  
86 3 my %bessel = (
87 a => 6377397.155,
88 e2 => 6674372e-9,
89 f_i => 299.1528128,
90 );
91  
92 3 my %etrs89 = (
93 a => 6378137,
94 e2 => 6694380e-9,
95 f_i => 298.257222101,
96 );
97  
98 # Transformation parameters from Bessel to ETRS89 with respect to
99 # Amersfoort.
100  
101 3 my %b2e = (
102 tx => 593.032,
103 ty => 26,
104 tz => 478.741,
105 a => 1.9848e-6,
106 b => -1.7439e-6,
107 c => 9.0587e-6,
108 d => 4.0772e-6,
109 );
110  
111 3 my %e2b = map {$_ => -$b2e{$_}} keys %b2e;
112  
113 3 my @amersfoort_b = ( 3903_453.148, 368_135.313, 5012_970.306 );
114 3 my @amersfoort_e = ( 3904_046.180, 368_161.313, 5013_449.047 );
115  
116 4 ▶sub from_rd {
Subroutine from_rd: called CC=9 SCAR=23.8
Pod documented
117 4 ▶ croak 'Geo::Coordinates::RDNAP::from_rd needs two or three arguments'
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 and @_ != 3 0 4 4
Condition33%1/3
@_ != 2 and @_ != 3
!ll&&!rl&&r
040
MC/DC0%0/2
@_ != 2 and @_ != 3
@_ != 2 @_ != 3
Truth table33%1/3
A and B
A= @_ != 2B= @_ != 3
ABresult
0X0
100
111
118 if (@_ !=2 && @_ != 3);
119  
120 4 my ($x, $y, $h) = (@_, 0);
121  
122 4 ▶ croak "Geo::Coordinates::RDNAP::from_rd: X out of bounds: $x"
Branch: 1 branch
BranchTrueFalseTotal
if $x < -7000 or $x > 300000 0 4 4
Condition33%1/3
$x < -7000 or $x > 300000
l!l&&r!l&&!r
004
MC/DC0%0/2
$x < -7000 or $x > 300000
$x < -7000 $x > 300000
Truth table33%1/3
A or B
A= $x < -7000B= $x > 300000
ABresult
1X1
011
000
123 if ($x < -7_000 or $x > 300_000);
124 4 ▶ croak "Geo::Coordinates::RDNAP::from_rd: Y out of bounds: $y"
Branch: 1 branch
BranchTrueFalseTotal
if $y < 289000 or $y > 629000 0 4 4
Condition33%1/3
$y < 289000 or $y > 629000
l!l&&r!l&&!r
004
MC/DC0%0/2
$y < 289000 or $y > 629000
$y < 289000 $y > 629000
Truth table33%1/3
A or B
A= $y < 289000B= $y > 629000
ABresult
1X1
011
000
125 if ($y < 289_000 or $y > 629_000);
126  
127 # Use the approximated transformation.
128 # Step 1: RD -> Bessel (spherical coords)
129  
130 4 $x = ($x/100_000) - 1.55;
131 4 $y = ($y/100_000) - 4.63;
132  
133 4 my $lat = (52*60*60) + (9*60) + 22.178;
134 4 my $lon = (5 *60*60) + (23*60) + 15.5;
135  
136 4 foreach my $i (keys %a) {
137 48 my ($m, $n) = split //, $i;
138 48 $lat += $a{$i} * ($x**$m) * ($y**$n);
139 }
140  
141 4 foreach my $i (keys %b) {
142 48 my ($m, $n) = split //, $i;
143 48 $lon += $b{$i} * ($x**$m) * ($y**$n);
144 }
145  
146 # Step 2: spherical coords -> X, Y, Z
147 4 my @coords = _ellipsoid_to_cartesian($lat/3600, $lon/3600, $h, \%bessel);
148  
149 # Step 3: Bessel -> ETRS89
150 4 @coords = _transform_datum( @coords, \%b2e, \@amersfoort_b );
151  
152 # Step 4: X, Y, Z -> spherical coords
153 4 return _cartesian_to_ellipsoid(@coords, \%etrs89);
154 }
155  
156 4 ▶sub to_rd {
Subroutine to_rd: called CC=9 SCAR=23.6
Pod documented
157 4 ▶ croak 'Geo::Coordinates::RDNAP::to_rd needs two or three arguments'
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 and @_ != 3 0 4 4
Condition33%1/3
@_ != 2 and @_ != 3
!ll&&!rl&&r
040
MC/DC0%0/2
@_ != 2 and @_ != 3
@_ != 2 @_ != 3
Truth table33%1/3
A and B
A= @_ != 2B= @_ != 3
ABresult
0X0
100
111
158 if (@_ !=2 && @_ != 3);
159  
160 4 my ($lat, $lon, $h) = (@_, 0);
161  
162 # Use the approximated transformation.
163 # Step 1: spherical coords -> X, Y, Z
164 4 my @coords = _ellipsoid_to_cartesian($lat, $lon, $h, \%etrs89);
165  
166 # Step 2: ETRS89 -> Bessel
167 4 @coords = _transform_datum( @coords, \%e2b, \@amersfoort_e );
168  
169 # Step 3: X, Y, Z -> spherical coords
170 4 ($lat, $lon, $h) = _cartesian_to_ellipsoid(@coords, \%bessel);
171  
172 # Step 4: Bessel -> RD'
173  
174 # Convert to units of 10_000 secs; as deltas from Amersfoort.
175 4 $lat = ($lat * 3600 - ((52*60*60) + (9*60) + 22.178))/10_000;
176 4 $lon = ($lon * 3600 - ((5 *60*60) + (23*60) + 15.5))/10_000;
177  
178 4 my $x = 155e3;
179 4 my $y = 463e3;
180  
181 4 foreach my $i (keys %c) {
182 36 my ($m, $n) = split //, $i;
183 36 $x += $c{$i} * ($lat**$m) * ($lon**$n);
184 }
185  
186 4 foreach my $i (keys %d) {
187 40 my ($m, $n) = split //, $i;
188 40 $y += $d{$i} * ($lat**$m) * ($lon**$n);
189 }
190  
191 4 ▶ croak "Geo::Coordinates::RDNAP::to_rd: X out of bounds: $x"
Branch: 1 branch
BranchTrueFalseTotal
if $x < -7000 or $x > 300000 0 4 4
Condition33%1/3
$x < -7000 or $x > 300000
l!l&&r!l&&!r
004
MC/DC0%0/2
$x < -7000 or $x > 300000
$x < -7000 $x > 300000
Truth table33%1/3
A or B
A= $x < -7000B= $x > 300000
ABresult
1X1
011
000
192 if ($x < -7_000 or $x > 300_000);
193 4 ▶ croak "Geo::Coordinates::RDNAP::to_rd: Y out of bounds: $y"
Branch: 1 branch
BranchTrueFalseTotal
if $y < 289000 or $y > 629000 0 4 4
Condition33%1/3
$y < 289000 or $y > 629000
l!l&&r!l&&!r
004
MC/DC0%0/2
$y < 289000 or $y > 629000
$y < 289000 $y > 629000
Truth table33%1/3
A or B
A= $y < 289000B= $y > 629000
ABresult
1X1
011
000
194 if ($y < 289_000 or $y > 629_000);
195  
196 4 return ($x, $y, $h);
197 }
198  
199 32 ▶sub _to_rads {
Subroutine _to_rads: called CC=1 SCAR=0.0
200 32 return $_[0] * 2*3.14159_26535_89793 /360;
201 }
202  
203 16 ▶sub _from_rads {
Subroutine _from_rads: called CC=1 SCAR=0.0
204 16 return $_[0] / (2*3.14159_26535_89793) *360;
205 }
206  
207 8 ▶sub _ellipsoid_to_cartesian {
Subroutine _ellipsoid_to_cartesian: called CC=1 SCAR=0.0
208 8 my ($lat, $lon, $h, $para) = @_;
209  
210 8 my $sinphi = sin(_to_rads($lat));
211 8 my $cosphi = cos(_to_rads($lat));
212 8 my $n = $para->{a}/sqrt(1 - $para->{e2}*$sinphi*$sinphi);
213  
214 return (($n+$h)*$cosphi*cos(_to_rads($lon)),
215 ($n+$h)*$cosphi*sin(_to_rads($lon)),
216 8 ($n*(1-$para->{e2})+$h)*$sinphi );
217 }
218  
219 # Returns (lat, lon, h) in degrees.
220  
221 8 ▶sub _cartesian_to_ellipsoid {
Subroutine _cartesian_to_ellipsoid: called CC=2 SCAR=6.9
222 8 my ($x, $y, $z, $para) = @_;
223  
224 8 my $lon = atan2($y, $x);
225  
226 8 my $r = sqrt($x*$x+$y*$y);
227 8 my $phi = 0;
228 8 my $n_sinphi = $z;
229 8 my $n;
230 my $oldphi;
231  
232 8 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
abs $oldphi - $phi > "1e-08" and do { do { $oldphi = $phi; $phi = atan2($z + $para->{"e2"} * $n_sinphi, $r); my $sinphi = sin $phi; $n = $para->{"a"} / sqrt(1 - $para->{"e2"} * $sinphi * $sinphi); $n_sinphi = $n * $sinphi } } 24 8 32
233 32 $oldphi = $phi;
234 32 $phi = atan2($z + $para->{e2}*$n_sinphi, $r);
235 32 my $sinphi = sin($phi);
236 32 $n = $para->{a}/sqrt(1-$para->{e2}*$sinphi*$sinphi);
237 32 $n_sinphi = $n*$sinphi;
238 } while (abs($oldphi-$phi) > 1e-8);
239  
240 8 my $h = $r/cos($phi) - $n;
241  
242 8 return (_from_rads($phi), _from_rads($lon), $h);
243 }
244  
245 8 ▶sub _transform_datum {
Subroutine _transform_datum: called CC=1 SCAR=0.0
246 8 my ($x, $y, $z, $t, $centre) = @_;
247  
248 return (
249 $x + $t->{d}*($x-$centre->[0]) + $t->{c}*($y-$centre->[1])
250 - $t->{b}*($z-$centre->[2]) + $t->{tx},
251 $y - $t->{c}*($x-$centre->[0]) + $t->{d}*($y-$centre->[1])
252 + $t->{a}*($z-$centre->[2]) + $t->{ty},
253 $z + $t->{b}*($x-$centre->[0]) - $t->{a}*($y-$centre->[1])
254 + $t->{d}*($z-$centre->[2]) + $t->{tz}
255 8 );
256 }
257  
258 3 1;
259 __END__
↑ Summary