blib/lib/GPS/Point/Cluster.pm

filter: Statementstmt 90.8% 79 / 87 Branchbran 81.5% 31 / 38 Conditioncond 88.8% 8 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 90.4% 19 / 21 Podpod 100% 18 / 18 totaltotal 88.7% 158 / 178 CC 23 SCAR 11.0
CC
23
Cov
88%
initialize
6.0
start_dt
6.0
end_dt
6.0
CRAP 3.0
↑ Summary
1 package GPS::Point::Cluster;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use Geo::Inverse;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION='0.05';
7  
8 =head1 NAME
9  
10 GPS::Point::Cluster - Groups GPS Points in to clusters
11  
12 =head1 SYNOPSIS
13  
14 use GPS::Point::Cluster;
15 my $cluster=GPS::Point::Cluster->new(
16 separation => 500, #meters
17 interlude => 600, #seconds
18 );
19 my @pt=({}, {}, {}, ...); #{lat=>39, lon=>-77, time=>$epoch_seconds}
20  
21 foreach my $pt (@pt) {
22 my $obj=$cluster->merge_attempt($pt);
23 if (defined $obj) {
24 print join(",", $cluster->index, $cluster->start_dt, $cluster->end_dt,
25 $cluster->lat, $cluster->lon, $cluster->weight), "\n";
26 $cluster=$obj;
27 }
28 }
29  
30  
31  
32 =head1 DESCRIPTION
33  
34 =head1 USAGE
35  
36 =head1 CONSTRUCTOR
37  
38 =head2 new
39  
40 my $cluster = GPS::Point::Cluster->new(separation=>500);
41  
42 =cut
43  
44 11 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
45 11 my $this = shift();
46 11 ▶ my $class = ref($this) || $this;
Condition66%2/3
ref $this || $this
l!l&&r!l&&!r
650
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table66%2/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
47 11 my $self = {};
48 11 bless $self, $class;
49 11 $self->initialize(@_);
50 11 return $self;
51 }
52  
53 =head1 METHODS
54  
55 =head2 initialize
56  
57 =cut
58  
59 11 ▶sub initialize {
Subroutine initialize: called CC=6 SCAR=17.9
Pod documented
60 11 my $self = shift();
61 11 %$self=@_;
62 11 ▶ $self->GeoInverse(Geo::Inverse->new)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->GeoInverse eq "Geo::Inverse" 5 6 11
63 unless ref($self->GeoInverse) eq "Geo::Inverse";
64 11 ▶ $self->separation(500) unless $self->separation;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->separation 5 6 11
65 11 ▶ $self->interlude(600) unless $self->interlude;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interlude 5 6 11
66 11 ▶ $self->index(0) unless $self->index;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->index 5 6 11
67 11 ▶ $self->weight(0) unless $self->weight;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->weight 5 6 11
68 }
69  
70 =head2 settings
71  
72 Returns a hash of default settings to transfer from one cluster to the next.
73  
74 my $hash=$cluster->settings;
75 my %hash=$cluster->settings;
76  
77 =cut
78  
79 6 ▶sub settings {
Subroutine settings: called CC=2 SCAR=7.0
Pod documented
80 6 my $self=shift;
81 6 my @keys=qw{separation interlude GeoInverse};
82 6 my %hash=(index=>$self->index + 1, map {$_=>$self->{$_}} @keys);
83 6 ▶ return wantarray ? %hash : \%hash;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 0 6
84 }
85  
86 =head2 index
87  
88 Returns the cluster index which is a running integer.
89  
90 =cut
91  
92 81 ▶sub index {
Subroutine index: called CC=2 SCAR=6.9
Pod documented
93 81 my $self=shift;
94 81 ▶ $self->{'index'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 5 76 81
95 81 return $self->{'index'};
96 }
97  
98 =head2 separation
99  
100 The threshold distance in meters between the cluster and the test point.
101  
102 =cut
103  
104 71 ▶sub separation {
Subroutine separation: called CC=2 SCAR=6.9
Pod documented
105 71 my $self=shift;
106 71 ▶ $self->{'separation'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 5 66 71
107 71 return $self->{'separation'};
108 }
109  
110 =head2 interlude
111  
112 The threshold duration in seconds between the cluster end time and the test point.
113  
114 =cut
115  
116 72 ▶sub interlude {
Subroutine interlude: called CC=2 SCAR=6.9
Pod documented
117 72 my $self=shift;
118 72 ▶ $self->{'interlude'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 5 67 72
119 72 return $self->{'interlude'};
120 }
121  
122 =head2 lat
123  
124 Latitude in decimal degrees WGS-84. The latitude is calculated as a mathimatical average of all latitudes that constitute the cluster.
125  
126 =cut
127  
128 217 ▶sub lat {
Subroutine lat: called CC=2 SCAR=6.9
Pod documented
129 217 my $self=shift;
130 217 ▶ $self->{'lat'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 52 165 217
131 217 return $self->{'lat'};
132 }
133  
134 =head2 lon
135  
136 Longitude in decimal degrees WGS-84. The longitude is calculated as a mathimatical average of all longitudes that constitute the cluster.
137  
138 =cut
139  
140 217 ▶sub lon {
Subroutine lon: called CC=2 SCAR=6.9
Pod documented
141 217 my $self=shift;
142 217 ▶ $self->{'lon'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 52 165 217
143 217 return $self->{'lon'};
144 }
145  
146 =head2 weight
147  
148 The count of points that constitute the cluster.
149  
150 =cut
151  
152 344 ▶sub weight {
Subroutine weight: called CC=2 SCAR=6.9
Pod documented
153 344 my $self=shift;
154 344 ▶ $self->{'weight'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 57 287 344
155 344 return $self->{'weight'};
156 }
157  
158 =head2 start
159  
160 Returns the cluster start date time as seconds from epoch
161  
162 =cut
163  
164 111 ▶sub start {
Subroutine start: called CC=2 SCAR=7.1
Pod documented
165 111 my $self=shift;
166 111 ▶ $self->{'start'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 111 111
167 111 return $self->{'start'};
168 }
169  
170 =head2 start_dt
171  
172 Returns the cluster start date time as a L<DateTime> object
173  
174 =cut
175  
176 0 ▶sub start_dt {
Subroutine start_dt: not called CC=2 SCAR=17.9
Pod documented
177 0 my $self=shift;
178 0 ▶ unless (defined $self->{'start_dt'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"start_dt"}) 0 0 0
179 0 $self->{'start_dt'}=DateTime->from_epoch(epoch=>$self->start);
180 }
181 0 return $self->{'start_dt'};
182 }
183  
184 =head2 end
185  
186 Returns the cluster end date time as seconds from epoch
187  
188 =cut
189  
190 164 ▶sub end {
Subroutine end: called CC=2 SCAR=6.9
Pod documented
191 164 my $self=shift;
192 164 ▶ $self->{'end'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 52 112 164
193 164 return $self->{'end'};
194 }
195  
196 =head2 end_dt
197  
198 Returns the cluster end date time as a L<DateTime> object
199  
200 =cut
201  
202 0 ▶sub end_dt {
Subroutine end_dt: not called CC=2 SCAR=17.9
Pod documented
203 0 my $self=shift;
204 0 ▶ unless (defined $self->{'end_dt'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"end_dt"}) 0 0 0
205 0 $self->{'end_dt'}=DateTime->from_epoch(epoch=>$self->end);
206 }
207 0 return $self->{'end_dt'};
208 }
209  
210 =head2 GeoInverse
211  
212 Returns a L<Geo::Inverse> object which is used to calculate geodetic distances.
213  
214 =cut
215  
216 70 ▶sub GeoInverse {
Subroutine GeoInverse: called CC=2 SCAR=6.9
Pod documented
217 70 my $self=shift;
218 70 ▶ $self->{'GeoInverse'}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 5 65 70
219 70 return $self->{'GeoInverse'};
220 }
221  
222 =head2 merge_attempt
223  
224 Attempts to merge the point into the cluster. If the point does not fit in the cluster then the method returns a new cluster. If it merged, then it returns undef.
225  
226 my $new_cluster=$cluster->merge_attempt($pt);
227 if (defined $new_cluster) {
228 #New cluster is constructed with $pt as the only member. $cluster is unmodified.
229 } else {
230 #$pt is added the cluster. The cluster is updated appropriately.
231 }
232  
233 =cut
234  
235 58 ▶sub merge_attempt {
Subroutine merge_attempt: called CC=4 SCAR=13.9
Pod documented
236 58 my $self=shift;
237 58 my $pt=shift;
238 58 ▶ if ( $self->weight
Branch: 1 branch
BranchTrueFalseTotal
if ($self->weight and $self->distance($pt) < $self->separation and $self->duration($pt) < $self->interlude) { } 52 6 58
Condition100%6/6
$self->weight and $self->distance($pt) < $self->separation and $self->duration($pt) < $self->interlude
!ll&&!rl&&r
5152
$self->weight and $self->distance($pt) < $self->separation
!ll&&!rl&&r
4153
MC/DC100%3/3
$self->weight and $self->distance($pt) < $self->separation and $self->duration($pt) < $self->interlude
$self->weight $self->distance($pt) < $self->separation $self->duration($pt) < $self->interlude
Truth table100%4/4
A and B and C
A= $self->weightB= $self->distance($pt) < $self->separationC= $self->duration($pt) < $self->interlude
ABCresult
0XX0
10X0
1100
1111
239 and $self->distance($pt) < $self->separation
240 and $self->duration($pt) < $self->interlude ) {
241 52 $self->merge($pt);
242 52 return undef;
243 } else {
244 return $self->new(%$pt, $self->settings,
245 start =>$pt->{'time'},
246 6 end =>$pt->{'time'},
247 weight =>1);
248 }
249 }
250  
251 =head2 distance
252  
253 Returns the distance in meters between the cluster and the point.
254  
255 my $distance=$cluster->distance($pt);
256  
257 =cut
258  
259 54 ▶sub distance {
Subroutine distance: called CC=1 SCAR=0.0
Pod documented
260 54 my $self=shift;
261 54 my $pt=shift;
262 my $distance=$self->GeoInverse->inverse($self->lat => $self->lon,
263 54 $pt->{'lat'} => $pt->{'lon'});
264 54 return $distance;
265 }
266  
267 =head2 duration
268  
269 Returns the duration in seconds between the cluster and the point.
270  
271 my $duration=$cluster->duration($pt);
272  
273 =cut
274  
275 53 ▶sub duration {
Subroutine duration: called CC=1 SCAR=0.0
Pod documented
276 53 my $self=shift;
277 53 my $pt=shift;
278 53 return $pt->{'time'} - $self->end;
279 }
280  
281 =head2 merge
282  
283 Merges point into cluster returns cluster.
284  
285 my $cluster->merge($pt);
286  
287 =cut
288  
289 52 ▶sub merge {
Subroutine merge: called CC=2 SCAR=6.9
Pod documented
290 52 my $self=shift;
291 52 my $pt=shift;
292 52 ▶ $self->start($pt->{'time'}) unless defined $self->start;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->start 0 52 52
293 52 $self->end($pt->{'time'});
294 52 $self->{'end_dt'}=undef;
295 52 my $weight=$self->weight;
296 52 $self->weight($weight+1);
297 52 $self->lat(($self->lat * $weight + $pt->{'lat'})/$self->weight);
298 52 $self->lon(($self->lon * $weight + $pt->{'lon'})/$self->weight);
299 52 return $self;
300 }
301  
302 =head1 BUGS
303  
304 =head1 SUPPORT
305  
306 =head1 AUTHOR
307  
308 Michael R. Davis
309 CPAN ID: MRDVT
310 STOP, LLC
311 domain=>michaelrdavis,tld=>com,account=>perl
312 http://www.stopllc.com/
313  
314 =head1 COPYRIGHT
315  
316 This program is free software licensed under the...
317  
318 The BSD License
319  
320 The full text of the license can be found in the
321 LICENSE file included with this module.
322  
323 =head1 SEE ALSO
324  
325 L<Algorithm::Cluster>, L<Algorithm::ClusterPoints>
326  
327 =cut
328  
329 4 1;
↑ Summary