3

I have a line layer that I want to rasterize. I run the Rasterize (vector to raster) tool in QGIS Processing to do so. However, the output is lacking in that it doesn't capture each cell that the line layer touches. See image below.

enter image description here

So, how can I rasterize a line layer in QGIS and make sure all intersected cells are given a value?

2

This can be achieved by converting your lines to polygons using the Vector-->Geoprocessing Tools-->Buffer tool and then rasterizing the buffer layer instead.

When polygons are rasterized, it seems the cell center needs to be covered by a polygon in order to be assigned values from the vector layer. So the trick would be to use a buffer size that is just more than half the diagonal of your intended cell cize.

So, assuming your intended cell size is 1 m, the buffer size could be set to sqrt(2 * cellsize^2) / 2 rounded up to the desired precision.

enter image description here

EDIT: A tip for the buffer tool parameters is to use round for both End cap style and for the Join style and to set the segments parameter sufficiently high to lower the risk of "missing" some cell center points.

EDIT2: While this approach would not "miss" any cells touched by the line, almost vertical/horizontal lines would assign values to cells on both sides of the lines, causing "thicker" lines in the resulting raster. So this approach assigns values to more cells than only those intersected by by the lines.

1

[EDIT]

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.

Screenshot of the "rasterize" tool window in QGIS; the "Additional command-line parameter" option is highlighted to show where to add the "-at" extra parameter for GDAL

Using the "-at" parameter, we can see that all of the raster cells intersecting with the lines are properly rasterized.

Example of rasterization with the "-at" parameter.

Without the "-at" parameter, we can see that some raster cells touching the lines are not rasterized.

Example of rasterization without the "-at" parameter.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.