Build Scenes from Custom Data Using RoadRunner HD Map (Python version)

31 views (last 30 days)
The link above shows how to generate RoadRunner HD Map files using custom data, along with example code written in C++. I want to generate RoadRunner HD Map files using my own vector data based on this method. However, I don't know anything about C++, and the installation and execution process is too complicated, so I have failed. I would like to ask if it is possible to get an example code written in Python.
  3 Comments
cheng
cheng on 28 Apr 2025
Hello Haelee,
I followed the official tutorial to compile the protocol buffer files into a Python API and attempted to reimplement the C++ example in Python. However, I encountered an issue in the last step. When I tried to open the serialized rrhd file in RoadRunner, no content was displayed.
After preparing the HDMap and MapHeader information, I wasn't sure how to serialize it, save it to an rrhd file, and store it on disk. The C++ serialization code in the example looks like this:
void WriteToRRHD(const string &filepath,
const google::protobuf::MessageLite &headerMessage,
const google::protobuf::MessageLite &HDMap)
{
// Open the file as output binary
fstream fileStream(filepath, ios::out | ios::binary);
// Write the delimited header message to the buffer
if (!google::protobuf::util::SerializeDelimitedToOstream(
headerMessage, &fileStream))
cerr << "Error writing the header message" << endl;
// Write the RoadRunner HD Map message to buffer
if (!HDMap.SerializeToOstream(&fileStream))
cerr << "Error writing the RoadRunner HD Map message" << endl;
}
How can I convert this code to Python?
Haelee
Haelee on 29 Apr 2025
Hi, @cheng.
I should admit that I'm not the best person to address your issue. How about posting a new question on MATLAB Answers? This will expose your query to other experts.

Sign in to comment.

Answers (1)

Harsh
Harsh on 24 Jul 2025
To create RRHD using a python script, I would recommend you to start with copying the "\RoadRunner R20xxVer\bin\win64\Proto\mathworks" folder to a separate location, after which you can proceed to excute the following commands in the directory which consists of the "mathworks" folder (this is important). Please note that I am using Ubuntu 22.04 here,
$ pip install "protobuf<=3.20.3"
$ protoc --python_out=. mathworks\scenario\common\*.proto
$ protoc --python_out=. mathworks\scenario\scene\hd\*.proto
After executing these commands some additional files should appear in the folders containing the protos of scene and common scenario, here's what your folder should consist of if the above commands are executed successfully.
% Mode LastWriteTime Length Name
% ---- ------------- ------ ----
% d----- 7/24/2025 12:30 PM __pycache__
% ------ 7/1/2024 8:09 PM 1392 hd_stencil_markings.proto
% ------ 7/24/2025 12:08 PM 6965 hd_static_objects_pb2.py
% ------ 7/1/2024 8:09 PM 832 hd_lane_markings.proto
% ------ 7/1/2024 8:09 PM 992 hd_junctions.proto
% ------ 7/1/2024 8:09 PM 5085 hd_lanes.proto
% ------ 7/24/2025 12:08 PM 3951 hd_junctions_pb2.py
% ------ 7/24/2025 12:08 PM 14365 hd_map_pb2.py
% ------ 7/1/2024 8:09 PM 2173 common_attributes.proto
% ------ 7/1/2024 8:09 PM 1264 hd_map_header.proto
% ------ 7/24/2025 11:58 AM 7978 sample_map.py
% ------ 7/24/2025 12:08 PM 29590 hd_lanes_pb2.py
% ------ 7/1/2024 8:09 PM 1344 hd_curve_markings.proto
% ------ 7/24/2025 12:08 PM 7910 hd_curve_markings_pb2.py
% ------ 7/1/2024 8:09 PM 1049 hd_signs.proto
% ------ 7/1/2024 8:09 PM 1214 hd_barriers.proto
% ------ 7/24/2025 12:08 PM 10698 common_attributes_pb2.py
% ------ 7/24/2025 12:08 PM 5121 hd_map_header_pb2.py
% ------ 7/1/2024 8:09 PM 1298 hd_static_objects.proto
% ------ 7/24/2025 12:08 PM 6651 hd_signs_pb2.py
% ------ 7/24/2025 12:08 PM 5279 hd_lane_markings_pb2.py
% ------ 7/24/2025 12:08 PM 7280 hd_barriers_pb2.py
% ------ 7/1/2024 8:09 PM 1480 hd_map.proto
% ------ 7/24/2025 12:08 PM 7043 hd_stencil_markings_pb2.py
Once you get these files, you can simply import them in your python script and create a sample RRHD, similar to how it was done using cpp.
from mathworks.scenario.scene.hd import hd_map_pb2
from mathworks.scenario.scene.hd import hd_map_header_pb2
def write_rrhd(filepath, header_message, hdmap):
with open(filepath, "wb") as f:
f.write(header_message.SerializeToString())
f.write(hdmap.SerializeToString())
def main():
mymodel = hd_map_pb2.HDMap()
# add lanes and lane boundaries
headermessage = hd_map_header_pb2.Header()
# add bounds and projection
filepath = "example.rrhd"
write_rrhd(filepath, headermessage, mymodel)
if __name__ == "__main__":
main()
You can refer to the above script to start by creating a blank ".rrhd" file.
I hope this helps, thanks!

Products


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!