# Convert lidar data to a 2D grid map

**PythonRobotics**

A tutorial on how to convert a 2D range measurement into a grid map.

## LIDAR to 2D grid map example

This simple tutorial shows how to read LIDAR (range) measurements from a file and convert it to occupancy grid.

Occupancy grid maps (*Hans Moravec, A.E. Elfes: High resolution maps from wide angle sonar, Proc. IEEE Int. Conf. Robotics Autom. (1985)*) are a popular, probabilistic approach to represent the environment. The grid is basically discrete representation of the environment, which shows if a grid cell is occupied or not. Here the map is represented as a `numpy array`

, and numbers close to 1 means the cell is occupied (*marked with red on the next image*), numbers close to 0 means they are free (*marked with green*). The grid has the ability to represent unknown (unobserved) areas, which are close to 0.5.

In order to construct the grid map from the measurement we need to discretise the values. But, first let's need to `import`

some necessary packages.

The measurement file contains the distances and the corresponding angles in a `csv`

(comma separated values) format. Let's write the `file_read`

method:

From the distances and the angles it is easy to determine the `x`

and `y`

coordinates with `sin`

and `cos`

.
In order to display it `matplotlib.pyplot`

(`plt`

) is used.

The `lidar_to_grid_map.py`

contains handy functions which can used to convert a 2D range measurement to a grid map. For example the `bresenham`

gives the a straight line between two points in a grid map. Let's see how this works.

To fill empty areas, a queue-based algorithm can be used that can be used on an initialized occupancy map. The center point is given: the algorithm checks for neighbour elements in each iteration, and stops expansion on obstacles and free boundaries.

This algotihm will fill the area bounded by the yellow lines starting from a center point (e.g. (10, 20)) with zeros:

Let's use this flood fill on real data: