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; |
|
|
} |
|
|
} |
|
|
|
|
|
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'; |
|
14 |
|
|
15 |
return if ( $got_it->{ $lat . $lon }++ ); |
my $trace_path = '/home/dpavlin/x/openmoko/gps/'; |
16 |
|
|
17 |
$lat = deg( $lat ) || return; |
# http://localhost/ |
18 |
$lon = deg( $lon ) || return; |
my $map_key = 'ABQIAAAAVQ5szt9Jd8ws6vgfVQOEmhT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQ1cKf0DwFJcwtpESJEI0hL8QgtYg'; |
19 |
|
|
20 |
warn "## $lon $lat\n"; |
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 as ', |
49 |
|
$q->checkbox( |
50 |
|
-name => 'line', |
51 |
|
-onChange => 'trace_frm.submit()', |
52 |
|
), |
53 |
|
' with ', |
54 |
|
$q->popup_menu( |
55 |
|
-name => 'placemark_filter', |
56 |
|
-values => [ 'every', 'total of' ], |
57 |
|
), |
58 |
|
$q->textfield( |
59 |
|
-name => 'placemark', |
60 |
|
-value => 5, |
61 |
|
-size => 2, |
62 |
|
-onChange => 'trace_frm.submit()', |
63 |
|
), |
64 |
|
' placemarks', |
65 |
|
$q->end_form, |
66 |
|
); |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
if ( my $trace = $q->param('trace') ) { |
71 |
|
$trace =~ s/\s.+$//; |
72 |
|
|
73 |
|
$trace = "$trace_path/$trace"; |
74 |
|
|
75 |
|
my $points = 0; |
76 |
|
my $center_point; |
77 |
|
my @points; |
78 |
|
my @placemarks; |
79 |
|
|
80 |
|
open(my $fh, '<', $trace) || die "can't open $trace: $!"; |
81 |
|
while( <$fh> ) { |
82 |
|
|
83 |
return [ $lon, $lat ]; |
my $hash = NMEA->line( $_ ) || next; |
|
} |
|
84 |
|
|
85 |
open(my $fh, '<', $trace) || die "can't open $trace: $!"; |
my $point = [ $hash->{lon}, $hash->{lat} ]; |
86 |
while( <$fh> ) { |
$center_point ||= $point; |
|
if ( m/\$GPRMC/ ) { |
|
|
chomp; |
|
|
my @a = split(/,/,$_); |
|
87 |
|
|
88 |
next unless $#a = 12; |
push @points, $point; |
89 |
|
|
90 |
warn "## [$#a] $_\n"; |
push @placemarks, { |
91 |
|
point => $point, |
92 |
|
html => join('<br/>', |
93 |
|
map { |
94 |
|
ucfirst($_) . ': ' . $hash->{$_} |
95 |
|
} ( qw/time lat lon speed course/ ) |
96 |
|
), |
97 |
|
}; |
98 |
|
|
99 |
my ( undef, $time, $validity, $lat, $lat_ns, $lon, $lon_ew, $speed, $course, $date, $var, $var_ew ) = @a; |
$points++; |
100 |
|
|
101 |
next unless $validity eq 'A'; |
} |
102 |
|
close($fh); |
103 |
|
|
104 |
my $point = recalc( $lat, $lat_ns, $lon, $lon_ew ) || next; |
if ( $points > 0 ) { |
105 |
|
|
106 |
$map->add_marker( |
my $map = HTML::GoogleMaps->new( |
107 |
point => $point, |
key => $map_key, |
108 |
html => "Time: $time<br/>Validity: $validity</br>Lat: $lat $lat_ns<br/>Lon: $lon $lon_ew<br/>Speed: $speed<br/>Course: $course", |
width => '800px', |
109 |
) if $validity eq 'A'; |
height => '600px', |
110 |
|
); |
111 |
|
#$map->center(point => "Zagreb, Hrvatska"); |
112 |
|
|
113 |
|
#$map->zoom(10); |
114 |
|
$map->v2_zoom(20); |
115 |
|
$map->controls("large_map_control", "map_type_control"); |
116 |
|
$map->map_type('hybrid'); |
117 |
|
$map->center( $center_point ) if $q->param('line') && $center_point; |
118 |
|
|
119 |
|
if ( $q->param('line') ) { |
120 |
|
warn "## points = ",dump( @points ); |
121 |
|
$map->add_polyline( points => [ @points ] ); |
122 |
|
} |
123 |
|
|
124 |
|
my $placemarks = 0; |
125 |
|
|
126 |
|
if ( my $placemark = $q->param('placemark') ) { |
127 |
|
if ( $q->param('placemark_filter') eq 'every' ) { |
128 |
|
foreach my $o ( 0 .. $#placemarks ) { |
129 |
|
next unless $o % $placemark == 0; |
130 |
|
$map->add_marker( %{ $placemarks[$o] } ); |
131 |
|
$placemarks++; |
132 |
|
} |
133 |
|
} else { |
134 |
|
# total of |
135 |
|
my $d = $points / ( $placemark - 1 ); |
136 |
|
foreach my $p ( 0 .. $placemark - 2 ) { |
137 |
|
my $o = int($p * $d); |
138 |
|
die "no placemark $p at $o from total of $#placemarks" unless $placemarks[$o]; |
139 |
|
$map->add_marker( %{ $placemarks[$o] } ); |
140 |
|
$placemarks++; |
141 |
|
} |
142 |
|
# add last one |
143 |
|
$map->add_marker( %{ $placemarks[$#placemarks] } ); |
144 |
|
$placemarks++; |
145 |
|
} |
146 |
|
} else { |
147 |
|
# show every placemark |
148 |
|
foreach my $marker ( @placemarks ) { |
149 |
|
$map->add_marker( %$marker ); |
150 |
|
$placemarks++; |
151 |
|
} |
152 |
|
} |
153 |
|
|
154 |
|
my ( $map_div, $map_script ); |
155 |
|
( $head, $map_div, $map_script ) = $map->render; |
156 |
|
|
157 |
|
$html .= join('', qq{ |
158 |
|
$points points from <tt>}, $q->param('trace'), qq{</tt> showing }, |
159 |
|
$#points > 0 ? $#points + 1 . ' points' . ( $placemarks ? ' and ' : '' ) : '', |
160 |
|
$placemarks ? $placemarks . ' placemarks' : '', |
161 |
|
qq{ |
162 |
|
$map_div |
163 |
|
$map_script |
164 |
|
<a href="http://aprs.gids.nl/nmea/">GPS - NMEA sentence information</a> |
165 |
|
}); |
166 |
|
|
167 |
|
} else { |
168 |
|
$html .= '<em>No points found for ' . $q->param('trace') . '</em>'; |
169 |
} |
} |
|
} |
|
|
close($fh); |
|
|
|
|
|
#$map->zoom(10); |
|
|
#$map->v2_zoom(0); |
|
|
$map->controls("large_map_control", "map_type_control"); |
|
|
$map->map_type('hybrid'); |
|
170 |
|
|
171 |
my ($head, $map_div, $map_script) = $map->render; |
} |
172 |
|
|
|
print "Content-type: text/html\n\r\n\r"; |
|
173 |
print qq{ |
print qq{ |
174 |
<html> |
<html> |
175 |
|
<head> |
176 |
<title>Read GPS - NMEA sentence and display it on GoogleMaps</title> |
<title>Read GPS - NMEA sentence and display it on GoogleMaps</title> |
177 |
<head>$head</head> |
$head |
178 |
|
</head> |
179 |
<body> |
<body> |
180 |
$map_div |
$html |
|
|
|
|
$map_script |
|
|
<a href="http://aprs.gids.nl/nmea/">GPS - NMEA sentence information</a> |
|
181 |
</body> |
</body> |
182 |
</html> |
</html> |
183 |
}; |
}; |