Skip to content

Instantly share code, notes, and snippets.

@kurtraschke
Created August 13, 2011 02:21
Embed
What would you like to do?
XSLT stylesheet to translate WMATA Metrobus positions to KML

Get an API key from developer.wmata.com, and put it in buspos.sh.

If you don't happen to have a web server running, but you do have Python, do the following:

  1. chmod +x buspos.sh
  2. chmod a+r bus.xslt
  3. mkdir cgi-bin
  4. mv buspos.sh bus.xslt cgi-bin
  5. python -m CGIHTTPServer 8001

The script will then be accessible at http://localhost:8001/cgi-bin/buspos.sh.

Either way, open Google Earth, and select Add>Network Link. Enter the URL to the script, and set the refresh rate to at least 2 minutes.

These files are in the public domain.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://www.wmata.com">
<xsl:template match="/">
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Style id="bus">
<IconStyle>
<scale>0.5</scale>
<Icon>
<href>http://maps.google.com/mapfiles/kml/shapes/bus.png</href>
</Icon>
</IconStyle>
<LabelStyle>
<scale>0</scale>
</LabelStyle>
</Style>
<open>1</open>
<Folder>
<open>1</open>
<name>Bus Positions</name>
<description>Near real-time positions for <xsl:value-of select="count(//w:BusPosition)" /> Metrobuses.</description>
<xsl:for-each select="//w:BusPosition">
<Placemark>
<name>Bus <xsl:value-of select="w:VehicleID" /></name>
<description>Route <xsl:value-of select="w:RouteID"/> to <xsl:value-of select="w:TripHeadsign" /></description>
<Point>
<coordinates><xsl:value-of select="w:Lon" />,<xsl:value-of select="w:Lat" /></coordinates>
</Point>
<styleUrl>#bus</styleUrl>
</Placemark>
</xsl:for-each>
</Folder>
</Document>
</kml>
</xsl:template>
</xsl:stylesheet>
#!/bin/sh
API_KEY=""
dir=$(dirname $(which $0))
echo -e "Content-type: application/vnd.google-earth.kml+xml\n"
curl "http://api.wmata.com/Bus.svc/BusPositions?api_key=${API_KEY}" 2>/dev/null | xsltproc "${dir}/bus.xslt" -
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment