blib/lib/Geo/Spline.pm

filter: Statementstmt 100% 91 / 91 Branchbran 85.7% 12 / 14 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 12 / 12 Podpod 100% 7 / 7 totaltotal 96.1% 124 / 129 CC 11 SCAR 9.0
CC
11
Cov
97%
timelist
4.0
pointlist
4.0
new
2.0
CRAP 2.5
↑ Summary
1 package Geo::Spline;
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 Geo::Ellipsoids;
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{deg_rad rad_deg round};
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.17';
9  
10 =head1 NAME
11  
12 Geo::Spline - Calculate geographic locations between GPS fixes.
13  
14 =head1 SYNOPSIS
15  
16 use Geo::Spline;
17 my $p0 = {
18 time => 1160449100.67, #seconds
19 lat => 39.197807, #degrees
20 lon => -77.263510, #degrees
21 speed => 31.124, #m/s
22 heading => 144.8300, #degrees clockwise from North
23 };
24 my $p1 = {
25 time => 1160449225.66,
26 lat => 39.167718,
27 lon => -77.242278,
28 speed => 30.615,
29 heading =>1 50.5300,
30 };
31 my $spline = Geo::Spline->new($p0, $p1);
32 my %point = $spline->point(1160449150);
33 print "Lat:", $point{"lat"}, ", Lon:", $point{"lon"}, "\n\n";
34  
35 my @points = $spline->pointlist();
36 foreach (@points) {
37 print "Lat:", $_->{"lat"}, ", Lon:", $_->{"lon"}, "\n";
38 }
39  
40 =head1 DESCRIPTION
41  
42 This program was developed to be able to calculate the position between two GPS fixes using a 2-dimensional 3rd order polynomial spline.
43  
44 f(t) = A + B(t-t0) + C(t-t0)^2 + D(t-t0)^3 #position in X and Y
45 f'(t) = B + 2C(t-t0) + 3D(t-t0)^2 #velocity in X and Y
46  
47 Formulas to calculate the unknowns from our knowns.
48  
49 A = x0 # when (t-t0)=0 in f(t)
50 B = v0 # when (t-t0)=0 in f'(t)
51 C = (x1-A-B(t1-t0)-D(t1-t0)^3)/(t1-t0)^2 # solve for C from f(t)
52 C = (v1-B-3D(t1-t0)^2)/2(t1-t0) # solve for C from f'(t)
53 D = (v1(t1-t0)+B(t1-t0)-2x1+2A)/(t1-t0)^3 # equate C=C then solve for D
54  
55 =head1 CONSTRUCTOR
56  
57 =head2 new
58  
59 my $spline = Geo::Spline->new($p0, $p1);
60  
61 =cut
62  
63 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
64 1 my $this = shift;
65 1 ▶ my $class = ref($this) ? ref($this) : $this;
Branch: 1 branch
BranchTrueFalseTotal
ref $this ? : 0 1 1
66 1 my $self = {};
67 1 bless $self, $class;
68 1 $self->initialize(@_);
69 1 return $self;
70 }
71  
72 =head2 initialize
73  
74 =cut
75  
76 1 ▶sub initialize {
Subroutine initialize: called CC=2 SCAR=6.9
Pod documented
77 1 my $self = shift();
78 1 $self->{'pt0'} = shift();
79 1 $self->{'pt1'} = shift();
80 1 my $ellipsoid = $self->ellipsoid('WGS84');
81 1 my $dt = $self->{'pt1'}->{'time'} - $self->{'pt0'}->{'time'};
82 1 ▶ die ('Delta time must be greater than zero.') if ($dt<=0);
Branch: 1 branch
BranchTrueFalseTotal
if $dt <= 0 0 1 1
83 my ($A, $B, $C, $D) = $self->ABCD(
84 $self->{'pt0'}->{'time'},
85 $self->{'pt0'}->{'lat'} * $ellipsoid->polar_circumference / 360,
86 $self->{'pt0'}->{'speed'} * cos(rad_deg($self->{'pt0'}->{'heading'})),
87 $self->{'pt1'}->{'time'},
88 $self->{'pt1'}->{'lat'} * $ellipsoid->polar_circumference / 360,
89 1 $self->{'pt1'}->{'speed'} * cos(rad_deg($self->{'pt1'}->{'heading'})));
90 1 $self->{'Alat'} = $A;
91 1 $self->{'Blat'} = $B;
92 1 $self->{'Clat'} = $C;
93 1 $self->{'Dlat'} = $D;
94 ($A, $B, $C, $D) = $self->ABCD(
95 $self->{'pt0'}->{'time'},
96 $self->{'pt0'}->{'lon'} * $ellipsoid->equatorial_circumference / 360,
97 $self->{'pt0'}->{'speed'} * sin(rad_deg($self->{'pt0'}->{'heading'})),
98 $self->{'pt1'}->{'time'},
99 $self->{'pt1'}->{'lon'} * $ellipsoid->equatorial_circumference / 360,
100 1 $self->{'pt1'}->{'speed'} * sin(rad_deg($self->{'pt1'}->{'heading'})));
101 1 $self->{'Alon'} = $A;
102 1 $self->{'Blon'} = $B;
103 1 $self->{'Clon'} = $C;
104 1 $self->{'Dlon'} = $D;
105 }
106  
107 =head1 METHODS
108  
109 =head2 ellipsoid
110  
111 Method to set or retrieve the current ellipsoid object. The ellipsoid is a Geo::Ellipsoids object.
112  
113 my $ellipsoid=$obj->ellipsoid; #Default is WGS84
114  
115 $obj->ellipsoid('Clarke 1866'); #Built in ellipsoids from Geo::Ellipsoids
116 $obj->ellipsoid({a=>1}); #Custom Sphere 1 unit radius
117  
118 =cut
119  
120 1259 ▶sub ellipsoid {
Subroutine ellipsoid: called CC=2 SCAR=6.9
Pod documented
121 1259 my $self = shift();
122 1259 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1258 1259
123 1 my $param = shift();
124 1 my $obj = Geo::Ellipsoids->new($param);
125 1 $self->{'ellipsoid'} = $obj;
126 }
127 1259 return $self->{'ellipsoid'};
128 }
129  
130 =head2 ABCD
131  
132 =cut
133  
134 2 ▶sub ABCD {
Subroutine ABCD: called CC=1 SCAR=0.0
Pod documented
135 2 my $self = shift();
136 2 my $t0 = shift();
137 2 my $x0 = shift();
138 2 my $v0 = shift();
139 2 my $t1 = shift();
140 2 my $x1 = shift();
141 2 my $v1 = shift();
142 #x=f(t)=A+B(t-t0)+C(t-t0)^2+D(t-t0)^3
143 #v=f'(t)=B+2C(t-t0)+3D(t-t0)^2
144 #A=x0
145 #B=v0
146 #C=(x1-A-B(t1-t0)-D(t1-t0)^3)/((t1-t0)^2) # from f(t)
147 #C=(v1-B-3D(t1-t0)^2)/2(t1-t0) # from f'(t)
148 #D=(v1t+Bt-2x1+2A)/t^3 # from C=C
149 2 my $A = $x0;
150 2 my $B = $v0;
151 #=(C3*(A3-A2)+B6*(A3-A2)-2*B3+2*B5)/(A3-A2)^3 # for Excel
152 2 my $D = ($v1*($t1-$t0)+$B*($t1-$t0)-2*$x1+2*$A)/($t1-$t0)**3;
153 #=(B3-B5-B6*(A3-A2)-B8*(A3-A2)^3)/(A3-A2)^2 # for Excel
154 2 my $C = ($x1-$A-$B*($t1-$t0)-$D*($t1-$t0)**3)/($t1-$t0)**2;
155 2 return($A,$B,$C,$D);
156 }
157  
158 =head2 point
159  
160 Method returns a single point from a single time.
161  
162 my $point = $spline->point($t1);
163 my %point = $spline->point($t1);
164  
165 =cut
166  
167 1258 ▶sub point {
Subroutine point: called CC=2 SCAR=6.9
Pod documented
168 1258 my $self = shift();
169 1258 my $timereal = shift();
170 1258 my $ellipsoid = $self->ellipsoid;
171 1258 my $t = $timereal-$self->{'pt0'}->{'time'};
172 1258 my ($Alat, $Blat, $Clat, $Dlat)=($self->{'Alat'}, $self->{'Blat'},$self->{'Clat'},$self->{'Dlat'});
173 1258 my ($Alon, $Blon, $Clon, $Dlon)=($self->{'Alon'}, $self->{'Blon'},$self->{'Clon'},$self->{'Dlon'});
174 1258 my $lat = $Alat + $Blat * $t + $Clat * $t ** 2 + $Dlat * $t ** 3;
175 1258 my $lon = $Alon + $Blon * $t + $Clon * $t ** 2 + $Dlon * $t ** 3;
176 1258 my $vlat = $Blat + 2 * $Clat * $t + 3 * $Dlat * $t ** 2;
177 1258 my $vlon = $Blon + 2 * $Clon * $t + 3 * $Dlon * $t ** 2;
178 1258 my $speed = sqrt($vlat ** 2 + $vlon ** 2);
179 1258 my $heading = PI()/2 - atan2($vlat,$vlon);
180 1258 $heading = deg_rad($heading);
181 1258 $lat /= $ellipsoid->polar_circumference / 360;
182 1258 $lon /= $ellipsoid->equatorial_circumference / 360;
183 1258 my %pt = (
184 time => $timereal,
185 lat => $lat,
186 lon => $lon,
187 speed => $speed,
188 heading => $heading,
189 );
190 1258 ▶ return wantarray ? %pt : \%pt;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1257 1 1258
191 }
192  
193 =head2 pointlist
194  
195 Method returns a list of points from a list of times.
196  
197 my $list = $spline->pointlist($t1,$t2,$t3);
198 my @list = $spline->pointlist($t1,$t2,$t3);
199  
200 =cut
201  
202 4 ▶sub pointlist {
Subroutine pointlist: called CC=4 SCAR=13.9
Pod documented
203 4 my $self = shift();
204 4 my @list = @_;
205 4 ▶ @list = $self->timelist() if (scalar(@list)== 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @list == 0 2 2 4
206 4 my @points = ();
207 4 foreach (@list) {
208 1256 push @points, {$self->point($_)};
209 }
210 4 ▶ return wantarray ? @points : \@points;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 1 4
211 }
212  
213 =head2 timelist
214  
215 Method returns a list of times (n+1). The default will return a list with an integer number of seconds between spline end points.
216  
217 my $list = $spline->timelist($samples);
218 my @list = $spline->timelist();
219  
220 =cut
221  
222 6 ▶sub timelist {
Subroutine timelist: called CC=4 SCAR=13.9
Pod documented
223 6 my $self = shift();
224 6 my $t0 = $self->{'pt0'}->{'time'};
225 6 my $t1 = $self->{'pt1'}->{'time'};
226 6 my $dt = $t1-$t0;
227 6 ▶ my $count = shift() || round($dt);
Condition66%2/3
shift() || round($dt)
l!l&&r!l&&!r
240
MC/DC0%0/2
shift() || round($dt)
shift() round($dt)
Truth table66%2/3
A || B
A= shift()B= round($dt)
ABresult
1X1
011
000
228 6 my @list;
229 6 foreach(0 .. $count) {
230 1506 my $t = $t0 + $dt*($_/$count);
231 1506 push @list, $t;
232 }
233 6 ▶ return wantarray ? @list : \@list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 1 6
234 }
235  
236 =head1 LIMITATIONS
237  
238 The conversion from degrees to meters and then back is accurate for short distances.
239  
240 =head1 AUTHOR
241  
242 Michael R. Davis
243  
244 =head1 LICENSE
245  
246 Copyright (c) 2006-2025 Michael R. Davis
247  
248 This library is free software; you can redistribute it and/or modify
249 it under the same terms as Perl itself.
250  
251 =head1 SEE ALSO
252  
253 http://search.cpan.org/src/MRDVT/Geo-Spline-0.16/doc/spline.xls
254 http://search.cpan.org/src/MRDVT/Geo-Spline-0.16/doc/spline.png
255  
256 L<Math::Spline>, L<Geo::Ellipsoids>
257  
258 =cut
259  
260 1 1;
↑ Summary