<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="text" indent="no" encoding="iso-8859-1" />

<!--
 OSM version 0.5 to gnuplot data conversion stylesheet 
 usage:
 xsltproc
 -stringparam mode way
 -stringparam key railway
 -stringparam val '' osm2gnuplot.xsl OpenStreetmapData.osm > osm_gnuplot_rail.dat
 (note: please use double "-" before the stringparam arg)

 Copyright 2008 by Alexander Kurz

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3, or (at your option)
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 $Id: osm2gnuplot.xsl,v 1.1 2008/01/27 18:54:31 akurz Exp $
-->

<!-- argh!: overwrite default template -->
<xsl:template match="text()">
</xsl:template> 

<!-- main -->
<xsl:template match="/">
  <xsl:choose>
    <xsl:when test="$mode = 'poi' and $val ='' " ><xsl:call-template name="poi_by_key" /></xsl:when>
    <xsl:when test="$mode = 'poi' and $val !='' " ><xsl:call-template name="poi_by_keyandvalue" /></xsl:when>
    <xsl:when test="$mode = 'way' and $val ='' " ><xsl:call-template name="ways_by_key" /></xsl:when>
    <xsl:when test="$mode = 'way' and $val !='' " ><xsl:call-template name="ways_by_keyandvalue" /></xsl:when>
    <xsl:otherwise><xsl:message>please use stringparams mode=[poi,way], way, key </xsl:message></xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- poi by key -->
<xsl:template name="poi_by_key">
  <xsl:for-each select="//osm/node[tag[@k=$key]]" >
    <xsl:value-of select="@lon" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="@lat" />
    <xsl:text>&#10;</xsl:text>
  </xsl:for-each>
</xsl:template>

<!-- poi by key and value-->
<xsl:template name="poi_by_keyandvalue">
  <xsl:for-each select="//osm/node[tag[@k=$key][@v=$val]]" >
    <xsl:value-of select="@lon" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="@lat" />
    <xsl:text>&#10;</xsl:text>
  </xsl:for-each>
</xsl:template>

<!-- ways by key -->
<xsl:template name="ways_by_key">
  <xsl:for-each select="//osm/way[tag[@k=$key]]" >
    <xsl:text>#</xsl:text>
    <xsl:value-of select="tag[@k='name']/@v" />
    <xsl:text>&#10;</xsl:text>
    <xsl:for-each select="nd" >
      <xsl:variable name="ndref" select="@ref"/>
      <xsl:call-template name="node_coordinates">
        <xsl:with-param name="node_ref" select="$ndref"/>
      </xsl:call-template>
    </xsl:for-each>
    <xsl:text>&#10;</xsl:text>
  </xsl:for-each>
</xsl:template>

<!-- ways by key and value-->
<xsl:template name="ways_by_keyandvalue">
  <xsl:for-each select="//osm/way[tag[@k=$key][@v=$val]]" >
    <xsl:text>#</xsl:text>
    <xsl:value-of select="tag[@k='name']/@v" />
    <xsl:text>&#10;</xsl:text>
    <xsl:for-each select="nd" >
      <xsl:variable name="ndref" select="@ref"/>
      <xsl:call-template name="node_coordinates">
        <xsl:with-param name="node_ref" select="$ndref"/>
      </xsl:call-template>
    </xsl:for-each>
    <xsl:text>&#10;</xsl:text>
  </xsl:for-each>
</xsl:template>

<!-- get node coordinates -->
<xsl:template name="node_coordinates">
  <xsl:param name="node_ref" />
  <xsl:for-each select="//osm/node[@id=$node_ref]">
    <xsl:value-of select="@lon" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="@lat" />
    <xsl:text>&#10;</xsl:text>
  </xsl:for-each>
</xsl:template>

<!-- ++++++++++++++++++++++++++++++ -->
</xsl:stylesheet>

