1 |
package NMEA; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use Data::Dump qw/dump/; |
7 |
|
8 |
sub hhmm { |
9 |
my $a = shift; |
10 |
if ( $a =~ m/^(\d+)(\d\d\.\d\d+)$/ ) { |
11 |
return sprintf("%1.6f", $1 + ( $2 / 60 )); |
12 |
} else { |
13 |
warn "## skipped $a\n"; |
14 |
return; |
15 |
} |
16 |
} |
17 |
|
18 |
my $got_it; |
19 |
my $stats; |
20 |
|
21 |
sub error { |
22 |
my ( $self, $message, $line ) = @_; |
23 |
push @{ $stats->{errors}->{$message} }, $line; |
24 |
} |
25 |
|
26 |
sub stats { |
27 |
my $self = shift; |
28 |
return $stats; |
29 |
} |
30 |
|
31 |
sub line { |
32 |
my $self = shift; |
33 |
my $l = shift || return; |
34 |
|
35 |
if ( m/\$GPRMC/ ) { |
36 |
chomp; |
37 |
my @a = split(/,/,$_); |
38 |
|
39 |
if ( $#a != 12 ) { |
40 |
$self->error( 'invalid line length' => $_ ); |
41 |
return; |
42 |
} |
43 |
|
44 |
# warn "## [$#a] ", join(' ', map { "$_:$a[$_]" } ( 0 .. $#a )), " from $_\n"; |
45 |
|
46 |
# is valid? |
47 |
if ( $a[2] ne 'A' ) { |
48 |
$self->error( 'not valid' => $_ ); |
49 |
return; |
50 |
} |
51 |
|
52 |
my $hash; |
53 |
my $i = 1; # skip GPRMC; |
54 |
$hash->{$_} = $a[$i++] foreach ( qw/ |
55 |
time validity lat_hhmm lat_ns lon_hhmm lon_ew speed course date var var_ew |
56 |
/ ); |
57 |
|
58 |
$hash->{lat} = hhmm( ( $hash->{lat_ns} eq 'S' ? -1 : 1 ) * $hash->{lat_hhmm} ) || return; |
59 |
$hash->{lon} = hhmm( ( $hash->{lon_ew} eq 'W' ? -1 : 1 ) * $hash->{lon_hhmm} ) || return; |
60 |
|
61 |
if ( $got_it->{ $hash->{lat} . ' ' . $hash->{lon} }++ ) { |
62 |
$self->error( 'duplicate line' => $_ ); |
63 |
return; |
64 |
} |
65 |
|
66 |
$stats->{ 'min_'.$_ } = $hash->{ $_ } < $stats->{ 'min_'.$_ } foreach ( qw/lat lon/ ); |
67 |
$stats->{ 'max_'.$_ } = $hash->{ $_ } > $stats->{ 'max_'.$_ } foreach ( qw/lat lon/ ); |
68 |
|
69 |
$hash->{number} = $stats->{total}++; |
70 |
|
71 |
# warn "##>>>> ",dump( $hash ); |
72 |
return $hash; |
73 |
} |
74 |
|
75 |
return; |
76 |
} |
77 |
|
78 |
1; |