Mercurial Hosting > traffic-intelligence
comparison python/storage.py @ 143:436b87d4b992
wrote the code to load positions from sqlite database
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 24 Aug 2011 19:43:44 -0400 |
parents | 99e807c29753 |
children | 319a04ba9033 |
comparison
equal
deleted
inserted
replaced
142:a3532db00c28 | 143:436b87d4b992 |
---|---|
9 | 9 |
10 ngsimUserTypes = {'twowheels':1, | 10 ngsimUserTypes = {'twowheels':1, |
11 'car':2, | 11 'car':2, |
12 'truck':3} | 12 'truck':3} |
13 | 13 |
14 def loadSqlite(filename, objectNumbers = -1): | |
15 '''Loads nObjects or the indices in objectNumbers from the database | |
16 TODO: load velocities''' | |
17 import sqlite3 | |
14 | 18 |
19 connection = sqlite3.connect(filename) # add test if it open | |
20 cursor = connection.cursor() | |
21 | |
22 if type(objectNumbers) == int: | |
23 if objectNumbers == -1: | |
24 cursor.execute('SELECT * from positions order by trajectory_id, frame_number') | |
25 else: | |
26 cursor.execute('SELECT * from positions where trajectory_id between 0 and {0} order by trajectory_id, frame_number'.format(objectNumbers)) | |
27 elif type(objectNumbers) == list: | |
28 cursor.execute('SELECT * from positions where trajectory_id in (' | |
29 +', '.join([str(n) for n in objectNumbers]) | |
30 +') order by trajectory_id, frame_number') | |
31 | |
32 objId = -1 | |
33 obj = None | |
34 objects = [] | |
35 for row in cursor: | |
36 if row[0] != objId: | |
37 objId = row[0] | |
38 if obj: | |
39 objects.append(obj) | |
40 obj = moving.MovingObject(row[0], timeInterval = moving.TimeInterval(row[1], row[1]), positions = moving.Trajectory([[row[2]],[row[3]]])) | |
41 else: | |
42 obj.timeInterval.last = row[1] | |
43 obj.positions.addPositionXY(row[2],row[3]) | |
44 | |
45 if obj: | |
46 objects.append(obj) | |
47 | |
48 connection.close() | |
49 return objects | |
15 | 50 |
16 def loadNgsimFile(filename, nObjects = -1, sequenceNum = -1): | 51 def loadNgsimFile(filename, nObjects = -1, sequenceNum = -1): |
17 '''Reads data from the trajectory data provided by NGSIM project | 52 '''Reads data from the trajectory data provided by NGSIM project |
18 and returns the list of Feature objects''' | 53 and returns the list of Feature objects''' |
19 objects = [] | 54 objects = [] |