diff python/storage.py @ 848:0cb69238e6f5

corrected load/save prototypes
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 21 Jul 2016 17:51:11 -0400
parents 5a68779d7777
children a414a7d58483
line wrap: on
line diff
--- a/python/storage.py	Wed Jul 20 17:52:40 2016 -0400
+++ b/python/storage.py	Thu Jul 21 17:51:11 2016 -0400
@@ -364,19 +364,23 @@
         printDBError(error)
     connection.close()
 
-def savePrototypesToSqlite(filename, prototypeIndices, trajectoryType, nMatchings = None):
+def savePrototypesToSqlite(filename, prototypeIndices, trajectoryType, nMatchings = None, dbFilenames = None):
     '''save the prototype indices
     nMatchings, if not None, is a dictionnary between indices and number of matches'''
     connection = sqlite3.connect(filename)
     cursor = connection.cursor()
     try:
-        cursor.execute('CREATE TABLE IF NOT EXISTS prototypes (id INTEGER PRIMARY KEY, trajectory_type VARCHAR CHECK (trajectory_type IN (\"feature\", \"object\")), nMatchings INTEGER)')
+        cursor.execute('CREATE TABLE IF NOT EXISTS prototypes (id INTEGER, dbfilename VARCHAR, trajectory_type VARCHAR CHECK (trajectory_type IN (\"feature\", \"object\")), nMatchings INTEGER, PRIMARY KEY (id, dbfilename))')
         for i in prototypeIndices:
             if nMatchings is not None:
                 n = nMatchings[i]
             else:
                 n = 'NULL'
-            cursor.execute('INSERT INTO prototypes (id, trajectory_type, nMatchings) VALUES ({},\"{}\",{})'.format(i, trajectoryType, n))
+            if dbFilenames is not None:
+                dbfn = dbFilenames[i]
+            else:
+                dbfn = filename
+            cursor.execute('INSERT INTO prototypes (id, dbfilename, trajectory_type, nMatchings) VALUES ({},\"{}\",\"{}\",{})'.format(i, dbfn, trajectoryType, n))
     except sqlite3.OperationalError as error:
         printDBError(error)
     connection.commit()
@@ -387,21 +391,23 @@
     connection = sqlite3.connect(filename)
     cursor = connection.cursor()
     prototypeIndices = []
+    dbFilenames = {}
     trajectoryTypes = []
     nMatchings = {}
     try:
         cursor.execute('SELECT * FROM prototypes')
         for row in cursor:
             prototypeIndices.append(row[0])
-            trajectoryTypes.append(row[1])
+            dbFilenames[row[0]] = row[1]
+            trajectoryTypes.append(row[2])
             if row[2] is not None:
-                nMatchings[row[0]] = row[2]
+                nMatchings[row[0]] = row[3]
     except sqlite3.OperationalError as error:
         printDBError(error)
     connection.close()
     if len(set(trajectoryTypes)) > 1:
         print('Different types of prototypes in database ({}).'.format(set(trajectoryTypes)))
-    return prototypeIndices, trajectoryTypes[0], nMatchings
+    return prototypeIndices, dbFilenames, trajectoryTypes[0], nMatchings
 
 def loadBBMovingObjectsFromSqlite(filename, objectType = 'bb', objectNumbers = None, timeStep = None):
     '''Loads bounding box moving object from an SQLite