--- web/googlemap.cgi 2007/12/03 14:25:40 14 +++ web/googlemap.cgi 2007/12/07 15:53:41 30 @@ -9,10 +9,12 @@ use File::Find; use Data::Dump qw/dump/; -use lib '../'; +use lib '../lib'; +use blib; use NMEA; +use KML; -my $trace_path = '/home/dpavlin/x/openmoko/gps/'; +my $trace_path = '../nmea/'; # http://localhost/ my $map_key = 'ABQIAAAAVQ5szt9Jd8ws6vgfVQOEmhT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQ1cKf0DwFJcwtpESJEI0hL8QgtYg'; @@ -24,15 +26,11 @@ my $q = CGI->new; -print $q->header; - -print qq{ - -Read GPS - NMEA sentence and display it on GoogleMaps - +my $head = ''; +my $html = join('', qq{

Select GPS NMEA dump

}, - $q->start_form, + $q->start_form( -id => 'trace_frm' ), $q->popup_menu( -name => 'trace', -values => [ @@ -40,58 +38,223 @@ "$_ (" . (stat("$trace_path/$_"))[7] . " bytes)" } sort { - (stat("$trace_path/$a"))[10] <=> (stat("$trace_path/$b"))[10] + (stat("$trace_path/$a"))[9] <=> (stat("$trace_path/$b"))[9] } @traces ], + -onChange => 'trace_frm.submit()', + ), + $q->submit( + -name => 'show', + -value => 'Show trace' + ), + ' Export to ', + $q->submit( + -name => 'export', + -value => 'KML' + ), + $q->br, + 'Draw ', + $q->popup_menu( + -name => 'points_filter', + -values => [ 'all', 'every', 'total of' ], ), - $q->submit( -value => 'Show trace' ), - $q->end_form; + $q->textfield( + -name => 'points_count', + -value => '', + -size => 2, + -onChange => 'trace_frm.submit()', + ), + ' points and ', + $q->popup_menu( + -name => 'placemark_filter', + -values => [ 'total of', 'every' ], + ), + $q->textfield( + -name => 'placemark_count', + -value => 5, + -size => 2, + -onChange => 'trace_frm.submit()', + ), + ' placemarks', + $q->end_form, +); + + if ( my $trace = $q->param('trace') ) { $trace =~ s/\s.+$//; - my $map = HTML::GoogleMaps->new(key => $map_key); - #$map->center(point => "Zagreb, Hrvatska"); - $trace = "$trace_path/$trace"; - my $points = 0; + my $center_point; + my @points; + my @placemarks; open(my $fh, '<', $trace) || die "can't open $trace: $!"; while( <$fh> ) { my $hash = NMEA->line( $_ ) || next; - $map->add_marker( - point => [ $hash->{lon}, $hash->{lat} ], + my $point = [ $hash->{lon}, $hash->{lat} ]; + $center_point ||= $point; + + push @points, $point; + + push @placemarks, { + point => $point, html => join('
', map { ucfirst($_) . ': ' . $hash->{$_} - } ( qw/time lat lon speed course/ ) + } ( qw/number time lat lon speed course/ ) ), - ); + %$hash, + }; - $points++; } close($fh); - #$map->zoom(10); - #$map->v2_zoom(0); - $map->controls("large_map_control", "map_type_control"); - $map->map_type('hybrid'); + if ( $#points >= 0 ) { - my ($head, $map_div, $map_script) = $map->render; + sub filter_array { + my $o = {@_}; + my ( $count, $filter, $code ) = + ( $o->{count}, $o->{filter}, $o->{code} ) ; + confess "no CODE?" unless ref($code) eq 'CODE'; + my @array = @{ $o->{array} }; + + warn "count: $count filter: $filter\n"; + + my $code_calls = 0; + + if ( $count && $filter =~ m/every/ ) { + foreach my $o ( 0 .. $#array ) { + next unless $o % $count == 0; + $code->( $array[$o] ); + $code_calls++; + } + } elsif ( $count && $filter =~ m/total/ ) { + # total of + if ( $count < 2 ) { + # first + if ( $array[0]) { + $code->( $array[0] ); + $code_calls++; + }; + # last + if ( $count > 1 && $#array > 0 ) { + $code->( $array[$#array] ); + $code_calls++; + }; + return $code_calls; + } + + my $d = $#array / ( $count - 1 ); + foreach my $p ( 0 .. $count - 1 ) { + my $o = int($p * $d); + die "no element $p at $o from total of ",$#array + 1 unless $array[$o]; + $code->( $array[$o] ); + $code_calls++; + } + } else { + # show every + foreach my $e ( @array ) { + $code->( $e ); + $code_calls++; + } + } + return $code_calls; + } + + my $map = HTML::GoogleMaps->new( + key => $map_key, + width => '800px', + height => '600px', + ); + + my @poly_points; + my $points = filter_array( + count => $q->param('points_count'), + filter => $q->param('points_filter'), + code => sub { + my $point = shift; + push @poly_points, $point; + }, + array => \@points, + ); - print qq{ -

Plotting $points points from }, $q->param('trace'), qq{

+ die "hum?" unless $#poly_points == $points - 1; + + my @filtered_placemarks; + + my $placemarks = filter_array( + count => $q->param('placemark_count'), + filter => $q->param('placemark_filter'), + code => sub { + my $placemark = shift; + $map->add_marker( %$placemark, noformat => 1 ); + push @filtered_placemarks, $placemark; + }, + array => \@placemarks, + ); + + if ( my $export = $q->param('export') ) { + if ( $export eq 'KML' ) { + print $q->header( + -type => 'application/xhtml+xml', + ), + KML->output( placemarks => \@filtered_placemarks ); + exit; + } else { + die "unknown export format $export"; + } + } + + + #$map->center(point => "Zagreb, Hrvatska"); + + #$map->zoom(10); + $map->v2_zoom(20); + $map->controls("large_map_control", "map_type_control"); + $map->map_type('hybrid'); + $map->center( $center_point ) if $q->param('line') && $center_point; + + $map->add_polyline( points => [ @poly_points ] ) if @poly_points; + + my ( $map_div, $map_script ); + ( $head, $map_div, $map_script ) = $map->render; + + $html .= join('', +$#points + 1, ' points from ', $q->param('trace'), ' showing ', + $points ? $points . ' points' . ( $placemarks ? ' and ' : '' ) : '', + $placemarks ? $placemarks . ' placemarks' : '', + qq{ $map_div $map_script GPS - NMEA sentence information - }; + }); + + my $stats = NMEA->stats; + $html .= ''; + foreach my $n ( keys %$stats ) { + $html .= ""; + } + $html .= '
$n" . $stats->{$n} . "
'; + + } else { + $html .= 'No points found for ' . $q->param('trace') . ''; + } } +print $q->header; print qq{ + + +Read GPS - NMEA sentence and display it on GoogleMaps +$head + + +$html };