comparison python/moving.py @ 113:606010d1d9a4

corrected errors in trajectories (if empty) and getTrajectoryInPolygon
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 18 Jul 2011 14:29:07 -0400
parents 6efe470ea5e5
children 680d4c82886d
comparison
equal deleted inserted replaced
112:67555e968b5e 113:606010d1d9a4
204 i.e. a temporal sequence of positions 204 i.e. a temporal sequence of positions
205 205
206 the class is iterable.''' 206 the class is iterable.'''
207 207
208 def __init__(self, positions=None): 208 def __init__(self, positions=None):
209 self.positions = positions 209 if positions:
210 self.positions = positions
211 else:
212 self.positions = [[],[]]
210 213
211 @staticmethod 214 @staticmethod
212 def load(line1, line2): 215 def load(line1, line2):
213 return Trajectory([[float(n) for n in line1.split(' ')], 216 return Trajectory([[float(n) for n in line1.split(' ')],
214 [float(n) for n in line2.split(' ')]]) 217 [float(n) for n in line2.split(' ')]])
232 else: 235 else:
233 self.iterInstantNum += 1 236 self.iterInstantNum += 1
234 return self[self.iterInstantNum-1] 237 return self[self.iterInstantNum-1]
235 238
236 def addPositionXY(self, x, y): 239 def addPositionXY(self, x, y):
237 if not self.positions: 240 self.positions[0].append(x)
238 self.positions = [[x],[y]] 241 self.positions[1].append(y)
239 else:
240 self.positions[0].append(x)
241 self.positions[1].append(y)
242 242
243 def addPosition(self, p): 243 def addPosition(self, p):
244 self.addPositionXY(p.x, p.y) 244 self.addPositionXY(p.x, p.y)
245 245
246 def draw(self, options = ''): 246 def draw(self, options = ''):
332 332
333 def getTrajectoryInPolygon(self, polygon): 333 def getTrajectoryInPolygon(self, polygon):
334 '''Returns the set of points inside the polygon 334 '''Returns the set of points inside the polygon
335 (array of Nx2 coordinates of the polygon vertices)''' 335 (array of Nx2 coordinates of the polygon vertices)'''
336 import matplotlib.nxutils as nx 336 import matplotlib.nxutils as nx
337 t = Trajectory() 337 traj = Trajectory()
338 result = nx.points_inside_poly(self.asArray().T, polygon) 338 result = nx.points_inside_poly(self.asArray().T, polygon)
339 for i in xrange(self.length()): 339 for i in xrange(self.length()):
340 if result[i]: 340 if result[i]:
341 t.addPositionXY(self.positions[0][i], self.positions[1][i]) 341 traj.addPositionXY(self.positions[0][i], self.positions[1][i])
342 if t.length()>0: 342 return traj
343 return t
344 else:
345 return None
346 343
347 # version 2: use shapely polygon contains 344 # version 2: use shapely polygon contains
348 345
349 ################## 346 ##################
350 # Moving Objects 347 # Moving Objects