comparison python/moving.py @ 762:d6f0e0cab07d dev

added functionalities for Trajectory
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Sat, 28 Nov 2015 16:54:03 -0500
parents 15ddc8715236
children f8e0a8ea8402
comparison
equal deleted inserted replaced
761:15ddc8715236 762:d6f0e0cab07d
818 if i < self.length(): 818 if i < self.length():
819 return self.cumulativeDistances[i] 819 return self.cumulativeDistances[i]
820 else: 820 else:
821 print('Index {} beyond trajectory length {}'.format(i, self.length())) 821 print('Index {} beyond trajectory length {}'.format(i, self.length()))
822 822
823 def getMaxDistance(self, metric):
824 'Returns the maximum distance between points in the trajectory'
825 positions = self.getPositions().asArray().T
826 return cdist(positions, positions, metric = metric).max()
827
823 def similarOrientation(self, refDirection, cosineThreshold, minProportion = 0.5): 828 def similarOrientation(self, refDirection, cosineThreshold, minProportion = 0.5):
824 '''Indicates whether the minProportion (<=1.) (eg half) of the trajectory elements (vectors for velocity) 829 '''Indicates whether the minProportion (<=1.) (eg half) of the trajectory elements (vectors for velocity)
825 have a cosine with refDirection is smaller than cosineThreshold''' 830 have a cosine with refDirection is smaller than cosineThreshold'''
826 count = 0 831 count = 0
827 lengthThreshold = float(self.length())*minProportion 832 lengthThreshold = float(self.length())*minProportion
829 if p.similarOrientation(refDirection, cosineThreshold): 834 if p.similarOrientation(refDirection, cosineThreshold):
830 count += 1 835 count += 1
831 return count >= lengthThreshold 836 return count >= lengthThreshold
832 837
833 def wiggliness(self): 838 def wiggliness(self):
834 return self.getCumulativeDistance(self.length()-1)/float(Point.distanceNorm2(self.__getitem__(0),self.__getitem__(self.length()-1))) 839 straightDistance = Point.distanceNorm2(self.__getitem__(0),self.__getitem__(self.length()-1))
840 if straightDistance > 0:
841 return self.getCumulativeDistance(self.length()-1)/float(straightDistance)
842 else:
843 return None
835 844
836 def getIntersections(self, p1, p2): 845 def getIntersections(self, p1, p2):
837 '''Returns a list of the indices at which the trajectory 846 '''Returns a list of the indices at which the trajectory
838 intersects with the segment of extremities p1 and p2 847 intersects with the segment of extremities p1 and p2
839 the list is empty if there is no crossing''' 848 the list is empty if there is no crossing'''
882 if inter.first >=0 and inter.last<= self.length(): 891 if inter.first >=0 and inter.last<= self.length():
883 return Trajectory([self.positions[0][inter.first:inter.last+1], 892 return Trajectory([self.positions[0][inter.first:inter.last+1],
884 self.positions[1][inter.first:inter.last+1]]) 893 self.positions[1][inter.first:inter.last+1]])
885 else: 894 else:
886 return None 895 return None
887 896
897 def subSample(self, step):
898 'Returns the positions very step'
899 return Trajectory([self.positions[0][::step],
900 self.positions[1][::step]])
901
888 if shapelyAvailable: 902 if shapelyAvailable:
889 def getTrajectoryInPolygon(self, polygon): 903 def getTrajectoryInPolygon(self, polygon):
890 '''Returns the trajectory built with the set of points inside the (shapely) polygon''' 904 '''Returns the trajectory built with the set of points inside the (shapely) polygon'''
891 traj = Trajectory() 905 traj = Trajectory()
892 points = [p.asShapely() for p in self] 906 points = [p.asShapely() for p in self]