Mercurial Hosting > traffic-intelligence
comparison python/storage.py @ 208:d9855499fc88
Added functions to write trajectories through sqlite
author | Francois Belisle <belisle.francois@gmail.com> |
---|---|
date | Tue, 05 Jun 2012 13:12:19 -0400 |
parents | 966c2cd2bd9f |
children | 746d02cea65f |
comparison
equal
deleted
inserted
replaced
206:82b4101d9a2f | 208:d9855499fc88 |
---|---|
1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
2 # -*- coding: utf-8 -*- | |
2 '''Various utilities to save and load data''' | 3 '''Various utilities to save and load data''' |
3 | 4 |
4 import utils | 5 import utils |
5 import moving | 6 import moving |
6 | 7 |
9 | 10 |
10 ngsimUserTypes = {'twowheels':1, | 11 ngsimUserTypes = {'twowheels':1, |
11 'car':2, | 12 'car':2, |
12 'truck':3} | 13 'truck':3} |
13 | 14 |
15 def writeTrajectoriesToSqlite(objects, outFile, trajectoryType, objectNumbers = -1): | |
16 """ | |
17 This function writers trajectories to a specified sqlite file | |
18 @param[in] objects -> a list of trajectories | |
19 @param[in] trajectoryType - | |
20 @param[out] outFile -> the .sqlite file containting the written objects | |
21 @param[in] objectNumber : number of objects loaded | |
22 """ | |
23 | |
24 import sqlite3 | |
25 connection = sqlite3.connect(outFile) | |
26 cursor = connection.cursor() | |
27 | |
28 #creation de la table | |
29 schema = "CREATE TABLE \"trajectories\"(trajectory_id INTEGER,frame_number INTEGER, x_coordinate REAL, y_coordinate REAL, PRIMARY KEY(trajectory_id, frame_number))" | |
30 cursor.execute(schema) | |
31 | |
32 trajectory_id = 0 | |
33 frame_number = 0 | |
34 if trajectoryType == 'feature': | |
35 if type(objectNumbers) == int and objectNumbers == -1: | |
36 for trajectory in objects: | |
37 trajectory_id += 1 | |
38 frame_number = 0 | |
39 for position in trajectory.getPositions(): | |
40 frame_number += 1 | |
41 requete = "insert into trajectories(trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)" | |
42 cursor.execute(requete,(trajectory_id,frame_number,position.x,position.y)) | |
43 | |
44 connection.commit() | |
45 connection.close() | |
46 | |
47 def testWrite(): | |
48 features = loadTrajectoriesFromSqlite("/home/francois/Unison/École/12Été/CRSNG/Data/prototypes.sqlite",'feature',-1) | |
49 writeTrajectoriesToSqlite(features, "/home/francois/Unison/École/12Été/CRSNG/TAAM-Experiments/resultats/testWrite.sqlite", 'feature') | |
50 | |
51 | |
14 def loadTrajectoriesFromSqlite(filename, trajectoryType, objectNumbers = -1): | 52 def loadTrajectoriesFromSqlite(filename, trajectoryType, objectNumbers = -1): |
15 '''Loads nObjects or the indices in objectNumbers from the database | 53 '''Loads nObjects or the indices in objectNumbers from the database |
16 TODO: load velocities (replace table name 'positions' by 'velocities' | 54 TODO: load velocities (replace table name 'positions' by 'velocities' |
17 TODO: load features as well, other ways of averaging trajectories | 55 TODO: load features as well, other ways of averaging trajectories |
18 ''' | 56 ''' |
19 import sqlite3 | 57 import sqlite3 |
20 | 58 |
21 connection = sqlite3.connect(filename) # add test if it open | 59 connection = sqlite3.connect(filename) # add test if it open |
22 cursor = connection.cursor() | 60 cursor = connection.cursor() |
23 | 61 |
24 if trajectoryType == 'feature': | 62 if trajectoryType == 'feature': |
25 if type(objectNumbers) == int: | 63 if type(objectNumbers) == int: |