I just found a much, much easier solution to the problem.
All it takes is to add a GDAL command-line parameter in the window of the "rasterize" tool of QGIS that specifically indicates that all cells touching a polygon or line must be rasterized.
This parameter is "-at", corresponding to the option "ALL_TOUCHED".
Indicate it in the "Additional command-line parameter" field in the "Advanced parameters" section, like on the image.
Using the "-at" parameter, we can see that all of the raster cells intersecting with the lines are properly rasterized.
Without the "-at" parameter, we can see that some raster cells touching the lines are not rasterized.
Previous solution (outdated) :
Previous answer by MattiasSp left me with a raster that still had "holes" in it, corresponding to lines that hadn't been properly "rasterized".
Here, I propose an alternative solution using a vectorial grid in QGIS, that is still pretty quick to do.
- After opening the processing toolbox ("Processing" tab => "Toolbox"),
look for the "grid" tool.
- Create a vectorial square grid using the "grid" tool with the same extent and resolution as your desired raster.
- [Optional] To increase the speed of the following operations, create a spatial index for both your grid and your line layers using the "Create spatial index" processing tool.
- Go to the "Vector" tab in QGIS, then "Research tools", and choose "Select by location". Choose to select features from your grid by comparing with features of your line layer, and select where the feature intersect, contain, our touch.
- Once that is done, you can save the selected squares of your grid by exporting them (Right click on layer => Export => Save selected features as...), or make them a temporary layer.
- [Optional] If you also want the attributes of your lines to be burned into the raster that you will get at the end (e.g. a road type), go to Vector => Data Management Tools, and select "Join attributes by location". Base layer will be the selected squares of your grid (that intersect/touch lines), Join layer will be the lines. Used the geometric predicates "intersect", "contains" and "touch". Choose the join type "Take attributes of the feature with largest overlap", and then execute the algorithm.
- Once this is done, just burn the position of the squares of your grid that were selected into a raster using the Raster => Conversion => Rasterize tool. If you want to burn the attribute, make sure to select it. Remember that your square grid must be the same resolution and extent as your desired raster.
This should give you a raster where every cell corresponded to a cell that was touching a line one way or another.