Mercurial > hg > nsaunier > traffic-intelligence
comparison python/storage.py @ 880:000555430b28
adapted code from Paul St-Aubin and udpated MovingObject.setFeatures to truly find the right features in a list that may not start at 0
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 14 Mar 2017 17:10:35 -0400 |
| parents | f9ea5083588e |
| children | 4749b71aa7fb |
comparison
equal
deleted
inserted
replaced
| 879:f9ea5083588e | 880:000555430b28 |
|---|---|
| 331 '''Writes features, ie the trajectory positions (and velocities if exist) | 331 '''Writes features, ie the trajectory positions (and velocities if exist) |
| 332 with their instants to a specified sqlite file | 332 with their instants to a specified sqlite file |
| 333 Either feature positions (and velocities if they exist) | 333 Either feature positions (and velocities if they exist) |
| 334 or curvilinear positions will be saved at a time''' | 334 or curvilinear positions will be saved at a time''' |
| 335 | 335 |
| 336 ## Sanitation | |
| 337 if(type(objects) is not list or len(objects)==0): return False | |
| 338 connection = sqlite3.connect(outputFilename) | 336 connection = sqlite3.connect(outputFilename) |
| 339 try: | 337 try: |
| 340 cursor = connection.cursor() | 338 cursor = connection.cursor() |
| 341 ## Extract features from objects | 339 # Parse feature and/or object structure and commit to DB |
| 342 if(trajectoryType == 'object'): | |
| 343 features = [] | |
| 344 for obj in objects: | |
| 345 if(obj.hasFeatures()): | |
| 346 features += obj.getFeatures() | |
| 347 elif(trajectoryType == 'feature'): | |
| 348 features = objects | |
| 349 ## Setup feature queries | |
| 350 if(trajectoryType == 'feature' or trajectoryType == 'object'): | 340 if(trajectoryType == 'feature' or trajectoryType == 'object'): |
| 341 # Extract features from objects | |
| 342 if(trajectoryType == 'object'): | |
| 343 features = [] | |
| 344 for obj in objects: | |
| 345 if(obj.hasFeatures()): | |
| 346 features += obj.getFeatures() | |
| 347 elif(trajectoryType == 'feature'): | |
| 348 features = objects | |
| 349 # Setup feature queries | |
| 351 createTrajectoryTable(cursor, "positions") | 350 createTrajectoryTable(cursor, "positions") |
| 352 createTrajectoryTable(cursor, "velocities") | 351 createTrajectoryTable(cursor, "velocities") |
| 353 positionQuery = insertTrajectoryQuery("positions") | 352 positionQuery = insertTrajectoryQuery("positions") |
| 354 velocityQuery = insertTrajectoryQuery("velocities") | 353 velocityQuery = insertTrajectoryQuery("velocities") |
| 355 ## Setup object queries | 354 # Setup object queries |
| 356 if(trajectoryType == 'object'): | 355 if(trajectoryType == 'object'): |
| 357 createObjectsTable(cursor) | 356 createObjectsTable(cursor) |
| 358 createObjectsFeaturesTable(cursor) | 357 createObjectsFeaturesTable(cursor) |
| 359 objectQuery = insertObjectQuery() | 358 objectQuery = insertObjectQuery() |
| 360 objectFeatureQuery = insertObjectFeatureQuery() | 359 objectFeatureQuery = insertObjectFeatureQuery() |
| 361 ## Parse feature and/or object structure and commit to DB | |
| 362 if(trajectoryType == 'feature' or trajectoryType == 'object'): | |
| 363 running_tally_of_unique_nums = [] | |
| 364 for feature in features: | 360 for feature in features: |
| 365 num = feature.getNum() | 361 num = feature.getNum() |
| 366 if(num not in running_tally_of_unique_nums): | 362 frameNum = feature.getFirstInstant() |
| 367 running_tally_of_unique_nums.append(num) | 363 for position in feature.getPositions(): |
| 368 frame_number = feature.getFirstInstant() | 364 cursor.execute(positionQuery, (num, frameNum, position.x, position.y)) |
| 369 for position in feature.getPositions(): | 365 frameNum += 1 |
| 370 cursor.execute(positionQuery, (num, frame_number, position.x, position.y)) | 366 velocities = feature.getVelocities() |
| 371 frame_number += 1 | 367 if velocities is not None: |
| 372 velocities = feature.getVelocities() | 368 frameNum = feature.getFirstInstant() |
| 373 if velocities is not None: | 369 for i in xrange(velocities.length()-1): |
| 374 frame_number = feature.getFirstInstant() | 370 v = velocities[i] |
| 375 for i in xrange(velocities.length()-1): | 371 cursor.execute(velocityQuery, (num, frameNum, v.x, v.y)) |
| 376 v = velocities[i] | 372 frameNum += 1 |
| 377 cursor.execute(velocityQuery, (num, frame_number, v.x, v.y)) | |
| 378 frame_number += 1 | |
| 379 if(trajectoryType == 'object'): | 373 if(trajectoryType == 'object'): |
| 380 running_tally_of_unique_nums = [] | |
| 381 for obj in objects: | 374 for obj in objects: |
| 382 if(obj.hasFeatures()): | 375 for feature in obj.getFeatures(): |
| 383 n_objects = len(obj.getFeatures()) | 376 featureNum = feature.getNum() |
| 384 for feature in obj.getFeatures(): | 377 cursor.execute(objectFeatureQuery, (obj.getNum(), featureNum)) |
| 385 feature_num = feature.getNum() | 378 cursor.execute(objectQuery, (obj.getNum(), obj.getUserType(), 1)) |
| 386 if(feature_num not in running_tally_of_unique_nums): | 379 # Parse curvilinear position structure |
| 387 running_tally_of_unique_nums.append(feature_num) | |
| 388 cursor.execute(objectFeatureQuery, (obj.getNum(), feature_num)) | |
| 389 else: | |
| 390 n_objects = 0 | |
| 391 cursor.execute(objectQuery, (obj.getNum(), obj.getUserType(), n_objects)) | |
| 392 ## Parse curvilinear position structure | |
| 393 elif(trajectoryType == 'curvilinear'): | 380 elif(trajectoryType == 'curvilinear'): |
| 394 createCurvilinearTrajectoryTable(cursor) | 381 createCurvilinearTrajectoryTable(cursor) |
| 395 curvilinearQuery = "insert into curvilinear_positions (trajectory_id, frame_number, s_coordinate, y_coordinate, lane) values (?,?,?,?,?)" | 382 curvilinearQuery = "insert into curvilinear_positions (trajectory_id, frame_number, s_coordinate, y_coordinate, lane) values (?,?,?,?,?)" |
| 396 for obj in objects: | 383 for obj in objects: |
| 397 num = obj.getNum() | 384 num = obj.getNum() |
| 398 frame_number = obj.getFirstInstant() | 385 frameNum = obj.getFirstInstant() |
| 399 for position in obj.getCurvilinearPositions(): | 386 for position in obj.getCurvilinearPositions(): |
| 400 cursor.execute(curvilinearQuery, (num, frame_number, position[0], position[1], position[2])) | 387 cursor.execute(curvilinearQuery, (num, frameNum, position[0], position[1], position[2])) |
| 401 frame_number += 1 | 388 frameNum += 1 |
| 402 else: | 389 else: |
| 403 print('Unknown trajectory type {}'.format(trajectoryType)) | 390 print('Unknown trajectory type {}'.format(trajectoryType)) |
| 404 connection.commit() | 391 connection.commit() |
| 405 except sqlite3.OperationalError as error: | 392 except sqlite3.OperationalError as error: |
| 406 printDBError(error) | 393 printDBError(error) |
