lib/Geo/Spline.pm

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