Mercurial Hosting > traffic-intelligence
comparison python/storage.py @ 918:3a06007a4bb7
modularized save trajectories, added slice to Trajectory, etc
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 05 Jul 2017 12:19:59 -0400 |
parents | 89cc05867c4c |
children | 7b3f2e0a2652 |
comparison
equal
deleted
inserted
replaced
917:89cc05867c4c | 918:3a06007a4bb7 |
---|---|
373 if objNum in objects: | 373 if objNum in objects: |
374 objects[objNum].curvilinearPositions.addPositionSYL(row[2],row[3],row[4]) | 374 objects[objNum].curvilinearPositions.addPositionSYL(row[2],row[3],row[4]) |
375 if len(missingObjectNumbers) > 0: | 375 if len(missingObjectNumbers) > 0: |
376 print('List of missing objects to attach corresponding curvilinear trajectories: {}'.format(missingObjectNumbers)) | 376 print('List of missing objects to attach corresponding curvilinear trajectories: {}'.format(missingObjectNumbers)) |
377 | 377 |
378 def saveTrajectoriesToSqlite(outputFilename, objects, trajectoryType, withFeatures = False): | 378 def saveTrajectoriesToTable(connection, objects, trajectoryType, tablePrefix = None): |
379 'Saves trajectories in table tableName' | |
380 cursor = connection.cursor() | |
381 # Parse feature and/or object structure and commit to DB | |
382 if(trajectoryType == 'feature' or trajectoryType == 'object'): | |
383 # Extract features from objects | |
384 if trajectoryType == 'object': | |
385 features = [] | |
386 for obj in objects: | |
387 if obj.hasFeatures(): | |
388 features += obj.getFeatures() | |
389 if len(features) == 0: | |
390 print('Warning, objects have no features') # todo save centroid trajectories? | |
391 elif trajectoryType == 'feature': | |
392 features = objects | |
393 # Setup feature queries | |
394 if tablePrefix is None: | |
395 prefix = '' | |
396 else: | |
397 prefix = tablePrefix+'_' | |
398 createTrajectoryTable(cursor, prefix+"positions") | |
399 createTrajectoryTable(cursor, prefix+"velocities") | |
400 positionQuery = insertTrajectoryQuery(prefix+"positions") | |
401 velocityQuery = insertTrajectoryQuery(prefix+"velocities") | |
402 # Setup object queries | |
403 if trajectoryType == 'object': | |
404 createObjectsTable(cursor) | |
405 createObjectsFeaturesTable(cursor) | |
406 objectQuery = insertObjectQuery() | |
407 objectFeatureQuery = insertObjectFeatureQuery() | |
408 for feature in features: | |
409 num = feature.getNum() | |
410 frameNum = feature.getFirstInstant() | |
411 for p in feature.getPositions(): | |
412 cursor.execute(positionQuery, (num, frameNum, p.x, p.y)) | |
413 frameNum += 1 | |
414 velocities = feature.getVelocities() | |
415 if velocities is not None: | |
416 frameNum = feature.getFirstInstant() | |
417 for v in velocities[:-1]: | |
418 cursor.execute(velocityQuery, (num, frameNum, v.x, v.y)) | |
419 frameNum += 1 | |
420 if trajectoryType == 'object': | |
421 for obj in objects: | |
422 if obj.hasFeatures(): | |
423 for feature in obj.getFeatures(): | |
424 featureNum = feature.getNum() | |
425 cursor.execute(objectFeatureQuery, (obj.getNum(), featureNum)) | |
426 cursor.execute(objectQuery, (obj.getNum(), obj.getUserType(), 1)) | |
427 # Parse curvilinear position structure | |
428 elif(trajectoryType == 'curvilinear'): | |
429 createCurvilinearTrajectoryTable(cursor) | |
430 curvilinearQuery = "insert into curvilinear_positions (trajectory_id, frame_number, s_coordinate, y_coordinate, lane) values (?,?,?,?,?)" | |
431 for obj in objects: | |
432 num = obj.getNum() | |
433 frameNum = obj.getFirstInstant() | |
434 for p in obj.getCurvilinearPositions(): | |
435 cursor.execute(curvilinearQuery, (num, frameNum, p[0], p[1], p[2])) | |
436 frameNum += 1 | |
437 else: | |
438 print('Unknown trajectory type {}'.format(trajectoryType)) | |
439 connection.commit() | |
440 | |
441 def saveTrajectoriesToSqlite(outputFilename, objects, trajectoryType): | |
379 '''Writes features, ie the trajectory positions (and velocities if exist) | 442 '''Writes features, ie the trajectory positions (and velocities if exist) |
380 with their instants to a specified sqlite file | 443 with their instants to a specified sqlite file |
381 Either feature positions (and velocities if they exist) | 444 Either feature positions (and velocities if they exist) |
382 or curvilinear positions will be saved at a time''' | 445 or curvilinear positions will be saved at a time''' |
383 | 446 |
384 connection = sqlite3.connect(outputFilename) | 447 connection = sqlite3.connect(outputFilename) |
385 try: | 448 try: |
386 cursor = connection.cursor() | 449 saveTrajectoriesToTable(connection, objects, trajectoryType, None) |
387 # Parse feature and/or object structure and commit to DB | |
388 if(trajectoryType == 'feature' or trajectoryType == 'object'): | |
389 # Extract features from objects | |
390 if(trajectoryType == 'object'): | |
391 features = [] | |
392 for obj in objects: | |
393 if(obj.hasFeatures()): | |
394 features += obj.getFeatures() | |
395 elif(trajectoryType == 'feature'): | |
396 features = objects | |
397 # Setup feature queries | |
398 createTrajectoryTable(cursor, "positions") | |
399 createTrajectoryTable(cursor, "velocities") | |
400 positionQuery = insertTrajectoryQuery("positions") | |
401 velocityQuery = insertTrajectoryQuery("velocities") | |
402 # Setup object queries | |
403 if(trajectoryType == 'object'): | |
404 createObjectsTable(cursor) | |
405 createObjectsFeaturesTable(cursor) | |
406 objectQuery = insertObjectQuery() | |
407 objectFeatureQuery = insertObjectFeatureQuery() | |
408 for feature in features: | |
409 num = feature.getNum() | |
410 frameNum = feature.getFirstInstant() | |
411 for position in feature.getPositions(): | |
412 cursor.execute(positionQuery, (num, frameNum, position.x, position.y)) | |
413 frameNum += 1 | |
414 velocities = feature.getVelocities() | |
415 if velocities is not None: | |
416 frameNum = feature.getFirstInstant() | |
417 for i in xrange(velocities.length()-1): | |
418 v = velocities[i] | |
419 cursor.execute(velocityQuery, (num, frameNum, v.x, v.y)) | |
420 frameNum += 1 | |
421 if(trajectoryType == 'object'): | |
422 for obj in objects: | |
423 for feature in obj.getFeatures(): | |
424 featureNum = feature.getNum() | |
425 cursor.execute(objectFeatureQuery, (obj.getNum(), featureNum)) | |
426 cursor.execute(objectQuery, (obj.getNum(), obj.getUserType(), 1)) | |
427 # Parse curvilinear position structure | |
428 elif(trajectoryType == 'curvilinear'): | |
429 createCurvilinearTrajectoryTable(cursor) | |
430 curvilinearQuery = "insert into curvilinear_positions (trajectory_id, frame_number, s_coordinate, y_coordinate, lane) values (?,?,?,?,?)" | |
431 for obj in objects: | |
432 num = obj.getNum() | |
433 frameNum = obj.getFirstInstant() | |
434 for position in obj.getCurvilinearPositions(): | |
435 cursor.execute(curvilinearQuery, (num, frameNum, position[0], position[1], position[2])) | |
436 frameNum += 1 | |
437 else: | |
438 print('Unknown trajectory type {}'.format(trajectoryType)) | |
439 connection.commit() | |
440 except sqlite3.OperationalError as error: | 450 except sqlite3.OperationalError as error: |
441 printDBError(error) | 451 printDBError(error) |
442 connection.close() | 452 connection.close() |
443 | 453 |
444 def loadBBMovingObjectsFromSqlite(filename, objectType = 'bb', objectNumbers = None, timeStep = None): | 454 def loadBBMovingObjectsFromSqlite(filename, objectType = 'bb', objectNumbers = None, timeStep = None): |
596 dbfn = dbFilenames[i] | 606 dbfn = dbFilenames[i] |
597 else: | 607 else: |
598 dbfn = filename | 608 dbfn = filename |
599 cursor.execute('INSERT INTO prototypes (id, dbfilename, trajectory_type, nmatchings, positions_id) VALUES ({},\"{}\",\"{}\",{}, {})'.format(protoId, dbfn, trajectoryType, n, i)) | 609 cursor.execute('INSERT INTO prototypes (id, dbfilename, trajectory_type, nmatchings, positions_id) VALUES ({},\"{}\",\"{}\",{}, {})'.format(protoId, dbfn, trajectoryType, n, i)) |
600 #cursor.execute('SELECT * from sqlite_master WHERE type = \"table\" and name = \"{}\"'.format(tableNames[trajectoryType])) | 610 #cursor.execute('SELECT * from sqlite_master WHERE type = \"table\" and name = \"{}\"'.format(tableNames[trajectoryType])) |
601 if objects is not None: | 611 if objects is not None: # save positions and velocities |
602 pass | 612 pass |
603 except sqlite3.OperationalError as error: | 613 except sqlite3.OperationalError as error: |
604 printDBError(error) | 614 printDBError(error) |
605 connection.commit() | 615 connection.commit() |
606 connection.close() | 616 connection.close() |