comparison python/storage.py @ 7:ffddccfab7f9

loading shell objects from NGSIM works
author Nicolas Saunier <nico@confins.net>
date Thu, 05 Nov 2009 17:17:20 -0500
parents aed8eb63cdde
children 8aed225f71d8
comparison
equal deleted inserted replaced
6:597d61c1eebe 7:ffddccfab7f9
1 #! /usr/bin/env python 1 #! /usr/bin/env python
2 '''Various utilities to save and load data''' 2 '''Various utilities to save and load data'''
3 3
4 import utils; 4 import utils
5 import moving
5 6
6 __metaclass__ = type 7 __metaclass__ = type
7 8
8 9
9 10
10 11
11 12
12 def loadNgsimFile(filename, nObjects = -1, sequenceNum = -1): 13 def loadNgsimFile(filename, nObjects = -1, sequenceNum = -1):
13 '''Reads data from the trajectory data provided by NGSIM project 14 '''Reads data from the trajectory data provided by NGSIM project
14 and returns the list of Feature objects''' 15 and returns the list of Feature objects'''
15 features = [] 16 objects = []
16 17
17 input = utils.open(filename) 18 input = utils.openCheck(filename)
18 if not input: 19 if not input:
19 import sys 20 import sys
20 sys.exit() 21 sys.exit()
21 22
22 def createFeature(numbers): 23 def createObject(numbers):
23 firstFrameNum = int(numbers[1]) 24 firstFrameNum = int(numbers[1])
24 lastFrameNum = firstFrameNum+int(numbers[2])-1 25 time = moving.TimeInterval(firstFrameNum, firstFrameNum+int(numbers[2])-1)
25 f = Feature(sequenceNum, firstFrameNum, lastFrameNum, int(numbers[0])) 26 obj = moving.MovingObject(num = int(numbers[0]), timeInterval = time)
26 f.sizeLength = float(numbers[8]) 27 # do the geometry and usertype
27 f.sizeWidth = float(numbers[9]) 28
28 f.userType = int(numbers[10]) 29 # firstFrameNum = int(numbers[1])
29 f.positions = [[float(numbers[6])],[float(numbers[7])]] 30 # lastFrameNum = firstFrameNum+int(numbers[2])-1
30 f.speeds = [float(numbers[11])] 31 # f = Feature(sequenceNum, firstFrameNum, lastFrameNum, int(numbers[0]))
31 return f 32 # f.sizeLength = float(numbers[8])
33 # f.sizeWidth = float(numbers[9])
34 # f.userType = int(numbers[10])
35 # f.positions = [[float(numbers[6])],[float(numbers[7])]]
36 # f.speeds = [float(numbers[11])]
37 return obj
32 38
33 numbers = input.readline().strip().split() 39 numbers = input.readline().strip().split()
34 if (len(numbers) > 0): 40 if (len(numbers) > 0):
35 f = createFeature(numbers) 41 obj = createObject(numbers)
36 42
37 for line in input: 43 for line in input:
38 numbers = line.strip().split() 44 numbers = line.strip().split()
39 if f.num != int(numbers[0]): 45 if obj.num != int(numbers[0]):
40 # check and adapt the length to deal with issues in NGSIM data 46 # check and adapt the length to deal with issues in NGSIM data
41 objLength = f.length() 47 #objLength = f.length()
42 if (objLength != len(f.positions[0])): 48 #if (objLength != len(f.positions[0])):
43 print 'length pb with object %s (%d,%d)' % (f.num,objLength,len(f.positions[0])) 49 # print 'length pb with object %s (%d,%d)' % (f.num,objLength,len(f.positions[0]))
44 f.lastFrameNum = f.getFirstFrameNum()+len(f.positions[0])-1 50 # f.lastFrameNum = f.getFirstFrameNum()+len(f.positions[0])-1
45 f.velocities = utils.computeVelocities(f.positions) # compare norm to speeds ? 51 #f.velocities = utils.computeVelocities(f.positions) # compare norm to speeds ?
46 features.append(f) 52 objects.append(obj)
47 if (nObjects>0) and (len(features)>=nObjects): 53 if (nObjects>0) and (len(objects)>=nObjects):
48 break 54 break
49 f = createFeature(numbers) 55 obj = createObject(numbers)
50 else: 56 else:
51 f.positions[0] += [float(numbers[6])] 57 print(numbers[6])
52 f.positions[1] += [float(numbers[7])] 58 # f.positions[0] += [float(numbers[6])]
53 f.speeds += [float(numbers[11])] 59 # f.positions[1] += [float(numbers[7])]
60 # f.speeds += [float(numbers[11])]
61
54 # if (f.sizeWidth != float(numbers[9])): 62 # if (f.sizeWidth != float(numbers[9])):
55 # print 'changed width obj %d' % (f.num) 63 # print 'changed width obj %d' % (f.num)
56 # if (f.sizeLength != float(numbers[8])): 64 # if (f.sizeLength != float(numbers[8])):
57 # print 'changed length obj %d' % (f.num) 65 # print 'changed length obj %d' % (f.num)
58 66
59 input.close() 67 input.close()
60 return features 68 return objects
61 69
62 def convertNgsimFile(inFile, outFile, append = False, nObjects = -1, sequenceNum = 0): 70 def convertNgsimFile(inFile, outFile, append = False, nObjects = -1, sequenceNum = 0):
63 '''Reads data from the trajectory data provided by NGSIM project 71 '''Reads data from the trajectory data provided by NGSIM project
64 and converts to our current format.''' 72 and converts to our current format.'''
65 if append: 73 if append: