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