<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
  xmlns:c="http://www.acooke.org/countries"
  xmlns:math="http://exslt.org/math"
  xmlns="http://www.w3.org/2000/svg"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:output method="xml" version="1.0" encoding="iso-8859-1"
   omit-xml-declaration="no" indent="yes"/>

 <xsl:template match="c:country[./c:name][./c:coords][string(number(./c:dollar)) != 'NaN'][./c:gdp]">
  <xsl:comment><xsl:value-of select="./c:name"/></xsl:comment>
  <xsl:variable name="id" select="./c:code"/>
  <xsl:variable name="scaled-dollar">
   <xsl:call-template name="norm-gamma">
    <xsl:with-param name="pX" select="./c:dollar"/>
    <xsl:with-param name="pMin" select="0"/>
    <xsl:with-param name="pMax" select="5e11"/>
    <xsl:with-param name="pScale" select="1.0"/>
    <xsl:with-param name="pPower" select="0.5"/>
   </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="scaled-gdp">
   <xsl:call-template name="norm-gamma">
    <xsl:with-param name="pX" select="./c:gdp"/>
    <xsl:with-param name="pMin" select="0"/>
    <xsl:with-param name="pMax" select="10"/>
    <xsl:with-param name="pScale" select="1.0"/>
    <xsl:with-param name="pPower" select="1.0"/>
   </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="colour">
   <xsl:call-template name="heat">
    <xsl:with-param name="pX" select="$scaled-gdp"/>
   </xsl:call-template>
  </xsl:variable>
  <xsl:call-template name="gradient">
   <xsl:with-param name="pId" select="$id"/>
   <xsl:with-param name="pOpacity" select="0.5"/>
   <xsl:with-param name="pColour" select="$colour"/>
  </xsl:call-template>
  <xsl:element name="circle">
   <xsl:attribute name="cy">
    <xsl:call-template name="scale-lat">
     <xsl:with-param name="pX" select="./c:coords/c:lat"/>
    </xsl:call-template>
   </xsl:attribute>
   <xsl:attribute name="cx">
    <xsl:call-template name="scale-long">
     <xsl:with-param name="pX" select="./c:coords/c:long"/>
    </xsl:call-template>
   </xsl:attribute>
   <xsl:attribute name="r">
    <xsl:value-of select="500.0 * $scaled-dollar"/>
   </xsl:attribute>
   <xsl:attribute name="fill">url(#<xsl:value-of select="$id"/>)</xsl:attribute>
  </xsl:element>
 </xsl:template>

 <xsl:template match="c:arms">
<!--  <svg height="3508" width="4960" xmlns="http://www.w3.org/2000/svg">
   <desc>Map of Arms Spending</desc>
   <g transform="translate(2480,1754) scale(2) translate(-1200,-600)"> -->
    <svg x="0" y="0" height="1200" width="2400">
     <rect x="0" y="0" height="1200" width="2400" fill="black"/>
     <rect x="200" y="100" width="2000" height="1000"
      fill="none" stroke="#888" stroke-width="2"/>
     <xsl:call-template name="key"/>
     <g transform="scale(2,1)">
      <xsl:apply-templates select="*"/>
     </g>
    </svg>
<!--   </g>
  </svg> -->
 </xsl:template>

 <xsl:template match="*"/>

 <xsl:template name="key">
  <xsl:variable name="d">
   <xsl:call-template name="norm-gamma">
    <xsl:with-param name="pX" select="1e11"/>
    <xsl:with-param name="pMin" select="0"/>
    <xsl:with-param name="pMax" select="5e11"/>
    <xsl:with-param name="pScale" select="500.0"/>
    <xsl:with-param name="pPower" select="0.5"/>
   </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="d2">
   <xsl:call-template name="norm-gamma">
    <xsl:with-param name="pX" select="1e10"/>
    <xsl:with-param name="pMin" select="0"/>
    <xsl:with-param name="pMax" select="5e11"/>
    <xsl:with-param name="pScale" select="500.0"/>
    <xsl:with-param name="pPower" select="0.5"/>
   </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="d3">
   <xsl:call-template name="norm-gamma">
    <xsl:with-param name="pX" select="1e9"/>
    <xsl:with-param name="pMin" select="0"/>
    <xsl:with-param name="pMax" select="5e11"/>
    <xsl:with-param name="pScale" select="500.0"/>
    <xsl:with-param name="pPower" select="0.5"/>
   </xsl:call-template>
  </xsl:variable>
  <defs>
   <linearGradient gradientUnits="objectBoundingBox" id="key">
    <stop offset="1%" stop-color="red" stop-opacity="0.5"/>
    <stop offset="50%" stop-color="yellow" stop-opacity="0.5"/>
    <stop offset="99%" stop-color="white" stop-opacity="0.5"/>
   </linearGradient>
  </defs>
  <g transform="rotate(-90,350,1090)">
   <xsl:element name="rect">
    <xsl:attribute name="x">350</xsl:attribute>
    <xsl:attribute name="y"><xsl:value-of select="1040"/></xsl:attribute>
    <xsl:attribute name="height">50</xsl:attribute>
    <xsl:attribute name="width"><xsl:value-of select="$d"/></xsl:attribute>
    <xsl:attribute name="fill">url(#key)</xsl:attribute>
    <xsl:attribute name="stroke">#888</xsl:attribute>
    <xsl:attribute name="stroke-width">2</xsl:attribute>
   </xsl:element>
  </g>
  <text x="290" y="1090" text-anchor="end" font-family="Verdana" font-size="10" fill="#ccc">0% GDP</text>
  <xsl:element name="text">
   <xsl:attribute name="x">290</xsl:attribute>
   <xsl:attribute name="y"><xsl:value-of select="1090 - $d"/></xsl:attribute>
   <xsl:attribute name="text-anchor">end</xsl:attribute>
   <xsl:attribute name="font-family">Verdana</xsl:attribute>
   <xsl:attribute name="font-size">10</xsl:attribute>
   <xsl:attribute name="fill">#ccc</xsl:attribute>10% GDP</xsl:element>
  <g>
   <clipPath id="arc">
    <path d="M 360 1090 L 360 0 L 2400 0 L 2400 1090 z"/>
   </clipPath>
   <xsl:element name="ellipse">
    <xsl:attribute name="cx">360</xsl:attribute>
    <xsl:attribute name="cy">1090</xsl:attribute>
    <xsl:attribute name="rx"><xsl:value-of select="2 * $d"/></xsl:attribute>
    <xsl:attribute name="ry"><xsl:value-of select="$d"/></xsl:attribute>
    <xsl:attribute name="clip-path">url(#arc)</xsl:attribute>
    <xsl:attribute name="stroke">#888</xsl:attribute>
    <xsl:attribute name="stroke-width">2</xsl:attribute>
   </xsl:element>
  <xsl:element name="text">
   <xsl:attribute name="x"><xsl:value-of select="370 + 2 * $d"/></xsl:attribute>
   <xsl:attribute name="y">1090</xsl:attribute>
   <xsl:attribute name="text-anchor">start</xsl:attribute>
   <xsl:attribute name="font-family">Verdana</xsl:attribute>
   <xsl:attribute name="font-size">10</xsl:attribute>
   <xsl:attribute name="fill">#ccc</xsl:attribute>US $100 billion</xsl:element>
   <xsl:element name="ellipse">
    <xsl:attribute name="cx">360</xsl:attribute>
    <xsl:attribute name="cy">1090</xsl:attribute>
    <xsl:attribute name="rx"><xsl:value-of select="2 * $d2"/></xsl:attribute>
    <xsl:attribute name="ry"><xsl:value-of select="$d2"/></xsl:attribute>
    <xsl:attribute name="clip-path">url(#arc)</xsl:attribute>
    <xsl:attribute name="stroke">#888</xsl:attribute>
    <xsl:attribute name="stroke-width">2</xsl:attribute>
   </xsl:element>
  <xsl:element name="text">
   <xsl:attribute name="x"><xsl:value-of select="370 + 2 * $d2"/></xsl:attribute>
   <xsl:attribute name="y">1090</xsl:attribute>
   <xsl:attribute name="text-anchor">start</xsl:attribute>
   <xsl:attribute name="font-family">Verdana</xsl:attribute>
   <xsl:attribute name="font-size">10</xsl:attribute>
   <xsl:attribute name="fill">#ccc</xsl:attribute>US $10 billion</xsl:element>
   <xsl:element name="ellipse">
    <xsl:attribute name="cx">360</xsl:attribute>
    <xsl:attribute name="cy">1090</xsl:attribute>
    <xsl:attribute name="rx"><xsl:value-of select="2 * $d3"/></xsl:attribute>
    <xsl:attribute name="ry"><xsl:value-of select="$d3"/></xsl:attribute>
    <xsl:attribute name="clip-path">url(#arc)</xsl:attribute>
    <xsl:attribute name="stroke">#888</xsl:attribute>
    <xsl:attribute name="stroke-width">2</xsl:attribute>
   </xsl:element>
  <xsl:element name="text">
   <xsl:attribute name="x"><xsl:value-of select="370 + 2 * $d3"/></xsl:attribute>
   <xsl:attribute name="y">1090</xsl:attribute>
   <xsl:attribute name="text-anchor">start</xsl:attribute>
   <xsl:attribute name="font-family">Verdana</xsl:attribute>
   <xsl:attribute name="font-size">10</xsl:attribute>
   <xsl:attribute name="fill">#ccc</xsl:attribute>US $1 billion</xsl:element>
  </g>
  <text x="200" y="1120" text-anchor="start" font-family="Verdana" font-size="10" fill="#ccc">Annual military spending by country.  Data from CIA World Factbook 2004.  Equal area cylindrical projection (long, sin(lat)).  Image copyright 2005 Andrew Cooke, released under the Creative Commons Attribution-NonCommercial License.  For downloads and more info see http://www.acooke.org</text>
 </xsl:template>

 <xsl:template name="gradient">
  <xsl:param name="pId"/>
  <xsl:param name="pOpacity"/>
  <xsl:param name="pColour"/>
  <defs>
   <xsl:element name="radialGradient">
    <xsl:attribute name="gradientUnits">objectBoundingBox</xsl:attribute>
    <xsl:attribute name="id">
     <xsl:value-of select="$pId"/>
    </xsl:attribute>
    <xsl:element name="stop">
     <xsl:attribute name="offset">1%</xsl:attribute>
     <xsl:attribute name="stop-color">
      <xsl:value-of select="$pColour"/>
     </xsl:attribute>
     <xsl:attribute name="stop-opacity">
      <xsl:value-of select="$pOpacity"/>
     </xsl:attribute>
    </xsl:element>
    <xsl:element name="stop">
     <xsl:attribute name="offset">99%</xsl:attribute>
     <xsl:attribute name="stop-color">
      <xsl:value-of select="$pColour"/>
     </xsl:attribute>
     <xsl:attribute name="stop-opacity">
      <xsl:value-of select="0.25 * $pOpacity"/>
     </xsl:attribute>
    </xsl:element>
   </xsl:element>
  </defs>
 </xsl:template>

 <xsl:template name="heat">
  <xsl:param name="pX"/>
  <xsl:choose>
   <xsl:when test="$pX &lt; 0.5">
    <xsl:variable name="g-dot" select="string(510 * $pX)"/>
    <xsl:variable name="g">
     <xsl:choose>
      <xsl:when test="contains($g-dot, '.')">
       <xsl:value-of select="substring-before($g-dot, '.')"/>
      </xsl:when>
      <xsl:otherwise>
       <xsl:value-of select="$g-dot"/>
      </xsl:otherwise>
     </xsl:choose>
    </xsl:variable>rgb(255,<xsl:value-of select="$g"/>,0)</xsl:when>
   <xsl:otherwise>
    <xsl:variable name="b-dot" select="string(510 * ($pX - 0.5))"/>
    <xsl:variable name="b">
     <xsl:choose>
      <xsl:when test="contains($b-dot, '.')">
       <xsl:value-of select="substring-before($b-dot, '.')"/>
      </xsl:when>
      <xsl:otherwise>
       <xsl:value-of select="$b-dot"/>
      </xsl:otherwise>
     </xsl:choose>
    </xsl:variable>rgb(255,255,<xsl:value-of select="$b"/>)</xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template name="norm-gamma">
  <xsl:param name="pX"/>
  <xsl:param name="pMin"/>
  <xsl:param name="pMax"/>
  <xsl:param name="pScale"/>
  <xsl:param name="pPower"/>
  <xsl:variable name="norm" select="($pX - $pMin) div ($pMax - $pMin)"/>
  <xsl:choose>
   <xsl:when test="$norm &gt; 0">
    <xsl:value-of select="$pScale * math:power($norm, $pPower)"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="0"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template name="scale-long">
  <xsl:param name="pX"/>
  <xsl:value-of select="100 + 500 + 500 * ($pX div 180)"/>
 </xsl:template>
 <xsl:template name="scale-lat">
  <xsl:param name="pX"/>
  <!-- invert because stupid coord system -->
  <xsl:value-of select="100 + 500 - 500 * math:sin($pX * 0.01745323)"/>
 </xsl:template>

</xsl:stylesheet>
