Improving performance when using get_nearest_edges repeatedly
When calling get_nearest_edges it "creates equally distanced points along the edges of the network" according to the doc. The time this operation takes varies depending on the size of the input graph. We would like to be able to use larger graphs and repeatedly call get_nearest_edges without the penalty of having to create the equally distanced points each time. We would like to see the graph preparation decoupled from the find operation, which should allow subsequent calls to execute much quicker since the graph has already been prepared.
Can you provide a complete minimal working example to reproduce your use case?
I uploaded a notebook here that should give you an idea what we're doing. This was a POC we put together in the Docker container. Ideally we'd like to load a large graph, say all of NYC, then run data through that graph repeatedly as it gets uploaded to us. The notebook doesn't exactly simulate that, it was just a POC after all, but if you use the small fixed area graph with different batch sizes, you can see that it takes longer overall to process the same amount of data in smaller batches. We think that is because each time we call get_nearest_edges it has to pre-process the graph to add the nodes it uses during the matching process, and it doesn't seem like there's a way to persist the graph in that state for subsequent calls.
This seems like an edge case, so I don't think it'll be added as a feature to OSMnx. The purpose of the get_nearest_edges is to quickly find the nearest edges in a reference graph to an arbitrarily large set of points. Every time that reference graph changes, you'll have to re-interpolate edge geometries and reconstruct a spatial index (kd tree or ball tree).
But you can already largely avoid the former (edge geometry interpolation) by setting a large value as the dist parameter: for example, if you're working with a projected graph, you might set dist=1000 to only distribute points in edges with length > 1km. But you cannot avoid the latter (a spatial index is needed, unless you intend to use the slow brute-force None method). You might want to pursue a custom optimization or function on your end.
See the docs for details and also see related issue #563.
Ok, that makes sense, but I'm not sure I understand about the dist parameter. The doc says it's the spacing length along edges and the units are the same as the graph’s geometries. In our case we're using lat/lng so wouldn't the units be in degrees so something like .0001 space the points at 11.1 meters and .001 at 111 meters? That's why I didn't change it from the 0.0001 default value because I thought it would use 11.1 meter spacing which is what we wanted. What happens if we make it much larger, say 1000? I'm guessing no intermediate points are created but what does that mean for the matching algorithm?
If you project your graph, you can work in meaningful consistent units like meters. Like it says in the docs: "Units are the same as the graph’s geometries."
Note that this functionality has been replaced with the new, faster, streamlined nearest_edges function as of OSMnx v1.1.0. See the function's documentation and #675.