def route_names(self, start, end): """ This method should return the list of road names you travel on to get from start to end. Paths often include edges that are the same road, as the path goes from block to block. If you remain on the same road it should only output that road name once. So instead of: [ '111 Ave NW', '111 Ave NW', '111 Ave NW', '99 St Ave', '99 St Ave', 'Jasper Ave NW' ] It should return: [ '111 Ave NW', '99 St Ave', 'Jasper Ave NW' ] """ path = digraph.least_cost_path(self.graph, self._nearest_vertex(start), self._nearest_vertex(end), self.cost_distance) names = [] for edge in pairwise(path): name = self.E_name[edge] # only add name to list if the list is empty # or it's already the last name in the list # which means the path's already on that road if len(names) == 0 or name != names[-1]: names.append(name) return names
def route_directions(self, start, end): """ This method should return a string with directions to go from start to end. Each road to be travelled on should result in another line in the directions. The directions should be in the form of: Go East on 111 Ave NW. Go South on 99 St Ave. Go East on Jasper Ave. The directions should only cover the four cardinal directions. A road travelling exactly NW could either appear as North or West. This specific implementation prefers North or South over East or West. """ path = digraph.least_cost_path(self.graph, self._nearest_vertex(start), self._nearest_vertex(end), self.cost_distance) directions = '' last_name = '' for edge in pairwise(path): name = self.E_name[edge] # only add road to string if it's changed to a different road if name != last_name: start_coord = self.V_coord[edge[0]] end_coord = self.V_coord[edge[1]] edge_dir = _direction(start_coord, end_coord) line = 'Go {} on {}.\n'.format(edge_dir, name) directions = directions + line last_name = name return directions.strip()
# parse the line into the start and end positions [startlat, startlon, endlat, endlon] = [None,None,None,None] try: [startlat, startlon, endlat, endlon] = [int(x)/100000 for x in request.strip(' \n').split(' ')] except: #bad stuff in stream, flush it and try again io.flush() continue # turn those positions into the nearest verticies in the graph start = nearest_vertex(startlat, startlon) end = nearest_vertex(endlat, endlon) # find the least cost path path = least_cost_path(graph, start, end, cost_distance) # print some diagnostics print("Results for request: `%s`" % request.strip(' \n')) # print out the path as the result if path: # print length print(len(path), file=sys.stdout) io.write(bytes(str(len(path)) + "\n", 'ASCII')) for nodeid in path: # print each node node = metadata[nodeid] print("{} {}".format(int(node[0]*100000), int(node[1]*100000)), file=sys.stdout) io.write(bytes("{} {}\n".format(int(node[0]*100000), int(node[1]*100000)), 'ASCII')) else:
def route(self, start, end): path = digraph.least_cost_path(self.graph, self._nearest_vertex(start), self._nearest_vertex(end), self.cost_distance) return [ self.V_coord[v] for v in path ]
lat_diff = (start_coord[0] - stop_coord[0])**2 lon_diff = (start_coord[1] - stop_coord[1])**2 distance = (lat_diff + lon_diff)**.5 return distance # load the Edmonton map data into a digraph object, and store the # ancillary information about street names and vertex locations (E, E_name, V, V_coord) = readModule.read_graph('edmonton-roads-digraph.txt') G = digraph.Digraph(E) while (debug == 0): # look for input of lat/lon (start_lat, start_lon, dest_lat, dest_lon) = input("lat1 lon1 lat2 lon2: ").split(' ') # find vertex associated with lat/lon start = readModule.value_search(V_coord, float(start_lat), float(start_lon)) dest = readModule.value_search(V_coord, float(dest_lat), float(dest_lon)) ## TODO: make sure start and dest are valid vertices # find least_cost_path path = digraph.least_cost_path(G, start, dest, cost_distance) print(path) ## TODO: print the path information correctly if __name__ == "__main__": import doctest doctest.testmod()