1 |
#!/usr/bin/perl |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
use HTML::GoogleMaps; |
7 |
use CGI; |
8 |
use CGI::Carp qw/fatalsToBrowser/; |
9 |
use File::Find; |
10 |
use Data::Dump qw/dump/; |
11 |
|
12 |
use lib '../lib'; |
13 |
use blib; |
14 |
use NMEA; |
15 |
|
16 |
my $trace_path = '../nmea/'; |
17 |
|
18 |
# http://localhost/ |
19 |
my $map_key = 'ABQIAAAAVQ5szt9Jd8ws6vgfVQOEmhT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQ1cKf0DwFJcwtpESJEI0hL8QgtYg'; |
20 |
|
21 |
my @traces; |
22 |
find({ wanted => sub { |
23 |
push @traces, $_ if -f $_; |
24 |
}}, $trace_path); |
25 |
|
26 |
my $q = CGI->new; |
27 |
|
28 |
print $q->header; |
29 |
|
30 |
my $head = ''; |
31 |
my $html = join('', qq{ |
32 |
<h1>Select GPS NMEA dump</h1> |
33 |
}, |
34 |
$q->start_form( -id => 'trace_frm' ), |
35 |
$q->popup_menu( |
36 |
-name => 'trace', |
37 |
-values => [ |
38 |
map { |
39 |
"$_ (" . (stat("$trace_path/$_"))[7] . " bytes)" |
40 |
} |
41 |
sort { |
42 |
(stat("$trace_path/$a"))[9] <=> (stat("$trace_path/$b"))[9] |
43 |
} @traces |
44 |
], |
45 |
-onChange => 'trace_frm.submit()', |
46 |
), |
47 |
$q->submit( -value => 'Show trace' ), |
48 |
$q->br, |
49 |
'Draw ', |
50 |
$q->popup_menu( |
51 |
-name => 'points_filter', |
52 |
-values => [ 'all', 'every', 'total of' ], |
53 |
), |
54 |
$q->textfield( |
55 |
-name => 'points_count', |
56 |
-value => '', |
57 |
-size => 2, |
58 |
-onChange => 'trace_frm.submit()', |
59 |
), |
60 |
' points and ', |
61 |
$q->popup_menu( |
62 |
-name => 'placemark_filter', |
63 |
-values => [ 'total of', 'every' ], |
64 |
), |
65 |
$q->textfield( |
66 |
-name => 'placemark_count', |
67 |
-value => 5, |
68 |
-size => 2, |
69 |
-onChange => 'trace_frm.submit()', |
70 |
), |
71 |
' placemarks', |
72 |
$q->end_form, |
73 |
); |
74 |
|
75 |
|
76 |
|
77 |
if ( my $trace = $q->param('trace') ) { |
78 |
$trace =~ s/\s.+$//; |
79 |
|
80 |
$trace = "$trace_path/$trace"; |
81 |
|
82 |
my $center_point; |
83 |
my @points; |
84 |
my @placemarks; |
85 |
|
86 |
open(my $fh, '<', $trace) || die "can't open $trace: $!"; |
87 |
while( <$fh> ) { |
88 |
|
89 |
my $hash = NMEA->line( $_ ) || next; |
90 |
|
91 |
my $point = [ $hash->{lon}, $hash->{lat} ]; |
92 |
$center_point ||= $point; |
93 |
|
94 |
push @points, $point; |
95 |
|
96 |
push @placemarks, { |
97 |
point => $point, |
98 |
html => join('<br/>', |
99 |
map { |
100 |
ucfirst($_) . ': ' . $hash->{$_} |
101 |
} ( qw/number time lat lon speed course/ ) |
102 |
), |
103 |
}; |
104 |
|
105 |
} |
106 |
close($fh); |
107 |
|
108 |
if ( $#points >= 0 ) { |
109 |
|
110 |
my $map = HTML::GoogleMaps->new( |
111 |
key => $map_key, |
112 |
width => '800px', |
113 |
height => '600px', |
114 |
); |
115 |
#$map->center(point => "Zagreb, Hrvatska"); |
116 |
|
117 |
#$map->zoom(10); |
118 |
$map->v2_zoom(20); |
119 |
$map->controls("large_map_control", "map_type_control"); |
120 |
$map->map_type('hybrid'); |
121 |
$map->center( $center_point ) if $q->param('line') && $center_point; |
122 |
|
123 |
sub filter_array { |
124 |
my $o = {@_}; |
125 |
my ( $count, $filter, $code ) = |
126 |
( $o->{count}, $o->{filter}, $o->{code} ) ; |
127 |
confess "no CODE?" unless ref($code) eq 'CODE'; |
128 |
my @array = @{ $o->{array} }; |
129 |
|
130 |
warn "count: $count filter: $filter\n"; |
131 |
|
132 |
my $code_calls = 0; |
133 |
|
134 |
if ( $count && $filter =~ m/every/ ) { |
135 |
foreach my $o ( 0 .. $#array ) { |
136 |
next unless $o % $count == 0; |
137 |
$code->( $array[$o] ); |
138 |
$code_calls++; |
139 |
} |
140 |
} elsif ( $count && $filter =~ m/total/ ) { |
141 |
# total of |
142 |
if ( $count < 2 ) { |
143 |
# first |
144 |
if ( $array[0]) { |
145 |
$code->( $array[0] ); |
146 |
$code_calls++; |
147 |
}; |
148 |
# last |
149 |
if ( $count > 1 && $#array > 0 ) { |
150 |
$code->( $array[$#array] ); |
151 |
$code_calls++; |
152 |
}; |
153 |
return $code_calls; |
154 |
} |
155 |
|
156 |
my $d = $#array / ( $count - 1 ); |
157 |
foreach my $p ( 0 .. $count - 1 ) { |
158 |
my $o = int($p * $d); |
159 |
die "no element $p at $o from total of ",$#array + 1 unless $array[$o]; |
160 |
$code->( $array[$o] ); |
161 |
$code_calls++; |
162 |
} |
163 |
} else { |
164 |
# show every |
165 |
foreach my $e ( @array ) { |
166 |
$code->( $e ); |
167 |
$code_calls++; |
168 |
} |
169 |
} |
170 |
return $code_calls; |
171 |
} |
172 |
|
173 |
my @poly_points; |
174 |
my $points = filter_array( |
175 |
count => $q->param('points_count'), |
176 |
filter => $q->param('points_filter'), |
177 |
code => sub { |
178 |
my $point = shift; |
179 |
push @poly_points, $point; |
180 |
}, |
181 |
array => \@points, |
182 |
); |
183 |
|
184 |
die "hum?" unless $#poly_points == $points - 1; |
185 |
|
186 |
$map->add_polyline( points => [ @poly_points ] ) if @poly_points; |
187 |
|
188 |
my $placemarks = filter_array( |
189 |
count => $q->param('placemark_count'), |
190 |
filter => $q->param('placemark_filter'), |
191 |
code => sub { |
192 |
my $placemark = shift; |
193 |
$map->add_marker( %$placemark, noformat => 1 ); |
194 |
}, |
195 |
array => \@placemarks, |
196 |
); |
197 |
|
198 |
my ( $map_div, $map_script ); |
199 |
( $head, $map_div, $map_script ) = $map->render; |
200 |
|
201 |
$html .= join('', |
202 |
$#points + 1, ' points from <tt>', $q->param('trace'), '</tt> showing ', |
203 |
$points ? $points . ' points' . ( $placemarks ? ' and ' : '' ) : '', |
204 |
$placemarks ? $placemarks . ' placemarks' : '', |
205 |
qq{ |
206 |
$map_div |
207 |
$map_script |
208 |
<a href="http://aprs.gids.nl/nmea/">GPS - NMEA sentence information</a> |
209 |
}); |
210 |
|
211 |
my $stats = NMEA->stats; |
212 |
$html .= '<table>'; |
213 |
foreach my $n ( keys %$stats ) { |
214 |
$html .= "<tr><td>$n</td><td>" . $stats->{$n} . "</td></tr>"; |
215 |
} |
216 |
$html .= '</table>'; |
217 |
|
218 |
} else { |
219 |
$html .= '<em>No points found for ' . $q->param('trace') . '</em>'; |
220 |
} |
221 |
|
222 |
} |
223 |
|
224 |
print qq{ |
225 |
<html> |
226 |
<head> |
227 |
<title>Read GPS - NMEA sentence and display it on GoogleMaps</title> |
228 |
$head |
229 |
</head> |
230 |
<body> |
231 |
$html |
232 |
</body> |
233 |
</html> |
234 |
}; |