# Build Scenes from Custom Data Using RoadRunner HD Map (Python version)
###### tags:`MathWorks`, `python`, `c++`
###### _Leslie Chiu_
Ref:[Build Scenes from Custom Data Using RoadRunner HD Map(C++ version)](https://www.mathworks.com/help/roadrunner/scenebuilder/build-scenes-from-custom-data-using-roadrunner-hd-map.html)
This example shows how to use RoadRunner HD Map protocol buffers to import custom data into RoadRunner and build scenes. In this example, you:
- Generate a RoadRunner HD Map file from custom data.
- Import the generated file into RoadRunner and preview the RoadRunner HD Map data.
- Build a RoadRunner scene from imported data. To build a scene, you must have a RoadRunner Scene Builder license.
This example uses **_python_** to implement the RoadRunner HD Map using protocol buffers.
## Introduction
RoadRunner HD Map is a road data model for representing high-definition (HD) map data in a RoadRunner scene. This model defines a simple data structure to represent road layouts using lanes, lane boundaries, lane markings, and junctions. The model also defines data structure for other scene elements such as barriers, signs, and different types of static objects. The data in this model is serialized using protocol buffers and saved in a binary format with the .rrhd extension. The RoadRunner HD Map binary file consists of these data sections:
- Varint Header Size — Specifies the size of the header message. For more information, see [Base 128 Varints](https://protobuf.dev/programming-guides/encoding/#varints) in the Google® Protocol Buffers guide.
- Header Message — Specifies information about the file author, map projection, and spatial bounds of the geometric data.
- RoadRunner HD Map Message — Specifies properties for lanes, lane boundaries, lane markings, and junctions.
You can create a RoadRunner HD Map binary file for your custom HD map data using protocol buffer schema files. These schema files describe the RoadRunner HD Map data model. Protocol buffers support various programming languages, so you can choose which language to use when implementing a RoadRunner HD Map. This example uses python.
[Delimiting Protobuf Messages](https://cwiki.apache.org/confluence/display/GEODE/Delimiting+Protobuf+Messages#:~:text=In%20Python%20you%20must%20write%20the%20var-int%20delimiters,in%20the%20buffer%20just%20after%20the%20size%20bytes.):In Python you must write the var-int delimiters yourself or use the internal methods _VarintBytes(size).
## Compile Protocol Buffers for RoadRunner HD Map
Your local installation of RoadRunner contains the protocol buffer schema files at this location, referred to as RoadRunnerProtoPath in this example.
_RRInstallFolder\bin\platform\Proto_
- _RRInstallFolder_ is your local RoadRunner installation folder.
- _platform_ is the folder name for your OS platform.
Inside the Proto folder, the top-level mathworks folder contains the hierarchy of protobuf files. This table shows the protobuf files that you need to compile and their locations.
| Protobuf Files to Compile | Protobuf File Locations |
| ------ | ------ |
| hd_map_header.proto | _RoadRunnerProtoPath\mathworks\scenario\scene\hd_ |
| hd_map.proto | |
| hd_lanes.proto | |
| hd_lane_markings.proto | |
| hd_junctions.proto | |
| hd_barriers.proto | |
| hd_signs.proto | |
| hd_static_objects.proto | |
| common_attributes.proto | |
| geometry.proto | _RoadRunnerProtoPath\mathworks\scenario\common_ |
To compile the protocol buffers using python follow these steps:
1. Install python, and set up the environment. [Python installation](https://docs.python.org/3/using/windows.html)
2. Download and install the protocol compiler protoc (minimum version 3.8.0) and the protobuf python runtime. Follow the instructions at [Github tutorial]( https://github.com/protocolbuffers/protobuf/tree/main/python.)
Open your Command Prompt, run following command.
```sh
$ pip install protobuf
```

(the latest version is 4.23)
3. Run the compiler. Specify file paths for these folders:
```sh
$ protoc [pyi_out] --[output_dir]
```
- pyi_out — Folder that contains the RoadRunner HD Map protobuf files.
- output_dir — Destination directory for the generated code.
For example, use this command to compile hd_map_header.proto, where DestinationPath specifies the destination directory.The current path in cmd would be _RRInstallFolder\bin\platform\Proto_.
```sh
$ protoc ./mathworks/scenario/scene/hd/hd_map_header.proto --python_out=./
```
4. Verify that the compiler creates these files for the .proto file:
- Implementation file with the extension _pb2.py


5. Repeat steps 2 and 3 to compile the other protobuf files.
## Prepare Custom Data
In practice, you must explore the data of the custom scene you are importing to determine its lane attributes. This example uses synthetic data to create a scene consisting of two longitudinally connected lane segments. Each lane has two lane boundaries. This table shows the attributes of these lanes and lane boundaries.
| Attribute | Value for Lane 1 | Value for Lane 2 |
| ------ | ------ | ------ |
|Lane ID| Lane1| Lane2|
|Geometry| [(0.782,-1.56) (50.78,23.43)]| [(50.78,23.43) (100.78,48.43)]|
|Type| Driving| Driving|
|Travel Direction| Forward| Forward|
|Predecessor ID| -| Lane1|
|Predecessor Alignment| -| Forward|
|Successor ID| Lane2| -|
|Successor Alignment| Forward| -|
|Left Lane Boundary ID| LaneBoundary1| LaneBoundary4|
|Left Lane Boundary Geometry| [(0,0) (50,25)]| [(50,25) (100,50)]|
|Right Lane Boundary ID| LaneBoundary2| LaneBoundary3|
|Right Lane Boundary Geometry| [(1.565,-3.13) (51.565,21.864)]| [(51.565,21.864) (101.565,46.869)]|
The example also shows how to represent data for other scene elements such as barriers, signs, trees and buildings.
## Create RoadRunner HD Map Binary File from Custom Data
Write python code to add custom data to the generated RoadRunner HD Map protocol buffer classes, after compilation. Then you would recieve **.rrhd** file in given folder.
<details>
<summary>python Code to Copy</summary>
```python
```
</details>
The table describes the code in this example.
## Import HD Map File into RoadRunner
To import the RoadRunner HD Map binary file into RoadRunner, follow these steps:
1. In RoadRunner, add the HD Map file to a folder in the **Library Browser**.
2. Select the file in the **Library Browser** to see the attributes of the file in the **Attributes** pane.
3. If the **Attributes** pane does not show the projection data, select **Set Custom Projection**, specify the latitude and longitude of the (0,0) point in the file, and select **Use Transverse Mercator At.** Then click **OK.**
4. Drag the file from the **Library Browser** into the scene. This action switches to the **Scene Builder Tool**.
The scene editing canvas shows the RoadRunner HD Map of the scene. Verify the imported data by selecting control points, lanes, and lane boundaries from the **Attributes** pane.

## Build Scene
To build the scene from the imported data, select Build Scene from the toolbar on the left of the scene editing canvas.
In the Scene Builder dialog box, build the scene using the default build options by selecting Build. To learn more about the build options, see Build Scenes Using HERE HD Live Map Data.
The Scene Builder Tool builds scenes from the RoadRunner HD Map data and displays information about the results in a dialog box. Close the dialog box by clicking Close.
To explore the built scene, disable the visibility of RoadRunner HD Map data by selecting a different tool.