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 testSnapBad(self): mid = LatLng(35.786960, -78.666960) start = LatLng(35.786100, -78.662430) end = LatLng(35.788140, -78.669680) self.__testSnapBad(end, mid, start) self.__testSnapBad(start, mid, end)
def testSnapGood(self): mid = LatLng(35.787350, -78.666755) start = LatLng(35.786100, -78.662430) end = LatLng(35.788140, -78.669680) self.__testSnap(end, mid, start) self.__testSnap(start, mid, end)
def testIsBetween(self): not_between = LatLng(35.786960, -78.666960) start = LatLng(35.786100, -78.662430) end = LatLng(35.788140, -78.669680) line = GeoLine(start, end) self.assertEquals(line.snap_point(not_between, WIDTH_OF_ROAD_KM), None)
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 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 testGeos(self): a = Point(0, 1) b = Point(1, 0) c = LineString([a.coords[0], b.coords[0]]) poly = polyline.from_linestring(c.coords) self.assertEqual(poly.first, LatLng(a.y, a.x)) self.assertEqual(poly.last, LatLng(b.y, b.x))
def testAngleTo(self): a = LatLng(32.07605, -81.10437) b = LatLng(32.07542, -81.104620) c = LatLng(32.07529, -81.10415) ab = GeoLine(a, b) bc = GeoLine(b, c) self.assertEqual(ab.angle_to(bc), bc.angle_to(ab))
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 testBuffer(self): distance_m = 5000.0 point = LatLng(35.0, 78.0) bounds = point.buffer(distance_m) self.assertAlmostEqual(bounds.height, distance_m * 2, SIGNIFICANT_PLACES) min_width = min(bounds.north_width, bounds.south_width) max_width = min(bounds.north_width, bounds.south_width) self.assertAlmostEqual(max_width, distance_m * 2, SIGNIFICANT_PLACES)
def _test_area(self, bounds): ''' Takes an area and tests how well gis to cartesion interpolation works ''' view = InterpolatedFlattener.from_latlngbounds(bounds) print 'Testing %s' % bounds print 'Widths (Top, Bottom): %fkm %fkm' % (bounds.north_width, bounds.south_width) difference = bounds.south_width - bounds.north_width average_width = (bounds.north_width + bounds.south_width) / 2.0 print 'Width Difference: %fcm, %f%%' % ((difference * 100000), 100.0 * (difference / average_width)) print 'Height: %fkm' % bounds.height nw = bounds.north_west ne = bounds.north_east sw = bounds.south_west se = bounds.south_east center = bounds.center def create_cart(geo): return (geo, Point(view.gis_to_cart_point(Point(geo.x, geo.y)))) NW = create_cart(nw) NE = create_cart(ne) SW = create_cart(sw) SE = create_cart(se) CENTER = create_cart(center) self._test_points(NW, SW) self._test_points(NW, NE) self._test_points(SW, SE) self._test_points(NW, SE) self._test_points(SW, NE) self._test_points(CENTER, SE) #test random points ds = [] for _ in range(0, 1000): point1 = LatLng(random.uniform(bounds.north, bounds.south), random.uniform(bounds.east, bounds.west)) point2 = LatLng(random.uniform(bounds.north, bounds.south), random.uniform(bounds.east, bounds.west)) _, _, d = self._test_points(create_cart(point1), create_cart(point2)) ds.append(abs(d)) print '%fcm, %fcm' % (max(ds), min(ds))
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 testBadSnaps(self): options = SnapOptions(max_distance=15.0 / 1000) self.__snap( LatLng(30.5, -91.168733), "{ivxDhmmkPeBVm@J[H_A^k@Xi@PW@W?k@I_@S{EmD_@Sm@Qk@CQA]Be@F{Bh@cAN", options, False)
def testInverse(self): start = LatLng(35.786100, -78.662430) end = LatLng(35.788140, -78.669680) line = GeoLine(start, end) inverse = line.inverse self.assertEqual(line.start, inverse.end) self.assertEqual(line.end, inverse.start) #test when line.angle and line.distance are not cached self.assertEqual(line.distance, inverse.distance) uncached_line_angle = line.angle uncached_inverse_angle = (inverse.angle + pi) % (2 * pi) self.assertAlmostEqual(uncached_inverse_angle, uncached_line_angle, 3) #test using cached line and angle inverse = line.inverse self.assertEqual(line.distance, inverse.distance)
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)
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 testGoodSnaps(self): options = SnapOptions(max_distance=15.0 / 1000) self.__snap( LatLng(30.435380, -91.168733), "{ivxDhmmkPeBVm@J[H_A^k@Xi@PW@W?k@I_@S{EmD_@Sm@Qk@CQA]Be@F{Bh@cAN", options, True) options.max_distance = 0.1 self.__snap(LatLng(42.32541, -71.179567), "esiaG|qlqLeB]AjAFh@Ax@AtAGfAEvAIj@Gf@Kl@Sz@zCdC", options, True) #this point is just outside of the last point of the polyline self.__snap(LatLng(42.337333, -71.103065), "g_laG~h~pL}PoKa@g@", options, True) self.__snap(LatLng(42.35902, -71.093656), "usnaGrt{pLem@zU_CfAsCvBoBtCu`@zw@", options, True) self.__snap(LatLng(42.359099, -71.093506), "usnaGrt{pLem@zU_CfAsCvBoBtCu`@zw@", options, True)
def testApplyAngleAndBearing(self): start = LatLng(35.786100, -78.662430) end = LatLng(35.788140, -78.669680) bearing = start.angle_to(end) distance = start.distance_to(end) test_end = start.apply_bearing_and_distance(bearing, distance) self.assertEqual(test_end, end)