Skip to content

Instantly share code, notes, and snippets.

@johnjohndoe
Last active December 29, 2019 20:18
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnjohndoe/5650449 to your computer and use it in GitHub Desktop.
Save johnjohndoe/5650449 to your computer and use it in GitHub Desktop.
Perl script to convert KML data exported from Google Latitude into GPX data.

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

Example

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
$

Changelog

2013-05-23

  • Rewrite perl script

Author

Website

#!/usr/bin/perl
# 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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1
http://www.topografix.com/GPX/1/1/gpx.xsd">
<trk>
EOS
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);
$dt->set_time_zone('+0000');
return $dt . "Z";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment