diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trafficintelligence/sumo.py	Fri Jun 15 11:19:10 2018 -0400
@@ -0,0 +1,53 @@
+#! /usr/bin/env python
+'''Libraries for the SUMO traffic simulation software
+http://sumo.dlr.de
+'''
+
+#import csv
+
+def loadTazEdges(inFilename):
+    '''Converts list of OSM edges per OSM edge and groups per TAZ
+    format is csv with first two columns the OSM id and TAZ id, then the list of SUMO edge id
+
+    Returns the list of SUMO edge per TAZ'''
+    data = []
+    tazs = {}
+    with open(inFilename,'r') as f:
+        f.readline() # skip the headers
+        for r in f:
+            tmp = r.strip().split(',')
+            tazID = tmp[1]
+            for edge in tmp[2:]:                
+                if len(edge) > 0:
+                    if tazID in tazs:
+                        if edge not in tazs[tazID]:
+                            tazs[tazID].append(edge)
+                    else:
+                        tazs[tazID] = [edge]
+    return tazs
+
+def edge2Taz(tazs):
+    '''Returns the associative array of the TAZ of each SUMO edge'''
+    edge2Tazs = {}
+    for taz, edges in tazs.items():
+        for edge in edges:
+            if edge in edge2Tazs:
+                print('error for edge: {} (taz {}/{})'.format(edge, edge2Tazs[edge], taz))
+            edge2Tazs[edge] = taz
+    return edge2Tazs
+
+def saveTazEdges(outFilename, tazs):
+    with open(outFilename,'w') as out:
+        out.write('<tazs>\n')
+        for tazID in tazs:
+            out.write('<taz id="{}" edges="'.format(tazID)+' '.join(tazs[tazID])+'"/>\n')
+        out.write('</tazs>\n')
+
+# TODO add utils from process-cyber.py?
+        
+# if __name__ == "__main__":
+#     import doctest
+#     import unittest
+#     suite = doctest.DocFileSuite('tests/sumo.txt')
+#     #suite = doctest.DocTestSuite()
+#     unittest.TextTestRunner().run(suite)