Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import osmnx as ox, networkx as nx, geopandas as gpd
- import matplotlib.pyplot as plt
- from shapely.geometry import Point
- from descartes import PolygonPatch
- class Car:
- def __init__(self, car_speed, driver_level):
- # car speed is equal to max car speed in km/h
- self.car_speed = car_speed
- # driver level from 0 to 1 exmpl 0.58
- self.driver_level = driver_level
- def callculateActualSpeed(self, street_conditon=1):
- return (self.car_speed * self.driver_level) * street_conditon
- def ShowShortestPath(self, G, origin_p, destination_p, isPolice=False):
- # creating shortest path based on length
- route = nx.shortest_path(G, origin_p, destination_p, weight='length')
- # additional print route!
- print "Route: ", route
- # plotting route
- if isPolice:
- fig, ax = ox.plot_graph_route(G, route, route_color='Blue', orig_dest_node_color="Blue",
- orig_dest_point_color="Blue")
- else:
- fig, ax = ox.plot_graph_route(G, route, route_color='Red', orig_dest_node_color="Red",
- orig_dest_point_color="Red")
- def returnRoute(self, G, origin_p, destination_p):
- # creating shortest path based on length
- route = nx.shortest_path(G, origin_p, destination_p, weight='length')
- return route
- def calculatePathTime(self, G, route, origin_p, destination_p):
- # creatin list and adding zero indez\x
- route_times = []
- route_times.append(0)
- # main block
- for n in range(1, len(route)):
- route_times.append((nx.shortest_path_length(G, origin_p, route[n], weight='length')/1000)*60/5)
- return route_times
- def pathTime(self, G, origin_p, destination_p):
- return nx.shortest_path_length(G, origin_p, destination_p, weight='length')/1000*60/5
- $$$$$$$$$$$$$$$$$$$$$$$$$$$$
- from Car import Car
- import osmnx as ox, networkx as nx, geopandas as gpd
- import matplotlib.pyplot as plt
- from shapely.geometry import Point
- from descartes import PolygonPatch
- from random import random, choice
- ox.config(log_file=True, log_console=True, use_cache=True)
- c = raw_input("Based on bounding box([y],[n]): ")
- if c == "y":
- # creating bounding box
- north, south, east, west = 39.7583, 39.7275, -105.4652, -105.5410
- G = ox.graph_from_bbox(north, south, east, west, network_type='drive')
- else: # creating grapg based on place
- place = "Idaho Springs, USA"
- # place = raw_input("Enter place: " )
- G = ox.graph_from_place(place, network_type='drive')
- # link to open street map
- print "Enter https://www.openstreetmap.org/#map=17/39.74302/-105.52421 \n"
- # show the simplified network with edges colored by edge length
- ec = ox.get_edge_colors_by_attr(G, num_bins=20, attr='length', cmap='GnBu', start=0.2)
- # fig, ax = ox.plot_graph(G,bgcolor = "black" , node_color='w', node_edgecolor='k', node_size=10,node_zorder=2, edge_color=ec, edge_linewidth=3)
- # creating 2 police cars and 1 robbers car
- p1, p2, r1 = Car(20, 0.50), Car(20, 0.50), Car(20, 0.25)
- # Creating exit nodes for robbers
- exit_nodes = [176598973, 176493948, 1410414960, 176567940, 176590273, 176557759, 176544697, 176472449]
- # If user want to add asome addiotnal adges
- while raw_input("Do you want to enter more exit_nodes? [y], [n]: ") == 'y':
- x = float(raw_input("Enter x coordinate from openStreetMap: "))
- print "Coordinate approved! %s" % (x)
- y = float(raw_input("Enter y coordinate from openStreetMap: "))
- print "Coordinate approved! %s" % (y)
- # adding the nearest node from point
- exit_nodes.append(ox.get_nearest_node(G, (x, y)))
- print exit_nodes
- # Randomly selected exit nodes based on number of outcoimng edges
- # for x in G.nodes():
- # if nx.degree(G,x) == 2:
- # exit_nodes.append(x)
- # cords for start desitantion
- x, y = (39.74356, -105.52440)
- starting_node = ox.get_nearest_node(G, (x, y))
- # Drawing and random exit node
- print "Starting node id: ", starting_node
- destination_node = choice(exit_nodes)
- print "Destination node id: ", destination_node
- # printing route based on wieght
- print "Path length: %s km!" % (nx.shortest_path_length(G, starting_node, destination_node, weight='length') / 1000)
- # calculation path times
- path_times = r1.calculatePathTime(G, r1.returnRoute(G, starting_node, destination_node), starting_node,
- destination_node)
- # adding best nodes
- best_nodes = []
- route = r1.returnRoute(G, starting_node, destination_node)
- valid_nodes = [1410414960,176490034]
- for m in G.nodes():
- if m in r1.returnRoute(G, starting_node, destination_node) or m in valid_nodes:
- continue
- else:
- for n in range(0, len(r1.returnRoute(G, starting_node, destination_node))):
- if p1.pathTime(G, m, route[n] ) <= path_times[n]:
- best_nodes.append((m, route[n]))
- for b in best_nodes:
- r1.ShowShortestPath(G, starting_node,b[1])
- p1.ShowShortestPath(G, b[0], b[1],1)
- $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
- from Car import Car
- import osmnx as ox, networkx as nx, geopandas as gpd
- import matplotlib.pyplot as plt
- from shapely.geometry import Point
- from descartes import PolygonPatch
- ox.config(log_file=True, log_console=True, use_cache=True)
- c = raw_input("Based on bounding box([y],[n]): ")
- if c == "y":
- # creating bounding box
- north, south, east, west = 39.7583, 39.7275, -105.4652, -105.5410
- G = ox.graph_from_bbox(north, south, east, west, network_type='drive')
- else: # creating grapg based on place
- place = "Idaho Springs, USA"
- # place = raw_input("Enter place: " )
- G = ox.graph_from_place(place, network_type='drive')
- # creating new robber car!
- r1 = Car(10, 0.58)
- print "Enter https://www.openstreetmap.org/#map=17/39.74302/-105.52421 \n"
- # Bank Robbery problem: A
- x, y = (39.74356, -105.52440)
- # x = float(raw_input("Enter x coordinate from openStreetMap: "))
- print "Coordinate approved! %s" % (x)
- # y = float(raw_input("Enter y coordinate from openStreetMap: "))
- print "Coordinate approved! %s" % (y)
- # minutes from begigng
- list_of_ints = list(range(1, 24, 4))
- escape_time = list_of_ints
- # setting starting node
- starting_node = ox.get_nearest_node(G, (x, y))
- # setting robber ar travelling speed
- meters_per_minute = r1.car_speed * 1000 / 60
- for u, v, k, data in G.edges(data=True, keys=True):
- data['time'] = data['length'] / meters_per_minute
- # get one color for each isochrone
- iso_colors = ox.get_colors(n=len(escape_time), cmap='GnBu', start=0.3, return_hex=True)
- # make the isochrone polygons
- isochrone_polys = []
- for trip_time in sorted(escape_time, reverse=True):
- subgraph = nx.ego_graph(G, starting_node, radius=trip_time, distance='time')
- node_points = [Point((data['x'], data['y'])) for node, data in subgraph.nodes(data=True)]
- bounding_poly = gpd.GeoSeries(node_points).unary_union.convex_hull
- isochrone_polys.append(bounding_poly)
- # plot the network then add isochrones as colored descartes polygon patches
- fig, ax = ox.plot_graph(G, fig_height=8, show=False, close=False, edge_color='k', edge_alpha=0.2, node_color='none')
- for polygon, fc in zip(isochrone_polys, iso_colors):
- patch = PolygonPatch(polygon, fc=fc, ec='none', alpha=0.6, zorder=-1)
- ax.add_patch(patch)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement