changeset 342:4d69486869a5

work on loading indicators
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 20 Jun 2013 15:47:33 -0400
parents 2f39c4ed0b62
children 74e437ab5f11
files python/storage.py
diffstat 1 files changed, 48 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/python/storage.py	Thu Jun 20 14:31:24 2013 -0400
+++ b/python/storage.py	Thu Jun 20 15:47:33 2013 -0400
@@ -57,6 +57,9 @@
     connection.commit()
     connection.close()
 
+def printDBError(error):
+    print('DB Error: {0}'.format(err))
+
 def loadPrototypeMatchIndexesFromSqlite(filename):
     """
     This function loads the prototypes table in the database of name <filename>.
@@ -70,8 +73,8 @@
 
     try:
         cursor.execute('SELECT * from prototypes order by prototype_id, trajectory_id_matched')
-    except sqlite3.OperationalError as err:
-        print('DB Error: {0}'.format(err))
+    except sqlite3.OperationalError as error:
+        printDBError(error)
         return []
 
     for row in cursor:
@@ -115,8 +118,8 @@
             cursor.execute('SELECT OF.object_id, P.frame_number, avg(P.x_coordinate), avg(P.y_coordinate) from '+tableName+' P, objects_features OF where P.trajectory_id = OF.trajectory_id '+objectIdQuery+'group by OF.object_id, P.frame_number order by OF.object_id, P.frame_number')
         else:
             print('no trajectory type was chosen')
-    except sqlite3.OperationalError as err:
-        print('DB Error: {0}'.format(err))
+    except sqlite3.OperationalError as error:
+        printDBError(error)
         return []
 
     objId = -1
@@ -186,8 +189,8 @@
             for obj in objects:
                 obj.userType = userTypes[obj.getNum()]
              
-        except sqlite3.OperationalError as err:
-            print('DB Error: {0}'.format(err))
+        except sqlite3.OperationalError as error:
+            printDBError(error)
             return []
 
     connection.close()
@@ -221,9 +224,12 @@
     import sqlite3
     connection = sqlite3.connect(filename)
     cursor = connection.cursor()
-    createInteractionTable(cursor)
-    for inter in interactions:
-        saveInteraction(cursor, inter)
+    try:
+        createInteractionTable(cursor)
+        for inter in interactions:
+            saveInteraction(cursor, inter)
+    except sqlite3.OperationalError as error:
+        printDBError(error)
     connection.commit()
     connection.close()
 
@@ -237,17 +243,42 @@
     import sqlite3
     connection = sqlite3.connect(filename)
     cursor = connection.cursor()
-    createInteractionTable(cursor)
-    createIndicatorTables(cursor)
-    for inter in interactions:
-        saveInteraction(cursor, inter)
-        for indicatorName in indicatorNames:
-            indicator = inter.getIndicator(indicatorName)
-            if indicator != None:
-                saveIndicator(cursor, inter.getNum(), indicator)
+    try:
+        createInteractionTable(cursor)
+        createIndicatorTables(cursor)
+        for inter in interactions:
+            saveInteraction(cursor, inter)
+            for indicatorName in indicatorNames:
+                indicator = inter.getIndicator(indicatorName)
+                if indicator != None:
+                    saveIndicator(cursor, inter.getNum(), indicator)
+    except sqlite3.OperationalError as error:
+        printDBError(error)
     connection.commit()
     connection.close()
 
+def loadIndicators(filename):
+    indicators = []
+    connection = sqlite3.connect(filename)
+    cursor = connection.cursor()
+    try:
+        cursor.execute('select INT.id, INT.object_id1, INT.object_id2, IND.indicator_type, IND.frame_number, IND.value from interactions INT, indicators IND where INT.id = IND.interaction_id, ORDER BY INT.id, IND.indicator_type')
+        interactionNum = -1
+        indicatorTypeNum = -1
+        for row in cursor:
+            if row[0] != interactionNum:
+                if indicatorNum >= 0:
+                    interactions.append(events.Interaction(interactionNum, moving.TimeInterval(), roadUserNumbers[0], roadUserNumbers[1])) # todo time interval from distance indicator (if available) and link to road user objects
+                interactionNum = row[0]
+                roadUserNumbers = row[1:3]
+                indicatorName = events.Interaction.indicatorNames[row[3]]
+                indicatorValues = {row[4]:row[5]}
+                # test when new interaction or new indicator indicatorTypeNum != row[3]
+
+    except sqlite3.OperationalError as error:
+        printDBError(error)
+    connection.close()
+
 
 #########################
 # txt files