def test_offset_calculations(self):
        """Tests the latitude/longitude to y_m/x_m offset calculations."""
        for value in (-2.0, -1.0, 0.0, 1.0, 2.0):
            self.assertAlmostEqual(
                value,
                Telemetry.offset_y_m_to_latitude(
                    Telemetry.latitude_to_m_offset(value)))
            self.assertAlmostEqual(
                value,
                Telemetry.latitude_to_m_offset(
                    Telemetry.offset_y_m_to_latitude(value)))
            for value_2 in (-0.01, 0.0, 0.01):
                self.assertAlmostEqual(
                    value,
                    Telemetry.offset_x_m_to_longitude(
                        Telemetry.longitude_to_m_offset(value, 0.0), value_2))
                self.assertAlmostEqual(
                    value,
                    Telemetry.offset_x_m_to_longitude(
                        Telemetry.longitude_to_m_offset(value, 0.0), value_2))

        rally_parking_lot = (40.020776, -105.248319)
        self.assertAlmostEqual(
            Telemetry.offset_x_m_to_longitude(
                Telemetry.longitude_to_m_offset(rally_parking_lot[1],
                                                rally_parking_lot[0]),
                rally_parking_lot[0]), rally_parking_lot[1])
    def _load_waypoints(kml_stream):
        """Loads and returns the waypoints from a KML string."""

        def get_child(element, tag_name):
            """Returns the child element with the given tag name."""
            try:
                return getattr(element, tag_name)
            except AttributeError:
                raise ValueError('No {tag} element found'.format(tag=tag_name))

        root = parser.parse(kml_stream).getroot()
        if 'kml' not in root.tag:
            raise ValueError('Not a KML file')

        document = get_child(root, 'Document')
        placemark = get_child(document, 'Placemark')
        line_string = get_child(placemark, 'LineString')
        # Unlike all of the other tag names, "coordinates" is not capitalized
        coordinates = get_child(line_string, 'coordinates')

        waypoints = []
        text = coordinates.text.strip()
        for csv in re.split(r'\s', text):
            (
                longitude,
                latitude,
                altitude  # pylint: disable=unused-variable
            ) = csv.split(',')

            waypoints.append((
                Telemetry.longitude_to_m_offset(float(longitude), float(latitude)),
                Telemetry.latitude_to_m_offset(float(latitude))
            ))
        return waypoints
Esempio n. 3
0
    def test_offset_calculations(self):
        """Tests the latitude/longitude to y_m/x_m offset calculations."""
        for value in (-2.0, -1.0, 0.0, 1.0, 2.0):
            self.assertAlmostEqual(
                value,
                Telemetry.offset_y_m_to_latitude(
                    Telemetry.latitude_to_m_offset(value)
                )
            )
            self.assertAlmostEqual(
                value,
                Telemetry.latitude_to_m_offset(
                    Telemetry.offset_y_m_to_latitude(
                        value
                    )
                )
            )
            for value_2 in (-0.01, 0.0, 0.01):
                self.assertAlmostEqual(
                    value,
                    Telemetry.offset_x_m_to_longitude(
                        Telemetry.longitude_to_m_offset(value, 0.0),
                        value_2
                    )
                )
                self.assertAlmostEqual(
                    value,
                    Telemetry.offset_x_m_to_longitude(
                        Telemetry.longitude_to_m_offset(value, 0.0),
                        value_2
                    )
                )

        rally_parking_lot = (40.020776, -105.248319)
        self.assertAlmostEqual(
            Telemetry.offset_x_m_to_longitude(
                Telemetry.longitude_to_m_offset(
                    rally_parking_lot[1],
                    rally_parking_lot[0]
                ),
                rally_parking_lot[0]
            ),
            rally_parking_lot[1]
        )
 def test_load_waypoints(self):
     """Tests loading waypoints from a KML format file."""
     coordinates_long_lat = zip(range(10), range(10, 0, -1))
     coordinates_str = " ".join(("{},{},50".format(long, lat) for long, lat in coordinates_long_lat))
     kml = KML_TEMPLATE.format(coordinates_str).encode("utf-8")
     kml_buffer = io.BytesIO(kml)
     waypoints = SimpleWaypointGenerator._load_waypoints(kml_buffer)
     for m_offset, long_lat in zip(waypoints, coordinates_long_lat):
         x_m_1, y_m_1 = m_offset
         long_, lat = long_lat
         x_m_2 = Telemetry.longitude_to_m_offset(long_)
         y_m_2 = Telemetry.latitude_to_m_offset(lat)
         self.assertEqual(x_m_1, x_m_2)
         self.assertEqual(y_m_1, y_m_2)
Esempio n. 5
0
 def test_load_waypoints(self):
     """Tests loading waypoints from a KML format file."""
     coordinates_long_lat = zip(range(10), range(10, 0, -1))
     coordinates_str = ' '.join(('{},{},50'.format(long, lat)
                                 for long, lat in coordinates_long_lat))
     kml = KML_TEMPLATE.format(coordinates_str).encode('utf-8')
     kml_buffer = io.BytesIO(kml)
     waypoints = SimpleWaypointGenerator._load_waypoints(kml_buffer)
     for m_offset, long_lat in zip(waypoints, coordinates_long_lat):
         x_m_1, y_m_1 = m_offset
         long_, lat = long_lat
         x_m_2 = Telemetry.longitude_to_m_offset(long_)
         y_m_2 = Telemetry.latitude_to_m_offset(lat)
         self.assertEqual(x_m_1, x_m_2)
         self.assertEqual(y_m_1, y_m_2)