Mercurial > hg > nsaunier > traffic-intelligence
comparison python/storage.py @ 780:1b22d81ef5ff dev
cleaned and checked storage with functions for curvilinear trajectories
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Mon, 08 Feb 2016 12:24:26 -0500 |
| parents | bd684e57c431 |
| children | f1040d739bbf |
comparison
equal
deleted
inserted
replaced
| 779:670bd6a35417 | 780:1b22d81ef5ff |
|---|---|
| 253 objects = [] | 253 objects = [] |
| 254 | 254 |
| 255 connection.close() | 255 connection.close() |
| 256 return objects | 256 return objects |
| 257 | 257 |
| 258 def loadCurvilinearTrajectoriesFromSqlite(filename, objects, objectNumbers = None): | 258 def addCurvilinearTrajectoriesFromSqlite(filename, objects): |
| 259 '''load alignement curvilinear positions (s_coordinate, y_coordinate, lane) | 259 '''Adds curvilinear positions (s_coordinate, y_coordinate, lane) |
| 260 from an object database to an existing MovingObject list''' | 260 from a database to an existing MovingObject dict (indexed by each objects's num)''' |
| 261 connection = sqlite3.connect(inputFilename) | 261 connection = sqlite3.connect(filename) |
| 262 cursor = connection.cursor() | 262 cursor = connection.cursor() |
| 263 | 263 |
| 264 try: | 264 try: |
| 265 cursor.execute('SELECT * from curvilinear_positions order by trajectory_id, frame_number') | 265 cursor.execute('SELECT * from curvilinear_positions order by trajectory_id, frame_number') |
| 266 except sqlite3.OperationalError as error: | 266 except sqlite3.OperationalError as error: |
| 269 | 269 |
| 270 objNum = None | 270 objNum = None |
| 271 for row in cursor: | 271 for row in cursor: |
| 272 if objNum != row[0]: | 272 if objNum != row[0]: |
| 273 objNum = row[0] | 273 objNum = row[0] |
| 274 objects[objNum].curvilinearPositions = moving.CurvilinearPositions() | 274 objects[objNum].curvilinearPositions = moving.CurvilinearTrajectory() |
| 275 else: | 275 objects[objNum].curvilinearPositions.addPositionSYL(row[2],row[3],row[4]) |
| 276 objects[objNum].curvilinearPositions.addPositionsSYL(row[2],row[3],row[4]) | |
| 277 | |
| 278 return objects | |
| 279 | 276 |
| 280 def saveTrajectoriesToSqlite(outputFilename, objects, trajectoryType, withFeatures = False): | 277 def saveTrajectoriesToSqlite(outputFilename, objects, trajectoryType, withFeatures = False): |
| 281 '''Writes features, ie the trajectories positions (and velocities if exist) | 278 '''Writes features, ie the trajectories positions (and velocities if exist) |
| 282 with their instants to a specified sqlite file | 279 with their instants to a specified sqlite file |
| 283 | 280 Either feature positions (and velocities if they exist) |
| 284 TODO: Not implemented for other trajectoryType than features | 281 or curvilinear positions will be saved at a time |
| 282 | |
| 283 TODO: Not implemented for trajectoryType MovingObject with features | |
| 285 For objects, with features will control whether the features | 284 For objects, with features will control whether the features |
| 286 corresponding to the object are also saved''' | 285 corresponding to the object are also saved''' |
| 287 | 286 |
| 288 connection = sqlite3.connect(outputFilename) | 287 connection = sqlite3.connect(outputFilename) |
| 289 try: | 288 try: |
| 290 cursor = connection.cursor() | 289 cursor = connection.cursor() |
| 291 | 290 |
| 292 if trajectoryType in ['feature', 'curvilinear']: | 291 if trajectoryType == 'feature': |
| 293 cursor.execute("CREATE TABLE IF NOT EXISTS positions (trajectory_id INTEGER, frame_number INTEGER, x_coordinate REAL, y_coordinate REAL, PRIMARY KEY(trajectory_id, frame_number))") | 292 cursor.execute("CREATE TABLE IF NOT EXISTS positions (trajectory_id INTEGER, frame_number INTEGER, x_coordinate REAL, y_coordinate REAL, PRIMARY KEY(trajectory_id, frame_number))") |
| 294 cursor.execute("CREATE TABLE IF NOT EXISTS velocities (trajectory_id INTEGER, frame_number INTEGER, x_coordinate REAL, y_coordinate REAL, PRIMARY KEY(trajectory_id, frame_number))") | 293 cursor.execute("CREATE TABLE IF NOT EXISTS velocities (trajectory_id INTEGER, frame_number INTEGER, x_coordinate REAL, y_coordinate REAL, PRIMARY KEY(trajectory_id, frame_number))") |
| 295 if trajectoryType == 'curvilinear': | |
| 296 cursor.execute("CREATE TABLE IF NOT EXISTS curvilinear_positions (trajectory_id INTEGER, frame_number INTEGER, s_coordinate REAL, y_coordinate REAL, lane TEXT, PRIMARY KEY(trajectory_id, frame_number))") | |
| 297 | 294 |
| 298 positionQuery = "insert into positions (trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)" | 295 positionQuery = "insert into positions (trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)" |
| 299 velocityQuery = "insert into velocities (trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)" | 296 velocityQuery = "insert into velocities (trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)" |
| 300 curvilinearQuery = "insert into curvilinear_positions (trajectory_id, frame_number, x_coordinate, y_coordinate) values (?,?,?,?)" | |
| 301 for obj in objects: | 297 for obj in objects: |
| 302 num = obj.getNum() | 298 num = obj.getNum() |
| 303 frame_number = obj.getFirstInstant() | 299 frame_number = obj.getFirstInstant() |
| 304 for position in obj.getPositions(): | 300 for position in obj.getPositions(): |
| 305 cursor.execute(positionQuery, (num, frame_number, position.x, position.y)) | 301 cursor.execute(positionQuery, (num, frame_number, position.x, position.y)) |
| 310 frame_number = obj.getFirstInstant() | 306 frame_number = obj.getFirstInstant() |
| 311 for i in xrange(velocities.length()-1): | 307 for i in xrange(velocities.length()-1): |
| 312 v = velocities[i] | 308 v = velocities[i] |
| 313 cursor.execute(velocityQuery, (num, frame_number, v.x, v.y)) | 309 cursor.execute(velocityQuery, (num, frame_number, v.x, v.y)) |
| 314 frame_number += 1 | 310 frame_number += 1 |
| 315 # curvilinear trajectories | 311 elif trajectoryType == 'curvilinear': |
| 316 if trajectoryType == 'curvilinear' and hasattr(obj, 'curvilinearPositions') is not None: | 312 cursor.execute("CREATE TABLE IF NOT EXISTS curvilinear_positions (trajectory_id INTEGER, frame_number INTEGER, s_coordinate REAL, y_coordinate REAL, lane TEXT, PRIMARY KEY(trajectory_id, frame_number))") |
| 317 frame_number = obj.getFirstInstant() | 313 curvilinearQuery = "insert into curvilinear_positions (trajectory_id, frame_number, s_coordinate, y_coordinate, lane) values (?,?,?,?,?)" |
| 318 for position in obj.getCurvilinearPositions(): | 314 for obj in objects: |
| 319 cursor.execute(curvilinearQuery, (num, frame_number, position[0], position[1], position[2])) | 315 num = obj.getNum() |
| 320 frame_number += 1 | 316 frame_number = obj.getFirstInstant() |
| 321 connection.commit() | 317 for position in obj.getCurvilinearPositions(): |
| 318 cursor.execute(curvilinearQuery, (num, frame_number, position[0], position[1], position[2])) | |
| 319 frame_number += 1 | |
| 322 #elif trajectoryType == 'object': | 320 #elif trajectoryType == 'object': |
| 323 else: | 321 else: |
| 324 print('Unknown trajectory type {}'.format(trajectoryType)) | 322 print('Unknown trajectory type {}'.format(trajectoryType)) |
| 323 connection.commit() | |
| 325 except sqlite3.OperationalError as error: | 324 except sqlite3.OperationalError as error: |
| 326 printDBError(error) | 325 printDBError(error) |
| 327 connection.close() | 326 connection.close() |
| 328 | 327 |
| 329 def savePrototypesToSqlite(filename, prototypes, trajectoryType = 'feature'): | 328 def savePrototypesToSqlite(filename, prototypes, trajectoryType = 'feature'): |
