def test_data(self): data = ''' 1 80,982 163,8164 170,2620 145,648 2 42,1689 127,9365 5,8026 170,9342 ''' expected = [ [], [(80, 982), (163, 8164), (170, 2620), (145, 648)], [(42, 1689), (127, 9365), (5, 8026), (170, 9342)], ] with patch('dijkstra.open', mock_open(read_data=data)): graph = load_data(data) self.assertEqual(graph, (expected)) self.assertEqual(len(graph), 3)
def flat_earth(filename = 'path_flat.kml'): """Test long path""" nodes, links = dijkstra.load_data() dijkstra.create_adjacency_lists(nodes, links) source = dijkstra.node_by_name(nodes, 'PASADENA', 'CA') destination = dijkstra.node_by_name(nodes, 'CAMBRIDGE', 'MA') ans = dijkstra.shortest_path(nodes, links, dijkstra.distance, source, destination) nhpn.Visualizer.toKML(ans, 'path_flat.kml') print "path_flat.kml created"
def curved_earth(filename = 'path_curved.kml'): """Test long path""" nodes, links = dijkstra.load_data() dijkstra.create_adjacency_lists(nodes, links) def distance(node1, node2): """Returns the distance between node1 and node2, including the Earth's curvature.""" A = node1.latitude*pi/10**6/180 B = node1.longitude*pi/10**6/180 C = node2.latitude*pi/10**6/180 D = node2.longitude*pi/10**6/180 return acos(sin(A)*sin(C)+cos(A)*cos(C)*cos(B-D)) source = dijkstra.node_by_name(nodes, 'PASADENA', 'CA') destination = dijkstra.node_by_name(nodes, 'CAMBRIDGE', 'MA') ans = dijkstra.shortest_path(nodes, links, distance, source, destination) nhpn.Visualizer.toKML(ans, 'path_curved.kml') print "path_curved.kml created"