Skip to content

Instantly share code, notes, and snippets.

@mapmeld
Created June 10, 2012 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mapmeld/2907473 to your computer and use it in GitHub Desktop.
Save mapmeld/2907473 to your computer and use it in GitHub Desktop.
MTA Bus Mapper
from datetime import datetime, timedelta
m31 = open('June 1.csv','r')
buses = { }
def zap(content):
return content.replace('\x00','').replace('\r\n','')
firstline = 1
minutebyminute = timedelta(minutes=1)
for line in m31:
if(firstline == 1):
firstline = 0
continue
r = zap(line).split(',')
if(len(r) < 5):
print r
elif(buses.has_key(r[4])):
timediff = datetime.strptime(r[1], '%Y-%m-%d %H:%M:%S.000') - buses[r[4]][ len( buses[r[4]] ) - 1 ][2]
if(timediff >= minutebyminute):
buses[r[4]].append([r[2], r[3], datetime.strptime(r[1], '%Y-%m-%d %H:%M:%S.000') ])
else:
buses[r[4]] = [ [r[2], r[3], datetime.strptime(r[1], '%Y-%m-%d %H:%M:%S.000') ] ]
for busout in buses.keys():
buswrite = open(busout + ".kml", 'w')
buswrite.write('''<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<name>Bus Track</name>
<open>1</open>
<Style id="seeadler-dot-icon">
<IconStyle>
<Icon>
<href>http://i.imgur.com/khmWg.png</href>
</Icon>
</IconStyle>
</Style>
<Folder>
<name>Bus Track</name>
<open>1</open>
<description>Bus Track</description>
<Style>
<ListStyle>
<listItemType>checkHideChildren</listItemType>
</ListStyle>
</Style>\n''')
for track in buses[busout]:
buswrite.write(''' <Placemark>
<TimeStamp>
<when>''' + str(track[2]).replace(' ','T') + '''Z</when>
</TimeStamp>
<styleUrl>#seeadler-dot-icon</styleUrl>
<Point>
<coordinates>''' + track[1] + ''',''' + track[0] + '''</coordinates>
</Point>
</Placemark>''')
buswrite.write(''' </Folder>
</Document>
</kml>''')
buswrite.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment