Mercurial Hosting > traffic-intelligence
comparison trafficintelligence/storage.py @ 1044:75a6ad604cc5
work on motion patterns
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Thu, 05 Jul 2018 17:06:40 -0400 |
parents | fc7c0f38e8a6 |
children | c9c03c97ed9f |
comparison
equal
deleted
inserted
replaced
1043:b735895c8815 | 1044:75a6ad604cc5 |
---|---|
245 else: | 245 else: |
246 for row in cursor: | 246 for row in cursor: |
247 attributes[row[0]] = row[1] | 247 attributes[row[0]] = row[1] |
248 return attributes | 248 return attributes |
249 | 249 |
250 def loadTrajectoriesFromSqlite(filename, trajectoryType, objectNumbers = None, withFeatures = False, timeStep = None, maxNObjectFeatures = 1): | 250 def loadTrajectoriesFromSqlite(filename, trajectoryType, objectNumbers = None, withFeatures = False, timeStep = None, nLongestFeaturesPerObject = None): |
251 '''Loads the trajectories (in the general sense, | 251 '''Loads the trajectories (in the general sense, |
252 either features, objects (feature groups), longest features per object, or bounding box series) | 252 either features, objects (feature groups), longest features per object, or bounding box series) |
253 types are only feature or object | |
254 if object, features can be loaded with withFeatures or nLongestObjectFeatures used to select the n longest features | |
253 | 255 |
254 The number loaded is either the first objectNumbers objects, | 256 The number loaded is either the first objectNumbers objects, |
255 or the indices in objectNumbers from the database''' | 257 or the indices in objectNumbers from the database''' |
256 objects = [] | 258 objects = [] |
257 with sqlite3.connect(filename) as connection: | 259 with sqlite3.connect(filename) as connection: |
258 if trajectoryType == 'objectfeature': | 260 objects = loadTrajectoriesFromTable(connection, 'positions', trajectoryType, objectNumbers, timeStep) |
259 objectFeatureNumbers = loadObjectFeatureFrameNumbers(filename, objectNumbers) | 261 objectVelocities = loadTrajectoriesFromTable(connection, 'velocities', trajectoryType, objectNumbers, timeStep) |
260 featureNumbers = [] | |
261 for numbers in objectFeatureNumbers.values(): | |
262 featureNumbers += numbers[:min(len(numbers), maxNObjectFeatures)] | |
263 objects = loadTrajectoriesFromTable(connection, 'positions', 'feature', featureNumbers, timeStep) | |
264 objectVelocities = loadTrajectoriesFromTable(connection, 'velocities', 'feature', featureNumbers, timeStep) | |
265 else: | |
266 objects = loadTrajectoriesFromTable(connection, 'positions', trajectoryType, objectNumbers, timeStep) | |
267 objectVelocities = loadTrajectoriesFromTable(connection, 'velocities', trajectoryType, objectNumbers, timeStep) | |
268 | 262 |
269 if len(objectVelocities) > 0: | 263 if len(objectVelocities) > 0: |
270 for o,v in zip(objects, objectVelocities): | 264 for o,v in zip(objects, objectVelocities): |
271 if o.getNum() == v.getNum(): | 265 if o.getNum() == v.getNum(): |
272 o.velocities = v.positions | 266 o.velocities = v.positions |
281 queryStatement = 'SELECT trajectory_id, object_id FROM objects_features' | 275 queryStatement = 'SELECT trajectory_id, object_id FROM objects_features' |
282 if objectNumbers is not None: | 276 if objectNumbers is not None: |
283 queryStatement += ' WHERE object_id '+getObjectCriteria(objectNumbers) | 277 queryStatement += ' WHERE object_id '+getObjectCriteria(objectNumbers) |
284 queryStatement += ' ORDER BY object_id' # order is important to group all features per object | 278 queryStatement += ' ORDER BY object_id' # order is important to group all features per object |
285 logging.debug(queryStatement) | 279 logging.debug(queryStatement) |
286 cursor.execute(queryStatement) | 280 cursor.execute(queryStatement) |
287 | 281 |
288 featureNumbers = {} | 282 featureNumbers = {} |
289 for row in cursor: | 283 for row in cursor: |
290 objId = row[1] | 284 objId = row[1] |
291 if objId not in featureNumbers: | 285 if objId not in featureNumbers: |
301 for obj in objects: | 295 for obj in objects: |
302 userType, nObjects = attributes[obj.getNum()] | 296 userType, nObjects = attributes[obj.getNum()] |
303 obj.setUserType(userType) | 297 obj.setUserType(userType) |
304 obj.setNObjects(nObjects) | 298 obj.setNObjects(nObjects) |
305 | 299 |
300 # add features | |
306 if withFeatures: | 301 if withFeatures: |
307 nFeatures = 0 | |
308 for obj in objects: | 302 for obj in objects: |
309 nFeatures = max(nFeatures, max(obj.featureNumbers)) | 303 obj.features = loadTrajectoriesFromSqlite(filename, 'feature', obj.featureNumbers, timeStep = timeStep) |
310 features = loadTrajectoriesFromSqlite(filename, 'feature', nFeatures+1, timeStep = timeStep) | 304 elif nLongestFeaturesPerObject is not None: |
311 for obj in objects: | 305 for obj in objects: |
312 obj.setFeatures(features) | 306 queryStatement = 'SELECT trajectory_id, max(frame_number)-min(frame_number) AS length FROM positions WHERE trajectory_id '+getObjectCriteria(obj.featureNumbers)+' GROUP BY trajectory_id ORDER BY length DESC' |
307 logging.debug(queryStatement) | |
308 cursor.execute(queryStatement) | |
309 obj.features = loadTrajectoriesFromSqlite(filename, 'feature', [row[0] for i,row in enumerate(cursor) if i<nLongestFeaturesPerObject], timeStep = timeStep) | |
313 | 310 |
314 except sqlite3.OperationalError as error: | 311 except sqlite3.OperationalError as error: |
315 printDBError(error) | 312 printDBError(error) |
316 return objects | 313 return objects |
317 | 314 |
335 objectFeatureNumbers[objId] = [row[1]] | 332 objectFeatureNumbers[objId] = [row[1]] |
336 return objectFeatureNumbers | 333 return objectFeatureNumbers |
337 except sqlite3.OperationalError as error: | 334 except sqlite3.OperationalError as error: |
338 printDBError(error) | 335 printDBError(error) |
339 return None | 336 return None |
340 | |
341 def loadObjectTrajectoriesFromSqlite(): | |
342 '''Loads object trajectories | |
343 either simply objects or features (defaults to loadTrajectoriesFromSqlite) | |
344 or the longest features for each object ''' | |
345 | |
346 | 337 |
347 def addCurvilinearTrajectoriesFromSqlite(filename, objects): | 338 def addCurvilinearTrajectoriesFromSqlite(filename, objects): |
348 '''Adds curvilinear positions (s_coordinate, y_coordinate, lane) | 339 '''Adds curvilinear positions (s_coordinate, y_coordinate, lane) |
349 from a database to an existing MovingObject dict (indexed by each objects's num)''' | 340 from a database to an existing MovingObject dict (indexed by each objects's num)''' |
350 with sqlite3.connect(filename) as connection: | 341 with sqlite3.connect(filename) as connection: |