/[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

Diff of /web/googlemap.cgi

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.13  
changed lines
  Added in v.27

  ViewVC Help
Powered by ViewVC 1.1.26