Mercurial > hg > nsaunier > traffic-intelligence
comparison python/moving.py @ 1013:d6f121ded971
Moving pull request
| author | Wendlasida |
|---|---|
| date | Fri, 01 Jun 2018 17:29:01 -0400 |
| parents | 01db14e947e4 |
| children | 026f6b3b122c |
comparison
equal
deleted
inserted
replaced
| 1012:01db14e947e4 | 1013:d6f121ded971 |
|---|---|
| 1152 | 1152 |
| 1153 @staticmethod | 1153 @staticmethod |
| 1154 def generate(num, p, v, timeInterval): | 1154 def generate(num, p, v, timeInterval): |
| 1155 positions, velocities = Trajectory.generate(p, v, int(timeInterval.length())) | 1155 positions, velocities = Trajectory.generate(p, v, int(timeInterval.length())) |
| 1156 return MovingObject(num = num, timeInterval = timeInterval, positions = positions, velocities = velocities) | 1156 return MovingObject(num = num, timeInterval = timeInterval, positions = positions, velocities = velocities) |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 | 1157 |
| 1161 @staticmethod | 1158 @staticmethod |
| 1162 def generateWithFeatures(num, features, userType): | 1159 def generateWithFeatures(num, features, userType): |
| 1163 newObjectTMP = MovingObject.concatenateWith(features[0],features[1:],num) | 1160 newObjectTMP = MovingObject.concatenateWith(features[0],features[1:],num) |
| 1164 newObject = MovingObject(newObjectTMP.getNum() if num is None else num, newObjectTMP.getTimeInterval(), newObjectTMP.positions, newObjectTMP.velocities, userType = userType) | 1161 newObject = MovingObject(newObjectTMP.getNum() if num is None else num, newObjectTMP.getTimeInterval(), newObjectTMP.positions, newObjectTMP.velocities, userType = userType) |
| 1165 newObject.features = [copy.deepcopy(f) for f in features] | 1162 newObject.features = [copy.deepcopy(f) for f in features] |
| 1166 return newObject | 1163 return newObject |
| 1167 | 1164 |
| 1168 @staticmethod | 1165 @staticmethod |
| 1169 def concatenateWith(obj1, ObjectList=[], num = None): #Concatenate with a list of MovingObjects | 1166 def concatenateWith(obj1, ObjectList=[], num = None): #Concatenate with a list of MovingObjects |
| 1170 if len(ObjectList)==0: | 1167 if len(ObjectList)==0: |
| 1171 newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) | 1168 newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) |
| 1178 newObject = MovingObject.concatenate(obj1, Order, num) | 1175 newObject = MovingObject.concatenate(obj1, Order, num) |
| 1179 ObjectList.remove(Order) | 1176 ObjectList.remove(Order) |
| 1180 newObject = MovingObject.concatenateWith(newObject,ObjectList) | 1177 newObject = MovingObject.concatenateWith(newObject,ObjectList) |
| 1181 return newObject | 1178 return newObject |
| 1182 | 1179 |
| 1183 '''@staticmethod | |
| 1184 def addFeatures(obj1, FeatureList=[], num = None): #Add features to an object | |
| 1185 if len(FeatureList)==0: #We return a clone | |
| 1186 newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) | |
| 1187 if obj1.features is not None: | |
| 1188 newObject.features = list(obj1.features) | |
| 1189 else : | |
| 1190 newObjectTMP = MovingObject.concatenateWith(obj1,FeatureList,num) | |
| 1191 newObject = MovingObject(newObjectTMP.getNum() if num is None else num, newObjectTMP.getTimeInterval(), newObjectTMP.positions, newObjectTMP.velocities, userType = newObjectTMP.getUserType()) | |
| 1192 if obj1.hasFeatures() and newObjectTMP.hasFeatures(): | |
| 1193 newObject.features = obj1.features + newObjectTMP.features | |
| 1194 | |
| 1195 return newObject''' | |
| 1196 | |
| 1197 | |
| 1198 @staticmethod | |
| 1199 def delFeatures(obj1, FeatureList=[], num = None): | |
| 1200 if obj1.features is not None: | |
| 1201 if len(FeatureList)>0: | |
| 1202 tmp = [ i for i in (obj1.features if obj1.features is not None else []) if i not in FeatureList] | |
| 1203 if len(tmp)==0: | |
| 1204 newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) | |
| 1205 newObject.features = [MovingObject(obj1.getNum() if num is None else num, newInterval, obj1.positions, obj1.velocities, userType = obj1.getUserType())] | |
| 1206 elif len(obj1.features if obj1.features is not None else [])!=len(tmp): | |
| 1207 newInterval = TimeInterval(min([i.getFirstInstant() for i in tmp]), max([i.getLastInstant() for i in tmp])) | |
| 1208 positions = Trajectory() | |
| 1209 for t in newInterval: | |
| 1210 nTotal = 0. | |
| 1211 p = Point(0.,0.) | |
| 1212 for obj in tmp: | |
| 1213 if obj.existsAtInstant(t): | |
| 1214 if obj.hasFeatures(): | |
| 1215 n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)]) | |
| 1216 else: | |
| 1217 n = 1. | |
| 1218 p += obj.getPositionAtInstant(t).__mul__(n) | |
| 1219 nTotal += n | |
| 1220 if nTotal==0: | |
| 1221 positions.addPosition(obj1.getPositionAtInstant(t)) | |
| 1222 else: | |
| 1223 positions.addPosition(p.divide(nTotal)) | |
| 1224 if hasattr(obj1, 'velocities'): | |
| 1225 velocities = Trajectory() | |
| 1226 for t in newInterval: | |
| 1227 nTotal = 0. | |
| 1228 p = Point(0.,0.) | |
| 1229 for obj in tmp: | |
| 1230 if obj.existsAtInstant(t): | |
| 1231 if obj.hasFeatures(): | |
| 1232 n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)]) | |
| 1233 else: | |
| 1234 n = 1. | |
| 1235 p += obj.getVelocityAtInstant(t).__mul__(n) | |
| 1236 nTotal += n | |
| 1237 if nTotal==0: | |
| 1238 velocities.addPosition(obj1.getVelocityAtInstant(t)) | |
| 1239 else: | |
| 1240 velocities.addPosition(p.divide(nTotal)) | |
| 1241 else: | |
| 1242 velocities = None | |
| 1243 | |
| 1244 newObject = MovingObject(obj1.getNum() if num is None else num, newInterval, positions, velocities, userType = obj1.getUserType()) | |
| 1245 newObject.features = tmp+[MovingObject(obj1.getNum() if num is None else num, newInterval, positions, velocities, userType = obj1.getUserType())] | |
| 1246 else: | |
| 1247 newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) | |
| 1248 if obj1.features is not None: | |
| 1249 newObject.features = list(obj1.features) | |
| 1250 else: | |
| 1251 newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) | |
| 1252 return newObject | |
| 1253 | |
| 1254 | |
| 1255 def updatePosition(self): | 1180 def updatePosition(self): |
| 1256 if self.features is not None: | 1181 if self.features is not None: |
| 1257 positions = Trajectory() | 1182 positions = Trajectory() |
| 1258 for t in self.getTimeInterval(): | 1183 for t in self.getTimeInterval(): |
| 1259 nTotal = 0. | 1184 nTotal = 0. |
| 1260 p = Point(0.,0.) | 1185 p = Point(0.,0.) |
| 1261 for obj in self.features: | 1186 for obj in self.features: |
| 1262 if obj.existsAtInstant(t): | 1187 if obj.existsAtInstant(t): |
| 1263 if obj.hasFeatures(): | 1188 if obj.hasFeatures(): |
| 1264 n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)]) | 1189 n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)]) |
| 1265 else: | 1190 else: |
| 1266 n = 1. | 1191 n = 1. |
| 1267 p += obj.getPositionAtInstant(t).__mul__(n) | 1192 p += obj.getPositionAtInstant(t).__mul__(n) |
| 1268 nTotal += n | 1193 nTotal += n |
| 1269 assert nTotal>0, 'there should be at least one point for each instant' | 1194 assert nTotal>0, 'there should be at least one point for each instant' |
| 1270 positions.addPosition(p.divide(nTotal)) | 1195 positions.addPosition(p.divide(nTotal)) |
| 1271 self.positions = positions | 1196 self.positions = positions |
| 1272 | 1197 |
| 1273 @staticmethod | 1198 @staticmethod |
| 1274 def concatenate(obj1, obj2, num = None): | 1199 def concatenate(obj1, obj2, num = None): |
| 1275 '''Concatenates two objects supposed to overlap temporally ''' | 1200 '''Concatenates two objects supposed to overlap temporally ''' |
| 1276 if num is None: | 1201 if num is None: |
| 1295 for t in emptyInterval: | 1220 for t in emptyInterval: |
| 1296 positions.addPositionXY(px,py) | 1221 positions.addPositionXY(px,py) |
| 1297 velocities.addPositionXY(vitessex,vitessey) | 1222 velocities.addPositionXY(vitessex,vitessey) |
| 1298 px+=vitessex | 1223 px+=vitessex |
| 1299 py+=vitessey | 1224 py+=vitessey |
| 1300 | 1225 newObject = MovingObject(newNum, emptyInterval, positions, velocities, userType = obj1.getUserType()) |
| 1301 newObject = MovingObject(newNum, emptyInterval, positions, velocities, userType = obj1.getUserType()) | 1226 newObject.features = [MovingObject(newNum, emptyInterval, positions, velocities, userType = obj1.getUserType())] #In case there is features to add when we recursively call concatenate |
| 1302 newObject.features = [MovingObject(newNum, emptyInterval, positions, velocities, userType = obj1.getUserType())] #In case there is features to add when we recursively call concatenate | |
| 1303 return MovingObject.concatenate(MovingObject.concatenate(obj1, newObject),obj2) | 1227 return MovingObject.concatenate(MovingObject.concatenate(obj1, newObject),obj2) |
| 1304 | 1228 |
| 1305 else: | 1229 else: |
| 1306 newTimeInterval = TimeInterval.union(obj1.getTimeInterval(), obj2.getTimeInterval()) | 1230 newTimeInterval = TimeInterval.union(obj1.getTimeInterval(), obj2.getTimeInterval()) |
| 1307 # positions | 1231 # positions |
| 1308 positions = Trajectory() | 1232 positions = Trajectory() |
| 1309 for t in newTimeInterval: | 1233 for t in newTimeInterval: |
| 1310 nTotal = 0. | 1234 nTotal = 0. |
| 1311 p = Point(0.,0.) | 1235 p = Point(0.,0.) |
| 1312 for obj in [obj1, obj2]: | 1236 for obj in [obj1, obj2]: |
| 1313 if obj.existsAtInstant(t): | 1237 if obj.existsAtInstant(t): |
| 1314 if obj.hasFeatures(): | 1238 if obj.hasFeatures(): |
| 1315 n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)]) | 1239 n = len([f for f in obj.getFeatures() if f.existsAtInstant(t)]) |
| 1316 else: | 1240 else: |
| 1317 n = 1. | 1241 n = 1. |
| 1318 p += obj.getPositionAtInstant(t).__mul__(n) | 1242 p += obj.getPositionAtInstant(t).__mul__(n) |
| 1319 nTotal += n | 1243 nTotal += n |
| 1320 assert nTotal>0, 'there should be at least one point for each instant' | 1244 assert nTotal>0, 'there should be at least one point for each instant' |
| 1321 positions.addPosition(p.divide(nTotal)) | 1245 positions.addPosition(p.divide(nTotal)) |
| 1322 # velocities: if any | 1246 # velocities: if any |
| 1323 if hasattr(obj1, 'velocities') and hasattr(obj2, 'velocities'): | 1247 if hasattr(obj1, 'velocities') and hasattr(obj2, 'velocities'): |
| 1324 velocities = Trajectory() | 1248 velocities = Trajectory() |
| 1325 for t in newTimeInterval: | 1249 for t in newTimeInterval: |
| 1326 nTotal = 0. | 1250 nTotal = 0. |
| 2053 for obj in objects: | 1977 for obj in objects: |
| 2054 obj.plot(colors.get(obj.userType)) | 1978 obj.plot(colors.get(obj.userType)) |
| 2055 axis('equal') | 1979 axis('equal') |
| 2056 | 1980 |
| 2057 | 1981 |
| 2058 if __name__ == "__main__": | 1982 '''if __name__ == "__main__": |
| 2059 import doctest | 1983 import doctest |
| 2060 import unittest | 1984 import unittest |
| 2061 suite = doctest.DocFileSuite('tests/moving.txt') | 1985 suite = doctest.DocFileSuite('tests/moving.txt') |
| 2062 #suite = doctest.DocTestSuite() | 1986 #suite = doctest.DocTestSuite() |
| 2063 unittest.TextTestRunner().run(suite) | 1987 unittest.TextTestRunner().run(suite) |
| 2064 #doctest.testmod() | 1988 #doctest.testmod() |
| 2065 #doctest.testfile("example.txt") | 1989 #doctest.testfile("example.txt") |
| 2066 if shapelyAvailable: | 1990 if shapelyAvailable: |
| 2067 suite = doctest.DocFileSuite('tests/moving_shapely.txt') | 1991 suite = doctest.DocFileSuite('tests/moving_shapely.txt') |
| 2068 unittest.TextTestRunner().run(suite) | 1992 unittest.TextTestRunner().run(suite) |
| 1993 for t in newTimeInterval:''' |
