-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbushacking.py
67 lines (50 loc) · 1.99 KB
/
bushacking.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import bs4
import json
import shapely
import shapely.geometry
from operator import attrgetter
with open('example.sfmta.geo.json') as jsonin:
geomess = json.loads( jsonin.read() )
def dictify_xmlline(filename):
with open(filename) as xmlin:
soup = bs4.BeautifulSoup( xmlin, 'xml' )
samples = [ { a: l[a] for a in l.attrs } for l in soup.find_all('vehicle') ]
return samples
routes = { f['properties']['shortName'].strip(): f['geometry']['geometries'] for f in geomess['features'] }
for k in routes.keys():
new_routes = []
for r in routes[k]:
seglist = [ shapely.geometry.LineString( p ) for p in r['coordinates'] ]
new_routes.append( shapely.geometry.MultiLineString( seglist ) )
routes[k] = new_routes
route_filehandles = { k: open('./routes/%s.json' % k, 'w') for k in routes.keys() }
for root, dirs, files in os.walk('./sf-muni'):
for f in files:
ts = os.path.basename(f).split('.')[0]
ts_list = dictify_xmlline( os.path.join( root, f ) )
for sample in ts_list:
sample['time'] = ts
try:
routeNo = sample.pop('routeTag').replace('_','-')
route_filehandles[ routeNo ].write( json.dumps( sample ) + '\n' )
except Exception as e:
print( "Route %s does not exist: %s" % (routeNo, str(e)) )
for fh in route_filehandles.values():
fh.close()
for k in routes.keys():
with open('./routes/%s.json' % k) as linesin:
samples = [ json.loads(l) for l in linesin.readlines() ]
if len(samples)==0:
continue
tree = {}
for s in samples:
busid = s.pop('id')
s['time'] = int(s['time'])
if tree.get( busid ) is None:
tree[busid] = []
tree[busid].append( s )
for i in tree.keys():
tree[i] = sorted(tree[i], key=lambda row: row['time'])
with open('./routes/%s.json' % k, 'w') as treeout:
treeout.write( json.dumps( tree ) )