Tag Archive for 'XSLT'

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!


A little KML to CSV converter

Today I had to take over some places a collegue marked in Google Earth. Maybe I was too blind or actually too stupid but I was not able find a simple tool for converting KML to an ArcGIS-compatible format. Except FME of course, but that would be the overkill for my purposes. So I wrote a quick and dirty XSLT-hack to convert a KML-file to a very basic CSV-structure, which I could later import as X/Y coordinates to ArcGIS.

I had following structure in Google Earth:

My Places
__Folder
____Placemark 1
____Placemark 2

Saving the folder as kml brought me this file (GE_original.kml).

For some reasons (I’m no XSLT expert) I had to delete the KML namespace declaration and of course add the XSL-stylesheet information. See the modified KML file here (GE_modified.kml).

The XSL-file I wrote you can find here (kml2csv.xsl).

Put your modfied KML file in the same folder as kml2csv.xsl and open it in a XSLT compatible Browser. You should see now coordinates and placemark names in a CSV structure instead of a KML-XML-tree:

Name,X,Y,Z
Placemark 1,13.08349600542125,47.79234503541417,0
Placemark 2,13.27061184961974,47.68484958984244,0

Save it as text file, open it in ArcGIS (or in your prefered GIS client, spatially enabled DBMS, spreadsheet app or wherever you want) and add the X/Y coordinates to your layer. For now the XSLT is limited to placemarks and it only works if your placemarks are contained by exactly one folder – as I said, a quick and dirty hack.

Further improvements to come (as soon as I have some time left):

  • the conversion of KML to GML would be a more elegant way
  • support of lines and polygons

Google Earth is a nifty tool to create some spatial notes and simple geographic objects. It’s easy to use, offers access to basic geodata but it’s definitely not a tool to produce large and complex geodatasets.