Mercurial Hosting > traffic-intelligence
comparison python/storage.py @ 910:b58a1061a717
loading is faster for longest object features
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 28 Jun 2017 15:36:25 -0400 |
parents | cd038493f8c6 |
children | 1cd878812529 |
comparison
equal
deleted
inserted
replaced
909:cd038493f8c6 | 910:b58a1061a717 |
---|---|
232 | 232 |
233 return objects | 233 return objects |
234 | 234 |
235 def loadUserTypesFromTable(cursor, objectNumbers): | 235 def loadUserTypesFromTable(cursor, objectNumbers): |
236 objectCriteria = getObjectCriteria(objectNumbers) | 236 objectCriteria = getObjectCriteria(objectNumbers) |
237 queryStatement = 'SELECT object_id, road_user_type from objects' | 237 queryStatement = 'SELECT object_id, road_user_type FROM objects' |
238 if objectNumbers is not None: | 238 if objectNumbers is not None: |
239 queryStatement += ' WHERE object_id '+objectCriteria | 239 queryStatement += ' WHERE object_id '+objectCriteria |
240 cursor.execute(queryStatement) | 240 cursor.execute(queryStatement) |
241 userTypes = {} | 241 userTypes = {} |
242 for row in cursor: | 242 for row in cursor: |
263 | 263 |
264 if trajectoryType == 'object': | 264 if trajectoryType == 'object': |
265 cursor = connection.cursor() | 265 cursor = connection.cursor() |
266 try: | 266 try: |
267 # attribute feature numbers to objects | 267 # attribute feature numbers to objects |
268 objectCriteria = getObjectCriteria(objectNumbers) | |
269 queryStatement = 'SELECT trajectory_id, object_id FROM objects_features' | 268 queryStatement = 'SELECT trajectory_id, object_id FROM objects_features' |
270 if objectNumbers is not None: | 269 if objectNumbers is not None: |
271 queryStatement += ' WHERE object_id '+objectCriteria | 270 queryStatement += ' WHERE object_id '+getObjectCriteria(objectNumbers) |
272 queryStatement += ' ORDER BY object_id' # order is important to group all features per object | 271 queryStatement += ' ORDER BY object_id' # order is important to group all features per object |
272 logging.debug(queryStatement) | |
273 cursor.execute(queryStatement) | 273 cursor.execute(queryStatement) |
274 logging.debug(queryStatement) | |
275 | 274 |
276 featureNumbers = {} | 275 featureNumbers = {} |
277 for row in cursor: | 276 for row in cursor: |
278 objId = row[1] | 277 objId = row[1] |
279 if objId not in featureNumbers: | 278 if objId not in featureNumbers: |
301 printDBError(error) | 300 printDBError(error) |
302 objects = [] | 301 objects = [] |
303 | 302 |
304 connection.close() | 303 connection.close() |
305 return objects | 304 return objects |
305 | |
306 def loadObjectFeatureFrameNumbers(filename, objectNumbers = None): | |
307 'Loads the feature frame numbers for each object' | |
308 connection = sqlite3.connect(filename) | |
309 cursor = connection.cursor() | |
310 try: | |
311 queryStatement = 'SELECT OF.object_id, TL.trajectory_id, TL.length FROM (SELECT trajectory_id, max(frame_number)-min(frame_number) AS length FROM positions GROUP BY trajectory_id) TL, objects_features OF WHERE TL.trajectory_id = OF.trajectory_id' | |
312 if objectNumbers is not None: | |
313 queryStatement += ' AND object_id '+getObjectCriteria(objectNumbers) | |
314 queryStatement += ' ORDER BY OF.object_id, TL.length DESC' | |
315 logging.debug(queryStatement) | |
316 cursor.execute(queryStatement) | |
317 objectFeatureNumbers = {} | |
318 for row in cursor: | |
319 objId = row[0] | |
320 if objId in objectFeatureNumbers: | |
321 objectFeatureNumbers[objId].append(row[1]) | |
322 else: | |
323 objectFeatureNumbers[objId] = [row[1]] | |
324 return objectFeatureNumbers | |
325 except sqlite3.OperationalError as error: | |
326 printDBError(error) | |
327 return None | |
306 | 328 |
307 def addCurvilinearTrajectoriesFromSqlite(filename, objects): | 329 def addCurvilinearTrajectoriesFromSqlite(filename, objects): |
308 '''Adds curvilinear positions (s_coordinate, y_coordinate, lane) | 330 '''Adds curvilinear positions (s_coordinate, y_coordinate, lane) |
309 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)''' |
310 connection = sqlite3.connect(filename) | 332 connection = sqlite3.connect(filename) |