I'm struggling with a rather odd problem with the python package OSMNx.

I have lots of GPS points in a pandas dataframe that were originally in EPSG 4326, but I've already projected them onto EPSG 32633 (UTM zone 33N). My aim is it to find the nearest OSM edge. For the sake of an example, let's look at the point (388860.7103015079, 5828739.389188443). One can look at this point here (might not work - maybe you have to enter the point manually).

Now I am using OSMNx and its get_nearest_edge() (and get_nearest_edges(), respectively) to find the nearest OSM edge. As the graph input, I use a rather small bounding box including the coordinates, as retrieved by, e.g.,

G = osmnx.graph.graph_from_bbox(52.607584, 52.573916, 13.357185999999, 13.424399999999999, network_type='drive', simplify = False),

but I guess it should also work, if you'd get the whole Berlin map.

G = osmnx.graph_from_place("Berlin, Germany", network_type="drive", simplify = False.

Normally, I have a whole pandas data frame of points and I run

osmnx.get_nearest_edges(G, df["lon_proj"], df["lat_proj"], method="kdtree", dist=0.0001)

For example's sake, let's run instead

osmnx.get_nearest_edge((388860.7103015079, 5828739.389188443))

Using the latter command, I end up with a tuple of OSM nodes specifying the edge I should be looking for: (1873509499, 732609150, 0). Now there are two problems. First, the OSM edge depends on the graph I am giving to that function (but note that both graphs G stated above contain the example point). Second, the nodes attained by this output are not really close to the original point (compare here).

Why won't the function give me the actual nearest edge, even though there are nodes much more close-by than the ones I am attaining? In the picture below you can see the node that is my desired output (orange) and the node attained by get_nearest_edge().

Edit: I figured that apparently osmnx.get_nearest_edge() takes a tuple of (lat, lon) coordinates (so latitude goes first), while osmnx.get_nearest_edges() does it the other way round. Now using the former works, but using the latter still gives super random results. Perhaps you guys have an idea about this.

enter image description here

  • 1
    Your nodes may be the closest in the space you were computing the distance in... So said, double check every thing is in the same Cartesian space (the better space I know for an easy computation of distances so far). Maybe this can help. – s.k Jan 18 at 6:03

The two problems are:

  • Must send (y, x) coordinates to the get_nearest_edge() method.
  • Must project the graph.
import osmnx

G = osmnx.graph_from_place("Berlin, Germany", network_type="drive", simplify = False)

P = osmnx.projection.project_graph(G)

nearedge = osmnx.get_nearest_edge(P, (5828739.389188443, 388860.7103015079), return_dist=True)


#(873725577, 1773988272, 0, 50.85667253241846)

For the get_nearest_edges() method, don't use a 0.0001 distance, because it is expressed in degrees, use something like 100.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.