#!/usr/bin/perl use LWP::Simple; use XML::Simple; # I'm just a simple guy. use Data::Dumper; # set a path to the gpsbabel program my $gpsbabel_cmd = 'gpsbabel'; # $ARGV could be 'sfo to oak to den' or 'myfile.gpx' # or myfile1.gpx to sfo to myfile2.gpx to den my $st = join " ", @ARGV; my @to_list= split / to /i, $st; # each location is delimited with the word 'to' # if a location is a file that exists and ends with gpx # the file is parsed with gpsbabel and the lat, longs # added to our list of locations my @locations; # human readable names my @names;; foreach my $row (@to_list) { $row =~ s/(^\s+)|(\s+$)//g; if ($row =~ /gpx$/i) { if (! -e $row) { warn "Sorry but gpx file $row doesn't exit\n"; next; } my @list = `gpsbabel -i gpx -f $row -o csv -F -`; foreach my $row (@list) { my ($lat, $long, $name) = split (/,/, $row); $lat =~ s/\s+//g; $long =~ s/\s+//g; push @locations, "$lat, $long";; push @names, $name; } } else { push @locations, $row; push @names, $row; } } # we have a series of locations that should be google maps parseable my $route_list = []; # this will be an array of hashrefs of the segments my $last_location = ''; my $cnt=0; foreach my $location (@locations) { if ($last_location) { my $data = get_one_route($last_location, $location); my $route = parse_google_js($$data); push @$route_list, $route; print "=" x 65 . "\n"; print "new route segment $last_location to $location\n"; print $names[$cnt-1] . ' to ' . $names[$cnt] . "\n"; output_route($route); } $last_location = $location; $cnt++; } exit; sub get_one_route { my ($from, $to) = @_; # get the js page my $data = get "http://maps.google.com/maps?q=$from+to+$to&output=js"; return \$data; } sub parse_google_js { my $st = shift; # get rid of everything before the opening tag $st =~ s/^.+\ $st =~ s/(\<\/segments).+/$1>/s; my $xml = XML::Simple->new( ForceArray => [ 'segment' ], KeyAttr => [], NormaliseSpace => 2 ); return $xml-> XMLin($st); } sub output_route { my $ref = shift; foreach my $seg (@{$ref->{segment}}){ print "-" x 65 . "\n"; my $cnt=0; #print " all of seq is: " . Dumper($seg); if (ref($seg->{content})) { foreach my $c (@{$seg->{content}}) { print " $c\t"; print $seg->{b}->[$cnt] . "\n"; $cnt++; } } else { print $seg->{content} . "\t"; print $seg->{b} . "\n"; } print "$seg->{distance} ($seg->{meters} meters) for $seg->{time}\n"; } }