Mercurial > hg > nsaunier > traffic-intelligence
comparison python/storage.py @ 212:ce44605f888a
minor modifications
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 06 Jun 2012 23:39:03 -0400 |
| parents | ada6e8fbe4c6 |
| children | 5e2983b05d4e |
comparison
equal
deleted
inserted
replaced
| 211:ada6e8fbe4c6 | 212:ce44605f888a |
|---|---|
| 23 | 23 |
| 24 import sqlite3 | 24 import sqlite3 |
| 25 connection = sqlite3.connect(outFile) | 25 connection = sqlite3.connect(outFile) |
| 26 cursor = connection.cursor() | 26 cursor = connection.cursor() |
| 27 | 27 |
| 28 #creation de la table | 28 schema = "CREATE TABLE \"positions\"(trajectory_id INTEGER,frame_number INTEGER, x_coordinate REAL, y_coordinate REAL, PRIMARY KEY(trajectory_id, frame_number))" |
| 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) | 29 cursor.execute(schema) |
| 30 | |
| 31 trajectory_id = 0 | 31 trajectory_id = 0 |
| 32 frame_number = 0 | 32 frame_number = 0 |
| 33 if trajectoryType == 'feature': | 33 if trajectoryType == 'feature': |
| 34 if type(objectNumbers) == int and objectNumbers == -1: | 34 if type(objectNumbers) == int and objectNumbers == -1: |
| 35 for trajectory in objects: | 35 for trajectory in objects: |
| 36 trajectory_id += 1 | 36 trajectory_id += 1 |
| 37 frame_number = 0 | 37 frame_number = 0 |
| 38 for position in trajectory.getPositions(): | 38 for position in trajectory.getPositions(): |
| 39 frame_number += 1 | 39 frame_number += 1 |
| 40 requete = "insert into trajectories(trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)" | 40 query = "insert into positions (trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)" |
| 41 cursor.execute(requete,(trajectory_id,frame_number,position.x,position.y)) | 41 cursor.execute(query,(trajectory_id,frame_number,position.x,position.y)) |
| 42 | 42 |
| 43 connection.commit() | 43 connection.commit() |
| 44 connection.close() | 44 connection.close() |
| 45 | |
| 46 def testWrite(): | |
| 47 features = loadTrajectoriesFromSqlite("/home/francois/Unison/École/12Été/CRSNG/Data/prototypes.sqlite",'feature',-1) | |
| 48 writeTrajectoriesToSqlite(features, "/home/francois/Unison/École/12Été/CRSNG/TAAM-Experiments/resultats/testWrite.sqlite", 'feature') | |
| 49 | 45 |
| 50 def loadPrototypeMatchIndexesFromSqlite(filename): | 46 def loadPrototypeMatchIndexesFromSqlite(filename): |
| 51 """ | 47 """ |
| 52 This function loads the prototypes table in the database of name <filename>. | 48 This function loads the prototypes table in the database of name <filename>. |
| 53 It returns a list of tuples representing matching ids : [(prototype_id, matched_trajectory_id),...] | 49 It returns a list of tuples representing matching ids : [(prototype_id, matched_trajectory_id),...] |
| 68 | 64 |
| 69 connection.close() | 65 connection.close() |
| 70 return matched_indexes | 66 return matched_indexes |
| 71 | 67 |
| 72 def testloadPrototypeMatchIndexesFromSqlite(): | 68 def testloadPrototypeMatchIndexesFromSqlite(): |
| 69 'TODO: write as doctest' | |
| 73 empty_list = loadPrototypeMatchIndexesFromSqlite("bidon") | 70 empty_list = loadPrototypeMatchIndexesFromSqlite("bidon") |
| 74 if empty_list == []: | 71 if empty_list == []: |
| 75 print "Empty list test Ok" | 72 print "Empty list test Ok" |
| 76 | 73 |
| 77 matches=loadPrototypeMatchIndexesFromSqlite("/home/francois/Unison/École/12Été/CRSNG/TAAM-Experiments/resultats/prototypes-with-matches.sqlite") | 74 matches=loadPrototypeMatchIndexesFromSqlite("/home/francois/Unison/École/12Été/CRSNG/TAAM-Experiments/resultats/prototypes-with-matches.sqlite") |
