How can I speed up the routing process in OSMNX?
Say I have two taxi orders with Origin1、Destination1 and Origin2、Destination2(O1,O2,D1,D2). I want to calculate the possibility of ridesharing, so I need the path between two different points. And, here's my code:
def path_time(point1, point2):
path = ox.distance.shortest_path(road, point1, point2, weight='travel_time')
if path is None:
#If there isn't a path, a big weight will be set, and it won't be selected during the matching process.
route_time = 9999
else:
route_time = int(sum(ox.utils_graph.get_route_edge_attributes(road, path, "travel_time")))
return route_time,path
Since there is four points, I need to do this six times, where tp means travel path :
tpO1O2 = path_time(O1,O2)
tpO1D1 = path_time(O1,D1)
tpO1D2 = path_time(O1,D2)
tpO2D1 = path_time(O2,D1)
tpO2D2 = path_time(O2,D2)
tpD1D2 = path_time(D1,D2)
It's okay if I only have two points, but I got a 2 million order set, and each order has hundreds of potential matched orders. So this will take me a lot of time.
Does anyone knows how can I speed up this process? Thank you!
do you know?
how many words do you know
See also questions close to this topic
-
Python File Tagging System does not retrieve nested dictionaries in dictionary
I am building a file tagging system using Python. The idea is simple. Given a directory of files (and files within subdirectories), I want to filter them out using a filter input and tag those files with a word or a phrase.
If I got the following contents in my current directory:
data/ budget.xls world_building_budget.txt a.txt b.exe hello_world.dat world_builder.spec
and I execute the following command in the shell:
py -3 tag_tool.py -filter=world -tag="World-Building Tool"
My output will be:
These files were tagged with "World-Building Tool": data/ world_building_budget.txt hello_world.dat world_builder.spec
My current output isn't exactly like this but basically, I am converting all files and files within subdirectories into a single dictionary like this:
def fs_tree_to_dict(path_): file_token = '' for root, dirs, files in os.walk(path_): tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in dirs} tree.update({f: file_token for f in files}) return tree
Right now, my dictionary looks like this:
key:''
.In the following function, I am turning the empty values
''
into empty lists (to hold my tags):def empty_str_to_list(d): for k,v in d.items(): if v == '': d[k] = [] elif isinstance(v, dict): empty_str_to_list(v)
When I run my entire code, this is my output:
hello_world.dat ['World-Building Tool'] world_builder.spec ['World-Building Tool']
But it does not see
data/world_building_budget.txt
. This is the full dictionary:{'data': {'world_building_budget.txt': []}, 'a.txt': [], 'hello_world.dat': [], 'b.exe': [], 'world_builder.spec': []}
This is my full code:
import os, argparse def fs_tree_to_dict(path_): file_token = '' for root, dirs, files in os.walk(path_): tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in dirs} tree.update({f: file_token for f in files}) return tree def empty_str_to_list(d): for k, v in d.items(): if v == '': d[k] = [] elif isinstance(v, dict): empty_str_to_list(v) parser = argparse.ArgumentParser(description="Just an example", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("--filter", action="store", help="keyword to filter files") parser.add_argument("--tag", action="store", help="a tag phrase to attach to a file") parser.add_argument("--get_tagged", action="store", help="retrieve files matching an existing tag") args = parser.parse_args() filter = args.filter tag = args.tag get_tagged = args.get_tagged current_dir = os.getcwd() files_dict = fs_tree_to_dict(current_dir) empty_str_to_list(files_dict) for k, v in files_dict.items(): if filter in k: if v == []: v.append(tag) print(k, v) elif isinstance(v, dict): empty_str_to_list(v) if get_tagged in v: print(k, v)
-
Actaully i am working on a project and in it, it is showing no module name pip_internal plz help me for the same. I am using pycharm(conda interpreter
File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\Scripts\pip.exe\__main__.py", line 4, in <module> File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\__init__.py", line 4, in <module> from pip_internal.utils import _log
I am using pycharm with conda interpreter.
-
Looping the function if the input is not string
I'm new to python (first of all) I have a homework to do a function about checking if an item exists in a dictionary or not.
inventory = {"apple" : 50, "orange" : 50, "pineapple" : 70, "strawberry" : 30} def check_item(): x = input("Enter the fruit's name: ") if not x.isalpha(): print("Error! You need to type the name of the fruit") elif x in inventory: print("Fruit found:", x) print("Inventory available:", inventory[x],"KG") else: print("Fruit not found") check_item()
I want the function to loop again only if the input written is not string. I've tried to type return Under print("Error! You need to type the name of the fruit") but didn't work. Help
- Efficient way to know region of a coordinate
-
Lighthouse CLI: how to use current browser settings(running with extensions installed)
Try lighthouse CLI to measure performance of URLs with some extensions on.
With
lighthouse https://www.bing.com/search?q=curse+words --output json --output-path D:\lighthouseResults\0506.json --disable-device-emulation --chrome-flags="--user-agent=edge --window-size=360,640 --enable-experimental-extension-apis"
, it doesn't seem that the extension was working(usually takes a few seconds, very slow), and did not find http requests that were suppose to be there in the report.How should I do it in Lighthouse or I'm looking at the wrong tool?
-
How to properly plot a tree (27k nodes) using a circular tree / leave layout
I have this (unbalanced) tree with 27k+ nodes. It is an hierachy. Now I would to plot it as such to obtain a plot something like this (no idea how you would describe it, but I would call it a circular leave tree...?
However, I am unable to achieve such a result unfortunately. I have already tried
igraph
,Networkx
,Maltego
,Graphviz
,Gephi
. Hopefully someone can help me / give me tips & tricks or hints.Maltego
Gives me quiet easily the following. However I cannot export it to pdf. Furthermore it has this wierd expansion on the top going to the left. I would be able to 'manually' (minimize) move it. However that is not what I would like.
This is btw in my view (with the manual fix) the best result. But I cannot export it to vector or high-res image.
igraph
import igraph as ig bigGraphAsTupleList = (('a','b'),('b','c'),('b','d'), ..., ('c','e')) g = ig.Graph.TupleList(bigGraphAsTupleList) layout = g.layout("rt_circular") #fr (fruchterman reingold), tree, circle, rt_circular (reingold_tilford_circular) # bbox = size of picture ig.plot(g,layout=layout,bbox=(10000,10000),target='mygraph.png')
This gives me something like below.
fruchterman reingold (so much overlap of nodes and connections)
Networkx
import networkx as nx import matplotlib.pyplot as plt G = nx.Graph() G.add_edge(...) #build graph nx.draw_circular(G) #nx.draw_spring(G) #nx.draw_spectral plt.draw() plt.show()
Graphviz (via Networkx)
Also a wierd result a bit similar as the next one Maltego. However
import networkx as nx import matplotlib.pyplot as plt import pydot from networkx.drawing.nx_pydot import graphviz_layout G = nx.Graph() G.add_edge(...) #build graph pos = graphviz_layout(G, prog="circo") plt.figure(1,figsize=(60,60)) nx.draw(G, pos,node_size=10) plt.show(block=False) plt.savefig("Graph.png", format="PNG")
-
Object and Pointers Graph Representation in Ruby
I am studying how to represent a graph in memory with Objects and Pointers in ruby and cannot find a representation of this anywhere. Can anyone point me in the right direction of how to build this ds?
-
How to set up traffic forwarding so my origin IP address remains the same when calling an external API from Azure or my laptop
I have an Azure App Service and multiple Azure Functions that need to call into a specific external API - let's call it external.api.com. I also frequently run the App Service and Azure Functions locally on my laptop during development/debugging etc.
My issue is that I can only have one IP address whitelisted to use this external API.
What is the best way to set up some kind of Virtual Network (or something else?) in Azure to enable all calls to this external API to have the same origin IP address?
I have seen tutorials on ways to do this from internal Azure services/functions, however I haven't been able to see anywhere that explains how to do this for calls from both internal Azure services/functions AND from external IP addresses (i.e. my laptop).
Thanks
-
What does this error mean and how would I fix it: throw new TypeError('app.use() requires a middleware function')?
I am trying to create a login page and sign up page, my app.js gives me this error, I think it is the last line of this code. I can send you the other components(files) for this express app. I cannot understand what is causing this error.
const express = require('express'); const mongoose = require('mongoose'); // Routes const authRoutes = require('./routes/authRoutes'); const app = express(); // middleware app.use(express.static('public')); app.use((err, req, res, next) => { res.locals.error = err; res.status(err.status); res.render('error'); }); // view engine app.set('view engine', 'ejs'); // database connection const dbURI = '<database, username and password>'; mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex:true }) .then((result) => app.listen(3000)) .catch((err) => console.log(err)); // routes app.get('/', (req, res) => res.render('home')); app.get('/smoothies', (req, res) => res.render('smoothies')); app.use(authRoutes);
authRoutes.js
const { Router } = require('express') const authController = require('./authController.js') const router = Router(); router.get('/signup', authController.signup_get); router.get('/signup', authController.signup_post); router.get('/login', authController.login_get); router.get('/login', authController.login_post); module.export = router;
authController.js
module.exports.signup_get = (req, res) => { res.render('signup'); } module.exports.login_get = (req, res) => { res.render('login'); } module.exports.signup_post = (req, res) => { res.send('signup'); } module.exports.login_post = (req, res) => { res.send('login'); }
-
Return to original OSMnx graph from simplified graph with edge geometries
Is it possible to generate the original OSMnx graph from the simplified graph (which has the edge geometries preserved)?
For instance:
import osmnx as ox place = 'Piedmont, California, USA' G = ox.graph_from_place(place, network_type='drive', simplify=False) G_simple = ox.simplify_graph(G)
G_simple has the original edge geometries of G stored as "geometry" on the simplified edges:
simple_nodes, simple_edges = ox.graph_to_gdfs(G_simple) print(simple_edges.iloc[10].geometry)
# LINESTRING (-122.2429303 37.8205234, -122.2426591 37.8207235, -122.2424827 37.820899, -122.2421775 37.8212363, -122.2420372 37.8214758, -122.2420254 37.8215051, -122.2419343 37.8217305, -122.2418551 37.8218894, -122.2415415 37.8222826)
Would it be possible to generate the original graph G from the simplified one? I have many simplified graphs stored on disk, but unfortunately cannot regenerate the unsimplified graphs, so I need to find a way to "unsimplify" them.
-
How can I find all the points that are a exactly certain distance, say 25km and 50km, from the start node?
I was writing a Python script that asks input coordinates from the user and uses it to get the nearest node as well as finding all the points that are 25km and 50km away from the nearest node, along the network. The network infrastructure here is railway.
In brief, how I went about this is that from the input coordinates, I obtained the nearest node using this method
ox.distance.nearest_nodes
, then I used the coordinates of the nearest node as the source / origin node. While the target(s) / destination nodes were all the other nodes in the graph. I then usednx.shortest_path_length
method to get distances from the source node to every other node on the graph. Then I used an if-condition to determine those points that are likely to be at exactly 25km and 50km respectively from the origin node. I created a range for this because it's almost unlikely to find a node at exactly 25km or 50km.This may have been the best hack from my arsenal. However, I'm finding some issues. First is that I don't get the point at exactly 25km or 50km but rather nodes close to that, because of my filter range. So what happens is that if I'm using a range of let's say 24800m and 25200m, I'll find several number of nodes within this range. They could be along the same path or even adjacent to each other.
Here is my code:
# import the required libraries import networkx as nx import osmnx as ox import requests import matplotlib.cm as cm import matplotlib.colors as colors import folium import warnings %matplotlib inline ox.config(log_console=True) ox.__version__ # filter warnings warnings.simplefilter(action="ignore", category=FutureWarning) warnings.simplefilter(action="ignore", category=PendingDeprecationWarning) warnings.simplefilter(action="ignore", category=UserWarning) ### Getting the railway infrastructure # Let's ask input from user - coordinates #latitude = float(input("Enter latitude: ")) #longitude = float(input("Enter longitude: ")) # graph from input coordinates # default coordinates for testing are 51.5 (lat) and -0.13 (lon) - London latitude = 51.5 longitude = -0.13 G = ox.graph_from_point( (latitude, longitude), retain_all=False, dist=70000, # I believe this also has an impact on speed. Since my ceiling target it 50km, can I put it at 50000? dist_type='network', truncate_by_edge=True, simplify=True, custom_filter='["railway"~"way"]', ) # plotting the graph fig, ax = ox.plot_graph(G, node_size=0, edge_color="w", edge_linewidth=0.2) # finding the node that is closest to the input coordinates gdf_nodes = ox.graph_to_gdfs(G, edges=False) closest_node = ox.distance.nearest_nodes(G, longitude, latitude) # getting the coordinates of the closest node closest_node_lat = gdf_nodes.loc[[closest_node]].y.values[0] closest_node_lon = gdf_nodes.loc[[closest_node]].x.values[0] # calculating the length of shortest distances start_node_coords = (latitude, longitude) orig_node = ox.get_nearest_node(G, (latitude, longitude)) edges = ox.graph_to_gdfs(G, nodes=False, edges=True) z = 1 length_list = [] # let's create a color code for points at x distance green = [] blue = [] red = [] for coordinate in gdf_nodes['geometry']: lat = coordinate.y lon = coordinate.x dest_node = ox.get_nearest_node(G, (lat, lon)) try: length = nx.shortest_path_length(G, source=orig_node, target=dest_node, weight='length') print(length, coordinate, z) length_list.append(length) # let's create a filter that finds if the length is either # 10km, 25km or 50km. We're going to find these points from # our origin node, along all the available paths if length > 9800 and length < 10200: ten_km_coordinates = (lat, lon) green.append(ten_km_coordinates) elif length > 24800 and length < 25200: twenty_five_km_coordinates = (lat, lon) blue.append(twenty_five_km_coordinates) elif length > 49900 and length < 50100: fifty_km_coordinates = (lat, lon) red.append(fifty_km_coordinates) z = z + 1 except nx.exception.NetworkXNoPath: G.remove_nodes_from([dest_node]) print(coordinate) # creating a map element map = folium.Map(location=[closest_node_lat, closest_node_lon], tiles="Stamen Terrain", zoom_start=12) folium.Marker(location=[latitude, longitude], popup="My Input", tooltip="My Input").add_to(map) folium.Marker(location=[closest_node_lat, closest_node_lon], popup="Closest Node", tooltip="Closest Node", icon=folium.Icon(color='green')).add_to(map) for i in range(0, len(green)): folium.Marker([green[i][0], green[i][1]], popup="10KM Away", icon=folium.Icon(color='green')).add_to(map) for i in range(0, len(blue)): folium.Marker([blue[i][0], blue[i][1]], popup="25KM Away", icon=folium.Icon(color='blue')).add_to(map) for i in range(0, len(red)): folium.Marker([red[i][0], red[i][1]], popup="50KM Away", icon=folium.Icon(color='red')).add_to(map) folium.Choropleth(edges.geometry, line_weight=2, line_color='yellow').add_to(map)
Is there a better way I could do this to achieve my objective or atleast improve this code to find the points at exactly 25km and 50km respectively and not the nodes, because for me I'm just finding the nodes? I want to find all the those points at every available path / route from the origin node, not just one path. More like saying, if I travel along this route, where will I be at 25km? What about along this other route? I also realized the speed of this depends on the number of nodes. The more the nodes, the more it will take the script to generate results. I will greatly appreciate any help. Thanks!
-
How to do I get the coordinates of all points 5 kilometers away from a given node along a network in osmnx?
I have identified a node id in osmnx graph (green point on the image). What I need is to create points that are exactly 5 kilometers from the identified node along the network path (yellow path). I have tried isochrones but they give all the nodes and not what I need.
Image Showing the Routes