Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:CARLA simulator Carla GlobalRoutePlanner Init

From Leeroopedia
Knowledge Sources
Domains Graph Theory, Road Networks, Autonomous Driving
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete tool for building a road network graph from a CARLA map provided by the CARLA simulator agents library. The GlobalRoutePlanner constructor accepts a CARLA map and a sampling resolution, then automatically constructs a networkx.DiGraph representing the full road network through a four-phase build pipeline.

Description

When the GlobalRoutePlanner is instantiated, the constructor performs the following sequence:

  1. Stores the carla.Map reference and sampling resolution
  2. Calls _build_topology() to extract and interpolate all lane segments from the map's topology
  3. Calls _build_graph() to convert the topology into a networkx.DiGraph with distance-weighted edges
  4. Calls _find_loose_ends() to repair dangling endpoints that lack outgoing connections
  5. Calls _lane_change_link() to add lateral edges for legal lane-change maneuvers

After construction, the instance holds:

  • self._graph: the networkx.DiGraph with nodes and weighted edges
  • self._topology: the raw topology dictionary with interpolated waypoint paths
  • self._id_map: a mapping from (road_id, section_id, lane_id) to graph node IDs
  • self._road_id_to_edge: a lookup from road identifiers to their corresponding graph edges

The graph is built once and reused for all subsequent trace_route() calls, amortizing the construction cost over many queries.

Usage

Use GlobalRoutePlanner when you need to compute routes between arbitrary locations on a CARLA map. Typical usage is as a dependency of BasicAgent or BehaviorAgent, but it can also be used standalone for route analysis or visualization.

Code Reference

Source Location

  • Repository: CARLA
  • File: PythonAPI/carla/agents/navigation/global_route_planner.py
  • Lines: L24-263

Signature

class GlobalRoutePlanner(object):
    def __init__(self, wmap, sampling_resolution):
        self._sampling_resolution = sampling_resolution
        self._wmap = wmap
        self._topology = []
        self._graph = None
        self._id_map = None
        self._road_id_to_edge = None

        # Build the graph on construction
        self._build_topology()
        self._build_graph()
        self._find_loose_ends()
        self._lane_change_link()

Import

from agents.navigation.global_route_planner import GlobalRoutePlanner

I/O Contract

Inputs

Name Type Required Description
wmap carla.Map Yes The CARLA map instance to build the road graph from. Obtained via world.get_map().
sampling_resolution float Yes Distance in meters between interpolated waypoints along each lane segment. Lower values yield finer graphs. Typical range: 1.0 to 4.0 m. Default in agents: 2.0 m.

Outputs

Name Type Description
(instance) GlobalRoutePlanner A fully constructed route planner with an internal networkx.DiGraph ready for trace_route() queries.

Usage Examples

Basic Example

import carla
from agents.navigation.global_route_planner import GlobalRoutePlanner

# Connect to CARLA
client = carla.Client('localhost', 2000)
world = client.get_world()
carla_map = world.get_map()

# Build the road network graph with 2m resolution
grp = GlobalRoutePlanner(carla_map, sampling_resolution=2.0)

# The graph is now ready for route queries
route = grp.trace_route(
    origin=carla.Location(x=10.0, y=20.0, z=0.0),
    destination=carla.Location(x=200.0, y=-50.0, z=0.0)
)

# Print the route maneuvers
for waypoint, road_option in route:
    print(f"Waypoint: {waypoint.transform.location}, Action: {road_option}")

Reusing Across Multiple Agents

from agents.navigation.basic_agent import BasicAgent
from agents.navigation.global_route_planner import GlobalRoutePlanner

# Build the graph once
carla_map = world.get_map()
shared_grp = GlobalRoutePlanner(carla_map, sampling_resolution=2.0)

# Share with multiple agents to avoid redundant graph construction
agent1 = BasicAgent(vehicle1, grp_inst=shared_grp)
agent2 = BasicAgent(vehicle2, grp_inst=shared_grp)

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment