diff python/storage.py @ 525:7124c7d2a663

first draft of loading from VISSIM file
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 20 Jun 2014 00:20:29 -0400
parents 1dced8932b08
children 21bdeb29f855
line wrap: on
line diff
--- a/python/storage.py	Thu Jun 19 13:31:00 2014 -0400
+++ b/python/storage.py	Fri Jun 20 00:20:29 2014 -0400
@@ -356,14 +356,14 @@
 # txt files
 #########################
 
-def openCheck(filename, option = 'r', quit = False):
+def openCheck(filename, option = 'r', quitting = False):
     '''Open file filename in read mode by default
     and checks it is open'''
     try:
         return open(filename, option)
     except IOError:
         print 'File %s could not be opened.' % filename
-        if quit:
+        if quitting:
             from sys import exit
             exit()
         return None
@@ -419,27 +419,40 @@
             finally: self.sechead = None
         else: return self.fp.readline()
 
-def loadTrajectoriesFromVissimFile(filename, nObjects = -1, sequenceNum = -1):
-    '''Reads data from VISSIM .fzp trajectory file'''
-    objects = []
+def loadTrajectoriesFromVissimFile(filename, simulationStepsPerTimeUnit, nObjects = -1, sequenceNum = -1):
+    '''Reads data from VISSIM .fzp trajectory file
+
+    Assumed to be sorted over time'''
+    objects = {} # dictionary of objects index by their id
+
+    infile = openCheck(filename, quitting = True)
+
+    # data = pd.read_csv(filename, skiprows=15, delimiter=';')
+    # skip header: 15 lines + 1
+    for i in range(16):
+        readline(infile)
 
-    infile = openCheck(filename)
-    if not infile:
-        import sys
-        sys.exit()
+    for line in infile:
+        data = line.strip().split(';')
+        objNum = int(data[1])
+        instant = int(float(data[0])*simulationStepsPerTimeUnit)
+        s = float(data[4])
+        y = float(data[5])
+        lane = int(data[3])
+        if objNum not in objects:
+            objects[objNum] = moving.MovingObject(num = objNum, timeInterval = moving.TimeInterval(instant, instant))
+            objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory()
+        objects[objNum].timeInterval.last = instant
+        objects[objNum].curvilinearPositions.addPosition(s, y, lane)
 
-    
-    return objects
+    return objects.values()
     
 def loadTrajectoriesFromNgsimFile(filename, nObjects = -1, sequenceNum = -1):
     '''Reads data from the trajectory data provided by NGSIM project 
     and returns the list of Feature objects'''
     objects = []
 
-    infile = openCheck(filename)
-    if not infile:
-        import sys
-        sys.exit()
+    infile = openCheck(filename, quitting = True)
 
     def createObject(numbers):
         firstFrameNum = int(numbers[1])