I am a PhD research student at the University of Aberdeen, working on driving behavior change which aims to use AI and Natural Language Generation (NLG) tools to enable a mobile app generate periodic feedbacks for drivers from their driving data collected during a trip non-western countries, Nigeria as case study.
I have tried to access road types and speed limits for Nigerian roads using a python scripts to querry OSM, but all efforts have failed, as I end up in errors after days and days of running the scripts.
I am using the longitude and latitude of each datapoint, and I have over 400,000 of them.
What I observed was that if I increase the distance, I am able to get the speed limits but at some point, it get lost again, and I fear the even the ones with so much distance from the point may not even be getting the right speed limit.
Does anybody have any suggestions?
Because I was actually thinking of starting a project that would begin the acquisition of road types their corresponding speed limits using a the crowed sourcing model, were an interface is hosted that enables people to enter the name of the road name, road type (Street, street-built-up, expressway, expressway-built-up, highway, highway-built-up), location type (rural or urban), local government and State. I have written an algorithm that would match the road types to the legal speed limits for each kind of road as defined in the High way code in the link here [IX. SPEED LIMITS ON DIFFERENT ROADS AND FOR DIFFERENT VEHICLES - Nigeria Highway Code] IX. SPEED LIMITS ON DIFFERENT ROADS AND FOR DIFFERENT VEHICLES - Nigeria Highway Code)
import osmnx as ox
def get_road_type(longitude: float, latitude: float) -> str:
# code to get road type using longitude and latitude
dist=5000
G = ox.graph_from_point((latitude, longitude),dist, network_type='drive')
edges = ox.distance.nearest_edges(G, longitude, latitude)
G_details=G[edges[0]][edges[1]]
# print(G_details)
return G_details[0]['highway']
import osmnx as ox
# Define the location by latitude and longitude
lat, lon =6.69507, 184100
# G = ox.graph_from_point((lat,lon), dist=2000, network_type='all')
G = ox.graph_from_point((lat,lon), dist=10, network_type='all')
# Get the nearest node to the location
nearest_node = ox.distance.nearest_nodes(G, lon, lat)
# Get the edges that the nearest node is a part of
edges = ox.graph_from_point((lat, lon), dist=100, network_type='drive')
# Loop through the edges and get the speed limit of each
n=0
for u, v, k, data in edges.edges(keys=True, data=True):
if 'maxspeed' in data:
print(f"The speed limit on {data['name']} is {data['maxspeed']} mph.")
else:
print(f"No speed limit information found for {data['name']}.")
Calling G = ox.graph_from_point((latitude, longitude),dist, network_type='drive') for each of them (if I understand code right) will not works well, that is beyond capacity of underlying services.
Thanks for sharing the code!
I contacted authors of that library and suggested to improve their documentation and give better guidance about limits of underlying services. Maybe also provide suggestions for working with data on larger scale (maybe osmx has support for importing extracts for a given country? or planet-scale imports? though I am not familiar with this specific library).