/[maps]/web/googlemap.cgi
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /web/googlemap.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 27 - (hide annotations)
Fri Dec 7 03:44:12 2007 UTC (11 years, 5 months ago) by dpavlin
File size: 4682 byte(s)
added number
1 dpavlin 13 #!/usr/bin/perl
2    
3     use warnings;
4     use strict;
5    
6     use HTML::GoogleMaps;
7 dpavlin 14 use CGI;
8 dpavlin 13 use CGI::Carp qw/fatalsToBrowser/;
9 dpavlin 14 use File::Find;
10 dpavlin 13 use Data::Dump qw/dump/;
11    
12 dpavlin 27 use lib '../lib';
13     use blib;
14 dpavlin 14 use NMEA;
15    
16 dpavlin 27 my $trace_path = '../nmea/';
17 dpavlin 14
18 dpavlin 13 # http://localhost/
19     my $map_key = 'ABQIAAAAVQ5szt9Jd8ws6vgfVQOEmhT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQ1cKf0DwFJcwtpESJEI0hL8QgtYg';
20    
21 dpavlin 14 my @traces;
22     find({ wanted => sub {
23     push @traces, $_ if -f $_;
24     }}, $trace_path);
25 dpavlin 13
26 dpavlin 14 my $q = CGI->new;
27 dpavlin 13
28 dpavlin 14 print $q->header;
29 dpavlin 13
30 dpavlin 15 my $head = '';
31     my $html = join('', qq{
32 dpavlin 14 <h1>Select GPS NMEA dump</h1>
33     },
34 dpavlin 19 $q->start_form( -id => 'trace_frm' ),
35 dpavlin 14 $q->popup_menu(
36     -name => 'trace',
37     -values => [
38     map {
39     "$_ (" . (stat("$trace_path/$_"))[7] . " bytes)"
40     }
41     sort {
42 dpavlin 27 (stat("$trace_path/$a"))[9] <=> (stat("$trace_path/$b"))[9]
43 dpavlin 14 } @traces
44     ],
45 dpavlin 19 -onChange => 'trace_frm.submit()',
46 dpavlin 14 ),
47     $q->submit( -value => 'Show trace' ),
48 dpavlin 23 $q->br,
49 dpavlin 25 '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 dpavlin 23 -onChange => 'trace_frm.submit()',
59     ),
60 dpavlin 25 ' points and ',
61 dpavlin 24 $q->popup_menu(
62     -name => 'placemark_filter',
63 dpavlin 25 -values => [ 'total of', 'every' ],
64 dpavlin 24 ),
65 dpavlin 23 $q->textfield(
66 dpavlin 25 -name => 'placemark_count',
67 dpavlin 23 -value => 5,
68     -size => 2,
69     -onChange => 'trace_frm.submit()',
70     ),
71 dpavlin 24 ' placemarks',
72 dpavlin 15 $q->end_form,
73     );
74 dpavlin 13
75 dpavlin 23
76    
77 dpavlin 14 if ( my $trace = $q->param('trace') ) {
78     $trace =~ s/\s.+$//;
79 dpavlin 13
80 dpavlin 14 $trace = "$trace_path/$trace";
81 dpavlin 13
82 dpavlin 24 my $center_point;
83     my @points;
84     my @placemarks;
85 dpavlin 13
86 dpavlin 14 open(my $fh, '<', $trace) || die "can't open $trace: $!";
87     while( <$fh> ) {
88 dpavlin 13
89 dpavlin 14 my $hash = NMEA->line( $_ ) || next;
90 dpavlin 13
91 dpavlin 23 my $point = [ $hash->{lon}, $hash->{lat} ];
92     $center_point ||= $point;
93 dpavlin 13
94 dpavlin 24 push @points, $point;
95 dpavlin 23
96 dpavlin 24 push @placemarks, {
97     point => $point,
98     html => join('<br/>',
99     map {
100     ucfirst($_) . ': ' . $hash->{$_}
101 dpavlin 27 } ( qw/number time lat lon speed course/ )
102 dpavlin 24 ),
103     };
104 dpavlin 23
105 dpavlin 14 }
106     close($fh);
107 dpavlin 13
108 dpavlin 25 if ( $#points >= 0 ) {
109 dpavlin 13
110 dpavlin 24 my $map = HTML::GoogleMaps->new(
111     key => $map_key,
112     width => '800px',
113     height => '600px',
114     );
115     #$map->center(point => "Zagreb, Hrvatska");
116    
117 dpavlin 18 #$map->zoom(10);
118 dpavlin 24 $map->v2_zoom(20);
119 dpavlin 18 $map->controls("large_map_control", "map_type_control");
120     $map->map_type('hybrid');
121 dpavlin 23 $map->center( $center_point ) if $q->param('line') && $center_point;
122 dpavlin 13
123 dpavlin 25 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 dpavlin 23
130 dpavlin 25 warn "count: $count filter: $filter\n";
131 dpavlin 24
132 dpavlin 25 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 dpavlin 24 }
140 dpavlin 25 } elsif ( $count && $filter =~ m/total/ ) {
141 dpavlin 24 # total of
142 dpavlin 25 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 dpavlin 24 my $o = int($p * $d);
159 dpavlin 25 die "no element $p at $o from total of ",$#array + 1 unless $array[$o];
160     $code->( $array[$o] );
161     $code_calls++;
162 dpavlin 24 }
163 dpavlin 25 } else {
164     # show every
165     foreach my $e ( @array ) {
166     $code->( $e );
167     $code_calls++;
168     }
169 dpavlin 24 }
170 dpavlin 25 return $code_calls;
171 dpavlin 24 }
172    
173 dpavlin 25 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 dpavlin 18 my ( $map_div, $map_script );
199     ( $head, $map_div, $map_script ) = $map->render;
200    
201 dpavlin 25 $html .= join('',
202     $#points + 1, ' points from <tt>', $q->param('trace'), '</tt> showing ',
203     $points ? $points . ' points' . ( $placemarks ? ' and ' : '' ) : '',
204 dpavlin 24 $placemarks ? $placemarks . ' placemarks' : '',
205     qq{
206 dpavlin 14 $map_div
207     $map_script
208     <a href="http://aprs.gids.nl/nmea/">GPS - NMEA sentence information</a>
209 dpavlin 18 });
210    
211     } else {
212     $html .= '<em>No points found for ' . $q->param('trace') . '</em>';
213     }
214 dpavlin 13
215     }
216    
217     print qq{
218 dpavlin 15 <html>
219     <head>
220     <title>Read GPS - NMEA sentence and display it on GoogleMaps</title>
221     $head
222     </head>
223     <body>
224     $html
225 dpavlin 13 </body>
226     </html>
227     };

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26