Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 46 lines (40 sloc) 1.068 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
from sys import argv
import psycopg2
import random
from xml.sax import saxutils

conn = psycopg2.connect("dbname=%s user=postgres" % argv[1])
cur = conn.cursor()

COLOURS = ['ff0000', '00ff00', '0000ff', 'ffff00', 'ff00ff', '00ffff']

print '''<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>traingraph</name>
<Style id="dot-icon">
<IconStyle>
<scale>0.25</scale>
<Icon>
<href>http://sleeper.demozoo.org/images/dot.png</href>
</Icon>
</IconStyle>
</Style>
'''

# extract railway lines
cur.execute('''
SELECT ST_ASKML(linestring)
FROM paths
''')
for (kml,) in cur:
print '<Placemark>%s<Style><LineStyle><width>2</width><color>ff%s</color></LineStyle></Style></Placemark>' % (kml, random.choice(COLOURS))

# extract stations with wgs84 lng/lat
cur.execute('''
SELECT name, ST_ASKML(geom)
FROM stations
''')
for (name, kml) in cur:
print '<Placemark>'
if name:
print '<name>%s</name>' % saxutils.escape(name)
print '<styleUrl>#dot-icon</styleUrl>'
print kml
print '</Placemark>'

print '</Document>'
print '</kml>'
Something went wrong with that request. Please try again.