Advertisement
Guest User

Bin

a guest
Nov 19th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.34 KB | None | 0 0
  1. import osmnx as ox, networkx as nx, geopandas as gpd
  2. import matplotlib.pyplot as plt
  3. from shapely.geometry import Point
  4. from descartes import PolygonPatch
  5.  
  6.  
  7. class Car:
  8.     def __init__(self, car_speed, driver_level):
  9.         # car speed is equal to max car speed in km/h
  10.         self.car_speed = car_speed
  11.         # driver level from 0 to 1 exmpl 0.58
  12.         self.driver_level = driver_level
  13.  
  14.     def callculateActualSpeed(self, street_conditon=1):
  15.         return (self.car_speed * self.driver_level) * street_conditon
  16.  
  17.     def ShowShortestPath(self, G, origin_p, destination_p, isPolice=False):
  18.         # creating shortest path based on length
  19.         route = nx.shortest_path(G, origin_p, destination_p, weight='length')
  20.         # additional print route!
  21.         print "Route: ", route
  22.  
  23.         # plotting route
  24.         if isPolice:
  25.             fig, ax = ox.plot_graph_route(G, route, route_color='Blue', orig_dest_node_color="Blue",
  26.                                           orig_dest_point_color="Blue")
  27.         else:
  28.             fig, ax = ox.plot_graph_route(G, route, route_color='Red', orig_dest_node_color="Red",
  29.                                           orig_dest_point_color="Red")
  30.  
  31.     def returnRoute(self, G, origin_p, destination_p):
  32.         # creating shortest path based on length
  33.         route = nx.shortest_path(G, origin_p, destination_p, weight='length')
  34.         return route
  35.  
  36.     def calculatePathTime(self, G, route, origin_p, destination_p):
  37.         # creatin list and adding zero indez\x
  38.         route_times = []
  39.         route_times.append(0)
  40.  
  41.         # main block
  42.         for n in range(1, len(route)):
  43.             route_times.append((nx.shortest_path_length(G, origin_p, route[n], weight='length')/1000)*60/5)
  44.  
  45.  
  46.         return route_times
  47.  
  48.     def pathTime(self, G, origin_p, destination_p):
  49.         return nx.shortest_path_length(G, origin_p, destination_p, weight='length')/1000*60/5
  50.  
  51. $$$$$$$$$$$$$$$$$$$$$$$$$$$$
  52.  
  53. from Car import Car
  54. import osmnx as ox, networkx as nx, geopandas as gpd
  55. import matplotlib.pyplot as plt
  56. from shapely.geometry import Point
  57. from descartes import PolygonPatch
  58. from random import random, choice
  59.  
  60. ox.config(log_file=True, log_console=True, use_cache=True)
  61.  
  62. c = raw_input("Based on bounding box([y],[n]): ")
  63. if c == "y":
  64.     # creating bounding box
  65.     north, south, east, west = 39.7583, 39.7275, -105.4652, -105.5410
  66.     G = ox.graph_from_bbox(north, south, east, west, network_type='drive')
  67. else:  # creating grapg based on place
  68.     place = "Idaho Springs, USA"
  69.     # place = raw_input("Enter place: " )
  70.     G = ox.graph_from_place(place, network_type='drive')
  71.  
  72. # link to open street map
  73. print "Enter https://www.openstreetmap.org/#map=17/39.74302/-105.52421 \n"
  74.  
  75. # show the simplified network with edges colored by edge length
  76. ec = ox.get_edge_colors_by_attr(G, num_bins=20, attr='length', cmap='GnBu', start=0.2)
  77. # 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)
  78.  
  79. # creating 2 police cars and 1 robbers car
  80. p1, p2, r1 = Car(20, 0.50), Car(20, 0.50), Car(20, 0.25)
  81.  
  82. # Creating exit nodes for robbers
  83. exit_nodes = [176598973, 176493948, 1410414960, 176567940, 176590273, 176557759, 176544697, 176472449]
  84.  
  85. # If user want to add asome addiotnal adges
  86. while raw_input("Do you want to enter more exit_nodes? [y], [n]: ") == 'y':
  87.     x = float(raw_input("Enter x coordinate from openStreetMap: "))
  88.     print "Coordinate approved! %s" % (x)
  89.     y = float(raw_input("Enter y coordinate from openStreetMap: "))
  90.     print "Coordinate approved! %s" % (y)
  91.     # adding the nearest node from point
  92.     exit_nodes.append(ox.get_nearest_node(G, (x, y)))
  93.  
  94. print exit_nodes
  95.  
  96. # Randomly selected exit nodes based on number of outcoimng edges
  97. # for x in G.nodes():
  98. #   if nx.degree(G,x) == 2:
  99. #       exit_nodes.append(x)
  100.  
  101.  
  102. # cords for start desitantion
  103. x, y = (39.74356, -105.52440)
  104. starting_node = ox.get_nearest_node(G, (x, y))
  105.  
  106. # Drawing and random exit node
  107.  
  108. print "Starting node id: ", starting_node
  109. destination_node = choice(exit_nodes)
  110. print "Destination node id: ", destination_node
  111.  
  112. # printing route based on wieght
  113. print "Path length: %s km!" % (nx.shortest_path_length(G, starting_node, destination_node, weight='length') / 1000)
  114.  
  115. # calculation path times
  116. path_times = r1.calculatePathTime(G, r1.returnRoute(G, starting_node, destination_node), starting_node,
  117.                                   destination_node)
  118.  
  119. # adding best nodes
  120. best_nodes = []
  121. route = r1.returnRoute(G, starting_node, destination_node)
  122. valid_nodes = [1410414960,176490034]
  123.  
  124.  
  125. for m in G.nodes():
  126.     if m in r1.returnRoute(G, starting_node, destination_node) or m in valid_nodes:
  127.         continue
  128.     else:
  129.         for n in range(0, len(r1.returnRoute(G, starting_node, destination_node))):
  130.             if p1.pathTime(G, m, route[n] ) <= path_times[n]:
  131.                 best_nodes.append((m, route[n]))
  132.  
  133. for b in best_nodes:
  134.     r1.ShowShortestPath(G, starting_node,b[1])
  135.     p1.ShowShortestPath(G, b[0], b[1],1)
  136.  
  137. $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  138.  
  139. from Car import Car
  140. import osmnx as ox, networkx as nx, geopandas as gpd
  141. import matplotlib.pyplot as plt
  142. from shapely.geometry import Point
  143. from descartes import PolygonPatch
  144.  
  145. ox.config(log_file=True, log_console=True, use_cache=True)
  146.  
  147. c = raw_input("Based on bounding box([y],[n]): ")
  148. if c == "y":
  149.     # creating bounding box
  150.     north, south, east, west = 39.7583, 39.7275, -105.4652, -105.5410
  151.     G = ox.graph_from_bbox(north, south, east, west, network_type='drive')
  152. else:  # creating grapg based on place
  153.     place = "Idaho Springs, USA"
  154.     # place = raw_input("Enter place: " )
  155.     G = ox.graph_from_place(place, network_type='drive')
  156.  
  157. # creating new robber car!
  158. r1 = Car(10, 0.58)
  159.  
  160. print "Enter https://www.openstreetmap.org/#map=17/39.74302/-105.52421 \n"
  161. # Bank Robbery problem: A
  162. x, y = (39.74356, -105.52440)
  163. # x = float(raw_input("Enter x coordinate from openStreetMap: "))
  164. print "Coordinate approved! %s" % (x)
  165. # y = float(raw_input("Enter y coordinate from openStreetMap: "))
  166. print "Coordinate approved! %s" % (y)
  167.  
  168. # minutes from begigng
  169. list_of_ints = list(range(1, 24, 4))
  170. escape_time = list_of_ints
  171.  
  172. # setting starting node
  173. starting_node = ox.get_nearest_node(G, (x, y))
  174.  
  175. # setting robber ar travelling speed
  176. meters_per_minute = r1.car_speed * 1000 / 60
  177. for u, v, k, data in G.edges(data=True, keys=True):
  178.     data['time'] = data['length'] / meters_per_minute
  179.  
  180. # get one color for each isochrone
  181. iso_colors = ox.get_colors(n=len(escape_time), cmap='GnBu', start=0.3, return_hex=True)
  182.  
  183. # make the isochrone polygons
  184. isochrone_polys = []
  185. for trip_time in sorted(escape_time, reverse=True):
  186.     subgraph = nx.ego_graph(G, starting_node, radius=trip_time, distance='time')
  187.     node_points = [Point((data['x'], data['y'])) for node, data in subgraph.nodes(data=True)]
  188.     bounding_poly = gpd.GeoSeries(node_points).unary_union.convex_hull
  189.     isochrone_polys.append(bounding_poly)
  190.  
  191. # plot the network then add isochrones as colored descartes polygon patches
  192. fig, ax = ox.plot_graph(G, fig_height=8, show=False, close=False, edge_color='k', edge_alpha=0.2, node_color='none')
  193. for polygon, fc in zip(isochrone_polys, iso_colors):
  194.     patch = PolygonPatch(polygon, fc=fc, ec='none', alpha=0.6, zorder=-1)
  195.     ax.add_patch(patch)
  196. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement