--- web/googlemap.cgi 2007/12/06 14:03:00 23 +++ web/googlemap.cgi 2007/12/07 03:44:12 27 @@ -9,10 +9,11 @@ use File::Find; use Data::Dump qw/dump/; -use lib '../'; +use lib '../lib'; +use blib; use NMEA; -my $trace_path = '/home/dpavlin/x/openmoko/gps/'; +my $trace_path = '../nmea/'; # http://localhost/ my $map_key = 'ABQIAAAAVQ5szt9Jd8ws6vgfVQOEmhT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQ1cKf0DwFJcwtpESJEI0hL8QgtYg'; @@ -38,43 +39,49 @@ "$_ (" . (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->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 every ', + ' points and ', + $q->popup_menu( + -name => 'placemark_filter', + -values => [ 'total of', 'every' ], + ), $q->textfield( - -name => 'nth_placemark', + -name => 'placemark_count', -value => 5, -size => 2, -onChange => 'trace_frm.submit()', ), - 'th placemark', + ' placemarks', $q->end_form, ); -my @points; -my $center_point; 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 $nth_placemark = $q->param('nth_placemark'); + my $center_point; + my @points; + my @placemarks; open(my $fh, '<', $trace) || die "can't open $trace: $!"; while( <$fh> ) { @@ -84,49 +91,118 @@ my $point = [ $hash->{lon}, $hash->{lat} ]; $center_point ||= $point; + push @points, $point; - if ( $q->param('line') ) { - push @points, $point; - } - - if ( ! $q->param('line') || - $nth_placemark && $points % $nth_placemark == 0 - ) { - - $map->add_marker( - point => $point, - html => join('
', - map { - ucfirst($_) . ': ' . $hash->{$_} - } ( qw/time lat lon speed course/ ) - ), - ); - - } - - $points++; + push @placemarks, { + point => $point, + html => join('
', + map { + ucfirst($_) . ': ' . $hash->{$_} + } ( qw/number time lat lon speed course/ ) + ), + }; } close($fh); - if ( $points > 0 ) { + if ( $#points >= 0 ) { + + my $map = HTML::GoogleMaps->new( + key => $map_key, + width => '800px', + height => '600px', + ); + #$map->center(point => "Zagreb, Hrvatska"); #$map->zoom(10); - #$map->v2_zoom(0); + $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; - if ( $q->param('line') ) { - warn "## points = ",dump( @points ); - $map->add_polyline( points => [ @points ] ); + 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 @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, + ); + + die "hum?" unless $#poly_points == $points - 1; + + $map->add_polyline( points => [ @poly_points ] ) if @poly_points; + + 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 ); + }, + array => \@placemarks, + ); + my ( $map_div, $map_script ); ( $head, $map_div, $map_script ) = $map->render; - $html .= join('', qq{ -

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

+ $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