/[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 18 by dpavlin, Mon Dec 3 15:06:33 2007 UTC revision 29 by dpavlin, Fri Dec 7 04:05:07 2007 UTC
# Line 9  use CGI::Carp qw/fatalsToBrowser/; Line 9  use CGI::Carp qw/fatalsToBrowser/;
9  use File::Find;  use File::Find;
10  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
11    
12  use lib '../';  use lib '../lib';
13    use blib;
14  use NMEA;  use NMEA;
15    
16  my $trace_path = '/home/dpavlin/x/openmoko/gps/';  my $trace_path = '../nmea/';
17    
18  # http://localhost/  # http://localhost/
19  my $map_key = 'ABQIAAAAVQ5szt9Jd8ws6vgfVQOEmhT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQ1cKf0DwFJcwtpESJEI0hL8QgtYg';  my $map_key = 'ABQIAAAAVQ5szt9Jd8ws6vgfVQOEmhT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQ1cKf0DwFJcwtpESJEI0hL8QgtYg';
# Line 30  my $head = ''; Line 31  my $head = '';
31  my $html = join('', qq{  my $html = join('', qq{
32  <h1>Select GPS NMEA dump</h1>  <h1>Select GPS NMEA dump</h1>
33          },          },
34          $q->start_form,          $q->start_form( -id => 'trace_frm' ),
35          $q->popup_menu(          $q->popup_menu(
36                  -name => 'trace',                  -name => 'trace',
37                  -values => [                  -values => [
# Line 38  my $html = join('', qq{ Line 39  my $html = join('', qq{
39                                  "$_ (" . (stat("$trace_path/$_"))[7] . " bytes)"                                  "$_ (" . (stat("$trace_path/$_"))[7] . " bytes)"
40                          }                          }
41                          sort {                          sort {
42                                  (stat("$trace_path/$a"))[10] <=> (stat("$trace_path/$b"))[10]                                  (stat("$trace_path/$a"))[9] <=> (stat("$trace_path/$b"))[9]
43                          } @traces                          } @traces
44                  ],                  ],
45                    -onChange => 'trace_frm.submit()',
46          ),          ),
47          $q->submit( -value => 'Show trace' ),          $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,          $q->end_form,
73  );  );
74    
75    
76    
77  if ( my $trace = $q->param('trace') ) {  if ( my $trace = $q->param('trace') ) {
78          $trace =~ s/\s.+$//;          $trace =~ s/\s.+$//;
79    
         my $map = HTML::GoogleMaps->new(key => $map_key);  
         #$map->center(point => "Zagreb, Hrvatska");  
   
80          $trace = "$trace_path/$trace";          $trace = "$trace_path/$trace";
81    
82          my $points = 0;          my $center_point;
83            my @points;
84            my @placemarks;
85    
86          open(my $fh, '<', $trace) || die "can't open $trace: $!";          open(my $fh, '<', $trace) || die "can't open $trace: $!";
87          while( <$fh> ) {          while( <$fh> ) {
88    
89                  my $hash = NMEA->line( $_ ) || next;                  my $hash = NMEA->line( $_ ) || next;
90    
91                  $map->add_marker(                  my $point = [ $hash->{lon}, $hash->{lat} ];
92                          point => [ $hash->{lon}, $hash->{lat} ],                  $center_point ||= $point;
93    
94                    push @points, $point;
95    
96                    push @placemarks, {
97                            point => $point,
98                          html => join('<br/>',                          html => join('<br/>',
99                                  map {                                  map {
100                                          ucfirst($_) . ': ' . $hash->{$_}                                          ucfirst($_) . ': ' . $hash->{$_}
101                                  } ( qw/time lat lon speed course/ )                                  } ( qw/number time lat lon speed course/ )
102                          ),                          ),
103                  );                  };
104    
                 $points++;  
105          }          }
106          close($fh);          close($fh);
107    
108          if ( $points > 0 ) {          if ( $#points >= 0 ) {
109    
110                    my $map = HTML::GoogleMaps->new(
111                            key => $map_key,
112                            width => '800px',
113                            height => '600px',
114                    );
115                    #$map->center(point => "Zagreb, Hrvatska");
116    
117                  #$map->zoom(10);                  #$map->zoom(10);
118                  #$map->v2_zoom(0);                  $map->v2_zoom(20);
119                  $map->controls("large_map_control", "map_type_control");                  $map->controls("large_map_control", "map_type_control");
120                  $map->map_type('hybrid');                  $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 );                  my ( $map_div, $map_script );
199                  ( $head, $map_div, $map_script ) = $map->render;                  ( $head, $map_div, $map_script ) = $map->render;
200    
201                  $html .= join('', qq{                  $html .= join('',
202  <h1>Plotting $points points from }, $q->param('trace'), qq{</h1>  $#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  $map_div
207  $map_script  $map_script
208  <a href="http://aprs.gids.nl/nmea/">GPS - NMEA sentence information</a>  <a href="http://aprs.gids.nl/nmea/">GPS - NMEA sentence information</a>
209                  });                  });
210    
211                    my $stats = NMEA->stats;
212                    $html .= '<table>';
213                    foreach my $n ( keys %$stats ) {
214                            $html .= "<tr><td>$n</td><td>" . $stats->{$n} . "</td></tr>";
215                    }
216                    $html .= '</table>';
217                    
218          } else {          } else {
219                  $html .= '<em>No points found for ' . $q->param('trace') . '</em>';                  $html .= '<em>No points found for ' . $q->param('trace') . '</em>';

Legend:
Removed from v.18  
changed lines
  Added in v.29

  ViewVC Help
Powered by ViewVC 1.1.26