Exemplo n.º 1
0
    def basic_test(self):
        """Test basic TurnByTurn point functionality"""
        point = TurnByTurnPoint(1.0,
                                2.0,
                                elevation=123.45,
                                message="foo message",
                                ssml_message="ssml message",
                                icon="some_icon_id")

        # distances should be None by default
        self.assertIsNone(point.current_distance)
        self.assertIsNone(point.distance_from_start)

        # visited should be False as well
        self.assertFalse(point.visited)

        self.assertEqual(point.ssml_message, "ssml message")
        self.assertEqual(point.icon, "some_icon_id")

        # check if the LLEMI tuple is correct as well
        self.assertEqual(point.llemi,
                         (1.0, 2.0, 123.45, "foo message", "some_icon_id"))

        # try setting the distances
        point.current_distance = 200
        point.distance_from_start = 300
        self.assertEqual(point.current_distance, 200)
        self.assertEqual(point.distance_from_start, 300)
Exemplo n.º 2
0
    def from_handmade(cls, start, middle_points, destination):
        """Convert hand-made route data to a way.

        :param start: starting point
        :param middle_points: list of middle points (if any)
        :param destination: destination point
        """
        if start and destination:
            # route points & message points are generated at once
            # * empty string as message => no message point, just route point
            route_points = [(start[0], start[1], None)]
            message_points = []
            m_length = 0  # in meters
            last_lat, last_lon = start[0], start[1]
            for point in middle_points:
                lat, lon, elevation, message = point
                m_length += geo.distance(last_lat, last_lon, lat, lon) * 1000
                route_points.append((lat, lon, elevation))
                if message != "":  # is it a message point ?
                    point = TurnByTurnPoint(lat, lon, elevation, message)
                    point.distance_from_start = m_length
                    message_points.append(point)
                last_lat, last_lon = lat, lon
            route_points.append((destination[0], destination[1], None))
            way = cls(route_points)
            way.add_message_points(message_points)
            # huge guestimation (avg speed 60 km/h = 16.7 m/s)
            seconds = m_length / 16.7
            way._set_duration(seconds)
            way._set_length(m_length)
            # done, return the result
            return way
        else:
            return None
Exemplo n.º 3
0
    def from_google_directions_result(cls, gd_result):
        """Convert Google Directions result to a way object.

        :param gd_result: Google Directions result
        """
        leg = gd_result['routes'][0]['legs'][0]
        steps = leg['steps']

        points = decode_polyline(
            gd_result['routes'][0]['overview_polyline']['points'])
        # length of the route can computed from its metadata
        if 'distance' in leg:  # this field might not be present
            m_length = leg['distance']['value']
        else:
            m_length = None
        # the route also contains the expected duration in seconds
        if 'duration' in leg:  # this field might not be present
            s_duration = leg['duration']['value']
        else:
            s_duration = None

        way = cls(points)
        way._set_length(m_length)
        way._set_duration(s_duration)
        message_points = []

        m_distance_from_start = 0

        for step in steps:
            # TODO: abbreviation filtering
            message = step['html_instructions']
            # as you can see, for some reason,
            # the coordinates in Google Directions steps are reversed:
            lat = step['start_location']['lat']
            lon = step['start_location']['lng']
            #TODO: end location ?
            point = TurnByTurnPoint(lat, lon, message=message)
            point.distance_from_start = m_distance_from_start
            # store point to temporary list
            message_points.append(point)
            # update distance for next point
            mDistanceFromLast = step["distance"]['value']
            m_distance_from_start = m_distance_from_start + mDistanceFromLast

        way.add_message_points(message_points)
        return way
Exemplo n.º 4
0
    def process_and_save_directions(self, route):
        """process and save directions"""

        # apply filters
        route = self.filter_directions(route)

        # add a fake destination step, so there is a "destination reached" message
        if route.point_count > 0:
            (lat, lon) = route.get_point_by_index(-1).getLL()
            dest_step = TurnByTurnPoint(lat, lon)
            dest_step.ssml_message = '<p xml:lang="en">you <b>should</b> be near the destination</p>'
            dest_step.description = 'you <b>should</b> be near the destination'
            dest_step.distance_from_start = route.length
            # TODO: make this multilingual
            # add it to the end of the message point list
            route.add_message_point(dest_step)

        # save
        self._directions = route