5/25/2013 - 7:26 PM

Perl script to convert KML data exported from Google Latitude into GPX data.

Perl script to convert KML data exported from Google Latitude into GPX data.

# ONO Hiroki onohiroki@cup.com
# http://onohiroki.cycling.jp/comp-google-latitude
# Usage: $ ./latitude_kml2gpx.pl *.kml

use strict;
use warnings;
use Data::Dumper;
use Encode;
use utf8;
use open ":utf8";
binmode STDIN,  ":utf8";
binmode STDOUT, ":utf8";
binmode STDERR, ":utf8";

foreach my $datafile (@ARGV) {
    printf "input: %-40s", $datafile;
    my $filename_str = $datafile;
    $filename_str =~ s/\.kml$//gi;
    #$filename_str =~ s/[^A-Za-z]//gi;
    my $output_filename = $filename_str.".gpx";

    unless (open KMLFILE, "< $datafile") {
        die "Cannot open file:$!";

    unless (open GPXFILE, "> $output_filename") {
        die "Cannot open file:$!";

    print GPXFILE <<'EOS';
<gpx xmlns="http://www.topografix.com/GPX/1/1"

    my $time = q{};
    my $date = q{};
    while (<KMLFILE>) {
        if (m#<when>(.*)</when>#) {
            $time = $1;
            if ($date eq q{}) {
                $date = $time;
                $date =~ s/T.*$//;
                print GPXFILE "  <name>$date</name>\n";
                print GPXFILE "  <desc>$date</desc>\n";
                print GPXFILE "  <trkseg>\n";
            $time = convert_datetime($time);
        if (m#<gx:coord>(\S+)\s+(\S+)\s+(\S+)</gx:coord>#) {
            print GPXFILE q{   <trkpt lat="}.$2.q{" lon="}.$1.q{">};
            print GPXFILE "\n    <ele>$3</ele>\n    <time>$time</time>\n   </trkpt>\n";
    close KMLFILE;
    print GPXFILE "  </trkseg>\n </trk>\n</gpx>\n";
    close GPXFILE;
    print " output: ", $output_filename;
    print "\n";

sub convert_datetime {
    use DateTime::Format::HTTP;
    my $str = shift;
    my $dt = DateTime::Format::HTTP->parse_datetime($str);
    return $dt . "Z";

KML to GPX converter

Download GPS data from Google Latitude

Google Latitude location history KML export is very interesting. GPS track log is easy to handle geotagging photos than Google Latitude KML data file. I wrote a perl program to convert from KML to GPX track log.

Google Latitude / KML data download

Convert Google Latitude KML data files to GPX track log data files


Install DateTime::Format::HTTP

$ cpanm DateTime::Format::HTTP
--> Working on DateTime::Format::HTTP
Fetching http://www.cpan.org/authors/id/C/CK/CKRAS/DateTime-Format-HTTP-0.40.tar.gz ... OK
Configuring DateTime-Format-HTTP-0.40 ... OK
Building and testing DateTime-Format-HTTP-0.40 ... OK
Successfully installed DateTime-Format-HTTP-0.40
1 distribution installed

Convert KML to GPX track log

$ ./latitude_kml2gpx.pl *.kml
input: history-05-19-2013.kml                   output: history-05-19-2013.gpx
input: history-05-20-2013.kml                   output: history-05-20-2013.gpx
input: history-05-21-2013.kml                   output: history-05-21-2013.gpx
input: history-05-22-2013.kml                   output: history-05-22-2013.gpx



  • Rewrite perl script