--- web/googlemap.cgi 2007/12/07 00:59:05 24 +++ web/googlemap.cgi 2007/12/08 20:37:50 31 @@ -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,8 +26,6 @@ my $q = CGI->new; -print $q->header; - my $head = ''; my $html = join('', qq{

Select GPS NMEA dump

@@ -38,31 +38,40 @@ "$_ (" . (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( -value => 'Show trace' ), + $q->submit( + -name => 'show', + -value => 'Show trace' + ), $q->br, - 'Draw as ', - $q->checkbox( - -name => 'line', + 'Draw ', + $q->popup_menu( + -name => 'points_filter', + -values => [ 'all', 'every', 'total of' ], + ), + $q->textfield( + -name => 'points_count', + -value => '', + -size => 2, -onChange => 'trace_frm.submit()', ), - ' with ', + ' points and ', $q->popup_menu( -name => 'placemark_filter', - -values => [ 'every', 'total of' ], + -values => [ 'total of', 'every' ], ), $q->textfield( - -name => 'placemark', + -name => 'placemark_count', -value => 5, -size => 2, -onChange => 'trace_frm.submit()', ), ' placemarks', - $q->end_form, + $q->br, ); @@ -72,7 +81,6 @@ $trace = "$trace_path/$trace"; - my $points = 0; my $center_point; my @points; my @placemarks; @@ -92,84 +100,154 @@ html => join('
', map { ucfirst($_) . ': ' . $hash->{$_} - } ( qw/time lat lon speed course/ ) + } ( qw/number time lat lon speed course/ ) ), + %$hash, }; - $points++; - } close($fh); - if ( $points > 0 ) { + if ( $#points >= 0 ) { + + 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', ); - #$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; + 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, + ); - if ( $q->param('line') ) { - warn "## points = ",dump( @points ); - $map->add_polyline( points => [ @points ] ); - } + die "hum?" unless $#poly_points == $points - 1; - my $placemarks = 0; + my @filtered_placemarks; - if ( my $placemark = $q->param('placemark') ) { - if ( $q->param('placemark_filter') eq 'every' ) { - foreach my $o ( 0 .. $#placemarks ) { - next unless $o % $placemark == 0; - $map->add_marker( %{ $placemarks[$o] } ); - $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 =~ m/KML/i ) { + print $q->header( + -type => 'application/vnd.google-earth.kml+xml', + ), + KML->output( placemarks => \@filtered_placemarks ); + exit; } else { - # total of - my $d = $points / ( $placemark - 1 ); - foreach my $p ( 0 .. $placemark - 2 ) { - my $o = int($p * $d); - die "no placemark $p at $o from total of $#placemarks" unless $placemarks[$o]; - $map->add_marker( %{ $placemarks[$o] } ); - $placemarks++; - } - # add last one - $map->add_marker( %{ $placemarks[$#placemarks] } ); - $placemarks++; - } - } else { - # show every placemark - foreach my $marker ( @placemarks ) { - $map->add_marker( %$marker ); - $placemarks++; + 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('', qq{ -$points points from }, $q->param('trace'), qq{ showing }, - $#points > 0 ? $#points + 1 . ' points' . ( $placemarks ? ' and ' : '' ) : '', + $html .= join('', +$#points + 1, ' points from ', $q->param('trace'), ' showing ', + $points ? $points . ' points' . ( $placemarks ? ' and ' : '' ) : '', $placemarks ? $placemarks . ' placemarks' : '', + ' export to ', + $q->submit( + -name => 'export', + -value => 'KML' + ), 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') . ''; } + $html .= $q->end_form; } +print $q->header; print qq{