plditallo
plditallo

Reputation: 701

The osmnx call graph_from_bbox() reports that it takes 1 positional argument, however, help(ox.graph.graph_from_bbox) appears to expect 4

I am writing a simple test driver to generate an html file, displaying two possible, real-world routes for a very limited area. When I run the python script, I get what I thought would be an easy error to resolve: "line 12, in <module> graph = ox.graph.graph_from_bbox(*bbox, network_type="drive") TypeError: graph_from_bbox() takes 1 positional argument but 4 positional arguments (and 1 keyword-only argument) were given".

Here is the code that uses the function:

import osmnx as ox
import networkx as nx
import folium

# Configure timeout for HTTP requests
ox.settings.timeout = 180  # Set timeout to 180 seconds

# Define the bounding box as (north, south, east, west)
bbox = (37.5, 32.0, -94.0, -104.0)  # Approximate bounding box for OK, TX, KS

# Create the graph using the bounding box
graph = ox.graph.graph_from_bbox(*bbox, network_type="drive")

# Define start and end points (Oklahoma City to Dallas)
start_point = (35.4676, -97.5164)  # Oklahoma City
end_point = (32.7767, -96.7970)    # Dallas

# Find the nearest nodes in the road network
start_node = ox.distance.nearest_nodes(graph, X=start_point[1], Y=start_point[0])
end_node = ox.distance.nearest_nodes(graph, X=end_point[1], Y=end_point[0])

# Calculate the shortest path using Dijkstra's algorithm
shortest_path = nx.shortest_path(graph, source=start_node, target=end_node, weight='length')

# Create a map centered between the start and end points
route_map = folium.Map(location=[(start_point[0] + end_point[0]) / 2, (start_point[1] + end_point[1]) / 2], zoom_start=7)

# Extract route geometry and plot it on the map
route_coords = [(graph.nodes[node]['y'], graph.nodes[node]['x']) for node in shortest_path]
folium.PolyLine(route_coords, color="blue", weight=5, opacity=0.8).add_to(route_map)

# Add markers for start and end points
folium.Marker(location=start_point, popup="Start: Oklahoma City").add_to(route_map)
folium.Marker(location=end_point, popup="End: Dallas").add_to(route_map)

# Save the map to an HTML file
route_map.save("real_world_route_map.html")

Since it is my first time working with osmnx, I thought I'd check the details of what the function expects by typing: help(ox.graph.graph_from_bbox) This is the usage text I see:

This function uses filters to query the Overpass API: you can either
specify a pre-defined `network_type` or provide your own `custom_filter`
with Overpass QL.

Use the `settings` module's `useful_tags_node` and `useful_tags_way`
settings to configure which OSM node/way tags are added as graph node/edge
attributes. You can also use the `settings` module to retrieve a snapshot
of historical OSM data as of a certain date, or to configure the Overpass
server timeout, memory allocation, and other custom settings.

Parameters
----------
bbox
    Bounding box as `(left, bottom, right, top)`. Coordinates should be in
    unprojected latitude-longitude degrees (EPSG:4326).
network_type
    {"all", "all_public", "bike", "drive", "drive_service", "walk"}
    What type of street network to retrieve if `custom_filter` is None.
simplify
    If True, simplify graph topology via the `simplify_graph` function.
retain_all
    If True, return the entire graph even if it is not connected. If
If
    False, retain only the largest weakly connected component.
truncate_by_edge
    If True, retain nodes outside bounding box if at least one of node's
    neighbors is within the bounding box.
custom_filter
    A custom ways filter to be used instead of the `network_type` presets,
    e.g. `'["power"~"line"]' or '["highway"~"motorway|trunk"]'`. If `str`,
    the intersection of keys/values will be used, e.g., `'[maxspeed=50][lanes=2]'`
    will return all ways having both maxspeed of 50 and two lanes. If
    `list`, the union of the `list` items will be used, e.g.,
    `['[maxspeed=50]', '[lanes=2]']` will return all ways having either
    maximum speed of 50 or two lanes. Also pass in a `network_type` that
    is in `settings.bidirectional_network_types` if you want the graph to
    be fully bidirectional.
    
    Returns
-------
    G

The error I received appears to say there is only 1 positional, instead of the 4 the function help indicates. What have I missed?

Upvotes: 0

Views: 217

Answers (1)

Nick ODell
Nick ODell

Reputation: 25409

The error I received appears to say there is only 1 positional, instead of the 4 the function help indicates. What have I missed?

The function help is saying that the bbox argument should be given as a single tuple argument of four numbers, rather than four separate arguments.

Example:

# Define the bounding box as (north, south, east, west)
bbox = (37.5, 32.0, -94.0, -104.0)  # Approximate bounding box for OK, TX, KS

# Create the graph using the bounding box
graph = ox.graph.graph_from_bbox(bbox, network_type="drive")

You may see code examples on the internet that use *bbox. This is because versions of osmnx prior to 2.0 allowed users to pass this as four separate arguments.

If you want to be compatible with both 2.0 and pre-2.0 versions of osmnx, you could also use

graph = ox.graph.graph_from_bbox(bbox=bbox, network_type="drive")

Upvotes: 2

Related Questions