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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26