Mercurial Hosting > traffic-intelligence
comparison trafficintelligence/sumo.py @ 1028:cc5cb04b04b0
major update using the trafficintelligence package name and install through pip
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 15 Jun 2018 11:19:10 -0400 |
parents | python/sumo.py@933670761a57 |
children | 3972d85e3b6c |
comparison
equal
deleted
inserted
replaced
1027:6129296848d3 | 1028:cc5cb04b04b0 |
---|---|
1 #! /usr/bin/env python | |
2 '''Libraries for the SUMO traffic simulation software | |
3 http://sumo.dlr.de | |
4 ''' | |
5 | |
6 #import csv | |
7 | |
8 def loadTazEdges(inFilename): | |
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''' | |
13 data = [] | |
14 tazs = {} | |
15 with open(inFilename,'r') as f: | |
16 f.readline() # skip the headers | |
17 for r in f: | |
18 tmp = r.strip().split(',') | |
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 | |
28 | |
29 def edge2Taz(tazs): | |
30 '''Returns the associative array of the TAZ of each SUMO edge''' | |
31 edge2Tazs = {} | |
32 for taz, edges in tazs.items(): | |
33 for edge in edges: | |
34 if edge in edge2Tazs: | |
35 print('error for edge: {} (taz {}/{})'.format(edge, edge2Tazs[edge], taz)) | |
36 edge2Tazs[edge] = taz | |
37 return edge2Tazs | |
38 | |
39 def saveTazEdges(outFilename, tazs): | |
40 with open(outFilename,'w') as out: | |
41 out.write('<tazs>\n') | |
42 for tazID in tazs: | |
43 out.write('<taz id="{}" edges="'.format(tazID)+' '.join(tazs[tazID])+'"/>\n') | |
44 out.write('</tazs>\n') | |
45 | |
46 # TODO add utils from process-cyber.py? | |
47 | |
48 # if __name__ == "__main__": | |
49 # import doctest | |
50 # import unittest | |
51 # suite = doctest.DocFileSuite('tests/sumo.txt') | |
52 # #suite = doctest.DocTestSuite() | |
53 # unittest.TextTestRunner().run(suite) |