Styling KML to GML

The proposed use of Google Earth to mark some places and produce simple datasets emerged as big success and was widely adopted by some of my colleagues. Actually one of them is using Google Earth pretty intensive.

So I extended my begun KML to CSV XSL stylesheet to take over Google Earth KMLs in a more convenient way. With the help of this XSL stylesheet you can transform a KML to a GML file. As input examples I took the three listings at Keyholes KML-tutorial. They cover three geometry types, so the developed XSL stylesheet should support:

  • Placemarks
  • Paths
  • Polygons.

With the help of Marc Liyanages TestXSLT I was able to test my XSL stylesheet against Sablotron, Gnome Libxslt, Saxon and Xalan-J. You’ll need one of those (or any other) XSLT processor to transform your KML to GML.

For processing XSLT on Mac OS X you can just use Marcs application, it’s easy to use and extremely helpful for developing and testing XSLT.
An online implementation of Gnome libxslt you can find here. Just copy & paste my XSL stylesheet and your KML into the corresponding fields and hit process. Copy the result in any text editor and save it as GML. E voilà!
How to transform XML with a simple java application is described in that tutorial and based on .NET here.

As in the former KML to CSV transformation you first have to delete the KML namespace declaration before processing your KML, otherwise it won’t work:

xmlns="http://earth.google.com/kml/2.0"

If you want to get some ESRI shapefiles out of your GML you can further use ogr2ogr for instance:

ogr2ogr -f "ESRI Shapefile" myfile.shp myfile.gml

I only did some very basic testing with Quantum GIS (which opens GML directly) and ArcGIS (via ogr2ogr). Possibly it won’t work with every KML.
Suggestions will be appreciated!


  • http://www.spatiallyadjusted.com/?p=443 Spatially Adjusted with James Fee » Blog Archive » Christian Spanring Transforms KML to GML

    [...] Link – Styling KML to GML So I extended my begun KML to CSV XSL stylesheet to take over Google Earth KMLs in a more convenient way. With the help of this XSL stylesheet you can transform a KML to a GML file. [...]

  • http://perrygeo.net Matt Perry

    Nice work! This is a superbly useful tool since so many folks are publishing their data straight to KML. It’s nice to know that all that info won’t be locked into one application!

    Your comment about ogr2ogr got me thinking about ways to automate the kml->shapefile process and I came up with a little script that seems to work: http://www.perrygeo.net/wordpress/?p=3. I’ve tried it on a few kml files and your stylesheet has worked each time. Thanks a million!

    Thanks a million for this one…

  • http://spanring.name/ Christian

    You’re welcome!
    With your script the whole process is still more convenient and straightforward. Thanks for pointing out the lack of 3D support in Quantum GIS!

  • Carl

    Christian -

    Nice tool. I just passed this information onto the GML Working Group in the OGC. Now that Google is a member of the OGC, we may be able to work any “harmonization” issues related to the use of KML and GML.

    Regards

  • http://spanring.name/ Christian

    Thanks Carl!
    As you can see in my XSL stylesheet harmonization of KML and GML shouldn’t be too difficult, even though this is a very basic example. It would be great to see some GML compatible Google products.

  • http://grass.itc.it Markus

    Hi,

    great work! You can use latest GRASS 6.1 to work
    with 3D vector data, see
    http://grass.itc.it/grass61/screenshots/vector.php

    If you dislike the GRASS viewer NVIZ, just export
    to VTK format and use ParaView or other VTK viewers.

    Regards

blog comments powered by Disqus