changeset 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 82b4101d9a2f
children 746d02cea65f
files python/storage.py
diffstat 1 files changed, 39 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/python/storage.py	Tue Apr 03 01:11:29 2012 -0400
+++ b/python/storage.py	Tue Jun 05 13:12:19 2012 -0400
@@ -1,4 +1,5 @@
 #! /usr/bin/env python
+# -*- coding: utf-8 -*-
 '''Various utilities to save and load data'''
 
 import utils
@@ -11,13 +12,50 @@
                   'car':2,
                   'truck':3}
 
+def writeTrajectoriesToSqlite(objects, outFile, trajectoryType, objectNumbers = -1):
+    """
+    This function writers trajectories to a specified sqlite file
+    @param[in] objects -> a list of trajectories
+    @param[in] trajectoryType -
+    @param[out] outFile -> the .sqlite file containting the written objects
+    @param[in] objectNumber : number of objects loaded
+    """
+
+    import sqlite3
+    connection = sqlite3.connect(outFile)
+    cursor = connection.cursor()
+
+    #creation de la table
+    schema = "CREATE TABLE \"trajectories\"(trajectory_id INTEGER,frame_number INTEGER, x_coordinate REAL, y_coordinate REAL, PRIMARY KEY(trajectory_id, frame_number))"
+    cursor.execute(schema)
+    
+    trajectory_id = 0
+    frame_number = 0
+    if trajectoryType == 'feature':
+        if type(objectNumbers) == int and objectNumbers == -1:
+            for trajectory in objects:
+                trajectory_id += 1
+                frame_number = 0
+                for position in trajectory.getPositions():
+                    frame_number += 1
+                    requete = "insert into trajectories(trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)"
+                    cursor.execute(requete,(trajectory_id,frame_number,position.x,position.y))
+                    
+    connection.commit()            
+    connection.close()
+
+def testWrite():
+    features = loadTrajectoriesFromSqlite("/home/francois/Unison/École/12Été/CRSNG/Data/prototypes.sqlite",'feature',-1)
+    writeTrajectoriesToSqlite(features, "/home/francois/Unison/École/12Été/CRSNG/TAAM-Experiments/resultats/testWrite.sqlite", 'feature')
+
+
 def loadTrajectoriesFromSqlite(filename, trajectoryType, objectNumbers = -1):
     '''Loads nObjects or the indices in objectNumbers from the database 
     TODO: load velocities (replace table name 'positions' by 'velocities'
     TODO: load features as well, other ways of averaging trajectories
     '''
     import sqlite3
-
+    
     connection = sqlite3.connect(filename) # add test if it open
     cursor = connection.cursor()