diff python/storage.py @ 915:13434f5017dd

work to save trajectory assignment to origin and destinations
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 04 Jul 2017 17:03:29 -0400
parents 1cd878812529
children 89cc05867c4c
line wrap: on
line diff
--- a/python/storage.py	Wed Jun 28 23:43:52 2017 -0400
+++ b/python/storage.py	Tue Jul 04 17:03:29 2017 -0400
@@ -507,7 +507,7 @@
         elif dataType == 'bb':
             dropTables(connection, ['bounding_boxes'])
         elif dataType == 'pois':
-            dropTables(connection, ['gaussians2d'])
+            dropTables(connection, ['gaussians2d', 'objects_pois'])
         elif dataType == 'prototype':
             dropTables(connection, ['prototypes'])
         else:
@@ -645,6 +645,19 @@
         printDBError(error)
     connection.close()
 
+def savePOIAssignments(filename, objects):
+    'save the od fields of objects'
+    connection = sqlite3.connect(filename)
+    cursor = connection.cursor()
+    try:
+        cursor.execute('CREATE TABLE IF NOT EXISTS objects_pois (object_id INTEGER, origin_poi_id INTEGER, destination_poi_id INTEGER, PRIMARY KEY(object_id))')
+        for o in objects:
+            cursor.execute('INSERT INTO objects_pois VALUES({},{},{})'.format(o.getNum(), o.od[0], o.od[1]))
+        connection.commit()
+    except sqlite3.OperationalError as error:
+        printDBError(error)
+    connection.close()
+    
 def loadPOIs(filename):
     'Loads all 2D Gaussians in the database'
     from sklearn import mixture # todo if not avalaible, load data in duck-typed class with same fields
@@ -698,25 +711,25 @@
 #########################
 
 def writePrototypesToSqlite(prototypes,nMatching, outputFilename):
-    """ prototype dataset is a dictionary with  keys== routes, values== prototypes Ids """
+    ''' prototype dataset is a dictionary with  keys== routes, values== prototypes Ids '''
     connection = sqlite3.connect(outputFilename)
     cursor = connection.cursor()
 
-    cursor.execute("CREATE TABLE IF NOT EXISTS prototypes (prototype_id INTEGER,routeIDstart INTEGER,routeIDend INTEGER, nMatching INTEGER, PRIMARY KEY(prototype_id))")
+    cursor.execute('CREATE TABLE IF NOT EXISTS prototypes (prototype_id INTEGER,routeIDstart INTEGER,routeIDend INTEGER, nMatching INTEGER, PRIMARY KEY(prototype_id))')
     
     for route in prototypes.keys():
         if prototypes[route]!=[]:
             for i in prototypes[route]:
-                cursor.execute("insert into prototypes (prototype_id, routeIDstart,routeIDend, nMatching) values (?,?,?,?)",(i,route[0],route[1],nMatching[route][i]))
+                cursor.execute('insert into prototypes (prototype_id, routeIDstart,routeIDend, nMatching) values (?,?,?,?)',(i,route[0],route[1],nMatching[route][i]))
                     
     connection.commit()
     connection.close()
     
 def readPrototypesFromSqlite(filename):
-    """
+    '''
     This function loads the prototype file in the database 
     It returns a dictionary for prototypes for each route and nMatching
-    """
+    '''
     prototypes = {}
     nMatching={}