Implementation:CARLA simulator Carla Python OSM2ODR Bindings
| Knowledge Sources | |
|---|---|
| Domains | Python Bindings, Map Conversion |
| Last Updated | 2026-02-15 05:00 GMT |
Overview
Boost.Python binding file that exposes the OSM2ODR converter for converting OpenStreetMap data to OpenDRIVE format within the CARLA Python API.
Description
This file defines the export_osm2odr() function, conditionally compiled when CARLA_PYTHON_API_HAS_OSM2ODR is defined. It exposes Osm2OdrSettings with configurable properties: use_offsets and offset_x/offset_y for coordinate offsets, default_lane_width for lane sizing, elevation_layer_height for elevation, proj_string for map projection, center_map for centering, generate_traffic_lights and all_junctions_with_traffic_lights for traffic light generation, and methods set_osm_way_types and set_traffic_light_excluded_way_types for filtering road types. The Osm2Odr class provides a static convert method that takes an OSM file string and optional settings, returning an OpenDRIVE string.
Usage
This binding is used to convert real-world OpenStreetMap road data into OpenDRIVE format that CARLA can load as a simulation world via client.generate_opendrive_world().
Code Reference
Source Location
- Repository: CARLA
- File: PythonAPI/carla/src/OSM2ODR.cpp
Signature
void export_osm2odr();
// Key classes exposed:
class_<OSM2ODRSettings>("Osm2OdrSettings", init<>())
class_<OSM2ODR>("Osm2Odr", no_init)
.def("convert", &ConvertOSMToOpenDRIVE, (arg("osm_file"), arg("settings") = OSM2ODRSettings()))
.staticmethod("convert")
Import
import carla
settings = carla.Osm2OdrSettings()
odr_string = carla.Osm2Odr.convert(osm_data, settings)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| osm_file | str | Yes | OpenStreetMap XML data as a string |
| settings | carla.Osm2OdrSettings | No | Conversion settings (default settings used if omitted) |
Outputs
| Name | Type | Description |
|---|---|---|
| opendrive_string | str | Generated OpenDRIVE XML data as a string |
Usage Examples
import carla
# Read OSM file
with open('map.osm', 'r') as f:
osm_data = f.read()
# Configure conversion
settings = carla.Osm2OdrSettings()
settings.use_offsets = True
settings.default_lane_width = 3.5
settings.generate_traffic_lights = True
# Convert and load
odr = carla.Osm2Odr.convert(osm_data, settings)
client = carla.Client('localhost', 2000)
client.generate_opendrive_world(odr)