Road types and Speed limits in Nigeria

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)

Have you seen Downloading data - OpenStreetMap Wiki ?

can you share the download part of the script?

1 Like

Thank you very much for this suggestion. I will try that.

Step1:
Download Nigeria OSM data

  • Geofabrik Download Server
    • for research , you can use some timestamped data , see “raw directory index”
      • example: https://download.geofabrik.de/africa/nigeria-231001.osm.pbf
    • the “nigeria-latest” is changing every day!

Step2:

Step3:
Create some python script - calculate some research data.

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
def return_road_type_smnx(driver_type:str,lat,lon):
    road_type=get_road_type(lon, lat)
    if driver_type == 'pud':
        if road_type == 'primary':
            speed_limit = 90
        elif road_type == 'secondary':
            speed_limit = 60
        elif road_type == 'tertiary':
            speed_limit = 60
        elif road_type == 'residential':
            speed_limit = 30
        elif road_type == 'unclassified':
            speed_limit = 60
        elif road_type == 'service':
            speed_limit = 40
        elif road_type == 'trunk':
            speed_limit = 90
        elif road_type == 'primary_link':
            speed_limit = 90
        elif road_type == 'trunk_link':
            speed_limit = 90
        else:
            print('invalid road type')

    elif driver_type=='prd':
        if road_type == 'primary':
            speed_limit = 100
        elif road_type == 'secondary':
            speed_limit = 80
        elif road_type == 'tertiary':
            speed_limit = 60
        elif road_type == 'residential':
            speed_limit = 30
        elif road_type == 'unclassified':
            speed_limit = 90
        elif road_type == 'service':
            speed_limit = 40
        elif road_type == 'trunk':
            speed_limit = 100
        elif road_type == 'primary_link':
            speed_limit = 100
        elif road_type == 'trunk_link':
            speed_limit = 100
        else:
            # print the closest road type
            print('Not Known to us',road_type)
    else:
        print(driver_type+'-Driver type not known')
    # print(speed_limit)
    
    return speed_limit, road_type
1 Like

I tried this firs

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']}.")

before I resorted to using the road type

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).

Their response is very promising.

Okay great! Hopefully, they are able to have a work around it.