def testDistance(self): point1 = LatLng(37.739323, -122.473586) point2 = LatLng(37.749832, -122.453332) distance1 = Polyline(point1, point2).distance assert round(distance1) == 2130.0, "distance calculation off." distance2 = Polyline(point1, point2, point1).distance assert round(distance2) == 4260.0, "distance calculation off."
def testConstructor(self): start = LatLng(35.786100, -78.662430) end = LatLng(35.788140, -78.669680) poly = Polyline([start, end]) self.assertEqual(poly.first, start) self.assertEqual(poly.last, end) poly = Polyline((start, end)) self.assertEqual(poly.first, start) self.assertEqual(poly.last, end) poly = Polyline(start, end) self.assertEqual(poly.first, start) self.assertEqual(poly.last, end)
def testListMethods(self): poly = Polyline( [LatLng(35.786100, -78.662430), LatLng(35.788140, -78.669680)]) self.assertEquals(len(poly), 2) self.assertEqual(poly[0], poly.first) self.assertEqual(poly[-1], poly.last) a = LatLng(0, 0) b = LatLng(2, 2) poly[0] = a poly.last = b self.assertEqual(poly.first, a) self.assertEqual(poly.last, b) poly.prepend(a) poly.append(b) self.assertEquals(len(poly), 4) poly.insert(2, LatLng(3, 3)) self.assertEquals(len(poly), 5) self.assertAlmostEquals(poly.bounds.north, 3) self.assertAlmostEquals(poly.bounds.south, 0) self.assertAlmostEquals(poly.bounds.west, 0) self.assertAlmostEquals(poly.bounds.east, 3)
def testInverse(self): poly = Polyline( [LatLng(35.786100, -78.662430), LatLng(35.788140, -78.669680)]) poly_i = poly.inverse self.assertEqual(poly.first, poly_i.last) self.assertEqual(poly.last, poly_i.first)
def testBounds(self): poly = Polyline( [LatLng(35.786100, -78.662430), LatLng(35.788140, -78.669680)]) self.assertAlmostEqual(poly.bounds.north, 35.788140) self.assertAlmostEqual(poly.bounds.south, 35.786100) self.assertAlmostEqual(poly.bounds.west, -78.669680) self.assertAlmostEqual(poly.bounds.east, -78.662430)
def decode_polyline(point_str): '''Decodes a polyline that has been encoded using Google's algorithm and returns a Polyline object. :param point_str: Encoded polyline string. :type point_str: string :returns: Decoded polyline :rtype: Polyline ''' latlngs = [LatLng(l[1], l[0]) for l in decode(point_str)] return None if len(latlngs) < 2 else Polyline(latlngs)
def testInterpolate(self): point1 = LatLng(37.739323, -122.473586) point2 = LatLng(37.749832, -122.453332) line = Polyline([point1, point2]) assert line.interpolate(0.0) == point1 assert line.interpolate(1.0) == point2 try: line.interpolate(-1.0) assert False, "Shouldn't accept values < 0" except ValueError: pass half = line.interpolate(0.5) self.assertAlmostEquals(half.distance_to(point1), line.distance / 2, 3) assert half == LatLng(37.7445779332, -122.4634597190)