środa, 7 marca 2012

Import KML files to GoogleMaps

The nuisance of GoogleMaps is that it does not know GPX and only KML/GeoRSS files can be uploaded. Fortunately it is easy to convert GPX to KML with gpsbabel:


gpsbabel -i gpx -f file.gpx -x simplify,count=333 -o kml -F file.kml

If there are several files they have to be convert one-by-one and then upload to Google. It would be more comfortable to merge them first and upload it in one go rather than uploading each individually.

As I could not find how to merge serveral GPX files into one using gpsbabel (merge option in gpsbabel puts all track points from all tracks into a single track and sorts them by time stamp. Points with identical time stamps will be dropped) I worked out the following simple Perl script:


#!/usr/bin/perl
#
# Combine GPX files into one
# usage: gpxmerge file1 file2 file3 ....
#
use XML::DOM;
binmode(STDOUT, ":utf8");

my $parser = new XML::DOM::Parser;

for my $file2parse (@ARGV) {
my $doc = $parser->parsefile ($file2parse);

for my $w ( $doc->getElementsByTagName ("wpt") ) { $waypoints .= $w->toString() . "\n"; }
for my $r ( $doc->getElementsByTagName ("rte") ) { $routes .= $r->toString() . "\n"; }
for my $t ( $doc->getElementsByTagName ("trk") ) { $tracks .= $t->toString() . "\n"; }
}

print "<?xml version='1.0' encoding='UTF-8' ?>
<gpx version='1.1' creator='GPXmerger'
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'>\n
<author>tomasz przechlewski </author>
<email>tprzechlewski[at]acm.org</email>
<url>http://pinkaccordions.homelinux.org/Geo/gpx/</url>";

print "$waypoints\n$routes\n$tracks\n";

print "</gpx>\n";

The resulting file structure is as follows: first all waypoints, then all routes and finally all tracks. This is perfectly legal GPX file.

Now I convert GPX to KML using gpsbabel:


gpsbabel -i gpx -f file.gpx -x simplify,count=333 -o kml -F file.kml

Since gpsbabel generates pretty verbose KML files I simplify them using XSLT stylesheet (perhaps this step is superfluous):


xsltproc -o simplified.kml kml2kml.xsl file.kml

and the stylesheet looks like:



<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:kml="http://www.opengis.net/kml/2.2" >

<xsl:output method="xml" indent='yes' />

<xsl:template match="/">

<!-- ;; http://www.jonmiles.co.uk/2007/07/using-xpaths-text-function/ ;; -->

<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
<Document>


<Folder>
<name>Waypoints</name>

<xsl:for-each select="//kml:Folder[kml:name/text()='Waypoints']/kml:Placemark//kml:Point">
<Placemark>
<!-- <name><xsl:value-of select='../kml:name'/></name> --><!-- niepotrzebne -->
<Point>
<coordinates>
<xsl:value-of select="kml:coordinates"/>
</coordinates>
</Point>
</Placemark>
</xsl:for-each>

</Folder>


<Folder>
<name>Tracks</name>

<xsl:for-each select="//kml:Placemark//kml:LineString">

<Placemark>
<name>Path</name>

<LineString>
<tessellate>1</tessellate>
<coordinates>

<xsl:value-of select="kml:coordinates"/>

</coordinates>
</LineString>
</Placemark>

</xsl:for-each>

</Folder>

</Document>
</kml>

</xsl:template>
</xsl:stylesheet>

Now KML file is ready to be imported to Google maps via Maps → My Places → Create map.

The result of example conversion can be be found here.

Brak komentarzy:

Prześlij komentarz