required packages:
- pyrosm (load and process pbf map)
- ipyleaflet (display and interactivemap interface)
- osmnx (convert to graph and find shortest path)
- geopandas (for geodata)
- mapclassify (for classification)
- igraph (for generating the graph)
installing pyrosm: conda install -c conda-forge pyrosm
installing ipyleaflet: conda install -c conda-forge ipyleaflet
installing osmnx: conda install osmnx
installing geopandas pip install geopandas
installing mapclassify pip install mapclassify
installing igraph pip install igraph
First load tehran osm.pbf file which we use it to access the maps network(nodes and roads) with osm.get_network(nodes=True,...). Convert osm map to networkx graph using osm.to_ghraph(). Then locate the start and end markers and get the X,Y coordinate to calculate the nearest nodes for them using osmnx.distance.nearest_nodes(). For calculating the shortest path use the networkx library networkx.shortest_path(). OSMnx generate the list of nodes for the shortest path. Access the X,Y coordinate of each nodes in graph using graph.nodes['each_node']['X' or 'Y']. After that draw the nodes as polylines with ipyleaflets PolyLine() function.
For the logic of not showing shortest paths repeatedly when changing marker locations, define line variable with None value a first (which is going to contain PolyLine() func output) and we delete the previous line variable every time we want to create a new one.
First load the tehran roads network using OSM and get network using osm.get_network(nodes = True,...). Then get the buildings and universities locations using osm.get_buldings() and osm.get_pois(). In osm.get_pois() use custom filters to get universities locations. Convert the osm to graph using osm.to_graph(node , edge) and get nodes using get_igraph_nodes(G). Then set the universities variable's (geopandas geodataframe) "geometry" column (which contains original polygon geometries like boundaries or shapes) into the exact polygons centroid point object. Then find the nearest node-id to each university using find_nearest_nodeids(nodes, universities) and store them in src_ids. Then iterate over all unversities to calculate the path lengths of university to all nodes with Dijkstra algorithm using G.shortest_paths_dijkstra(source=src_id,...)(we can atually dont use the dijkstra and just do it with modules own algorithm) & attach the distances to the node-id with creating a new column for nodes data. Then find the center location of each building and set to centroid column in buildings variable(geopands) and after that, set the active geometry to the centroid using buildings.set_geometry("centroid") so any operations will be performed on the centroid points and not the full polygon shapes. Then store the closest node and its distance to each building using nearest_neighbor(buildings, nodes, return_dist=True). Then merge the nodes ["distance_to_closest", "node_id"] columns to bulidings dataframe & store it in "access" dataframe which is going to contain buildings information along with the complete travel distance from each building to universities. Then classify the distance to 8 levels with width of 1 using mapclassify module. At the end use matplootlib module to show the graph.


