annotate python/processing.py @ 246:583a2c4622f9

created new module for algorithms with function to extract speeds
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 17 Jul 2012 16:28:24 -0400
parents
children 15e244d2a1b5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
246
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
1 #! /usr/bin/env python
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
2 '''Algorithms to process trajectories and moving objects'''
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
3
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
4 __metaclass__ = type
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
5
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
6 import numpy as np
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
7
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
8 import moving
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
9
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
10 def extractSpeeds(objects, zone):
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
11 speeds = {}
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
12 objectsNotInZone = []
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
13 import matplotlib.nxutils as nx
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
14 for o in objects:
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
15 inPolygon = nx.points_inside_poly(o.getPositions().asArray().T, zone.T)
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
16 if inPolygon.any():
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
17 objspeeds = [o.getVelocityAt(i).norm2() for i in xrange(int(o.length()-1)) if inPolygon[i]]
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
18 speeds[o.num] = np.mean(objspeeds) # km/h
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
19 else:
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
20 objectsNotInZone.append(o)
583a2c4622f9 created new module for algorithms with function to extract speeds
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
21 return speeds.values(), speeds, objectsNotInZone