comparison python/sumo.py @ 968:32a34a143c27

work on sumo and metadata
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 07 Dec 2017 15:13:06 -0500
parents 40af0f20ee2d
children 933670761a57
comparison
equal deleted inserted replaced
967:373e8ef6ee25 968:32a34a143c27
1 #! /usr/bin/env python 1 #! /usr/bin/env python
2 '''Libraries for the SUMO traffic simulation software 2 '''Libraries for the SUMO traffic simulation software
3 http://sumo.dlr.de 3 http://sumo.dlr.de
4 ''' 4 '''
5 5
6 import csv 6 #import csv
7 7
8 def loadTazEdges(inFilename): 8 def loadTazEdges(inFilename):
9 '''Converts list of OSM edges per OSM edge and groups per TAZ''' 9 '''Converts list of OSM edges per OSM edge and groups per TAZ
10 format is csv with first two columns the OSM id and TAZ id, then the list of SUMO edge id
11
12 Returns the list of SUMO edge per TAZ'''
10 data = [] 13 data = []
11 tazs = {} 14 tazs = {}
12 with open(inFilename,'r') as f: 15 with open(inFilename,'r') as f:
13 csv_reader = csv.reader(f, delimiter=",") 16 f.readline() # skip the headers
14 next(csv_reader, None) # skip the headers 17 for r in f:
15 for row in csv_reader: 18 tmp = r.strip().split(',')
16 data.append(row) 19 tazID = tmp[1]
20 for edge in tmp[2:]:
21 if len(edge) > 0:
22 if tazID in tazs:
23 if edge not in tazs[tazID]:
24 tazs[tazID].append(edge)
25 else:
26 tazs[tazID] = [edge]
27 return tazs
17 28
18 for datum in data: 29 def edge2Taz(tazs):
19 if len(datum) > 5 and datum[5] is not None: 30 '''Returns the associative array of the TAZ of each SUMO edge'''
20 tazID = datum[3] 31 edge2Tazs = {}
21 for edge in datum[15:]: 32 for taz, edges in tazs.iteritems():
22 if len(edge) > 0: 33 for edge in edges:
23 if edge is not None: 34 if edge in edge2Tazs:
24 if tazID in tazs: 35 print('error for edge: {} (taz {}/{})'.format(edge, edge2Tazs[edge], taz))
25 if edge not in tazs[tazID]: 36 edge2Tazs[edge] = taz
26 tazs[tazID].append(edge) 37 return edge2Tazs
27 else: 38
28 tazs[tazID] = [edge]
29 return tazs
30
31 def saveTazEdges(outFilename, tazs): 39 def saveTazEdges(outFilename, tazs):
32 with open(outFilename,'w') as out: 40 with open(outFilename,'w') as out:
33 out.write('<tazs>\n') 41 out.write('<tazs>\n')
34 for tazID in tazs: 42 for tazID in tazs:
35 out.write('<taz id="{}" edges="'.format(tazID)+' '.join(tazs[tazID])+'"/>\n') 43 out.write('<taz id="{}" edges="'.format(tazID)+' '.join(tazs[tazID])+'"/>\n')