0

I have DataFrame contain two columns (latitude, longitude ) for 30000 points as follows:

enter image description here

I need to obtain the start node and end node of the nearest edge to each point.

I used the following code for a sample (contain 5 points only) using a method from osmnx library (https://osmnx.readthedocs.io/en/stable/search.html?q=get_nearest_edge&check_keywords=yes&area=default#) :

def find_nearest_edges(row):
    near_edge=ox.get_nearest_edge(G,(row['LATITUDE'],row['LONGITUDE']))
    start=intr_stp_nodes[1]
    end=intr_stp_nodes[2]
    return pd.Series([start,  end])
sample_df[['start','end']]=sample_df.apply(find_nearest_edges,axis=1)

Although I got the Resulting data frame, it took a lot of time to compute 5 points:

The resulting data frame :

enter image description here

I tried to use the @gboeing recommendation and create the next function:

def find_nearest_edges(row): 
    shp,start,end=ox.get_nearest_edges(G,row['LONGITUDE'],row['LATITUDE'],method='kdtree',dist=0.0001) 
    return pd.Series([start, end]) 

sample_df[['start','end']]=sample_df.apply(find_nearest_edges,axis=1)

I applied the previous function but it takes a lot of time without any result.

Ahmad Senousi
  • 613
  • 2
  • 12
  • 24

1 Answers1

1

You can use ox.get_nearest_edges with a kdtree for fast lookup of nearest edges to a set of xy points: https://osmnx.readthedocs.io/en/stable/osmnx.html#osmnx.utils.get_nearest_edges

gboeing
  • 5,691
  • 2
  • 15
  • 41
  • Without a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) it's hard to provide further suggestions. – gboeing Jun 11 '19 at 14:32