Mercurial > hg > nsaunier > traffic-intelligence
comparison trafficintelligence/storage.py @ 1039:5621e4ad2428
removing prefix option to loadtrajectories from SQLite
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 04 Jul 2018 12:24:37 -0400 |
| parents | 6a6c37eb3a74 |
| children | 20799ac9524e |
comparison
equal
deleted
inserted
replaced
| 1038:d24deb61f550 | 1039:5621e4ad2428 |
|---|---|
| 238 userTypes = {} | 238 userTypes = {} |
| 239 for row in cursor: | 239 for row in cursor: |
| 240 userTypes[row[0]] = row[1] | 240 userTypes[row[0]] = row[1] |
| 241 return userTypes | 241 return userTypes |
| 242 | 242 |
| 243 def loadTrajectoriesFromSqlite(filename, trajectoryType, objectNumbers = None, withFeatures = False, timeStep = None, tablePrefix = None): | 243 def loadTrajectoriesFromSqlite(filename, trajectoryType, objectNumbers = None, withFeatures = False, timeStep = None): |
| 244 '''Loads the trajectories (in the general sense, | 244 '''Loads the trajectories (in the general sense, |
| 245 either features, objects (feature groups) or bounding box series) | 245 either features, objects (feature groups) or bounding box series) |
| 246 The number loaded is either the first objectNumbers objects, | 246 The number loaded is either the first objectNumbers objects, |
| 247 or the indices in objectNumbers from the database''' | 247 or the indices in objectNumbers from the database''' |
| 248 objects = [] | 248 objects = [] |
| 249 with sqlite3.connect(filename) as connection: | 249 with sqlite3.connect(filename) as connection: |
| 250 if tablePrefix is None: | 250 objects = loadTrajectoriesFromTable(connection, 'positions', trajectoryType, objectNumbers, timeStep) |
| 251 prefix = '' | 251 objectVelocities = loadTrajectoriesFromTable(connection, 'velocities', trajectoryType, objectNumbers, timeStep) |
| 252 else: | |
| 253 prefix = tablePrefix + '_' | |
| 254 objects = loadTrajectoriesFromTable(connection, prefix+'positions', trajectoryType, objectNumbers, timeStep) | |
| 255 objectVelocities = loadTrajectoriesFromTable(connection, prefix+'velocities', trajectoryType, objectNumbers, timeStep) | |
| 256 | 252 |
| 257 if len(objectVelocities) > 0: | 253 if len(objectVelocities) > 0: |
| 258 for o,v in zip(objects, objectVelocities): | 254 for o,v in zip(objects, objectVelocities): |
| 259 if o.getNum() == v.getNum(): | 255 if o.getNum() == v.getNum(): |
| 260 o.velocities = v.positions | 256 o.velocities = v.positions |
| 322 return objectFeatureNumbers | 318 return objectFeatureNumbers |
| 323 except sqlite3.OperationalError as error: | 319 except sqlite3.OperationalError as error: |
| 324 printDBError(error) | 320 printDBError(error) |
| 325 return None | 321 return None |
| 326 | 322 |
| 323 def loadObjectTrajectoriesFromSqlite(): | |
| 324 '''Loads object trajectories | |
| 325 either simply objects or features (defaults to loadTrajectoriesFromSqlite) | |
| 326 or the longest features for each object ''' | |
| 327 | |
| 328 | |
| 327 def addCurvilinearTrajectoriesFromSqlite(filename, objects): | 329 def addCurvilinearTrajectoriesFromSqlite(filename, objects): |
| 328 '''Adds curvilinear positions (s_coordinate, y_coordinate, lane) | 330 '''Adds curvilinear positions (s_coordinate, y_coordinate, lane) |
| 329 from a database to an existing MovingObject dict (indexed by each objects's num)''' | 331 from a database to an existing MovingObject dict (indexed by each objects's num)''' |
| 330 with sqlite3.connect(filename) as connection: | 332 with sqlite3.connect(filename) as connection: |
| 331 cursor = connection.cursor() | 333 cursor = connection.cursor() |
| 348 if objNum in objects: | 350 if objNum in objects: |
| 349 objects[objNum].curvilinearPositions.addPositionSYL(row[2],row[3],row[4]) | 351 objects[objNum].curvilinearPositions.addPositionSYL(row[2],row[3],row[4]) |
| 350 if len(missingObjectNumbers) > 0: | 352 if len(missingObjectNumbers) > 0: |
| 351 print('List of missing objects to attach corresponding curvilinear trajectories: {}'.format(missingObjectNumbers)) | 353 print('List of missing objects to attach corresponding curvilinear trajectories: {}'.format(missingObjectNumbers)) |
| 352 | 354 |
| 353 def saveTrajectoriesToTable(connection, objects, trajectoryType, tablePrefix = None): | 355 def saveTrajectoriesToTable(connection, objects, trajectoryType): |
| 354 'Saves trajectories in table tableName' | 356 'Saves trajectories in table tableName' |
| 355 cursor = connection.cursor() | 357 cursor = connection.cursor() |
| 356 # Parse feature and/or object structure and commit to DB | 358 # Parse feature and/or object structure and commit to DB |
| 357 if(trajectoryType == 'feature' or trajectoryType == 'object'): | 359 if(trajectoryType == 'feature' or trajectoryType == 'object'): |
| 358 # Extract features from objects | 360 # Extract features from objects |
| 364 if len(features) == 0: | 366 if len(features) == 0: |
| 365 print('Warning, objects have no features') # todo save centroid trajectories? | 367 print('Warning, objects have no features') # todo save centroid trajectories? |
| 366 elif trajectoryType == 'feature': | 368 elif trajectoryType == 'feature': |
| 367 features = objects | 369 features = objects |
| 368 # Setup feature queries | 370 # Setup feature queries |
| 369 if tablePrefix is None: | 371 createTrajectoryTable(cursor, "positions") |
| 370 prefix = '' | 372 createTrajectoryTable(cursor, "velocities") |
| 371 else: | 373 positionQuery = insertTrajectoryQuery("positions") |
| 372 prefix = tablePrefix+'_' | 374 velocityQuery = insertTrajectoryQuery("velocities") |
| 373 createTrajectoryTable(cursor, prefix+"positions") | |
| 374 createTrajectoryTable(cursor, prefix+"velocities") | |
| 375 positionQuery = insertTrajectoryQuery(prefix+"positions") | |
| 376 velocityQuery = insertTrajectoryQuery(prefix+"velocities") | |
| 377 # Setup object queries | 375 # Setup object queries |
| 378 if trajectoryType == 'object': | 376 if trajectoryType == 'object': |
| 379 createObjectsTable(cursor) | 377 createObjectsTable(cursor) |
| 380 createObjectsFeaturesTable(cursor) | 378 createObjectsFeaturesTable(cursor) |
| 381 objectQuery = insertObjectQuery() | 379 objectQuery = insertObjectQuery() |
