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