Mercurial Hosting > traffic-intelligence
comparison python/moving.py @ 1011:4f0312bee393
moving.py
author | Wendlasida |
---|---|
date | Fri, 01 Jun 2018 10:43:23 -0400 |
parents | c90c4682c67e |
children | 01db14e947e4 |
comparison
equal
deleted
inserted
replaced
1010:c90c4682c67e | 1011:4f0312bee393 |
---|---|
8 from numpy import median, mean, array, arange, zeros, ones, hypot, NaN, std, floor, float32, argwhere, minimum | 8 from numpy import median, mean, array, arange, zeros, ones, hypot, NaN, std, floor, float32, argwhere, minimum |
9 from matplotlib.pyplot import plot, text | 9 from matplotlib.pyplot import plot, text |
10 from scipy.stats import scoreatpercentile | 10 from scipy.stats import scoreatpercentile |
11 from scipy.spatial.distance import cdist | 11 from scipy.spatial.distance import cdist |
12 from scipy.signal import savgol_filter | 12 from scipy.signal import savgol_filter |
13 import copy | |
13 | 14 |
14 try: | 15 try: |
15 from shapely.geometry import Polygon, Point as shapelyPoint | 16 from shapely.geometry import Polygon, Point as shapelyPoint |
16 from shapely.prepared import prep, PreparedGeometry | 17 from shapely.prepared import prep, PreparedGeometry |
17 shapelyAvailable = True | 18 shapelyAvailable = True |
1139 | 1140 |
1140 @staticmethod | 1141 @staticmethod |
1141 def generate(num, p, v, timeInterval): | 1142 def generate(num, p, v, timeInterval): |
1142 positions, velocities = Trajectory.generate(p, v, int(timeInterval.length())) | 1143 positions, velocities = Trajectory.generate(p, v, int(timeInterval.length())) |
1143 return MovingObject(num = num, timeInterval = timeInterval, positions = positions, velocities = velocities) | 1144 return MovingObject(num = num, timeInterval = timeInterval, positions = positions, velocities = velocities) |
1145 | |
1146 | |
1147 | |
1148 | |
1149 @staticmethod | |
1150 def generateWithFeatures(num, features, userType): | |
1151 newObjectTMP = MovingObject.concatenateWith(features[0],features[1:],num) | |
1152 newObject = MovingObject(newObjectTMP.getNum() if num is None else num, newObjectTMP.getTimeInterval(), newObjectTMP.positions, newObjectTMP.velocities, userType = userType) | |
1153 newObject.features = [copy.deepcopy(f) for f in features] | |
1154 return newObject | |
1144 | 1155 |
1145 @staticmethod | 1156 @staticmethod |
1146 def concatenateWith(obj1, ObjectList=[], num = None): #Concatenate with a list of MovingObjects | 1157 def concatenateWith(obj1, ObjectList=[], num = None): #Concatenate with a list of MovingObjects |
1147 if len(ObjectList)==0: | 1158 if len(ObjectList)==0: |
1148 newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) | 1159 newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) |
1155 newObject = MovingObject.concatenate(obj1, Order, num) | 1166 newObject = MovingObject.concatenate(obj1, Order, num) |
1156 ObjectList.remove(Order) | 1167 ObjectList.remove(Order) |
1157 newObject = MovingObject.concatenateWith(newObject,ObjectList) | 1168 newObject = MovingObject.concatenateWith(newObject,ObjectList) |
1158 return newObject | 1169 return newObject |
1159 | 1170 |
1160 @staticmethod | 1171 '''@staticmethod |
1161 def addFeatures(obj1, FeatureList=[], num = None): #Add features to an object | 1172 def addFeatures(obj1, FeatureList=[], num = None): #Add features to an object |
1162 if len(FeatureList)==0: #We return a clone | 1173 if len(FeatureList)==0: #We return a clone |
1163 newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) | 1174 newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) |
1164 if obj1.features is not None: | 1175 if obj1.features is not None: |
1165 newObject.features = list(obj1.features) | 1176 newObject.features = list(obj1.features) |
1167 newObjectTMP = MovingObject.concatenateWith(obj1,FeatureList,num) | 1178 newObjectTMP = MovingObject.concatenateWith(obj1,FeatureList,num) |
1168 newObject = MovingObject(newObjectTMP.getNum() if num is None else num, newObjectTMP.getTimeInterval(), newObjectTMP.positions, newObjectTMP.velocities, userType = newObjectTMP.getUserType()) | 1179 newObject = MovingObject(newObjectTMP.getNum() if num is None else num, newObjectTMP.getTimeInterval(), newObjectTMP.positions, newObjectTMP.velocities, userType = newObjectTMP.getUserType()) |
1169 if obj1.hasFeatures() and newObjectTMP.hasFeatures(): | 1180 if obj1.hasFeatures() and newObjectTMP.hasFeatures(): |
1170 newObject.features = obj1.features + newObjectTMP.features | 1181 newObject.features = obj1.features + newObjectTMP.features |
1171 | 1182 |
1172 return newObject | 1183 return newObject''' |
1173 | 1184 |
1174 | 1185 |
1175 @staticmethod | 1186 @staticmethod |
1176 def delFeatures(obj1, FeatureList=[], num = None): | 1187 def delFeatures(obj1, FeatureList=[], num = None): |
1177 if obj1.features is not None: | 1188 if obj1.features is not None: |
1178 if len(FeatureList)>0: | 1189 if len(FeatureList)>0: |
1179 tmp = [ i for i in obj1.features and i not in FeatureList] | 1190 tmp = [ i for i in (obj1.features if obj1.features is not None else []) if i not in FeatureList] |
1180 if len(obj1.features)!=len(tmp): | 1191 if len(tmp)==0: |
1192 newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) | |
1193 newObject.features = [MovingObject(obj1.getNum() if num is None else num, newInterval, obj1.positions, obj1.velocities, userType = obj1.getUserType())] | |
1194 elif len(obj1.features if obj1.features is not None else [])!=len(tmp): | |
1181 newInterval = TimeInterval(min([i.getFirstInstant() for i in tmp]), max([i.getLastInstant() for i in tmp])) | 1195 newInterval = TimeInterval(min([i.getFirstInstant() for i in tmp]), max([i.getLastInstant() for i in tmp])) |
1182 positions = Trajectory() | 1196 positions = Trajectory() |
1183 for t in newInterval: | 1197 for t in newInterval: |
1184 nTotal = 0. | 1198 nTotal = 0. |
1185 p = Point(0.,0.) | 1199 p = Point(0.,0.) |
1195 positions.addPosition(obj1.getPositionAtInstant(t)) | 1209 positions.addPosition(obj1.getPositionAtInstant(t)) |
1196 else: | 1210 else: |
1197 positions.addPosition(p.divide(nTotal)) | 1211 positions.addPosition(p.divide(nTotal)) |
1198 if hasattr(obj1, 'velocities'): | 1212 if hasattr(obj1, 'velocities'): |
1199 velocities = Trajectory() | 1213 velocities = Trajectory() |
1200 for t in newTimeInterval: | 1214 for t in newInterval: |
1201 nTotal = 0. | 1215 nTotal = 0. |
1202 p = Point(0.,0.) | 1216 p = Point(0.,0.) |
1203 for obj in tmp: | 1217 for obj in tmp: |
1204 if obj.existsAtInstant(t): | 1218 if obj.existsAtInstant(t): |
1205 if obj.hasFeatures(): | 1219 if obj.hasFeatures(): |
1215 else: | 1229 else: |
1216 velocities = None | 1230 velocities = None |
1217 | 1231 |
1218 newObject = MovingObject(obj1.getNum() if num is None else num, newInterval, positions, velocities, userType = obj1.getUserType()) | 1232 newObject = MovingObject(obj1.getNum() if num is None else num, newInterval, positions, velocities, userType = obj1.getUserType()) |
1219 newObject.features = tmp+[MovingObject(obj1.getNum() if num is None else num, newInterval, positions, velocities, userType = obj1.getUserType())] | 1233 newObject.features = tmp+[MovingObject(obj1.getNum() if num is None else num, newInterval, positions, velocities, userType = obj1.getUserType())] |
1234 else: | |
1235 newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) | |
1236 if obj1.features is not None: | |
1237 newObject.features = list(obj1.features) | |
1220 else: | 1238 else: |
1221 newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) | 1239 newObject = MovingObject(obj1.getNum() if num is None else num, obj1.getTimeInterval(), obj1.positions, obj1.velocities, userType = obj1.getUserType()) |
1222 return newObject | 1240 return newObject |
1223 | 1241 |
1224 | 1242 |
1225 def updatePosition(self): | 1243 def updatePosition(self): |
1226 if self.features is not None: | 1244 if self.features is not None: |
1227 print 'On update ici' | |
1228 positions = Trajectory() | 1245 positions = Trajectory() |
1229 for t in self.getTimeInterval(): | 1246 for t in self.getTimeInterval(): |
1230 nTotal = 0. | 1247 nTotal = 0. |
1231 p = Point(0.,0.) | 1248 p = Point(0.,0.) |
1232 for obj in self.features: | 1249 for obj in self.features: |