I have a large dataset (about 50,000,000 rows) of GPS x,y points related to GPS equipped taxis with timestamps and speeds. I also have a shp. of the OSM links and I want to find the nearest link to each GPS point in order to calculate average and stdev of speeds for each link at 5-min intervals.
I am using the following code in Spyder to check the results for the first row of my GPS x,y point dataset and it returns the result (OSM link ID) correctly.
subset_point = points_gdf.head(1).copy()
subset_point.crs = 'EPSG:4326'
subset_point['geometry'] = gpd.points_from_xy(subset_point['lon'], subset_point['lat'])
def find_nearest_link(point, links, search_radius=0.00001):
nearest_link = links.loc[links.geometry.apply(lambda geom: point.distance(geom) < search_radius)]
if not nearest_link.empty:
return nearest_link.iloc[0]['osm_id']
else:
return None
subset_point['nearest_Link'] = subset_point['geometry'].apply(lambda point: find_nearest_link(point, osm_links_3))
print(subset_point[['lon', 'lat', 'nearest_Link']])
However, when performing the following code for all my GPS x,y points the procedure becomes very slow:
def find_nearest_link(point, links, search_radius=0.00001):
nearest_link = links.loc[links.geometry.apply(lambda geom: point.distance(geom) < search_radius)]
if not nearest_link.empty:
return nearest_link.iloc[0]['osm_id']
else:
return None
points_gdf['nearest_Link'] = points_gdf['geometry'].progress_apply(lambda point: find_nearest_link(point, osm_links_3))
print(points_gdf[['lon', 'lat', 'nearest_Link']]).
Should I use another method? Is there something more appropriate given the length of my dataset?