Mercurial Hosting > traffic-intelligence
diff python/moving.py @ 781:7c38250ddfc7 dev
updated to deal with prepared polygons from shapely, and to extract the same positions from a second trajectory in a polygon (for velocities for example)
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Thu, 11 Feb 2016 11:56:50 -0500 |
parents | 1b22d81ef5ff |
children | 3aa6102ccc12 |
line wrap: on
line diff
--- a/python/moving.py Mon Feb 08 12:24:26 2016 -0500 +++ b/python/moving.py Thu Feb 11 11:56:50 2016 -0500 @@ -12,7 +12,7 @@ try: from shapely.geometry import Polygon, Point as shapelyPoint - from shapely.prepared import prep + from shapely.prepared import prep, PreparedGeometry shapelyAvailable = True except ImportError: print('Shapely library could not be loaded') @@ -349,8 +349,11 @@ if shapelyAvailable: def pointsInPolygon(points, polygon): - '''Optimized tests of a series of points within (Shapely) polygon ''' - prepared_polygon = prep(polygon) + '''Optimized tests of a series of points within (Shapely) polygon (not prepared)''' + if type(polygon) == PreparedGeometry: + prepared_polygon = polygon + else: + prepared_polygon = prep(polygon) return filter(prepared_polygon.contains, points) # Functions for coordinate transformation @@ -933,32 +936,50 @@ self.positions[1][::step]]) if shapelyAvailable: - def getTrajectoryInPolygon(self, polygon): - '''Returns the trajectory built with the set of points inside the (shapely) polygon''' + def getTrajectoryInPolygon(self, polygon, t2 = None): + '''Returns the trajectory built with the set of points inside the (shapely) polygon + The polygon could be a prepared polygon (faster) from prepared.prep + + t2 is another trajectory (could be velocities) + which is filtered based on the first (self) trajectory''' traj = Trajectory() - points = [p.asShapely() for p in self] - for p in pointsInPolygon(points, polygon): - traj.addPositionXY(p.x, p.y) - return traj + inPolygon = [] + for x, y in zip(self.positions[0], self.positions[1]): + inPolygon.append(polygon.contains(shapelyPoint(x, y))) + if inPolygon[-1]: + traj.addPositionXY(x, y) + traj2 = Trajectory() + if t2 is not None: + for inp, x, y in zip(inPolygon, t2.positions[0], t2.positions[1]): + if inp: + traj2.addPositionXY(x, y) + return traj, traj2 def proportionInPolygon(self, polygon, minProportion = 0.5): - pointsIn = pointsInPolygon([p.asShapely() for p in self], polygon) + inPolygon = [polygon.contains(shapelyPoint(x, y)) for x, y in zip(self.positions[0], self.positions[1])] lengthThreshold = float(self.length())*minProportion - return len(pointsIn) >= lengthThreshold + return sum(inPolygon) >= lengthThreshold else: - def getTrajectoryInPolygon(self, polygon): + def getTrajectoryInPolygon(self, polygon, t2 = None): '''Returns the trajectory built with the set of points inside the polygon (array of Nx2 coordinates of the polygon vertices)''' traj = Trajectory() + inPolygon = [] for p in self: - if p.inPolygon(polygon): + inPolygon.append(p.inPolygon(polygon)) + if inPolygon[-1]: traj.addPosition(p) - return traj + traj2 = Trajectory() + if t2 is not None: + for inp, x, y in zip(inPolygon, t2.positions[0], t2.positions[1]): + if inp: + traj2.addPositionXY(p.x, p.y) + return traj, traj2 def proportionInPolygon(self, polygon, minProportion = 0.5): - pointsInPolygon = [p.inPolygon(polygon) for p in self] + inPolygon = [p.inPolygon(polygon) for p in self] lengthThreshold = float(self.length())*minProportion - return len(pointsInPolygon) >= lengthThreshold + return sum(inPolygon) >= lengthThreshold @staticmethod def lcss(t1, t2, lcss):