blib/lib/Geo/Inverse.pm

filter: Statementstmt 98.9% 91 / 92 Branchbran 75.0% 9 / 12 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 93.6% 117 / 125 SCAR 22.0
CC
9
Cov
94%
_inverse
6.0
initialize
2.0
ellipsoid
2.0
CRAP 9.0
↑ Summary
1 package Geo::Inverse;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base qw{Package::New};
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Geo::Constants qw{PI};
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Geo::Functions qw{rad_deg deg_rad};
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Geo::Ellipsoids;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.07';
10  
11 =head1 NAME
12  
13 Geo::Inverse - Calculate geographic distance from a latitude and longitude pair
14  
15 =head1 SYNOPSIS
16  
17 use Geo::Inverse;
18 my $obj = Geo::Inverse->new(); #default "WGS84"
19 my ($lat1, $lon1, $lat2, $lon2) = (38.87, -77.05, 38.95, -77.23);
20 my ($faz, $baz, $dist) = $obj->inverse($lat1,$lon1,$lat2,$lon2); #array context
21 my $dist=$obj->inverse($lat1, $lon1, $lat2, $lon2); #scalar context
22 print "Input Lat: $lat1 Lon: $lon1\n";
23 print "Input Lat: $lat2 Lon: $lon2\n";
24 print "Output Distance: $dist\n";
25 print "Output Forward Azimuth: $faz\n";
26 print "Output Back Azimuth: $baz\n";
27  
28 =head1 DESCRIPTION
29  
30 This module is a pure Perl port of the NGS program in the public domain "inverse" by Robert (Sid) Safford and Stephen J. Frakes.
31  
32 =head1 CONSTRUCTOR
33  
34 =head2 new
35  
36 The new() constructor may be called with any parameter that is appropriate to the ellipsoid method which establishes the ellipsoid.
37  
38 my $obj = Geo::Inverse->new(); # default "WGS84"
39  
40 =head1 METHODS
41  
42 =head2 initialize
43  
44 =cut
45  
46 sub initialize {
47 1 ▶ my $self = shift;
Subroutine initialize: called CC=2 SCAR=7.1
Pod documented
48 1 ▶ my $param = shift || undef;
Condition50%1/2
shift() || undef
l!l
01
MC/DC0%0/1
shift() || undef
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
49 1 $self->ellipsoid($param);
50 }
51  
52 =head2 ellipsoid
53  
54 Method to set or retrieve the current ellipsoid object. The ellipsoid is a Geo::Ellipsoids object.
55  
56 my $ellipsoid = $obj->ellipsoid; #Default is WGS84
57  
58 $obj->ellipsoid('Clarke 1866'); #Built in ellipsoids from Geo::Ellipsoids
59 $obj->ellipsoid({a=>1}); #Custom Sphere 1 unit radius
60  
61 =cut
62  
63 sub ellipsoid {
64 5 ▶ my $self = shift();
Subroutine ellipsoid: called CC=2 SCAR=6.9
Pod documented
65 5 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 4 5
66 1 my $param = shift();
67 1 my $obj = Geo::Ellipsoids->new($param);
68 1 $self->{'ellipsoid'}=$obj;
69 }
70 5 return $self->{'ellipsoid'};
71 }
72  
73 =head2 inverse
74  
75 This method is the user frontend to the mathematics. This interface will not change in future versions.
76  
77 my ($faz, $baz, $dist) = $obj->inverse($lat1,$lon1,$lat2,$lon2);
78  
79 =cut
80  
81 sub inverse {
82 4 ▶ my $self = shift();
Subroutine inverse: called CC=2 SCAR=6.9
Pod documented
83 4 my $lat1 = shift(); #degrees
84 4 my $lon1 = shift(); #degrees
85 4 my $lat2 = shift(); #degrees
86 4 my $lon2 = shift(); #degrees
87 4 my ($faz, $baz, $dist) = $self->_inverse(rad_deg($lat1), rad_deg($lon1),
88 rad_deg($lat2), rad_deg($lon2));
89 4 ▶ return wantarray ? (deg_rad($faz), deg_rad($baz), $dist) : $dist;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 1 4
90 }
91  
92 ########################################################################
93 #
94 # This function was copied from Geo::Ellipsoid
95 # Copyright 2005-2006 Jim Gibson, all rights reserved.
96 #
97 # This program is free software; you can redistribute it and/or modify it
98 # under the same terms as Perl itself.
99 #
100 # internal functions
101 #
102 # inverse
103 #
104 # Calculate the displacement from origin to destination.
105 # The input to this subroutine is
106 # ( latitude-1, longitude-1, latitude-2, longitude-2 ) in radians.
107 #
108 # Return the results as the list (range,bearing) with range in meters
109 # and bearing in radians.
110 #
111 ########################################################################
112  
113 sub _inverse {
114 4 ▶ my $self = shift;
Subroutine _inverse: called CC=6 SCAR=17.9
115 4 my( $lat1, $lon1, $lat2, $lon2 ) = (@_);
116  
117 4 my $ellipsoid=$self->ellipsoid;
118 4 my $a = $ellipsoid->a;
119 4 my $f = $ellipsoid->f;
120  
121 4 my $eps = 1.0e-23;
122 4 my $max_loop_count = 20;
123 4 my $pi=PI;
124 4 my $twopi = 2 * $pi;
125  
126 4 my $r = 1.0 - $f;
127 4 my $tu1 = $r * sin($lat1) / cos($lat1);
128 4 my $tu2 = $r * sin($lat2) / cos($lat2);
129 4 my $cu1 = 1.0 / ( sqrt(($tu1*$tu1) + 1.0) );
130 4 my $su1 = $cu1 * $tu1;
131 4 my $cu2 = 1.0 / ( sqrt( ($tu2*$tu2) + 1.0 ));
132 4 my $s = $cu1 * $cu2;
133 4 my $baz = $s * $tu2;
134 4 my $faz = $baz * $tu1;
135 4 my $dlon = $lon2 - $lon1;
136  
137 4 my $x = $dlon;
138 4 my $cnt = 0;
139 4 my( $c2a, $c, $cx, $cy, $cz, $d, $del, $e, $sx, $sy, $y );
140 4 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
abs $del > $eps and ++$cnt <= $max_loop_count and do { do { $sx = sin $x; $cx = cos $x; $tu1 = $cu2 * $sx; $tu2 = $baz - $su1 * $cu2 * $cx; $sy = sqrt $tu1 * $tu1 + $tu2 * $tu2; $cy = $s * $cx + $faz; $y = atan2($sy, $cy); my $sa; if ($sy == 0) { $sa = 1; } else { $sa = $s * $sx / $sy; } ; $c2a = 1 - $sa * $sa; $cz = $faz + $faz; if ($c2a > 0) { $cz = -$cz / $c2a + $cy; } ; $e = 2 * $cz * $cz - 1; $c = ((-3 * $c2a + 4) * $f + 4) * $c2a * $f / 16; $d = $x; $x = (($e * $cy * $c + $cz) * $sy * $c + $y) * $sa; $x = (1 - $c) * $x * $f + $dlon; $del = $d - $x } } 18 4 22
Condition66%2/3
abs $del > $eps and ++$cnt <= $max_loop_count
!ll&&!rl&&r
4018
MC/DC50%1/2
abs $del > $eps and ++$cnt <= $max_loop_count
abs $del > $eps ++$cnt <= $max_loop_count
Truth table66%2/3
A and B
A= abs $del > $epsB= ++$cnt <= $max_loop_count
ABresult
0X0
100
111
141 22 $sx = sin($x);
142 22 $cx = cos($x);
143 22 $tu1 = $cu2*$sx;
144 22 $tu2 = $baz - ($su1*$cu2*$cx);
145 22 $sy = sqrt( $tu1*$tu1 + $tu2*$tu2 );
146 22 $cy = $s*$cx + $faz;
147 22 $y = atan2($sy,$cy);
148 22 my $sa;
149 22 ▶ if( $sy == 0.0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sy == 0.0) { } 0 22 22
150 0 $sa = 1.0;
151 }else{
152 22 $sa = ($s*$sx) / $sy;
153 }
154  
155 22 $c2a = 1.0 - ($sa*$sa);
156 22 $cz = $faz + $faz;
157 22 ▶ if( $c2a > 0.0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c2a > 0) 22 0 22
158 22 $cz = ((-$cz)/$c2a) + $cy;
159 }
160 22 $e = ( 2.0 * $cz * $cz ) - 1.0;
161 22 $c = ( ((( (-3.0 * $c2a) + 4.0)*$f) + 4.0) * $c2a * $f )/16.0;
162 22 $d = $x;
163 22 $x = ( ($e * $cy * $c + $cz) * $sy * $c + $y) * $sa;
164 22 $x = ( 1.0 - $c ) * $x * $f + $dlon;
165 22 $del = $d - $x;
166
167 } while( (abs($del) > $eps) && ( ++$cnt <= $max_loop_count ) );
168  
169 4 $faz = atan2($tu1,$tu2);
170 4 $baz = atan2($cu1*$sx,($baz*$cx - $su1*$cu2)) + $pi;
171 4 $x = sqrt( ((1.0/($r*$r)) -1.0 ) * $c2a+1.0 ) + 1.0;
172 4 $x = ($x-2.0)/$x;
173 4 $c = 1.0 - $x;
174 4 $c = (($x*$x)/4.0 + 1.0)/$c;
175 4 $d = ((0.375*$x*$x) - 1.0)*$x;
176 4 $x = $e*$cy;
177  
178 4 $s = 1.0 - $e - $e;
179 4 $s = (((((((( $sy * $sy * 4.0 ) - 3.0) * $s * $cz * $d/6.0) - $x) *
180 $d /4.0) + $cz) * $sy * $d) + $y ) * $c * $a * $r;
181  
182 # adjust azimuth to (0,360)
183 4 ▶ $faz += $twopi if $faz < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $faz < 0 4 0 4
184  
185 4 return($faz, $baz, $s);
186 }
187  
188 =head1 BUGS
189  
190 Please open an issue on GitHub
191  
192 =head1 LIMITS
193  
194 No guarantees that Perl handles all of the double precision calculations in the same manner as Fortran.
195  
196 =head1 LICENSE
197  
198 MIT License
199  
200 Copyright (c) 2022 Michael R. Davis
201  
202 =head1 SEE ALSO
203  
204 L<Geo::Ellipsoid>, L<GIS::Distance::GeoEllipsoid>, L<Geo::Calc>
205  
206 =cut
207  
208 1 1;
↑ Summary