comparison trafficintelligence/moving.py @ 1041:fc7c0f38e8a6

added nObjects to MovingObject, with loading/saving
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 04 Jul 2018 16:06:23 -0400
parents 6a6c37eb3a74
children 75a6ad604cc5
comparison
equal deleted inserted replaced
1040:20799ac9524e 1041:fc7c0f38e8a6
1139 '''Class for moving objects: a spatio-temporal object 1139 '''Class for moving objects: a spatio-temporal object
1140 with a trajectory and a geometry (constant volume over time) 1140 with a trajectory and a geometry (constant volume over time)
1141 and a usertype (e.g. road user) coded as a number (see userTypeNames) 1141 and a usertype (e.g. road user) coded as a number (see userTypeNames)
1142 ''' 1142 '''
1143 1143
1144 def __init__(self, num = None, timeInterval = None, positions = None, velocities = None, geometry = None, userType = userType2Num['unknown']): 1144 def __init__(self, num = None, timeInterval = None, positions = None, velocities = None, geometry = None, userType = userType2Num['unknown'], nObjects = None):
1145 super(MovingObject, self).__init__(num, timeInterval) 1145 super(MovingObject, self).__init__(num, timeInterval)
1146 self.positions = positions 1146 self.positions = positions
1147 self.velocities = velocities 1147 self.velocities = velocities
1148 self.geometry = geometry 1148 self.geometry = geometry
1149 self.userType = userType 1149 self.userType = userType
1150 self.setNObjects(nObjects) # a feature has None for nObjects
1150 self.features = None 1151 self.features = None
1151 # compute bounding polygon from trajectory 1152 # compute bounding polygon from trajectory
1152 1153
1153 @staticmethod 1154 @staticmethod
1154 def aggregateTrajectories(features, interval = None, aggFunc = mean): 1155 def aggregateTrajectories(features, interval = None, aggFunc = mean):
1211 featureVelocities.addPosition(v) 1212 featureVelocities.addPosition(v)
1212 p=p+v 1213 p=p+v
1213 for t in secondObject.getTimeInterval(): 1214 for t in secondObject.getTimeInterval():
1214 positions.addPosition(secondObject.getPositionAtInstant(t)) 1215 positions.addPosition(secondObject.getPositionAtInstant(t))
1215 velocities.addPosition(secondObject.getVelocityAtInstant(t)) 1216 velocities.addPosition(secondObject.getVelocityAtInstant(t))
1216 newObject = MovingObject(newNum, TimeInterval(firstObject.getFirstInstant(), secondObject.getLastInstant()), positions, velocities) 1217 newObject = MovingObject(newNum, TimeInterval(firstObject.getFirstInstant(), secondObject.getLastInstant()), positions, velocities, nObjects = 1)
1217 if hasattr(obj1, 'featureNumbers') and hasattr(obj2, 'featureNumbers'): 1218 if hasattr(obj1, 'featureNumbers') and hasattr(obj2, 'featureNumbers'):
1218 if newFeatureNum is not None: 1219 if newFeatureNum is not None:
1219 newObject.featureNumbers = obj1.featureNumbers+obj2.featureNumbers+[newFeatureNum] 1220 newObject.featureNumbers = obj1.featureNumbers+obj2.featureNumbers+[newFeatureNum]
1220 else: 1221 else:
1221 print('Issue, new created feature has no num id') 1222 print('Issue, new created feature has no num id')
1222 if obj1.hasFeatures() and obj2.hasFeatures(): 1223 if obj1.hasFeatures() and obj2.hasFeatures():
1223 newObject.features = obj1.getFeatures()+obj2.getFeatures()+[MovingObject(newFeatureNum, TimeInterval(emptyInterval.first+1, emptyInterval.last-1), featurePositions, featureVelocities)] 1224 newObject.features = obj1.getFeatures()+obj2.getFeatures()+[MovingObject(newFeatureNum, TimeInterval(emptyInterval.first+1, emptyInterval.last-1), featurePositions, featureVelocities)]
1224 else: # time intervals overlap 1225 else: # time intervals overlap
1225 newTimeInterval = TimeInterval.union(obj1.getTimeInterval(), obj2.getTimeInterval()) 1226 newTimeInterval = TimeInterval.union(obj1.getTimeInterval(), obj2.getTimeInterval())
1226 newObject = MovingObject(newNum, newTimeInterval) 1227 newObject = MovingObject(newNum, newTimeInterval, nObjects = 1) # hypothesis is that it's the same object being reunited
1227 if hasattr(obj1, 'featureNumbers') and hasattr(obj2, 'featureNumbers'): 1228 if hasattr(obj1, 'featureNumbers') and hasattr(obj2, 'featureNumbers'):
1228 newObject.featureNumbers = obj1.featureNumbers+obj2.featureNumbers 1229 newObject.featureNumbers = obj1.featureNumbers+obj2.featureNumbers
1229 if obj1.hasFeatures() and obj2.hasFeatures(): 1230 if obj1.hasFeatures() and obj2.hasFeatures():
1230 newObject.features = obj1.getFeatures()+obj2.getFeatures() 1231 newObject.features = obj1.getFeatures()+obj2.getFeatures()
1231 newObject.updatePositions() 1232 newObject.updatePositions()
1241 '''Returns a new object extracted from self, 1242 '''Returns a new object extracted from self,
1242 restricted to time interval inter''' 1243 restricted to time interval inter'''
1243 intersection = TimeInterval.intersection(inter, self.getTimeInterval()) 1244 intersection = TimeInterval.intersection(inter, self.getTimeInterval())
1244 if not intersection.empty(): 1245 if not intersection.empty():
1245 trajectoryInterval = TimeInterval(intersection.first-self.getFirstInstant(), intersection.last-self.getFirstInstant()) 1246 trajectoryInterval = TimeInterval(intersection.first-self.getFirstInstant(), intersection.last-self.getFirstInstant())
1246 obj = MovingObject(self.num, intersection, self.positions.getTrajectoryInInterval(trajectoryInterval), self.geometry, self.userType) 1247 obj = MovingObject(self.num, intersection, self.positions.getTrajectoryInInterval(trajectoryInterval), self.geometry, self.userType, self.nObjects)
1247 if self.velocities is not None: 1248 if self.velocities is not None:
1248 obj.velocities = self.velocities.getTrajectoryInInterval(trajectoryInterval) 1249 obj.velocities = self.velocities.getTrajectoryInInterval(trajectoryInterval)
1249 return obj 1250 return obj
1250 else: 1251 else:
1251 print('The object does not exist at {}'.format(inter)) 1252 print('The object does not exist at {}'.format(inter))
1328 print('Object {} has no curvilinear positions'.format(self.getNum())) 1329 print('Object {} has no curvilinear positions'.format(self.getNum()))
1329 1330
1330 def setUserType(self, userType): 1331 def setUserType(self, userType):
1331 self.userType = userType 1332 self.userType = userType
1332 1333
1334 def getNObjects(self):
1335 return self.nObjects
1336
1337 def setNObjects(self, nObjects):
1338 if nObjects is None or nObjects >= 1:
1339 self.nObjects = nObjects
1340 else:
1341 print('Number of objects represented by object {} must be greater or equal to 1 ({})'.format(self.getNum(), nObjects))
1342
1333 def setFeatures(self, features, featuresOrdered = False): 1343 def setFeatures(self, features, featuresOrdered = False):
1334 '''Sets the features in the features field based on featureNumbers 1344 '''Sets the features in the features field based on featureNumbers
1335 if not all features are loaded from 0, one needs to renumber in a dict''' 1345 if not all features are loaded from 0, one needs to renumber in a dict'''
1336 if featuresOrdered: 1346 if featuresOrdered:
1337 tmp = features 1347 tmp = features