TransWikia.com

Rasterizing all intersected cells when rasterizing line layer in QGIS

Geographic Information Systems Asked by hexamon on January 11, 2021

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 Answers

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.

Answered by MattiasSp on January 11, 2021

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.

Answered by Gwynbleidd on January 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP