I have a large raster and a line.

I want to preserve the values of the pixels that are below the line, and reclassify all other pixels to NO VALUE or 9999, so that only the pixels where the line overlapped are the same.

enter image description here

1 Answer 1


You can create a buffer around the line to convert the line as a polygon (the distance value depends from your raster resolution) and use the resultant layer as a mask to clip your raster (with the algorithm Clip Raster by mask layer) and create a raster that contains only the pixel overlapping the line.

Then, reclassify the original raster changing all the values in 0.

Next step, with the GDAL > Raster Calculator, create a new raster adding to the raster with no value the clipped raster.

enter image description here

  • It works great, but takes some guessing to get the right buffer size. This seems to be because the buffer has to be larger than the pixels in order to clip them. Is there no way of doing this if the buffer is smaller than the pixel size?
    – Steffan
    Mar 3, 2020 at 12:59

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.