def preprocessRoutes(routeFile): edges = nx.read_shp(routeFile) posNodes = pre.getPositionOfNodesFromEdges(edges) posEdges = pre.getPositionOfEdges(edges) edg = pre.getEdges(posEdges,posNodes) network = pre.createNetwork(posNodes,edg) return network
def main(): ##### Read in edges ##### print('input data: ', pathToEdges) ##### Pre-process ##### # Build network edges = nx.read_shp(pathToEdges) posNodes = pre.getPositionOfNodesFromEdges(edges) posEdges = pre.getPositionOfEdges(edges) # Create a list of tuples containing the start and end node of an edge edg = pre.getEdges(posEdges, posNodes) # Create the network based on the position of nodes and the edges defined by start and end nodes IDs network = pre.createNetwork(posNodes, edg) drawNetwork(network, posNodes) ##### Add Weight to edges ##### # 1. Get coordinates of edges and apply to the network coordinates = pre.createDictFromEdgesCoords(edges, edg) nx.set_edge_attributes(network, name='coord', values=coordinates) # 2. Get Local Score of edges and apply to the network LocalScore = createDictFromLocalScore(edges, edg) nx.set_edge_attributes(network, name='LS', values=LocalScore) # 3. Get Distant Score of edges and apply to the network DistantScore = createDictFromDistantScore(edges, edg) nx.set_edge_attributes(network, name='DS', values=DistantScore) # 4. Calculate the distance and apply to the network distance = pre.calcDistance(network, edg) nx.set_edge_attributes(network, name='dist', values=distance) ##### Run A Star alghoritm ##### print('Start node: ', startNode) print('End node: ', endNode) #print('Network cost: ', networkWeight) print('Weight scenario: ', weightScenario) print('TS - Total Score, DS - Distant Score, LS - Local Score') print(' 1: TS = 0.5 LS + 0.5 DS') print( ' 2: TS = (DS = 0.8 and LS = 0.2. Linear change of DS and LS towards the destination till DS = 0.2 and LS = 0.8))' ) print('Scale factor of the Total Score in reagrds to heurestic: ', TotalScoreToHeuresticScaleFactor) astar = astar_path_Kasia(network, startNode, endNode, weightScenario, posNodes) print('Results of A Star algorithm:') print(astar) shortestPathAStar = drawShortestPath(network, astar, posNodes) shortestPathAStar # Save results # Subset a graph astar_result = network.subgraph(astar) # Save as points exportResultsToPoints(astar_result, astar)
def analyseODFromDijkstra(): #arrays for calculations angularChange = [] waitingTime = [] #Read in edges path = os.path.join(cwd,'Results','Dijkstra','*.shp') files = glob.glob(path) files for file in files: #edges = nx.read_shp(file) edges = nx.read_shp(file) #print(list(edges.edges(data=True))[12]) posNodes = pre.getPositionOfNodesFromEdgesForAnalysis(edges) posNodes.keys() #New Edge position posEdges = pre.getPositionOfEdgesForAnalysis(edges) #New edge calculation edg = pre.getEdgesForAnalysis(posEdges,posNodes) network = pre.createNetwork(posNodes,edg) coordinates = pre.createDictFromEdgesCoords(edges,edg) nx.set_edge_attributes(network, name='coord', values=coordinates) removeEdges = getEdgesWithoutCoords(network) network.remove_edges_from(removeEdges) distance = pre.calcDistance(network,edg) nx.set_edge_attributes(network, name='dist', values=distance) trafficLights = pre.readcsv(os.path.join(cwd,'traffic_lights.csv')) trafficDict = pre.createDictFromTrafficLights(trafficLights) TrafficDuration = pre.createCompleteTrafficDict(network,trafficDict) nx.set_edge_attributes(network, name='traffic', values=TrafficDuration) adjacency = pre.calcAdjacency(network) adjacentAndNeighborNodesDict = pre.getAdjacentAndNeigborNodes(adjacency) #Calc TotalAngularchange and traffic light waiting time anglesForEachOD = getAngles(network,adjacentAndNeighborNodesDict) angularChange.append(anglesForEachOD) trafficWaitingForEachOD = getWaitingTime(network) waitingTime.append(trafficWaitingForEachOD) return angularChange, waitingTime
def main(): #Read in edges pathToEdges = os.path.join(cwd,'data','Muenster_edges.shp') edges = nx.read_shp(pathToEdges) posNodes = pre.getPositionOfNodesFromEdges(edges) #New Edge position posEdges = pre.getPositionOfEdges(edges) #New edge calculation edg = pre.getEdges(posEdges,posNodes) #pre.getEdges(posEd,posNo) network = pre.createNetwork(posNodes,edg) #Set position of Nodes for exporting as shp pre.setPosNodes(network, posNodes) #pre.drawNetwork(network,posNodes) #Add Weight to edges #Get coordinates of edges coordinates = pre.createDictFromEdgesCoords(edges,edg) nx.set_edge_attributes(network, name='coord', values=coordinates) #Calc Distance distance = pre.calcDistance(network,edg) key_max = max(distance.keys(), key=(lambda k: distance[k])) key_min = min(distance.keys(), key=(lambda k: distance[k])) print(key_max) print(key_min) distance.get((51, 2795)) distance.get((5513, 6486)) #Apply dist on edges nx.set_edge_attributes(network, name='dist', values=distance) H1 = network.subgraph([24,1946]) H = network.edge_subgraph([(24,1946)]) nx.draw_networkx_nodes(H1,posNodes,node_size=10, node_color='r') nx.draw_networkx_edges(H,posNodes,edge_color = 'b') adjacency = pre.calcAdjacency(network) adjacentAndNeighborNodesDict = pre.getAdjacentAndNeigborNodes(adjacency) trafficLights = pre.readcsv(os.path.join(cwd,'traffic_lights.csv')) trafficDict = pre.createDictFromTrafficLights(trafficLights) TrafficDuration = pre.createCompleteTrafficDict(network,trafficDict) nx.set_edge_attributes(network, name='traffic', values=TrafficDuration) totalCosts = pre.calcCosts(network) nx.set_edge_attributes(network, name='costs', values=totalCosts) dijkstraOD1 = dijkstra_path(network,395,7775,adjacentAndNeighborNodesDict,weight='costs') dijkstraOD2 = dijkstra_path(network,7775,2196,adjacentAndNeighborNodesDict,weight='costs') dijkstraOD3 = dijkstra_path(network,2196,6768,adjacentAndNeighborNodesDict,weight='costs') print(dijkstraOD1) print(dijkstraOD2) print(dijkstraOD3)