def _followingRoute(self): """are we still following the route or is rerouting needed""" start1 = time.clock() pos = self.get('pos', None) proj = self.m.get('projection', None) if pos and proj: pLat, pLon = pos # we use Radians to get rid of radian conversion overhead for # the geographic distance computation method radiansLL = self.radiansRoute pLat = geo.radians(pLat) pLon = geo.radians(pLon) if len(radiansLL) == 0: self.log.error("Divergence: can't follow a zero point route") return False elif len(radiansLL) == 1: # 1 point route aLat, aLon = radiansLL[0] minDistance = geo.distanceApproxRadians(pLat, pLon, aLat, aLon) else: # 2+ points route aLat, aLon = radiansLL[0] bLat, bLon = radiansLL[1] minDistance = geo.distancePointToLineRadians(pLat, pLon, aLat, aLon, bLat, bLon) aLat, aLon = bLat, bLon for point in radiansLL[1:]: bLat, bLon = point dist = geo.distancePointToLineRadians(pLat, pLon, aLat, aLon, bLat, bLon) if dist < minDistance: minDistance = dist aLat, aLon = bLat, bLon # the multiplier tries to compensate for high speed movement threshold = float( self.get('reroutingThreshold', REROUTING_DEFAULT_THRESHOLD)) * self.reroutingThresholdMultiplier self.log.debug("Divergence from route: %1.2f/%1.2f m computed in %1.0f ms", minDistance * 1000, float(threshold), (1000 * (time.clock() - start1))) return minDistance * 1000 < threshold