comparison 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
comparison
equal deleted inserted replaced
524:1dced8932b08 525:7124c7d2a663
354 354
355 ######################### 355 #########################
356 # txt files 356 # txt files
357 ######################### 357 #########################
358 358
359 def openCheck(filename, option = 'r', quit = False): 359 def openCheck(filename, option = 'r', quitting = False):
360 '''Open file filename in read mode by default 360 '''Open file filename in read mode by default
361 and checks it is open''' 361 and checks it is open'''
362 try: 362 try:
363 return open(filename, option) 363 return open(filename, option)
364 except IOError: 364 except IOError:
365 print 'File %s could not be opened.' % filename 365 print 'File %s could not be opened.' % filename
366 if quit: 366 if quitting:
367 from sys import exit 367 from sys import exit
368 exit() 368 exit()
369 return None 369 return None
370 370
371 def readline(f, commentChar = commentChar): 371 def readline(f, commentChar = commentChar):
417 if self.sechead: 417 if self.sechead:
418 try: return self.sechead 418 try: return self.sechead
419 finally: self.sechead = None 419 finally: self.sechead = None
420 else: return self.fp.readline() 420 else: return self.fp.readline()
421 421
422 def loadTrajectoriesFromVissimFile(filename, nObjects = -1, sequenceNum = -1): 422 def loadTrajectoriesFromVissimFile(filename, simulationStepsPerTimeUnit, nObjects = -1, sequenceNum = -1):
423 '''Reads data from VISSIM .fzp trajectory file''' 423 '''Reads data from VISSIM .fzp trajectory file
424 objects = [] 424
425 425 Assumed to be sorted over time'''
426 infile = openCheck(filename) 426 objects = {} # dictionary of objects index by their id
427 if not infile: 427
428 import sys 428 infile = openCheck(filename, quitting = True)
429 sys.exit() 429
430 430 # data = pd.read_csv(filename, skiprows=15, delimiter=';')
431 431 # skip header: 15 lines + 1
432 return objects 432 for i in range(16):
433 readline(infile)
434
435 for line in infile:
436 data = line.strip().split(';')
437 objNum = int(data[1])
438 instant = int(float(data[0])*simulationStepsPerTimeUnit)
439 s = float(data[4])
440 y = float(data[5])
441 lane = int(data[3])
442 if objNum not in objects:
443 objects[objNum] = moving.MovingObject(num = objNum, timeInterval = moving.TimeInterval(instant, instant))
444 objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory()
445 objects[objNum].timeInterval.last = instant
446 objects[objNum].curvilinearPositions.addPosition(s, y, lane)
447
448 return objects.values()
433 449
434 def loadTrajectoriesFromNgsimFile(filename, nObjects = -1, sequenceNum = -1): 450 def loadTrajectoriesFromNgsimFile(filename, nObjects = -1, sequenceNum = -1):
435 '''Reads data from the trajectory data provided by NGSIM project 451 '''Reads data from the trajectory data provided by NGSIM project
436 and returns the list of Feature objects''' 452 and returns the list of Feature objects'''
437 objects = [] 453 objects = []
438 454
439 infile = openCheck(filename) 455 infile = openCheck(filename, quitting = True)
440 if not infile:
441 import sys
442 sys.exit()
443 456
444 def createObject(numbers): 457 def createObject(numbers):
445 firstFrameNum = int(numbers[1]) 458 firstFrameNum = int(numbers[1])
446 # do the geometry and usertype 459 # do the geometry and usertype
447 460